Initial public release
Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
81
crates/luxe-core/tests/engine/carryover.rs
Normal file
81
crates/luxe-core/tests/engine/carryover.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
//! Carryover — the reader's mode, model and thinking level, carried into an engine rebuilt to
|
||||
//! resume another session elsewhere.
|
||||
//!
|
||||
//! An engine that loads another session in place keeps all three. One rebuilt for a session in
|
||||
//! another checkout starts from the last one's carryover, and has to land exactly where that one
|
||||
//! was: a mode with a model of its own still overriding the reader's pick, and leaving the mode
|
||||
//! still restoring it.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::fixtures::{base_config, first, MockProvider};
|
||||
use luxe_core::engine::{Action, Engine, EngineConfig};
|
||||
use luxe_core::event::{EngineState, Event};
|
||||
use luxe_core::mode::{Mode, Modes};
|
||||
use luxe_core::provider::{ModelInfo, ModelRef, ThinkingLevel};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
|
||||
/// A model that reasons, so the level it runs at is the level asked for.
|
||||
fn model(id: &str) -> ModelInfo {
|
||||
ModelInfo { r#ref: ModelRef::new("mock", id), reasoning: true, ..ModelInfo::default() }
|
||||
}
|
||||
|
||||
/// An engine on `launched`, whose plan mode runs on `planner` at `high`.
|
||||
fn config(store: JsonlSessionStore, session: luxe_core::session::SessionId) -> EngineConfig {
|
||||
EngineConfig {
|
||||
model: model("launched"),
|
||||
modes: Modes::built_in(
|
||||
Mode::plan(Vec::new()).with_model(model("planner")).with_thinking(ThinkingLevel::High),
|
||||
),
|
||||
..base_config(store, session, Arc::new(MockProvider::new()))
|
||||
}
|
||||
}
|
||||
|
||||
async fn state(events: &mut tokio::sync::mpsc::Receiver<Event>, what: &str) -> EngineState {
|
||||
first(events, what, |event| match event {
|
||||
Event::StateChanged(state) => Some(state),
|
||||
_ => None,
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// The reader picks a model and a level, then plans; the rebuilt engine reports plan on the
|
||||
/// planner when its session loads, and leaving plan lands on the model and level the reader picked.
|
||||
#[tokio::test]
|
||||
async fn a_rebuilt_engine_starts_where_the_last_one_was() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session = store.create("/tmp/test").unwrap();
|
||||
let (actions, events, task) = Engine::spawn_task(config(store, session.clone()));
|
||||
actions.send(Action::SetModel(model("picked"))).await.unwrap();
|
||||
actions.send(Action::SetThinkingLevel(ThinkingLevel::Low)).await.unwrap();
|
||||
actions.send(Action::SetMode(luxe_core::mode::PLAN.into())).await.unwrap();
|
||||
actions.send(Action::Close).await.unwrap();
|
||||
let carryover = task.await.unwrap();
|
||||
drop(events);
|
||||
|
||||
let store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let (actions, mut events, _task) =
|
||||
Engine::spawn_task_carrying(config(store, session), Some(carryover));
|
||||
let started = state(&mut events, "the carried state").await;
|
||||
assert_eq!(started.mode, luxe_core::mode::PLAN);
|
||||
assert_eq!(started.model.model, "planner", "plan's own model still overrides the pick");
|
||||
|
||||
actions.send(Action::SetMode(luxe_core::mode::BUILD.into())).await.unwrap();
|
||||
let built = state(&mut events, "leaving plan").await;
|
||||
assert_eq!(built.model.model, "picked", "and leaving plan restores it");
|
||||
assert_eq!(built.thinking, ThinkingLevel::Low);
|
||||
let _ = actions.send(Action::Close).await;
|
||||
}
|
||||
|
||||
/// Without a carryover an engine starts as it always has, on the launch's model and mode.
|
||||
#[tokio::test]
|
||||
async fn without_a_carryover_an_engine_starts_on_its_launch_settings() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session = store.create("/tmp/test").unwrap();
|
||||
let (actions, mut events, _task) = Engine::spawn_task_carrying(config(store, session), None);
|
||||
let started = state(&mut events, "the session loading").await;
|
||||
assert_eq!((started.mode.as_str(), started.model.model.as_str()), ("build", "launched"));
|
||||
let _ = actions.send(Action::Close).await;
|
||||
}
|
||||
694
crates/luxe-core/tests/engine/compaction.rs
Normal file
694
crates/luxe-core/tests/engine/compaction.rs
Normal file
@@ -0,0 +1,694 @@
|
||||
//! Regression: compaction has to change the request, not just the log.
|
||||
//!
|
||||
//! It summarized, billed for the call, wrote the summary to disk — and then sent the whole
|
||||
//! transcript anyway, because a session log is append-only and nothing applied the cut. Two
|
||||
//! independent breaks did it: the `Compaction` entry was chained under `first_kept` instead of
|
||||
//! after the leaf, so it sat on a fork nothing walked; and the engine discarded the id, so the
|
||||
//! next request was built from the pre-compaction leaf regardless. Occupancy never moved, so the
|
||||
//! threshold fired again on the following turn, and the next, each one paying for a summarizer
|
||||
//! call that could not take effect.
|
||||
//!
|
||||
//! Everything here is asserted against the request the mock provider actually received.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::fixtures::base_config;
|
||||
use futures::stream;
|
||||
use futures::stream::BoxStream;
|
||||
use luxe_core::compaction::CompactionSettings;
|
||||
use luxe_core::engine::{Action, Engine, EngineConfig};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::message::{ContentBlock, Message, Role};
|
||||
use luxe_core::provider::{
|
||||
CompletionRequest, ModelInfo, ModelRef, PromptReuse, Provider, ProviderError, ProviderEvent,
|
||||
StopReason, Usage,
|
||||
};
|
||||
use luxe_core::session::{EntryKind, JsonlSessionStore, SessionId, SessionStore};
|
||||
use luxe_core::tool::ToolRegistry;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// `usable = 27_700 - min(1_000, 20_000)` = 26 700, and the trigger is 90% of that.
|
||||
const CONTEXT_WINDOW: u32 = 27_700;
|
||||
const MAX_TOKENS: u32 = 1_000;
|
||||
/// What the settings above work out to, quoted so an assertion can name it.
|
||||
const THRESHOLD: u64 = 24_030;
|
||||
|
||||
/// Roughly what one fixture prompt costs, at four characters to the token.
|
||||
const PROMPT_TOKENS: u64 = 2_000;
|
||||
|
||||
/// The text the fake summarizer produces, so an assertion can find it in the next request.
|
||||
const SUMMARY_MARKER: &str = "Summarized-Goal-Marker";
|
||||
|
||||
/// Enough turns that occupancy climbs past the threshold, compaction fires, and several turns run
|
||||
/// *after* it without firing again — which is the property that was broken.
|
||||
const TURNS: usize = 15;
|
||||
|
||||
/// Records every request and answers two kinds of call: a summarization (no tools, and a system
|
||||
/// prompt that says what it is) and an ordinary turn.
|
||||
///
|
||||
/// Reports the occupancy of the request it was **actually given**, the way a real provider does.
|
||||
/// A mock that reports a fixed number can't tell a compaction that worked from one that didn't:
|
||||
/// the reading stays pinned either way, and the threshold trips again on the next turn.
|
||||
struct CompactingProvider {
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
}
|
||||
|
||||
impl CompactingProvider {
|
||||
fn is_summarization(request: &CompletionRequest) -> bool {
|
||||
request.system.as_deref().is_some_and(|system| system.contains("session compactor"))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for CompactingProvider {
|
||||
fn name(&self) -> &str {
|
||||
"compacting-mock"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let summarizing = Self::is_summarization(&request);
|
||||
let occupancy = occupancy_of(&request);
|
||||
self.calls.lock().await.push(request);
|
||||
|
||||
// Summary text a real summarizer would plausibly return: the engine refuses one that
|
||||
// carries no prose, because an empty summary silently deletes everything it replaces.
|
||||
let text = match summarizing {
|
||||
true => format!(
|
||||
"## Goal\n{SUMMARY_MARKER}: keep the conversation going while the window stays \
|
||||
under its limit.\n\n## Next Steps\nCarry on with the remaining turns."
|
||||
),
|
||||
false => "ok".to_string(),
|
||||
};
|
||||
let mut events = vec![ProviderEvent::TextDelta { index: 0, delta: text }];
|
||||
// A summarization call measures its own prompt, not the turn's context — reporting the
|
||||
// turn's occupancy from it would keep the reading pinned at the pre-compaction figure.
|
||||
if !summarizing {
|
||||
events.push(ProviderEvent::Usage(Usage {
|
||||
prompt_tokens: occupancy,
|
||||
completion_tokens: 1,
|
||||
total_tokens: occupancy,
|
||||
..Usage::default()
|
||||
}));
|
||||
}
|
||||
events.push(ProviderEvent::Done { reason: StopReason::EndTurn });
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
/// What a provider reports for a request: the conversation *and* the prefix above it. A mock that
|
||||
/// counts only the messages under-reports the way no real endpoint does, and compaction reads that
|
||||
/// figure to work out how much of the window its own prompt is already using.
|
||||
fn occupancy_of(request: &CompletionRequest) -> u32 {
|
||||
let messages = luxe_core::compaction::estimate_message_tokens(&request.messages);
|
||||
let prefix =
|
||||
luxe_core::compaction::estimate_prefix_tokens(request.system.as_deref(), &request.tools);
|
||||
(messages + prefix).min(u32::MAX.into()) as u32
|
||||
}
|
||||
|
||||
fn model() -> ModelInfo {
|
||||
ModelInfo {
|
||||
r#ref: ModelRef::new("mock", "compacting"),
|
||||
context_window: CONTEXT_WINDOW,
|
||||
max_tokens: MAX_TOKENS,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn settings() -> CompactionSettings {
|
||||
CompactionSettings {
|
||||
enabled: true,
|
||||
reserve_tokens: 20_000,
|
||||
// Several fixture prompts' worth, deliberately. A `keep_recent` smaller than one message
|
||||
// puts the cut on the leaf itself, and that hides the fork the old code created — with
|
||||
// nothing after `first_kept`, there is nothing left to strand.
|
||||
keep_recent_tokens: 9_000,
|
||||
compact_at_fraction: 0.9,
|
||||
// Off: pruning would shrink the same tool output this test is measuring.
|
||||
prune_protect_tokens: u32::MAX,
|
||||
prune_min_tokens: u32::MAX,
|
||||
prune_at_fraction: 1.0,
|
||||
summarizer: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `invoked_from` is the test's own directory on purpose.
|
||||
///
|
||||
/// Left unset, the engine takes the *process's* directory — this repository — so the system prompt
|
||||
/// these tests measure against a threshold quietly includes Luxe's own `AGENTS.md`, its skills, and
|
||||
/// the name of the branch they run on. Editing `AGENTS.md` then fails three compaction tests, which
|
||||
/// is a fixture reading the room rather than a defect.
|
||||
fn config(
|
||||
store: JsonlSessionStore,
|
||||
session_id: SessionId,
|
||||
provider: Arc<dyn Provider + Send + Sync>,
|
||||
invoked_from: &std::path::Path,
|
||||
) -> EngineConfig {
|
||||
EngineConfig {
|
||||
model: model(),
|
||||
compaction_settings: settings(),
|
||||
invoked_from: Some(invoked_from.to_path_buf()),
|
||||
..base_config(store, session_id, provider)
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_turn(
|
||||
action_tx: &tokio::sync::mpsc::Sender<Action>,
|
||||
event_rx: &mut tokio::sync::mpsc::Receiver<Event>,
|
||||
prompt: String,
|
||||
) {
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage { content: prompt, images: Vec::new(), remediation: None })
|
||||
.await
|
||||
.unwrap();
|
||||
loop {
|
||||
match tokio::time::timeout(std::time::Duration::from_secs(10), event_rx.recv()).await {
|
||||
Ok(Some(Event::TurnEnded { .. })) => break,
|
||||
Ok(Some(_)) => {}
|
||||
Ok(None) => panic!("event channel closed"),
|
||||
Err(_) => panic!("timed out waiting for the turn to end"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every text block in a request, in order.
|
||||
fn texts(request: &CompletionRequest) -> Vec<String> {
|
||||
request
|
||||
.messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.filter_map(|block| match block {
|
||||
ContentBlock::Text(text) => Some(text.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A prompt worth [`PROMPT_TOKENS`], so a dozen of them clear the threshold.
|
||||
fn bulky(label: &str) -> String {
|
||||
format!("{label} {}", "x".repeat(PROMPT_TOKENS as usize * 4))
|
||||
}
|
||||
|
||||
/// Which turns' prompts survived into a set of sent texts, in order.
|
||||
fn turns_present(sent: &[String]) -> Vec<usize> {
|
||||
sent.iter()
|
||||
.filter_map(|text| text.strip_prefix("TURN-"))
|
||||
.filter_map(|rest| rest.split_whitespace().next()?.parse().ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The kept side of a cut is a *contiguous* suffix of the conversation, ending at the live turn.
|
||||
///
|
||||
/// The assertion the obvious ones miss. Chaining the summary under `first_kept` instead of after
|
||||
/// the leaf forks the branch, and a fork does not look like data loss from either end: the oldest
|
||||
/// turns are gone because they were summarized, the newest is there because it was committed after
|
||||
/// the fork — and the turns in between quietly fall off the path.
|
||||
fn assert_kept_tail_is_intact(sent: &[String]) {
|
||||
let present = turns_present(sent);
|
||||
assert!(present.len() > 1, "more than the live turn is kept: {present:?}");
|
||||
let expected: Vec<usize> = (present[0]..TURNS).collect();
|
||||
assert_eq!(present, expected, "every turn since the cut is still in the conversation");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_compacted_turn_sends_the_summary_instead_of_the_history() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(CompactingProvider { calls: calls.clone() });
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(config(store, session_id.clone(), provider, tmp.path()));
|
||||
|
||||
// Occupancy climbs with each turn; partway through it clears the threshold and compaction
|
||||
// runs before that turn's provider call.
|
||||
for turn in 0..TURNS {
|
||||
run_turn(&action_tx, &mut event_rx, bulky(&format!("TURN-{turn}"))).await;
|
||||
}
|
||||
|
||||
let calls = calls.lock().await;
|
||||
let summarizations =
|
||||
calls.iter().filter(|call| CompactingProvider::is_summarization(call)).count();
|
||||
// Once. Not once per turn from there on — which is what a compaction that never reached the
|
||||
// wire produced, because the context it was meant to free never actually freed.
|
||||
assert_eq!(summarizations, 1, "compaction ran exactly once in {TURNS} turns");
|
||||
|
||||
// The summarizer's prompt is a whole context window by construction, assembled once and never
|
||||
// sent again — so asking to cache it is a quarter more than sending it plainly, for nothing.
|
||||
// Every turn is the opposite: the next one extends its prefix.
|
||||
for call in calls.iter() {
|
||||
let expected = match CompactingProvider::is_summarization(call) {
|
||||
true => PromptReuse::OneOff,
|
||||
false => PromptReuse::Conversation,
|
||||
};
|
||||
assert_eq!(
|
||||
call.reuse,
|
||||
expected,
|
||||
"summarizing={}",
|
||||
CompactingProvider::is_summarization(call)
|
||||
);
|
||||
}
|
||||
|
||||
let last = calls.last().expect("a final turn request");
|
||||
assert!(!CompactingProvider::is_summarization(last), "the last call is the turn itself");
|
||||
let sent = texts(last);
|
||||
|
||||
// The summary leads, framed so the model reads it as a report rather than a turn to answer.
|
||||
assert!(sent[0].contains(SUMMARY_MARKER), "the summary is sent");
|
||||
assert!(sent[0].contains("<summary>"), "and says what it is: {}", &sent[0][..60]);
|
||||
assert_eq!(last.messages[0].role, Role::User, "as a user message, not a stray system turn");
|
||||
|
||||
// What it replaced is gone …
|
||||
assert!(
|
||||
!sent.iter().any(|text| text.starts_with("TURN-0")),
|
||||
"the summarized turn must not also be sent"
|
||||
);
|
||||
// … and everything since the cut is there, verbatim and unbroken.
|
||||
assert_kept_tail_is_intact(&sent);
|
||||
// The whole point: the request got smaller.
|
||||
assert!(
|
||||
luxe_core::compaction::estimate_message_tokens(&last.messages) < THRESHOLD,
|
||||
"the compacted request is back under the threshold"
|
||||
);
|
||||
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// `/compact` is the user overruling the threshold, and the only feedback they get is the event.
|
||||
#[tokio::test]
|
||||
async fn compacting_on_request_ignores_the_threshold_and_says_what_it_freed() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(CompactingProvider { calls: calls.clone() });
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config(store, session_id, provider, tmp.path()));
|
||||
|
||||
// Enough that there is something older than the recent tail to summarize, and nowhere near
|
||||
// the threshold — auto-compaction would not fire here.
|
||||
for turn in 0..6 {
|
||||
run_turn(&action_tx, &mut event_rx, bulky(&format!("TURN-{turn}"))).await;
|
||||
}
|
||||
assert!(
|
||||
!calls.lock().await.iter().any(CompactingProvider::is_summarization),
|
||||
"nothing has tripped the threshold yet"
|
||||
);
|
||||
|
||||
action_tx.send(Action::CompactNow { focus: Some("the API decisions".into()) }).await.unwrap();
|
||||
let mut freed = None;
|
||||
while let Ok(Some(event)) =
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), event_rx.recv()).await
|
||||
{
|
||||
if let Event::CompactionDone { tokens_before, tokens_after } = event {
|
||||
freed = Some((tokens_before, tokens_after));
|
||||
break;
|
||||
}
|
||||
}
|
||||
let (before, after) = freed.expect("a compaction_done event");
|
||||
assert!(before > after, "it reports what it freed: {before} -> {after}");
|
||||
|
||||
let calls = calls.lock().await;
|
||||
let summarization =
|
||||
calls.iter().find(|call| CompactingProvider::is_summarization(call)).expect("it ran");
|
||||
// The steer reaches the summarizer, and reaches it before the transcript — after fifty
|
||||
// thousand characters of conversation an instruction reads as more conversation.
|
||||
let prompt = texts(summarization).join("\n");
|
||||
assert!(prompt.contains("the API decisions"), "the focus is passed through");
|
||||
assert!(
|
||||
prompt.find("the API decisions") < prompt.find("TURN-0"),
|
||||
"the instruction leads the transcript it is about"
|
||||
);
|
||||
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// The summary has to be the tip of the session, or a resume walks a path that misses it — and the
|
||||
/// entries committed after it are stranded on the far side of a fork.
|
||||
#[tokio::test]
|
||||
async fn a_compacted_session_resumes_through_its_summary() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path().join("sessions");
|
||||
let mut store = JsonlSessionStore::new(root.clone());
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(CompactingProvider { calls: calls.clone() });
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(config(store, session_id.clone(), provider, tmp.path()));
|
||||
for turn in 0..TURNS {
|
||||
run_turn(&action_tx, &mut event_rx, bulky(&format!("TURN-{turn}"))).await;
|
||||
}
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
|
||||
let reopened = JsonlSessionStore::new(root);
|
||||
let entries = reopened.load(&session_id).unwrap();
|
||||
let leaf = luxe_core::session::latest_leaf(&entries).expect("a leaf");
|
||||
let path = reopened.active_path(&session_id, &leaf).unwrap();
|
||||
|
||||
// Everything ever written is still reachable — compaction is a cut, not a delete.
|
||||
let compactions = path
|
||||
.iter()
|
||||
.filter(|entry| matches!(entry.kind, EntryKind::Compaction { .. }))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(compactions.len(), 1, "the compaction is on the resumed path");
|
||||
assert!(
|
||||
path.iter().any(|entry| matches!(&entry.kind,
|
||||
EntryKind::Message(message) if message.content.iter().any(|block|
|
||||
matches!(block, ContentBlock::Text(text) if text.starts_with("TURN-0"))))),
|
||||
"the summarized message is kept in the log for audit and /tree"
|
||||
);
|
||||
|
||||
// A `tokens_before` of zero was the old placeholder; it is what the summary replaced.
|
||||
match &compactions[0].kind {
|
||||
EntryKind::Compaction { tokens_before, .. } => {
|
||||
assert!(*tokens_before > 0, "the compaction records what it replaced")
|
||||
}
|
||||
other => panic!("expected a compaction, got {other:?}"),
|
||||
}
|
||||
|
||||
// And the request that resume would build is the cut one.
|
||||
let messages = luxe_core::compaction::context_messages(&path);
|
||||
let sent: Vec<String> = messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.filter_map(|block| match block {
|
||||
ContentBlock::Text(text) => Some(text.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert!(sent[0].contains(SUMMARY_MARKER));
|
||||
assert!(!sent.iter().any(|text| text.starts_with("TURN-0")));
|
||||
assert_kept_tail_is_intact(&sent);
|
||||
assert!(
|
||||
luxe_core::compaction::context_tokens(&path)
|
||||
< luxe_core::compaction::estimate_tokens(&path),
|
||||
"the resumed occupancy is the compacted one"
|
||||
);
|
||||
}
|
||||
|
||||
/// Compacting twice must fold the previous summary in, not restart from the beginning of a session
|
||||
/// whose opening messages were replaced two compactions ago.
|
||||
#[tokio::test]
|
||||
async fn a_second_compaction_summarizes_only_what_the_first_one_kept() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let mut parent = luxe_core::session::latest_leaf(&store.load(&session_id).unwrap());
|
||||
let mut ids = Vec::new();
|
||||
for index in 0..8 {
|
||||
let message = Message::user(bulky(&format!("M{index}")));
|
||||
let id = store.append(&session_id, parent, EntryKind::Message(message)).unwrap();
|
||||
parent = Some(id.clone());
|
||||
ids.push(id);
|
||||
}
|
||||
|
||||
let budget = luxe_core::compaction::ContextBudget {
|
||||
context_window: CONTEXT_WINDOW as u64,
|
||||
max_tokens: MAX_TOKENS,
|
||||
observed_tokens: Some(THRESHOLD + PROMPT_TOKENS),
|
||||
..Default::default()
|
||||
};
|
||||
let settings = settings();
|
||||
|
||||
let path = store.active_path(&session_id, ids.last().unwrap()).unwrap();
|
||||
let first = luxe_core::compaction::find_compaction_point(
|
||||
&path,
|
||||
&settings,
|
||||
budget,
|
||||
luxe_core::compaction::CompactionTrigger::Threshold,
|
||||
)
|
||||
.unwrap();
|
||||
let compacted = store
|
||||
.compact(
|
||||
&session_id,
|
||||
ids.last().cloned(),
|
||||
"## Goal\nFIRST-SUMMARY\n",
|
||||
&first.first_kept,
|
||||
first.tokens_before,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// More conversation, then a second compaction.
|
||||
let mut parent = Some(compacted);
|
||||
for index in 8..14 {
|
||||
let message = Message::user(bulky(&format!("M{index}")));
|
||||
parent = Some(store.append(&session_id, parent, EntryKind::Message(message)).unwrap());
|
||||
}
|
||||
|
||||
let path = store.active_path(&session_id, parent.as_ref().unwrap()).unwrap();
|
||||
let second = luxe_core::compaction::find_compaction_point(
|
||||
&path,
|
||||
&settings,
|
||||
budget,
|
||||
luxe_core::compaction::CompactionTrigger::Threshold,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// It continues the first summary rather than ignoring it …
|
||||
assert_eq!(second.previous_summary.as_deref(), Some("## Goal\nFIRST-SUMMARY\n"));
|
||||
// … and its input starts at what the first compaction kept, not at the session's first message.
|
||||
let summarized: Vec<String> = second
|
||||
.messages_to_summarize
|
||||
.iter()
|
||||
.filter_map(|entry| match &entry.kind {
|
||||
EntryKind::Message(message) => match message.content.first() {
|
||||
Some(ContentBlock::Text(text)) => {
|
||||
text.split_whitespace().next().map(str::to_string)
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert!(!summarized.is_empty(), "there is something to summarize");
|
||||
assert!(
|
||||
!summarized.contains(&"M0".to_string()),
|
||||
"the first compaction already replaced M0: {summarized:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// A tool whose output is large enough that a handful of calls fills a window.
|
||||
struct BulkyTool;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl luxe_core::tool::Tool for BulkyTool {
|
||||
fn name(&self) -> &str {
|
||||
"read"
|
||||
}
|
||||
|
||||
fn schema(&self) -> luxe_core::provider::ToolSchema {
|
||||
luxe_core::provider::ToolSchema {
|
||||
name: "read".to_string(),
|
||||
description: "Read a file.".to_string(),
|
||||
parameters: serde_json::json!({"type": "object", "properties": {}}),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(
|
||||
&self,
|
||||
_args: serde_json::Value,
|
||||
_cx: &luxe_core::tool::ToolContext,
|
||||
) -> luxe_core::tool::ToolResultData {
|
||||
luxe_core::tool::ToolResultData {
|
||||
content: vec![ContentBlock::Text(bulky("RESULT"))],
|
||||
is_error: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls the bulky tool over and over inside one turn, reporting the occupancy it was handed.
|
||||
///
|
||||
/// `overflow_first` makes the very first call fail the way an endpoint refuses an oversized
|
||||
/// prompt, which is the one provider error the harness can do something about.
|
||||
struct AgenticProvider {
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
iterations: usize,
|
||||
overflow_first: bool,
|
||||
refused: Arc<Mutex<bool>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for AgenticProvider {
|
||||
fn name(&self) -> &str {
|
||||
"agentic-mock"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let summarizing = CompactingProvider::is_summarization(&request);
|
||||
if self.overflow_first && !summarizing {
|
||||
let mut refused = self.refused.lock().await;
|
||||
if !*refused {
|
||||
*refused = true;
|
||||
return Err(ProviderError::Other(
|
||||
"400: prompt is too long: maximum context length exceeded".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
let occupancy = occupancy_of(&request);
|
||||
let call = {
|
||||
let mut calls = self.calls.lock().await;
|
||||
calls.push(request);
|
||||
calls.len()
|
||||
};
|
||||
|
||||
if summarizing {
|
||||
return Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta {
|
||||
index: 0,
|
||||
delta: format!(
|
||||
"## Goal\n{SUMMARY_MARKER}: keep the conversation going while the window \
|
||||
stays under its limit.\n\n## Next Steps\nCarry on with the remaining \
|
||||
turns."
|
||||
),
|
||||
},
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])));
|
||||
}
|
||||
|
||||
let mut events = vec![ProviderEvent::Usage(Usage {
|
||||
prompt_tokens: occupancy,
|
||||
completion_tokens: 1,
|
||||
total_tokens: occupancy,
|
||||
..Usage::default()
|
||||
})];
|
||||
if call <= self.iterations {
|
||||
events.extend([
|
||||
ProviderEvent::ToolCallStart {
|
||||
index: 0,
|
||||
id: format!("call-{call}"),
|
||||
name: "read".to_string(),
|
||||
},
|
||||
ProviderEvent::ToolCallDelta {
|
||||
index: 0,
|
||||
json_delta: format!(r#"{{"path": "f{call}.rs"}}"#),
|
||||
},
|
||||
ProviderEvent::ToolCallEnd { index: 0 },
|
||||
ProviderEvent::Done { reason: StopReason::ToolCall },
|
||||
]);
|
||||
} else {
|
||||
events.push(ProviderEvent::TextDelta { index: 0, delta: "done".to_string() });
|
||||
events.push(ProviderEvent::Done { reason: StopReason::EndTurn });
|
||||
}
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
fn agentic_config(
|
||||
store: JsonlSessionStore,
|
||||
session_id: SessionId,
|
||||
provider: Arc<dyn Provider + Send + Sync>,
|
||||
invoked_from: &std::path::Path,
|
||||
) -> EngineConfig {
|
||||
let mut config = config(store, session_id, provider, invoked_from);
|
||||
let mut tools = ToolRegistry::new();
|
||||
tools.register(Arc::new(BulkyTool));
|
||||
config.tool_registry = tools;
|
||||
config
|
||||
}
|
||||
|
||||
/// The window fills *inside* a turn, and that is where it has to be dealt with.
|
||||
///
|
||||
/// The check used to run once, before the loop, so a turn asked to build a feature could spend
|
||||
/// hundreds of calls growing a context nobody was watching and only compact when the user typed
|
||||
/// again — on exactly the turns that need it, and on the model's last chance to have any room.
|
||||
#[tokio::test]
|
||||
async fn a_long_turn_compacts_without_waiting_for_the_next_message() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(AgenticProvider {
|
||||
calls: calls.clone(),
|
||||
iterations: 30,
|
||||
overflow_first: false,
|
||||
refused: Arc::new(Mutex::new(false)),
|
||||
});
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(agentic_config(store, session_id, provider, tmp.path()));
|
||||
|
||||
// One turn. Everything below happens without the user saying anything else.
|
||||
run_turn(&action_tx, &mut event_rx, bulky("TURN-0")).await;
|
||||
|
||||
let calls = calls.lock().await;
|
||||
let summarizations =
|
||||
calls.iter().filter(|call| CompactingProvider::is_summarization(call)).count();
|
||||
assert!(summarizations >= 1, "the turn has to summarize itself: {} calls", calls.len());
|
||||
|
||||
// And the point of doing it: the requests after it are back under the window.
|
||||
let last = calls.last().expect("a final request");
|
||||
assert!(!CompactingProvider::is_summarization(last));
|
||||
assert!(
|
||||
luxe_core::compaction::estimate_message_tokens(&last.messages) < THRESHOLD,
|
||||
"the turn ended under the threshold rather than over it"
|
||||
);
|
||||
assert!(texts(last).iter().any(|text| text.contains(SUMMARY_MARKER)), "the summary is sent");
|
||||
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// Every figure on the way in is an estimate, so a prompt can clear the threshold and still be
|
||||
/// refused. That refusal is the one the harness already knows how to fix.
|
||||
#[tokio::test]
|
||||
async fn a_prompt_the_provider_refuses_as_too_long_is_summarized_and_retried() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
// Enough turns to leave something older than `keep_recent` worth summarizing, while staying
|
||||
// under the threshold — so the only compaction in this test is the one the refusal forces.
|
||||
let provider = Arc::new(AgenticProvider {
|
||||
calls: calls.clone(),
|
||||
iterations: 0,
|
||||
overflow_first: false,
|
||||
refused: Arc::new(Mutex::new(false)),
|
||||
});
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(agentic_config(store, session_id.clone(), provider, tmp.path()));
|
||||
for turn in 0..8 {
|
||||
run_turn(&action_tx, &mut event_rx, bulky(&format!("TURN-{turn}"))).await;
|
||||
}
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
|
||||
// Same session, a provider that refuses the next prompt exactly once.
|
||||
let store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(AgenticProvider {
|
||||
calls: calls.clone(),
|
||||
iterations: 0,
|
||||
overflow_first: true,
|
||||
refused: Arc::new(Mutex::new(false)),
|
||||
});
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(agentic_config(store, session_id, provider, tmp.path()));
|
||||
run_turn(&action_tx, &mut event_rx, "and now this one".to_string()).await;
|
||||
|
||||
let calls = calls.lock().await;
|
||||
assert!(
|
||||
calls.iter().any(CompactingProvider::is_summarization),
|
||||
"the refusal has to be answered by making room"
|
||||
);
|
||||
let last = calls.last().expect("the retry happened");
|
||||
assert!(!CompactingProvider::is_summarization(last), "and the turn ran after it");
|
||||
assert!(
|
||||
texts(last).iter().any(|text| text.contains(SUMMARY_MARKER)),
|
||||
"the retry carries the summary rather than the prompt that was refused"
|
||||
);
|
||||
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
mod cancel;
|
||||
mod refusals;
|
||||
mod summaries;
|
||||
420
crates/luxe-core/tests/engine/compaction/cancel.rs
Normal file
420
crates/luxe-core/tests/engine/compaction/cancel.rs
Normal file
@@ -0,0 +1,420 @@
|
||||
//! A cancel reaches the summarizer: no summary lands after it, and what the call billed stays billed.
|
||||
|
||||
use futures::StreamExt;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
|
||||
use luxe_core::usage::UsageSource;
|
||||
|
||||
use super::*;
|
||||
use crate::fixtures::events_until;
|
||||
|
||||
/// Sets its flag when dropped, so a test can see the summarizer's stream was let go.
|
||||
struct DropFlag(Arc<AtomicBool>);
|
||||
|
||||
impl Drop for DropFlag {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
/// How the summarizer answers its nth call.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Summary {
|
||||
/// Bills, then says nothing ever again: still thinking when the reader gives up on it
|
||||
Stalls,
|
||||
/// Never even returns its stream
|
||||
NeverOpens,
|
||||
/// Bills, then finishes a real summary once the test releases it
|
||||
AfterRelease,
|
||||
}
|
||||
|
||||
/// Answers turns like [`CompactingProvider`], and each summarization as `script` says, the last
|
||||
/// entry repeating. `overflow_first` refuses the first turn call as too long for the window.
|
||||
struct ScriptedSummaryProvider {
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
summarizing: Arc<tokio::sync::Notify>,
|
||||
release: Arc<tokio::sync::Notify>,
|
||||
dropped: Arc<AtomicBool>,
|
||||
script: Vec<Summary>,
|
||||
summaries: AtomicUsize,
|
||||
overflow_first: AtomicBool,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for ScriptedSummaryProvider {
|
||||
fn name(&self) -> &str {
|
||||
"scripted-summary-mock"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let summarizing = CompactingProvider::is_summarization(&request);
|
||||
if !summarizing && self.overflow_first.swap(false, Ordering::SeqCst) {
|
||||
return Err(ProviderError::Other(
|
||||
"400: prompt is too long: maximum context length exceeded".to_string(),
|
||||
));
|
||||
}
|
||||
let occupancy = occupancy_of(&request);
|
||||
self.calls.lock().await.push(request);
|
||||
if !summarizing {
|
||||
return Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: "ok".to_string() },
|
||||
ProviderEvent::Usage(Usage {
|
||||
prompt_tokens: occupancy,
|
||||
completion_tokens: 1,
|
||||
total_tokens: occupancy,
|
||||
..Usage::default()
|
||||
}),
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])));
|
||||
}
|
||||
let nth = self.summaries.fetch_add(1, Ordering::SeqCst);
|
||||
let step = self.script[nth.min(self.script.len() - 1)];
|
||||
self.summarizing.notify_one();
|
||||
let guard = DropFlag(self.dropped.clone());
|
||||
let billed = ProviderEvent::Usage(Usage {
|
||||
prompt_tokens: 1_234,
|
||||
completion_tokens: 0,
|
||||
total_tokens: 1_234,
|
||||
..Usage::default()
|
||||
});
|
||||
// The mock ignores its cancel receiver, as a provider mid-request may: the engine has to
|
||||
// stop waiting on its own
|
||||
let rest: BoxStream<'static, ProviderEvent> = match step {
|
||||
Summary::NeverOpens => {
|
||||
let _held = guard;
|
||||
std::future::pending::<()>().await;
|
||||
unreachable!()
|
||||
}
|
||||
Summary::Stalls => Box::pin(stream::pending()),
|
||||
Summary::AfterRelease => {
|
||||
let release = self.release.clone();
|
||||
let summary = format!("## Goal\n{SUMMARY_MARKER}: carry on with what is left.");
|
||||
Box::pin(
|
||||
stream::once(async move {
|
||||
release.notified().await;
|
||||
ProviderEvent::TextDelta { index: 0, delta: summary }
|
||||
})
|
||||
.chain(stream::iter([ProviderEvent::Done { reason: StopReason::EndTurn }])),
|
||||
)
|
||||
}
|
||||
};
|
||||
let events = stream::iter([billed]).chain(rest).map(move |event| {
|
||||
let _held = &guard;
|
||||
event
|
||||
});
|
||||
Ok(Box::pin(events))
|
||||
}
|
||||
}
|
||||
|
||||
/// What a test holds of its provider while the engine owns it.
|
||||
struct Script {
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
summarizing: Arc<tokio::sync::Notify>,
|
||||
release: Arc<tokio::sync::Notify>,
|
||||
dropped: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
fn scripted(script: &[Summary], overflow_first: bool) -> (Arc<ScriptedSummaryProvider>, Script) {
|
||||
let provider = Arc::new(ScriptedSummaryProvider {
|
||||
calls: Arc::new(Mutex::new(Vec::new())),
|
||||
summarizing: Arc::new(tokio::sync::Notify::new()),
|
||||
release: Arc::new(tokio::sync::Notify::new()),
|
||||
dropped: Arc::new(AtomicBool::new(false)),
|
||||
script: script.to_vec(),
|
||||
summaries: AtomicUsize::new(0),
|
||||
overflow_first: AtomicBool::new(overflow_first),
|
||||
});
|
||||
let held = Script {
|
||||
calls: provider.calls.clone(),
|
||||
summarizing: provider.summarizing.clone(),
|
||||
release: provider.release.clone(),
|
||||
dropped: provider.dropped.clone(),
|
||||
};
|
||||
(provider, held)
|
||||
}
|
||||
|
||||
fn stalled() -> (Arc<ScriptedSummaryProvider>, Script) {
|
||||
scripted(&[Summary::Stalls], false)
|
||||
}
|
||||
|
||||
async fn summarizer_asked(script: &Script) {
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), script.summarizing.notified())
|
||||
.await
|
||||
.expect("the summarizer was asked");
|
||||
}
|
||||
|
||||
fn compaction_billed(events: &[Event]) -> bool {
|
||||
events.iter().any(|event| {
|
||||
matches!(
|
||||
event,
|
||||
Event::TurnUsage { source: UsageSource::Compaction, prompt_tokens: 1_234, .. }
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn no_compaction_on_disk(root: &std::path::Path, session_id: &SessionId) -> bool {
|
||||
let entries = JsonlSessionStore::new(root.join("sessions")).load(session_id).unwrap();
|
||||
!entries.iter().any(|entry| matches!(entry.kind, EntryKind::Compaction { .. }))
|
||||
}
|
||||
|
||||
/// A session `turns` turns long on `provider`, with nothing compacted yet.
|
||||
async fn session_of(
|
||||
turns: usize,
|
||||
provider: Arc<ScriptedSummaryProvider>,
|
||||
tmp: &tempfile::TempDir,
|
||||
) -> (Sender, Receiver, SessionId) {
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(config(store, session_id.clone(), provider, tmp.path()));
|
||||
for turn in 0..turns {
|
||||
run_turn(&action_tx, &mut event_rx, bulky(&format!("TURN-{turn}"))).await;
|
||||
}
|
||||
(action_tx, event_rx, session_id)
|
||||
}
|
||||
|
||||
type Sender = tokio::sync::mpsc::Sender<Action>;
|
||||
type Receiver = tokio::sync::mpsc::Receiver<Event>;
|
||||
|
||||
/// Sends `/compact`, waits for the summarizer to be asked, and cancels it; returns what the engine
|
||||
/// said up to the refresh sent after the cancel, which proves it is answering again.
|
||||
async fn compact_then_cancel(
|
||||
action_tx: &Sender,
|
||||
event_rx: &mut Receiver,
|
||||
script: &Script,
|
||||
) -> Vec<Event> {
|
||||
action_tx.send(Action::CompactNow { focus: None }).await.unwrap();
|
||||
summarizer_asked(script).await;
|
||||
action_tx.send(Action::Cancel).await.unwrap();
|
||||
action_tx.send(Action::RefreshWorkspace).await.unwrap();
|
||||
events_until(event_rx, "the engine to answer after the cancel", |event| {
|
||||
matches!(event, Event::WorkspaceRefreshRequested)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Cancel during a threshold compaction ends the turn there: the summarizer's stream is dropped,
|
||||
/// no summary is committed, and no further provider call goes out.
|
||||
///
|
||||
/// The break this catches: compaction read its summary over a private cancel channel that was
|
||||
/// never raised, so a cancel landing mid-summary left the turn waiting on the summarizer — and a
|
||||
/// reply that arrived later still replaced the conversation the reader had just stopped.
|
||||
#[tokio::test]
|
||||
async fn a_cancel_during_threshold_compaction_ends_the_turn_and_commits_no_summary() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (provider, script) = stalled();
|
||||
let (action_tx, mut event_rx, session_id) = session_of(0, provider, &tmp).await;
|
||||
|
||||
let mut turn = 0;
|
||||
loop {
|
||||
assert!(turn < TURNS, "the threshold never fired");
|
||||
let prompt = bulky(&format!("TURN-{turn}"));
|
||||
let submit =
|
||||
Action::SubmitUserMessage { content: prompt, images: vec![], remediation: None };
|
||||
action_tx.send(submit).await.unwrap();
|
||||
turn += 1;
|
||||
tokio::select! {
|
||||
() = script.summarizing.notified() => break,
|
||||
_ = events_until(&mut event_rx, "a turn", |e| matches!(e, Event::TurnEnded { .. })) => {}
|
||||
}
|
||||
}
|
||||
|
||||
action_tx.send(Action::Cancel).await.unwrap();
|
||||
let seen = events_until(&mut event_rx, "the cancelled turn to end", |event| {
|
||||
matches!(event, Event::TurnEnded { .. })
|
||||
})
|
||||
.await;
|
||||
let Some(Event::TurnEnded { reason }) = seen.last() else { unreachable!() };
|
||||
assert_eq!(*reason, luxe_core::event::StopReason::Other("cancelled".into()));
|
||||
assert!(!seen.iter().any(|e| matches!(e, Event::CompactionDone { .. })), "{seen:?}");
|
||||
assert!(script.dropped.load(Ordering::SeqCst), "the summarizer's stream is let go");
|
||||
let calls = script.calls.lock().await;
|
||||
let last = calls.last().unwrap();
|
||||
assert!(CompactingProvider::is_summarization(last), "nothing was sent after the cancel");
|
||||
drop(calls);
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
assert!(no_compaction_on_disk(tmp.path(), &session_id), "no summary was committed");
|
||||
}
|
||||
|
||||
/// `/compact` is serviced like a turn, so a cancel reaches it, the engine answers what comes next,
|
||||
/// and what the summarizer billed stays billed.
|
||||
///
|
||||
/// The break this catches: the action loop awaited the summarizer inline, so a cancel sat in the
|
||||
/// inbox behind it and nothing after it was served until the summarizer gave up on its own.
|
||||
#[tokio::test]
|
||||
async fn a_cancel_during_compaction_on_request_stops_it_and_the_engine_answers_again() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (provider, script) = stalled();
|
||||
let (action_tx, mut event_rx, session_id) = session_of(6, provider, &tmp).await;
|
||||
|
||||
let seen = compact_then_cancel(&action_tx, &mut event_rx, &script).await;
|
||||
|
||||
assert!(!seen.iter().any(|e| matches!(e, Event::CompactionDone { .. })), "{seen:?}");
|
||||
assert!(
|
||||
!seen
|
||||
.iter()
|
||||
.any(|e| matches!(e, Event::Error(message) if message.contains("Nothing to compact"))),
|
||||
"a cancelled compaction is not reported as having nothing to do: {seen:?}"
|
||||
);
|
||||
assert!(script.dropped.load(Ordering::SeqCst), "the summarizer's stream is let go");
|
||||
assert!(compaction_billed(&seen), "what the summarizer billed stays billed: {seen:?}");
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
assert!(no_compaction_on_disk(tmp.path(), &session_id), "no summary was committed");
|
||||
}
|
||||
|
||||
/// A cancel reaches a summarizer that has not even returned its stream yet.
|
||||
#[tokio::test]
|
||||
async fn a_cancel_before_the_summarizer_answers_at_all_still_lands() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (provider, script) = scripted(&[Summary::NeverOpens], false);
|
||||
let (action_tx, mut event_rx, session_id) = session_of(6, provider, &tmp).await;
|
||||
|
||||
compact_then_cancel(&action_tx, &mut event_rx, &script).await;
|
||||
|
||||
assert!(script.dropped.load(Ordering::SeqCst), "the pending request is let go");
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
assert!(no_compaction_on_disk(tmp.path(), &session_id), "no summary was committed");
|
||||
}
|
||||
|
||||
/// Cancelled compactions are not failures: as many as the breaker allows, and the next time the
|
||||
/// window fills it still compacts.
|
||||
///
|
||||
/// The break this catches: a cancel unwound through the failure path, so a reader who stopped
|
||||
/// three summaries switched automatic compaction off for the rest of the session.
|
||||
#[tokio::test]
|
||||
async fn cancelled_compactions_do_not_trip_the_failure_breaker() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let breaker = luxe_core::compaction::MAX_COMPACTION_FAILURES as usize;
|
||||
let mut script = vec![Summary::Stalls; breaker];
|
||||
script.push(Summary::AfterRelease);
|
||||
let (provider, script) = scripted(&script, false);
|
||||
let (action_tx, mut event_rx, _) = session_of(6, provider, &tmp).await;
|
||||
for _ in 0..breaker {
|
||||
compact_then_cancel(&action_tx, &mut event_rx, &script).await;
|
||||
}
|
||||
|
||||
script.release.notify_one();
|
||||
for turn in 6..TURNS {
|
||||
run_turn(&action_tx, &mut event_rx, bulky(&format!("TURN-{turn}"))).await;
|
||||
}
|
||||
|
||||
let calls = script.calls.lock().await;
|
||||
let summaries = calls.iter().filter(|call| CompactingProvider::is_summarization(call)).count();
|
||||
assert!(summaries > breaker, "the threshold still compacts: {summaries} summaries");
|
||||
let last = calls.last().unwrap();
|
||||
assert!(texts(last).iter().any(|text| text.contains(SUMMARY_MARKER)), "and it took effect");
|
||||
drop(calls);
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// A cancel during the summary an overflowing prompt forces ends the turn as cancelled, not as an
|
||||
/// error.
|
||||
///
|
||||
/// The break this catches: the retry read an unchanged leaf as "nothing left to summarize" and
|
||||
/// failed the turn, which a loop counts toward its consecutive-error budget.
|
||||
#[tokio::test]
|
||||
async fn a_cancel_during_overflow_compaction_ends_the_turn_as_cancelled() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (provider, _) = scripted(&[Summary::Stalls], false);
|
||||
let (action_tx, event_rx, session_id) = session_of(8, provider, &tmp).await;
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
|
||||
let store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let (provider, script) = scripted(&[Summary::Stalls], true);
|
||||
let (action_tx, mut event_rx2) = Engine::spawn(config(store, session_id, provider, tmp.path()));
|
||||
drop(event_rx);
|
||||
let submit =
|
||||
Action::SubmitUserMessage { content: "and now".into(), images: vec![], remediation: None };
|
||||
action_tx.send(submit).await.unwrap();
|
||||
summarizer_asked(&script).await;
|
||||
action_tx.send(Action::Cancel).await.unwrap();
|
||||
let seen = events_until(&mut event_rx2, "the turn to end", |event| {
|
||||
matches!(event, Event::TurnEnded { .. })
|
||||
})
|
||||
.await;
|
||||
|
||||
let Some(Event::TurnEnded { reason }) = seen.last() else { unreachable!() };
|
||||
assert_eq!(*reason, luxe_core::event::StopReason::Other("cancelled".into()), "{seen:?}");
|
||||
assert!(!seen.iter().any(|e| matches!(e, Event::Error(_))), "{seen:?}");
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// A steer sent while `/compact` runs gets a turn of its own once it finishes, as a steer with no
|
||||
/// turn running always does.
|
||||
///
|
||||
/// The break this catches: serviced like a turn, the compaction buffered the steer for a turn
|
||||
/// that never came, and it surfaced only after the reader's next message.
|
||||
#[tokio::test]
|
||||
async fn a_steer_sent_during_compaction_on_request_runs_as_a_turn_after_it() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (provider, script) = scripted(&[Summary::AfterRelease], false);
|
||||
let (action_tx, mut event_rx, _) = session_of(6, provider, &tmp).await;
|
||||
|
||||
action_tx.send(Action::CompactNow { focus: None }).await.unwrap();
|
||||
summarizer_asked(&script).await;
|
||||
action_tx.send(Action::SteerTurn { content: "STEERED-WHILE-COMPACTING".into() }).await.unwrap();
|
||||
buffered_mid_work(&action_tx, &mut event_rx).await;
|
||||
script.release.notify_one();
|
||||
let seen = events_until(&mut event_rx, "the steer's turn", |event| {
|
||||
matches!(event, Event::TurnEnded { .. })
|
||||
})
|
||||
.await;
|
||||
|
||||
assert!(seen.iter().any(|e| matches!(e, Event::CompactionDone { .. })), "{seen:?}");
|
||||
let calls = script.calls.lock().await;
|
||||
let last = calls.last().unwrap();
|
||||
assert!(texts(last).iter().any(|text| text == "STEERED-WHILE-COMPACTING"), "{:?}", texts(last));
|
||||
drop(calls);
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// Returns once the engine has answered a glance it serves mid-work, so whatever was sent before
|
||||
/// it reached the work in progress rather than waiting behind it.
|
||||
async fn buffered_mid_work(action_tx: &Sender, event_rx: &mut Receiver) {
|
||||
let request_id = luxe_core::project_files::ListingRequestId::new();
|
||||
let path = String::new();
|
||||
let listing = Action::ListDirectory { request_id, path, include_ignored: false };
|
||||
action_tx.send(listing).await.unwrap();
|
||||
events_until(event_rx, "the listing", |event| {
|
||||
matches!(event, Event::DirectoryListed { .. } | Event::DirectoryListFailed { .. })
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// A cancelled `/compact` drops the steers sent during it, the way a cancel with no turn running
|
||||
/// does: no turn runs for them.
|
||||
#[tokio::test]
|
||||
async fn a_cancelled_compaction_runs_no_turn_for_steers_sent_during_it() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (provider, script) = stalled();
|
||||
let (action_tx, mut event_rx, _) = session_of(6, provider, &tmp).await;
|
||||
let turns_before = script.calls.lock().await.len();
|
||||
|
||||
action_tx.send(Action::CompactNow { focus: None }).await.unwrap();
|
||||
summarizer_asked(&script).await;
|
||||
action_tx.send(Action::SteerTurn { content: "STEERED-THEN-CANCELLED".into() }).await.unwrap();
|
||||
buffered_mid_work(&action_tx, &mut event_rx).await;
|
||||
action_tx.send(Action::Cancel).await.unwrap();
|
||||
action_tx.send(Action::RefreshWorkspace).await.unwrap();
|
||||
let seen = events_until(&mut event_rx, "the engine to answer", |event| {
|
||||
matches!(event, Event::WorkspaceRefreshRequested)
|
||||
})
|
||||
.await;
|
||||
action_tx.send(Action::RefreshWorkspace).await.unwrap();
|
||||
let seen_after = events_until(&mut event_rx, "the engine to answer again", |event| {
|
||||
matches!(event, Event::WorkspaceRefreshRequested)
|
||||
})
|
||||
.await;
|
||||
|
||||
for event in seen.iter().chain(&seen_after) {
|
||||
assert!(!matches!(event, Event::TurnEnded { .. }), "a turn ran: {event:?}");
|
||||
}
|
||||
let calls = script.calls.lock().await;
|
||||
assert_eq!(calls.len(), turns_before + 1, "only the summary was asked for");
|
||||
drop(calls);
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
178
crates/luxe-core/tests/engine/compaction/refusals.rs
Normal file
178
crates/luxe-core/tests/engine/compaction/refusals.rs
Normal file
@@ -0,0 +1,178 @@
|
||||
//! Summaries that must not replace the conversation they were meant to summarize.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A summarizer that answers with nothing at all, which a local model on a bad day really does.
|
||||
struct EmptySummaryProvider {
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for EmptySummaryProvider {
|
||||
fn name(&self) -> &str {
|
||||
"empty-summary-mock"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let summarizing = CompactingProvider::is_summarization(&request);
|
||||
let occupancy = occupancy_of(&request);
|
||||
self.calls.lock().await.push(request);
|
||||
|
||||
let mut events = Vec::new();
|
||||
if summarizing {
|
||||
// Nothing. Not an error, not a refusal — an empty reply.
|
||||
events.push(ProviderEvent::TextDelta { index: 0, delta: String::new() });
|
||||
} else {
|
||||
events.push(ProviderEvent::TextDelta { index: 0, delta: "ok".to_string() });
|
||||
events.push(ProviderEvent::Usage(Usage {
|
||||
prompt_tokens: occupancy,
|
||||
completion_tokens: 1,
|
||||
total_tokens: occupancy,
|
||||
..Usage::default()
|
||||
}));
|
||||
}
|
||||
events.push(ProviderEvent::Done { reason: StopReason::EndTurn });
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
/// An empty summary must not be committed over the transcript it was meant to summarize.
|
||||
///
|
||||
/// The break this catches, measured on a live loop with a 27B local summarizer: the model returned
|
||||
/// nothing usable, the formatter supplied the six required headings and the running file list
|
||||
/// anyway, and a 20,677-token transcript was replaced by that — logged as a successful compaction.
|
||||
/// The session lost everything it knew, silently, and the next turn started from an empty document
|
||||
/// of headings.
|
||||
#[tokio::test]
|
||||
async fn a_summary_that_says_nothing_does_not_replace_the_conversation() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(EmptySummaryProvider { calls: calls.clone() });
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(config(store, session_id.clone(), provider, tmp.path()));
|
||||
for turn in 0..TURNS {
|
||||
run_turn(&action_tx, &mut event_rx, bulky(&format!("TURN-{turn}"))).await;
|
||||
}
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
|
||||
let calls = calls.lock().await;
|
||||
assert!(
|
||||
calls.iter().any(CompactingProvider::is_summarization),
|
||||
"the threshold still fired; this is about what happens next"
|
||||
);
|
||||
let last = calls.last().expect("a final turn request");
|
||||
assert!(!CompactingProvider::is_summarization(last), "the last call is the turn itself");
|
||||
let sent = texts(last);
|
||||
assert!(
|
||||
sent.iter().any(|text| text.starts_with("TURN-0")),
|
||||
"the conversation an unusable summary could not replace is still there: {sent:?}"
|
||||
);
|
||||
|
||||
// And nothing on disk claims a compaction happened.
|
||||
let store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let entries = store.load(&session_id).unwrap();
|
||||
assert!(
|
||||
!entries.iter().any(|entry| matches!(entry.kind, EntryKind::Compaction { .. })),
|
||||
"a refused compaction leaves no summary behind"
|
||||
);
|
||||
}
|
||||
|
||||
/// How a summarizer's reply can stop short after writing a summary that looks complete.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum CutOff {
|
||||
Error,
|
||||
Length,
|
||||
EndOfStream,
|
||||
}
|
||||
|
||||
/// A summarizer that writes a substantive summary, then fails to finish it.
|
||||
struct CutOffSummaryProvider {
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
cut_off: CutOff,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for CutOffSummaryProvider {
|
||||
fn name(&self) -> &str {
|
||||
"cut-off-summary-mock"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let summarizing = CompactingProvider::is_summarization(&request);
|
||||
let occupancy = occupancy_of(&request);
|
||||
self.calls.lock().await.push(request);
|
||||
|
||||
if !summarizing {
|
||||
return Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: "ok".to_string() },
|
||||
ProviderEvent::Usage(Usage {
|
||||
prompt_tokens: occupancy,
|
||||
completion_tokens: 1,
|
||||
total_tokens: occupancy,
|
||||
..Usage::default()
|
||||
}),
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])));
|
||||
}
|
||||
let mut events = vec![ProviderEvent::TextDelta {
|
||||
index: 0,
|
||||
delta: format!("## Goal\n{SUMMARY_MARKER}: half of what happened, and then"),
|
||||
}];
|
||||
match self.cut_off {
|
||||
CutOff::Error => events.push(ProviderEvent::Error {
|
||||
message: "connection reset".into(),
|
||||
kind: luxe_core::provider::ProviderErrorKind::Other("transport".into()),
|
||||
}),
|
||||
CutOff::Length => events.push(ProviderEvent::Done { reason: StopReason::Length }),
|
||||
CutOff::EndOfStream => {}
|
||||
}
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
/// A summary the summarizer did not finish must not replace the conversation it summarizes.
|
||||
///
|
||||
/// The break this catches: the reply loop took an error, a length stop or a dropped stream as
|
||||
/// success, and the half-summary that had already arrived passed the content check, so the
|
||||
/// session's history was replaced by a fragment.
|
||||
#[tokio::test]
|
||||
async fn a_summary_cut_off_partway_does_not_replace_the_conversation() {
|
||||
for cut_off in [CutOff::Error, CutOff::Length, CutOff::EndOfStream] {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(CutOffSummaryProvider { calls: calls.clone(), cut_off });
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(config(store, session_id.clone(), provider, tmp.path()));
|
||||
for turn in 0..TURNS {
|
||||
run_turn(&action_tx, &mut event_rx, bulky(&format!("TURN-{turn}"))).await;
|
||||
}
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
|
||||
let calls = calls.lock().await;
|
||||
assert!(calls.iter().any(CompactingProvider::is_summarization), "{cut_off:?}: it ran");
|
||||
let sent = texts(calls.last().expect("a final turn request"));
|
||||
assert!(
|
||||
sent.iter().any(|text| text.starts_with("TURN-0")),
|
||||
"{cut_off:?}: the conversation is still sent whole"
|
||||
);
|
||||
let entries =
|
||||
JsonlSessionStore::new(tmp.path().join("sessions")).load(&session_id).unwrap();
|
||||
assert!(
|
||||
!entries.iter().any(|entry| matches!(entry.kind, EntryKind::Compaction { .. })),
|
||||
"{cut_off:?}: no summary was committed"
|
||||
);
|
||||
}
|
||||
}
|
||||
112
crates/luxe-core/tests/engine/compaction/summaries.rs
Normal file
112
crates/luxe-core/tests/engine/compaction/summaries.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
//! Summaries — compaction replaces the history it summarized in the next request.
|
||||
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Plan, summarize, append — over a real parent chain, ending in the request the next turn sends.
|
||||
///
|
||||
/// The fixture used to append every entry with `parent: None`, a flat forest with no path through
|
||||
/// it, and assert only that a `Compaction` entry existed afterwards. That is why compaction could
|
||||
/// summarize, bill for the call, write the summary to disk and still send the entire transcript
|
||||
/// for months without a red test.
|
||||
#[tokio::test]
|
||||
async fn compaction_replaces_the_history_it_summarized() {
|
||||
use luxe_core::compaction::CompactionSettings;
|
||||
use luxe_core::session::EntryKind;
|
||||
|
||||
let tmp_dir = std::env::temp_dir().join(format!("luxe-test-compaction-{}", Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&tmp_dir).unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp_dir.clone());
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
// A real chain. For a 128K window / 32K max the usable budget is 128K - min(32K, 16_384) =
|
||||
// 111_616 tokens and compaction fires at 90% of that (~100K); at ~1 token / 4 chars, 100
|
||||
// entries of ~5000 chars clears it.
|
||||
let long_text = "a".repeat(5000);
|
||||
let mut parent = luxe_core::session::latest_leaf(&store.load(&session_id).unwrap());
|
||||
for index in 0..100 {
|
||||
let kind = match index % 2 {
|
||||
0 => EntryKind::Message(luxe_core::message::Message::user(format!(
|
||||
"Message {index} {long_text}"
|
||||
))),
|
||||
_ => EntryKind::Message(luxe_core::message::Message::assistant(format!(
|
||||
"Response {index} {long_text}"
|
||||
))),
|
||||
};
|
||||
parent = Some(store.append(&session_id, parent, kind).unwrap());
|
||||
}
|
||||
let leaf = parent.expect("a leaf");
|
||||
|
||||
let settings = CompactionSettings {
|
||||
enabled: true,
|
||||
reserve_tokens: 16_384,
|
||||
keep_recent_tokens: 5_000,
|
||||
compact_at_fraction: 0.9,
|
||||
prune_protect_tokens: 40_000,
|
||||
prune_min_tokens: 20_000,
|
||||
prune_at_fraction: 0.8,
|
||||
summarizer: None,
|
||||
};
|
||||
let budget = luxe_core::compaction::ContextBudget {
|
||||
context_window: 128_000,
|
||||
max_tokens: 32_000,
|
||||
observed_tokens: None,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let entries = store.active_path(&session_id, &leaf).unwrap();
|
||||
let plan = luxe_core::compaction::find_compaction_point(
|
||||
&entries,
|
||||
&settings,
|
||||
budget,
|
||||
luxe_core::compaction::CompactionTrigger::Threshold,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!plan.messages_to_summarize.is_empty());
|
||||
assert!(!luxe_core::compaction::serialize_for_summary(&plan.messages_to_summarize).is_empty());
|
||||
|
||||
let formatted = luxe_core::compaction::format_structured_summary(
|
||||
"## Goal\nTest compaction.\n\n## Next Steps\nContinue testing.",
|
||||
&plan.file_ops,
|
||||
);
|
||||
assert!(formatted.contains("Test compaction"));
|
||||
|
||||
let compacted = store
|
||||
.compact(&session_id, Some(leaf), &formatted, &plan.first_kept, plan.tokens_before)
|
||||
.unwrap();
|
||||
|
||||
// The compaction is the tip, and the whole session is still reachable through it — nothing is
|
||||
// deleted, so a later `/tree` or an audit can still read what was summarized.
|
||||
let path = store.active_path(&session_id, &compacted).unwrap();
|
||||
assert_eq!(path.len(), 102, "header + 100 messages + compaction");
|
||||
|
||||
// But the request built from that path carries the summary in place of what it replaced.
|
||||
let messages = luxe_core::compaction::context_messages(&path);
|
||||
let rendered: Vec<String> = messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.filter_map(|block| match block {
|
||||
luxe_core::message::ContentBlock::Text(text) => Some(text.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
assert!(messages.len() < 20, "the summarized span is gone: {} messages", messages.len());
|
||||
assert!(rendered[0].contains("Test compaction"), "the summary leads: {}", &rendered[0][..80]);
|
||||
assert!(rendered[0].contains("<summary>"), "and says what it is");
|
||||
assert!(
|
||||
!rendered[1..].iter().any(|text| text.starts_with("Message 0 ")),
|
||||
"the oldest message was summarized away"
|
||||
);
|
||||
assert!(
|
||||
rendered.iter().any(|text| text.starts_with("Response 99 ")),
|
||||
"the newest message survives verbatim"
|
||||
);
|
||||
|
||||
// And the occupancy the next threshold check reads is the smaller one it produced.
|
||||
let before = luxe_core::compaction::estimate_tokens(&path);
|
||||
let after = luxe_core::compaction::context_tokens(&path);
|
||||
assert!(after * 4 < before, "context shrank from {before} to {after} tokens");
|
||||
|
||||
std::fs::remove_dir_all(&tmp_dir).unwrap();
|
||||
}
|
||||
460
crates/luxe-core/tests/engine/fixtures.rs
Normal file
460
crates/luxe-core/tests/engine/fixtures.rs
Normal file
@@ -0,0 +1,460 @@
|
||||
//! Fixtures — the doubles and waiters the engine scenarios share.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::stream::{self, BoxStream};
|
||||
use luxe_core::engine::EngineConfig;
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::hook::{HookCtx, HookEvent, HookOutcome, HookPoint, HookRegistry};
|
||||
use luxe_core::provider::{
|
||||
CompletionRequest, ModelInfo, Provider, ProviderError, ProviderEvent, StopReason,
|
||||
};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionId};
|
||||
use luxe_core::tool::ToolRegistry;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
/// How long a test waits for an engine event before calling it a hang.
|
||||
///
|
||||
/// Generous on purpose. Every assertion here is about *what* the engine emitted, never how fast, and
|
||||
/// the work behind a guided-review load is a dozen `git` invocations. Five seconds was enough on a
|
||||
/// developer machine and not on a Windows runner with the whole suite running in parallel, which
|
||||
/// reads as a product failure and is not one. Still well inside nextest's own 120s cutoff, so a real
|
||||
/// hang still fails rather than wedging the run.
|
||||
pub(crate) const EVENT_WAIT: std::time::Duration = std::time::Duration::from_secs(30);
|
||||
|
||||
/// Mock provider that scripts a simple conversation:
|
||||
/// 1. First turn: emits text "Let me read the file" → tool_call(read) → done
|
||||
/// 2. Second turn: emits text "File edited successfully" → done
|
||||
pub(crate) struct MockProvider {
|
||||
turn_count: Arc<RwLock<usize>>,
|
||||
requests: Arc<RwLock<Vec<CompletionRequest>>>,
|
||||
}
|
||||
|
||||
impl MockProvider {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self { turn_count: Arc::new(RwLock::new(0)), requests: Arc::new(RwLock::new(Vec::new())) }
|
||||
}
|
||||
|
||||
/// Every request it was sent, in order
|
||||
pub(crate) fn requests(&self) -> Arc<RwLock<Vec<CompletionRequest>>> {
|
||||
self.requests.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for MockProvider {
|
||||
fn name(&self) -> &str {
|
||||
"mock"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
self.requests.write().await.push(request);
|
||||
let mut turn_count = self.turn_count.write().await;
|
||||
*turn_count += 1;
|
||||
let current_turn = *turn_count;
|
||||
|
||||
let events = if current_turn == 1 {
|
||||
// First turn: text → tool_call(read) → done
|
||||
vec![
|
||||
ProviderEvent::Start,
|
||||
ProviderEvent::TextStart { index: 0 },
|
||||
ProviderEvent::TextDelta { index: 0, delta: "Let me read the file".to_string() },
|
||||
ProviderEvent::TextEnd { index: 0 },
|
||||
ProviderEvent::ToolCallStart {
|
||||
index: 0,
|
||||
id: "call_1".to_string(),
|
||||
name: "read".to_string(),
|
||||
},
|
||||
ProviderEvent::ToolCallDelta {
|
||||
index: 0,
|
||||
json_delta: r#"{"path": "test.txt"}"#.to_string(),
|
||||
},
|
||||
ProviderEvent::ToolCallEnd { index: 0 },
|
||||
ProviderEvent::Done { reason: StopReason::ToolCall },
|
||||
]
|
||||
} else {
|
||||
// Second turn: text → done
|
||||
vec![
|
||||
ProviderEvent::Start,
|
||||
ProviderEvent::TextStart { index: 0 },
|
||||
ProviderEvent::TextDelta {
|
||||
index: 0,
|
||||
delta: "File edited successfully".to_string(),
|
||||
},
|
||||
ProviderEvent::TextEnd { index: 0 },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
]
|
||||
};
|
||||
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple hook that records all hook points it sees.
|
||||
pub(crate) struct RecordingHook {
|
||||
points: Arc<RwLock<Vec<HookPoint>>>,
|
||||
}
|
||||
|
||||
impl RecordingHook {
|
||||
pub(crate) fn new() -> (Self, Arc<RwLock<Vec<HookPoint>>>) {
|
||||
let points = Arc::new(RwLock::new(Vec::new()));
|
||||
(Self { points: points.clone() }, points)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl luxe_core::hook::Hook for RecordingHook {
|
||||
fn name(&self) -> &'static str {
|
||||
"recording"
|
||||
}
|
||||
|
||||
fn interest(&self) -> &[HookPoint] {
|
||||
&[
|
||||
HookPoint::SessionStart,
|
||||
HookPoint::SessionEnd,
|
||||
HookPoint::UserPromptSubmit,
|
||||
HookPoint::AgentStart,
|
||||
HookPoint::AgentEnd,
|
||||
HookPoint::BeforeToolUse,
|
||||
HookPoint::AfterToolUse,
|
||||
]
|
||||
}
|
||||
|
||||
async fn on(&self, point: HookPoint, _ev: &HookEvent, _cx: &HookCtx) -> HookOutcome {
|
||||
self.points.write().await.push(point);
|
||||
HookOutcome::Continue
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider that turns the bounded Guide prompt into a complete manifest for integration coverage.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct GuideManifestProvider {
|
||||
/// Guide passes it was asked for, feedback turns aside
|
||||
pub(crate) guides: std::sync::atomic::AtomicUsize,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for GuideManifestProvider {
|
||||
fn name(&self) -> &str {
|
||||
"guide-manifest"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let prompt = request.messages.last().and_then(|message| {
|
||||
message.content.iter().find_map(|block| match block {
|
||||
luxe_core::message::ContentBlock::Text(text) => Some(text),
|
||||
_ => None,
|
||||
})
|
||||
});
|
||||
if let Some(feedback) = prompt.filter(|text| text.starts_with("Guided review feedback")) {
|
||||
let review_id = feedback
|
||||
.lines()
|
||||
.find_map(|line| line.strip_prefix("Review: "))
|
||||
.expect("feedback prompt includes review ID");
|
||||
let revision_id = feedback
|
||||
.lines()
|
||||
.find_map(|line| line.strip_prefix("Source revision: "))
|
||||
.expect("feedback prompt includes source revision");
|
||||
// The bullet now leads with the reviewer-facing label (`R1`), so remediation keys off
|
||||
// the explicit durable-ID line rather than parsing display text.
|
||||
let finding_id = feedback
|
||||
.lines()
|
||||
.find_map(|line| line.trim_start().strip_prefix("Finding ID: "))
|
||||
.expect("feedback prompt includes finding ID");
|
||||
let reply = format!(
|
||||
"Applied the focused change.\n```luxe-remediation\n{{\"review_id\":\"{review_id}\",\"revision_id\":\"{revision_id}\",\"findings\":[{{\"finding_id\":\"{finding_id}\",\"status\":\"fixed\",\"note\":\"Updated the tracked source.\"}}]}}\n```"
|
||||
);
|
||||
return Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: reply },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])));
|
||||
}
|
||||
self.guides.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
let manifest = prompt
|
||||
.and_then(|text| text.rsplit_once('\n').map(|(_, input)| input))
|
||||
.and_then(|input| serde_json::from_str::<serde_json::Value>(input).ok())
|
||||
.map(|input| {
|
||||
let item_ids = input["items"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|item| item["item_id"].as_str().map(str::to_owned))
|
||||
.collect::<Vec<_>>();
|
||||
serde_json::json!({
|
||||
"chapters": [{
|
||||
"title": "Model Guide",
|
||||
"rationale": "Groups the immutable items supplied to the model.",
|
||||
"kind": "core",
|
||||
"item_ids": item_ids,
|
||||
}]
|
||||
})
|
||||
.to_string()
|
||||
})
|
||||
.unwrap_or_else(|| "{}".into());
|
||||
Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: manifest },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])))
|
||||
}
|
||||
}
|
||||
|
||||
/// An engine on `project_root` that answers guides and feedback with [`GuideManifestProvider`].
|
||||
pub(crate) fn review_engine_config(
|
||||
project_root: &std::path::Path,
|
||||
session_id: SessionId,
|
||||
) -> EngineConfig {
|
||||
let store = JsonlSessionStore::new(project_root.to_path_buf());
|
||||
EngineConfig {
|
||||
guided_review_project_root: Some(project_root.to_path_buf()),
|
||||
..base_config(store, session_id, Arc::new(GuideManifestProvider::default()))
|
||||
}
|
||||
}
|
||||
|
||||
/// The first event `wanted` picks out of `events`, within the wait.
|
||||
pub(crate) async fn first<T>(
|
||||
events: &mut tokio::sync::mpsc::Receiver<Event>,
|
||||
what: &str,
|
||||
mut wanted: impl FnMut(Event) -> Option<T>,
|
||||
) -> T {
|
||||
tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match events.recv().await {
|
||||
Some(event) => {
|
||||
if let Some(found) = wanted(event) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
None => panic!("the event stream closed before {what}"),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|_| panic!("timed out waiting for {what}"))
|
||||
}
|
||||
|
||||
/// The unstaged empty-tree source the review-feedback tests submit against.
|
||||
pub(crate) fn review_source() -> luxe_core::guided_review::ReviewSource {
|
||||
use luxe_core::guided_review::{ReviewBase, ReviewRevisionId, ReviewSource};
|
||||
ReviewSource {
|
||||
base: ReviewBase::EmptyTree,
|
||||
content_digest: ReviewRevisionId::from_canonical_bytes(b"review"),
|
||||
includes_staged: false,
|
||||
includes_unstaged: true,
|
||||
included_untracked_files: 0,
|
||||
captured_at_ms: 42,
|
||||
uncovered_paths: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RemediationProvider {
|
||||
pub(crate) reply: String,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for RemediationProvider {
|
||||
fn name(&self) -> &str {
|
||||
"remediation"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
_request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: self.reply.clone() },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])))
|
||||
}
|
||||
}
|
||||
|
||||
/// An engine on `store` with every optional piece off: no tools, hooks, reviewer, compaction, cache
|
||||
/// warming or project root, and the default model.
|
||||
///
|
||||
/// A scenario overrides the fields it is about and leaves the rest, so what a test configures is
|
||||
/// what it reads. A scenario that needs compaction or cache warming asks for it by name.
|
||||
pub(crate) fn base_config(
|
||||
store: JsonlSessionStore,
|
||||
session_id: SessionId,
|
||||
provider: Arc<dyn Provider + Send + Sync>,
|
||||
) -> EngineConfig {
|
||||
EngineConfig {
|
||||
session_store: std::sync::RwLock::new(Box::new(store)),
|
||||
provider,
|
||||
providers: HashMap::new(),
|
||||
model: ModelInfo::default(),
|
||||
tool_registry: ToolRegistry::new(),
|
||||
hook_registry: HookRegistry::new(),
|
||||
session_id,
|
||||
system_prompt: None,
|
||||
todo_store: None,
|
||||
compaction_settings: luxe_core::compaction::CompactionSettings {
|
||||
enabled: false,
|
||||
..luxe_core::compaction::CompactionSettings::default()
|
||||
},
|
||||
permission: Default::default(),
|
||||
modes: Default::default(),
|
||||
reviewer: None,
|
||||
formatter_registry: None,
|
||||
spend_sink: None,
|
||||
guided_review_project_root: None,
|
||||
invoked_from: None,
|
||||
external_work: None,
|
||||
review_context_files: Vec::new(),
|
||||
review_exclude: Vec::new(),
|
||||
remember_runtime_state: false,
|
||||
cache_warming: luxe_core::cache_warming::CacheWarming::Off,
|
||||
review_insight_provider: None,
|
||||
github: None,
|
||||
permissions_file: None,
|
||||
max_tool_iterations: luxe_core::engine::DEFAULT_MAX_TOOL_ITERATIONS,
|
||||
startup_notices: Vec::new(),
|
||||
action_tx: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A `read` the test can count: it answers every call with the same text and keeps what it was
|
||||
/// asked.
|
||||
///
|
||||
/// Registered where a scenario says a tool ran, so the claim is something to assert — an engine
|
||||
/// that never dispatched the call leaves [`Self::invocations`] at zero, and one that dispatched it
|
||||
/// but lost the answer leaves the next request without [`WITNESS_OUTPUT`].
|
||||
pub(crate) struct WitnessReadTool {
|
||||
calls: std::sync::Mutex<Vec<serde_json::Value>>,
|
||||
}
|
||||
|
||||
/// What [`WitnessReadTool`] says every time
|
||||
pub(crate) const WITNESS_OUTPUT: &str = "witness read: Hello, World!";
|
||||
|
||||
impl WitnessReadTool {
|
||||
pub(crate) fn new() -> Arc<Self> {
|
||||
Arc::new(Self { calls: std::sync::Mutex::new(Vec::new()) })
|
||||
}
|
||||
|
||||
/// The arguments of each call that ran
|
||||
pub(crate) fn invocations(&self) -> Vec<serde_json::Value> {
|
||||
self.calls.lock().expect("no panic holds this lock").clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl luxe_core::tool::Tool for WitnessReadTool {
|
||||
fn name(&self) -> &str {
|
||||
"read"
|
||||
}
|
||||
|
||||
fn schema(&self) -> luxe_core::provider::ToolSchema {
|
||||
luxe_core::provider::ToolSchema {
|
||||
name: "read".to_string(),
|
||||
description: "Read a file.".to_string(),
|
||||
parameters: serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": { "path": { "type": "string" } },
|
||||
"required": ["path"],
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(
|
||||
&self,
|
||||
args: serde_json::Value,
|
||||
_cx: &luxe_core::tool::ToolContext,
|
||||
) -> luxe_core::tool::ToolResultData {
|
||||
self.calls.lock().expect("no panic holds this lock").push(args);
|
||||
luxe_core::tool::ToolResultData {
|
||||
content: vec![luxe_core::message::ContentBlock::Text(WITNESS_OUTPUT.to_string())],
|
||||
is_error: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Events until `stop` says so, then all of them; a panic naming `waiting_for` when the wait runs
|
||||
/// out or the stream closes first.
|
||||
///
|
||||
/// Fails, rather than returning what it has: a drain that gives up quietly lets the assertions
|
||||
/// after it pass on whatever had arrived by then.
|
||||
pub(crate) async fn events_until(
|
||||
events: &mut tokio::sync::mpsc::Receiver<Event>,
|
||||
waiting_for: &str,
|
||||
stop: impl Fn(&Event) -> bool,
|
||||
) -> Vec<Event> {
|
||||
let mut seen = Vec::new();
|
||||
loop {
|
||||
match tokio::time::timeout(EVENT_WAIT, events.recv()).await {
|
||||
Ok(Some(event)) => {
|
||||
let done = stop(&event);
|
||||
seen.push(event);
|
||||
if done {
|
||||
return seen;
|
||||
}
|
||||
}
|
||||
Ok(None) => panic!("the event stream closed before {waiting_for}"),
|
||||
Err(_) => panic!("timed out waiting for {waiting_for}; got {} events", seen.len()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every event up to and including the turn's end.
|
||||
pub(crate) async fn events_until_turn_end(
|
||||
events: &mut tokio::sync::mpsc::Receiver<Event>,
|
||||
) -> Vec<Event> {
|
||||
events_until(events, "the turn to end", |event| matches!(event, Event::TurnEnded { .. })).await
|
||||
}
|
||||
|
||||
/// Wait until the engine has actually *taken* the cancel, rather than sleeping and hoping.
|
||||
///
|
||||
/// `Action::Cancel` travels the same channel as every other action, so "sent" and "in effect" are
|
||||
/// two different moments. The engine announces the second one with a `cancelled` state snapshot,
|
||||
/// which is the only sound synchronization point a test has.
|
||||
pub(crate) async fn await_cancel_taken(events: &mut tokio::sync::mpsc::Receiver<Event>) {
|
||||
events_until(
|
||||
events,
|
||||
"the cancel to take effect",
|
||||
|event| matches!(event, Event::StateChanged(state) if state.status == "cancelled"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
/// The ids of every tool call in `messages`, and of every call something answered.
|
||||
pub(crate) fn pairing(messages: &[luxe_core::message::Message]) -> (Vec<String>, Vec<String>) {
|
||||
use luxe_core::message::ContentBlock;
|
||||
let mut calls = Vec::new();
|
||||
let mut results = Vec::new();
|
||||
for block in messages.iter().flat_map(|message| &message.content) {
|
||||
match block {
|
||||
ContentBlock::ToolCall { id, .. } => calls.push(id.clone()),
|
||||
ContentBlock::ToolResult { call_id, .. } => results.push(call_id.clone()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
(calls, results)
|
||||
}
|
||||
|
||||
/// The text of the result that answers `call_id`; panics when nothing does.
|
||||
pub(crate) fn result_text(messages: &[luxe_core::message::Message], call_id: &str) -> String {
|
||||
use luxe_core::message::{ContentBlock, ToolOutput};
|
||||
for block in messages.iter().flat_map(|message| &message.content) {
|
||||
if let ContentBlock::ToolResult { call_id: id, content, .. } = block {
|
||||
if id == call_id {
|
||||
return content
|
||||
.iter()
|
||||
.map(|output| match output {
|
||||
ToolOutput::Text(text) => text.clone(),
|
||||
ToolOutput::Image { .. } => "[image]".to_string(),
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
panic!("no result for {call_id}");
|
||||
}
|
||||
738
crates/luxe-core/tests/engine/interruption.rs
Normal file
738
crates/luxe-core/tests/engine/interruption.rs
Normal file
@@ -0,0 +1,738 @@
|
||||
//! An interrupted turn must not brick the session.
|
||||
//!
|
||||
//! One real incident produced all of this: a model emitted three tool calls in one message, the first
|
||||
//! wedged on a host that never answered, the user interrupted, and Luxe quit. What it left behind was
|
||||
//! an assistant message whose calls had no results — which the OpenAI Responses API answers with
|
||||
//! `400 No tool output found for function call <id>` on **every** later request, and Anthropic with
|
||||
//! the equivalent about `tool_use` ids. The session, and every resume of it, was over.
|
||||
//!
|
||||
//! A second incident, a year of sessions later, came through the other door and ended the same
|
||||
//! way: the interrupt itself was handled correctly, and then the turn *after* it was born
|
||||
//! cancelled — its stream dropped before a single event, committing an empty assistant message
|
||||
//! that Anthropic answers with `400 text content blocks must be non-empty` forever after. The
|
||||
//! reader saw the spinner stop, sent one more message, and lost the session to a 400 about a
|
||||
//! message nobody had written.
|
||||
//!
|
||||
//! What is asserted here, in the order the incidents hit:
|
||||
//!
|
||||
//! 1. Cancel ends the *batch*, so the queued calls never run.
|
||||
//! 2. Whatever happens, every call in the transcript has a result.
|
||||
//! 3. A transcript that already has the gap is repaired on the way to the provider, so a session
|
||||
//! saved by an older build still resumes.
|
||||
//! 4. The next turn does not inherit the cancel.
|
||||
//! 5. A call that comes back with nothing commits nothing, and says so.
|
||||
//! 6. A transcript that already carries an empty reply is scrubbed on the way out, same rule.
|
||||
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::fixtures::{
|
||||
await_cancel_taken, base_config, events_until_turn_end, pairing, result_text, EVENT_WAIT,
|
||||
};
|
||||
use futures::stream::{self, BoxStream};
|
||||
use luxe_core::engine::{Action, Engine, EngineConfig};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::message::{ContentBlock, Message, Role};
|
||||
use luxe_core::provider::{
|
||||
CompletionRequest, ModelInfo, Provider, ProviderError, ProviderEvent, StopReason, ToolSchema,
|
||||
Usage,
|
||||
};
|
||||
use luxe_core::session::{EntryKind, JsonlSessionStore, SessionId, SessionStore};
|
||||
use luxe_core::tool::{Tool, ToolContext, ToolRegistry, ToolResultData};
|
||||
use luxe_core::usage::{UsageSource, UsageTotals};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// A tool that blocks until released — the wedged call at the head of the batch.
|
||||
struct GateTool {
|
||||
entered: Arc<tokio::sync::Notify>,
|
||||
release: Arc<tokio::sync::Notify>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Tool for GateTool {
|
||||
fn name(&self) -> &str {
|
||||
"gate"
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "gate".to_string(),
|
||||
description: "Block until released.".to_string(),
|
||||
parameters: serde_json::json!({"type": "object", "properties": {}}),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&self, _args: serde_json::Value, _cx: &ToolContext) -> ToolResultData {
|
||||
self.entered.notify_one();
|
||||
self.release.notified().await;
|
||||
ToolResultData { content: vec![ContentBlock::Text("gate open".into())], is_error: false }
|
||||
}
|
||||
}
|
||||
|
||||
/// A tool that never returns, and reports whether its future was dropped.
|
||||
///
|
||||
/// The drop flag is the real assertion: the engine stops a wedged tool by dropping its future, which
|
||||
/// is only a *stop* if the work goes with it. A tool holding a child process relies on exactly this
|
||||
/// (`kill_on_drop`), so a regression that merely stopped waiting — leaving the browser running — has to
|
||||
/// fail here.
|
||||
struct WedgedTool {
|
||||
entered: Arc<tokio::sync::Notify>,
|
||||
dropped: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
struct DropFlag(Arc<AtomicUsize>);
|
||||
|
||||
impl Drop for DropFlag {
|
||||
fn drop(&mut self) {
|
||||
self.0.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Tool for WedgedTool {
|
||||
fn name(&self) -> &str {
|
||||
"wedged"
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "wedged".to_string(),
|
||||
description: "Never return.".to_string(),
|
||||
parameters: serde_json::json!({"type": "object", "properties": {}}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Short enough to assert on, and the same mechanism a 60s network bound uses.
|
||||
fn timeout(&self) -> Option<std::time::Duration> {
|
||||
Some(std::time::Duration::from_millis(120))
|
||||
}
|
||||
|
||||
async fn run(&self, _args: serde_json::Value, _cx: &ToolContext) -> ToolResultData {
|
||||
let _flag = DropFlag(self.dropped.clone());
|
||||
self.entered.notify_one();
|
||||
std::future::pending::<()>().await;
|
||||
unreachable!("a pending future never resolves")
|
||||
}
|
||||
}
|
||||
|
||||
/// A tool that counts the times it ran. It must never run after a cancel.
|
||||
struct CounterTool(Arc<AtomicUsize>);
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Tool for CounterTool {
|
||||
fn name(&self) -> &str {
|
||||
"counter"
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "counter".to_string(),
|
||||
description: "Count invocations.".to_string(),
|
||||
parameters: serde_json::json!({"type": "object", "properties": {}}),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&self, _args: serde_json::Value, _cx: &ToolContext) -> ToolResultData {
|
||||
self.0.fetch_add(1, Ordering::SeqCst);
|
||||
ToolResultData { content: vec![ContentBlock::Text("ran".into())], is_error: false }
|
||||
}
|
||||
}
|
||||
|
||||
/// Emits the incident's shape on its first call — one `gate`, then two `counter` calls in the same
|
||||
/// message — and records every request so a test can inspect what reached the wire.
|
||||
struct BatchProvider {
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for BatchProvider {
|
||||
fn name(&self) -> &str {
|
||||
"batch"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let first = {
|
||||
let mut calls = self.calls.lock().await;
|
||||
calls.push(request);
|
||||
calls.len() == 1
|
||||
};
|
||||
let events = if first {
|
||||
let mut events = vec![tool_start(0, "call_gate", "gate")];
|
||||
events.push(tool_start(1, "call_skipped_a", "counter"));
|
||||
events.push(tool_start(2, "call_skipped_b", "counter"));
|
||||
events.push(ProviderEvent::Done { reason: StopReason::ToolCall });
|
||||
events
|
||||
} else {
|
||||
vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: "done".to_string() },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
]
|
||||
};
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
fn tool_start(index: usize, id: &str, name: &str) -> ProviderEvent {
|
||||
ProviderEvent::ToolCallStart { index, id: id.to_string(), name: name.to_string() }
|
||||
}
|
||||
|
||||
/// Calls one named tool on its first turn, then answers in prose.
|
||||
struct SingleCallProvider {
|
||||
tool: &'static str,
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for SingleCallProvider {
|
||||
fn name(&self) -> &str {
|
||||
"single"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let first = {
|
||||
let mut calls = self.calls.lock().await;
|
||||
calls.push(request);
|
||||
calls.len() == 1
|
||||
};
|
||||
let events = if first {
|
||||
vec![
|
||||
tool_start(0, "call_wedged", self.tool),
|
||||
ProviderEvent::Done { reason: StopReason::ToolCall },
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: "gave up".to_string() },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
]
|
||||
};
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Answers with plain text and reports usage, so the resumed-transcript test can see the request.
|
||||
struct EchoProvider {
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for EchoProvider {
|
||||
fn name(&self) -> &str {
|
||||
"echo"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
self.calls.lock().await.push(request);
|
||||
Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: "ok".to_string() },
|
||||
ProviderEvent::Usage(Usage {
|
||||
prompt_tokens: 1_000,
|
||||
completion_tokens: 10,
|
||||
total_tokens: 1_010,
|
||||
..Usage::default()
|
||||
}),
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])))
|
||||
}
|
||||
}
|
||||
|
||||
fn config(
|
||||
store: JsonlSessionStore,
|
||||
session_id: SessionId,
|
||||
provider: Arc<dyn Provider + Send + Sync>,
|
||||
tools: ToolRegistry,
|
||||
) -> EngineConfig {
|
||||
EngineConfig {
|
||||
model: ModelInfo { context_window: 200_000, ..ModelInfo::default() },
|
||||
tool_registry: tools,
|
||||
..base_config(store, session_id, provider)
|
||||
}
|
||||
}
|
||||
|
||||
/// Cancelling ends the batch. The queued calls do not run, and all three still come back answered —
|
||||
/// with the distinction that matters: the one that started may have half-happened, the two that never
|
||||
/// started are safe to retry.
|
||||
#[tokio::test]
|
||||
async fn cancelling_a_batch_stops_the_queued_calls_and_answers_all_of_them() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let entered = Arc::new(tokio::sync::Notify::new());
|
||||
let release = Arc::new(tokio::sync::Notify::new());
|
||||
let ran = Arc::new(AtomicUsize::new(0));
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
|
||||
let mut tools = ToolRegistry::new();
|
||||
tools.register(Arc::new(GateTool { entered: entered.clone(), release: release.clone() }));
|
||||
tools.register(Arc::new(CounterTool(ran.clone())));
|
||||
|
||||
let reader = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let (action_tx, mut event_rx) =
|
||||
Engine::spawn(config(store, session_id.clone(), Arc::new(BatchProvider { calls }), tools));
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "capture the site".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Interrupt while the first call is genuinely in flight.
|
||||
tokio::time::timeout(EVENT_WAIT, entered.notified()).await.unwrap();
|
||||
action_tx.send(Action::Cancel).await.unwrap();
|
||||
await_cancel_taken(&mut event_rx).await;
|
||||
// Nothing needs releasing — the cancel drops the tool's future where it stands. Release anyway,
|
||||
// so a regression that ignores cancellation fails an assertion instead of hanging the suite.
|
||||
release.notify_one();
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
assert_eq!(
|
||||
ran.load(Ordering::SeqCst),
|
||||
0,
|
||||
"a cancelled turn must not start the calls still queued behind the one it interrupted"
|
||||
);
|
||||
|
||||
let messages: Vec<Message> = reader
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter_map(|entry| match entry.kind {
|
||||
EntryKind::Message(message) => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let (made, answered) = pairing(&messages);
|
||||
assert_eq!(made.len(), 3, "the assistant message committed three calls");
|
||||
assert_eq!(
|
||||
answered.len(),
|
||||
3,
|
||||
"and the transcript answers all three — an unanswered call is a permanent 400"
|
||||
);
|
||||
|
||||
// The three explanations are not interchangeable: only the calls that never started may be
|
||||
// described as having had no effect.
|
||||
assert!(
|
||||
result_text(&messages, "call_gate").contains("partially completed"),
|
||||
"the interrupted call may have done something"
|
||||
);
|
||||
for skipped in ["call_skipped_a", "call_skipped_b"] {
|
||||
assert!(
|
||||
result_text(&messages, skipped).contains("no effect"),
|
||||
"{skipped} never started, so it is safe to retry"
|
||||
);
|
||||
}
|
||||
|
||||
let _ = action_tx.send(Action::Close).await;
|
||||
}
|
||||
|
||||
/// A session saved with the gap still resumes: the repair happens on the way to the provider, so no
|
||||
/// migration is needed and the durable log keeps saying what actually happened.
|
||||
#[tokio::test]
|
||||
async fn a_transcript_that_already_has_orphaned_calls_still_reaches_the_provider_valid() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
// Exactly the incident: three calls in one assistant message, no tool message, a user message
|
||||
// chained straight after it, and a usage record for the call that made them.
|
||||
let user = store
|
||||
.append(&session_id, None, EntryKind::Message(Message::user("look at the site")))
|
||||
.unwrap();
|
||||
let assistant = store
|
||||
.append(
|
||||
&session_id,
|
||||
Some(user),
|
||||
EntryKind::Message(Message {
|
||||
role: Role::Assistant,
|
||||
content: vec![
|
||||
ContentBlock::ToolCall {
|
||||
id: "call_orphan_a".into(),
|
||||
name: "web_fetch".into(),
|
||||
arguments: serde_json::json!({"url": "https://example.invalid"}),
|
||||
},
|
||||
ContentBlock::ToolCall {
|
||||
id: "call_orphan_b".into(),
|
||||
name: "screenshot".into(),
|
||||
arguments: serde_json::json!({"mode": "url"}),
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
let usage = store
|
||||
.append(
|
||||
&session_id,
|
||||
Some(assistant),
|
||||
EntryKind::Usage {
|
||||
source: UsageSource::Agent,
|
||||
model: "test-model".into(),
|
||||
totals: UsageTotals {
|
||||
calls: 1,
|
||||
prompt_tokens: 69_897,
|
||||
completion_tokens: 415,
|
||||
..UsageTotals::default()
|
||||
},
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
store
|
||||
.append(
|
||||
&session_id,
|
||||
Some(usage),
|
||||
EntryKind::Message(Message::user("never mind, do this instead")),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config(
|
||||
store,
|
||||
session_id.clone(),
|
||||
Arc::new(EchoProvider { calls: calls.clone() }),
|
||||
ToolRegistry::new(),
|
||||
));
|
||||
|
||||
action_tx.send(Action::LoadSession(session_id.to_string())).await.unwrap();
|
||||
|
||||
// Resume reports the occupancy the provider measured, not a guess and not zero. The dock read
|
||||
// 0% of 1.1M on a session holding ~70K tokens, because the estimator counted only text blocks.
|
||||
let mut observed = None;
|
||||
let deadline = tokio::time::Instant::now() + EVENT_WAIT;
|
||||
while observed.is_none() && tokio::time::Instant::now() < deadline {
|
||||
match tokio::time::timeout(EVENT_WAIT, event_rx.recv()).await {
|
||||
Ok(Some(Event::ContextUsage { used_tokens, context_window })) => {
|
||||
observed = Some((used_tokens, context_window));
|
||||
}
|
||||
Ok(Some(_)) => {}
|
||||
Ok(None) => break,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
observed,
|
||||
Some((70_312, 200_000)),
|
||||
"resumed occupancy is the last call's prompt + completion tokens"
|
||||
);
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "carry on".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
let requests = calls.lock().await;
|
||||
let request = requests.first().expect("the turn made a provider call");
|
||||
let (made, answered) = pairing(&request.messages);
|
||||
assert_eq!(made.len(), 2, "both orphaned calls are still replayed");
|
||||
assert_eq!(answered.len(), 2, "and both now carry a result");
|
||||
for orphan in ["call_orphan_a", "call_orphan_b"] {
|
||||
assert!(
|
||||
result_text(&request.messages, orphan).contains("unknown"),
|
||||
"a replayed orphan's outcome is genuinely unknowable, and must say so"
|
||||
);
|
||||
}
|
||||
|
||||
// The durable log is a record, not a workspace: it still shows the calls unanswered.
|
||||
let reader = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let persisted: Vec<Message> = reader
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter_map(|entry| match entry.kind {
|
||||
EntryKind::Message(message) => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let (_, persisted_results) = pairing(&persisted);
|
||||
assert!(
|
||||
!persisted_results.iter().any(|id| id == "call_orphan_a"),
|
||||
"the repair belongs to the outgoing copy; rewriting history would lose what happened"
|
||||
);
|
||||
|
||||
let _ = action_tx.send(Action::Close).await;
|
||||
}
|
||||
|
||||
/// A tool that never returns costs its declared bound, not the session — and the work is dropped, not
|
||||
/// merely abandoned. That distinction is the whole reason the deadline lives in the engine: a headless
|
||||
/// browser left running would still be holding the port, the memory, and the confusion.
|
||||
#[tokio::test]
|
||||
async fn a_wedged_tool_hits_its_deadline_and_its_work_is_dropped() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let entered = Arc::new(tokio::sync::Notify::new());
|
||||
let dropped = Arc::new(AtomicUsize::new(0));
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
|
||||
let mut tools = ToolRegistry::new();
|
||||
tools.register(Arc::new(WedgedTool { entered: entered.clone(), dropped: dropped.clone() }));
|
||||
|
||||
let reader = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config(
|
||||
store,
|
||||
session_id.clone(),
|
||||
Arc::new(SingleCallProvider { tool: "wedged", calls }),
|
||||
tools,
|
||||
));
|
||||
|
||||
let started = tokio::time::Instant::now();
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "capture luxe.dev".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
tokio::time::timeout(EVENT_WAIT, entered.notified()).await.unwrap();
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
assert!(
|
||||
started.elapsed() < std::time::Duration::from_secs(5),
|
||||
"the turn ended on the tool's bound, not on the test's patience"
|
||||
);
|
||||
assert_eq!(
|
||||
dropped.load(Ordering::SeqCst),
|
||||
1,
|
||||
"the tool's future must be dropped, so anything it holds (a child process) goes with it"
|
||||
);
|
||||
|
||||
let messages: Vec<Message> = reader
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter_map(|entry| match entry.kind {
|
||||
EntryKind::Message(message) => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let (made, answered) = pairing(&messages);
|
||||
assert_eq!(made, answered, "a call stopped by its deadline is still a call with a result");
|
||||
let text = result_text(&messages, "call_wedged");
|
||||
assert!(text.contains("`wedged`"), "name the tool that hung: {text}");
|
||||
assert!(text.contains("still running after"), "and say what happened: {text}");
|
||||
|
||||
let _ = action_tx.send(Action::Close).await;
|
||||
}
|
||||
|
||||
/// Records the cancel flag each call is handed, then answers in prose (first call: one `gate`).
|
||||
struct CancelWatchProvider {
|
||||
flags: Arc<Mutex<Vec<bool>>>,
|
||||
calls: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for CancelWatchProvider {
|
||||
fn name(&self) -> &str {
|
||||
"cancel-watch"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
self.flags.lock().await.push(*cancel.borrow());
|
||||
let first = {
|
||||
let mut calls = self.calls.lock().await;
|
||||
calls.push(request);
|
||||
calls.len() == 1
|
||||
};
|
||||
let events = if first {
|
||||
vec![
|
||||
tool_start(0, "call_gate", "gate"),
|
||||
ProviderEvent::Done { reason: StopReason::ToolCall },
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: "carrying on".to_string() },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
]
|
||||
};
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_turn_after_a_cancel_is_not_born_cancelled() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let entered = Arc::new(tokio::sync::Notify::new());
|
||||
let release = Arc::new(tokio::sync::Notify::new());
|
||||
let flags = Arc::new(Mutex::new(Vec::new()));
|
||||
|
||||
let mut tools = ToolRegistry::new();
|
||||
tools.register(Arc::new(GateTool { entered: entered.clone(), release: release.clone() }));
|
||||
|
||||
let reader = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config(
|
||||
store,
|
||||
session_id.clone(),
|
||||
Arc::new(CancelWatchProvider { flags: flags.clone(), calls: Arc::new(Mutex::new(vec![])) }),
|
||||
tools,
|
||||
));
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "launch it".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
tokio::time::timeout(EVENT_WAIT, entered.notified()).await.unwrap();
|
||||
action_tx.send(Action::Cancel).await.unwrap();
|
||||
await_cancel_taken(&mut event_rx).await;
|
||||
release.notify_one();
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "it is open".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
assert_eq!(
|
||||
*flags.lock().await,
|
||||
vec![false, false],
|
||||
"a fresh turn must not inherit the last turn's cancel"
|
||||
);
|
||||
|
||||
let messages: Vec<Message> = reader
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter_map(|entry| match entry.kind {
|
||||
EntryKind::Message(message) => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert!(
|
||||
messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.any(|block| matches!(block, ContentBlock::Text(text) if text == "carrying on")),
|
||||
"the second turn actually got an answer: {messages:#?}"
|
||||
);
|
||||
assert!(
|
||||
!messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.any(|block| matches!(block, ContentBlock::Text(text) if text.trim().is_empty())),
|
||||
"an empty text block is a permanent 400: {messages:#?}"
|
||||
);
|
||||
|
||||
let _ = action_tx.send(Action::Close).await;
|
||||
}
|
||||
|
||||
mod empty_replies;
|
||||
|
||||
/// A front-end that closes mid-turn ends the turn and the engine with it, the way a cancel ends a
|
||||
/// turn, rather than leaving the turn to run on behind a front-end that has gone.
|
||||
///
|
||||
/// The break this catches: `Close` waited behind the turn, so a TUI leaving for another session
|
||||
/// gave up waiting and started the next engine while the old one's tools kept running, in a
|
||||
/// directory that may no longer have been theirs. The call it interrupted is still answered.
|
||||
#[tokio::test]
|
||||
async fn closing_mid_turn_stops_the_turn_and_the_engine() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let entered = Arc::new(tokio::sync::Notify::new());
|
||||
let release = Arc::new(tokio::sync::Notify::new());
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let mut tools = ToolRegistry::new();
|
||||
tools.register(Arc::new(GateTool { entered: entered.clone(), release: release.clone() }));
|
||||
|
||||
let reader = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let provider = Arc::new(SingleCallProvider { tool: "gate", calls });
|
||||
let (action_tx, _event_rx, task) =
|
||||
Engine::spawn_task(config(store, session_id.clone(), provider, tools));
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "wait at the gate".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tokio::time::timeout(EVENT_WAIT, entered.notified()).await.unwrap();
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), task)
|
||||
.await
|
||||
.expect("the engine closed without the gate ever opening")
|
||||
.unwrap();
|
||||
|
||||
let messages: Vec<Message> = reader
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter_map(|entry| match entry.kind {
|
||||
EntryKind::Message(message) => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let (made, answered) = pairing(&messages);
|
||||
assert_eq!((made.len(), answered.len()), (1, 1), "the interrupted call is answered");
|
||||
}
|
||||
|
||||
/// A message queued behind a running turn does not start once a close has stopped that turn: the
|
||||
/// close is served first, so the engine ends rather than running the next turn behind a front-end
|
||||
/// that has gone.
|
||||
#[tokio::test]
|
||||
async fn a_close_outranks_a_message_queued_behind_the_turn() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let entered = Arc::new(tokio::sync::Notify::new());
|
||||
let release = Arc::new(tokio::sync::Notify::new());
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let mut tools = ToolRegistry::new();
|
||||
tools.register(Arc::new(GateTool { entered: entered.clone(), release: release.clone() }));
|
||||
|
||||
let provider = Arc::new(SingleCallProvider { tool: "gate", calls: calls.clone() });
|
||||
let (action_tx, _event_rx, task) =
|
||||
Engine::spawn_task(config(store, session_id, provider, tools));
|
||||
let submit = |content: &str| Action::SubmitUserMessage {
|
||||
content: content.to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
};
|
||||
action_tx.send(submit("wait at the gate")).await.unwrap();
|
||||
tokio::time::timeout(EVENT_WAIT, entered.notified()).await.unwrap();
|
||||
action_tx.send(submit("and then this")).await.unwrap();
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), task)
|
||||
.await
|
||||
.expect("the engine closed")
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(calls.lock().await.len(), 1, "the queued message never reached the provider");
|
||||
}
|
||||
150
crates/luxe-core/tests/engine/interruption/empty_replies.rs
Normal file
150
crates/luxe-core/tests/engine/interruption/empty_replies.rs
Normal file
@@ -0,0 +1,150 @@
|
||||
//! Empty replies — a call that produced nothing commits nothing, and an empty reply is scrubbed.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Answers with nothing at all — the shape a stream dropped by a cancel leaves behind.
|
||||
struct SilentProvider;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for SilentProvider {
|
||||
fn name(&self) -> &str {
|
||||
"silent"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
_request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
Ok(Box::pin(stream::iter(Vec::new())))
|
||||
}
|
||||
}
|
||||
|
||||
/// A call that produces nothing commits nothing, and says so.
|
||||
///
|
||||
/// The placeholder this used to write was an empty text block, which Anthropic answers with
|
||||
/// `400 text content blocks must be non-empty` on every later request — so the reply nobody saw
|
||||
/// took the session with it.
|
||||
#[tokio::test]
|
||||
async fn a_call_that_comes_back_empty_commits_nothing_and_says_so() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
let reader = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config(
|
||||
store,
|
||||
session_id.clone(),
|
||||
Arc::new(SilentProvider),
|
||||
ToolRegistry::new(),
|
||||
));
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "are you there".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let reported =
|
||||
events_until_turn_end(&mut event_rx).await.into_iter().find_map(|event| match event {
|
||||
Event::Error(message) => Some(message),
|
||||
_ => None,
|
||||
});
|
||||
let reported = reported.expect("an answer nobody can see is still an answer to report");
|
||||
assert!(reported.contains("empty response"), "{reported}");
|
||||
|
||||
let messages: Vec<Message> = reader
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter_map(|entry| match entry.kind {
|
||||
EntryKind::Message(message) => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(messages.len(), 1, "only the user's own message is in the transcript");
|
||||
assert_eq!(messages[0].role, Role::User);
|
||||
|
||||
let _ = action_tx.send(Action::Close).await;
|
||||
}
|
||||
|
||||
/// A session an older build poisoned still resumes: the empty block is dropped on the way to the
|
||||
/// provider, so no migration is needed and the durable log keeps saying what actually happened.
|
||||
#[tokio::test]
|
||||
async fn a_transcript_that_already_carries_an_empty_reply_still_reaches_the_provider_valid() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
|
||||
// Copied out of the session this fixed: the interrupted call's result, the message the user
|
||||
// sent next, and the empty reply the cancelled stream committed in place of an answer.
|
||||
let user = store
|
||||
.append(&session_id, None, EntryKind::Message(Message::user("Strata is open.")))
|
||||
.unwrap();
|
||||
store
|
||||
.append(
|
||||
&session_id,
|
||||
Some(user),
|
||||
EntryKind::Message(Message {
|
||||
role: Role::Assistant,
|
||||
content: vec![ContentBlock::Text(String::new())],
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config(
|
||||
store,
|
||||
session_id.clone(),
|
||||
Arc::new(EchoProvider { calls: calls.clone() }),
|
||||
ToolRegistry::new(),
|
||||
));
|
||||
|
||||
action_tx.send(Action::LoadSession(session_id.to_string())).await.unwrap();
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "Please continue.".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
let requests = calls.lock().await;
|
||||
let request = requests.first().expect("the turn made a provider call");
|
||||
assert!(
|
||||
!request
|
||||
.messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.any(|block| matches!(block, ContentBlock::Text(text) if text.trim().is_empty())),
|
||||
"the empty block must not reach the wire: {:#?}",
|
||||
request.messages
|
||||
);
|
||||
assert!(
|
||||
request.messages.iter().all(|message| !message.content.is_empty()),
|
||||
"and nothing may be left as a message with no content"
|
||||
);
|
||||
|
||||
// The durable log is a record, not a workspace: it still shows the reply that never came.
|
||||
let reader = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let persisted: Vec<Message> = reader
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter_map(|entry| match entry.kind {
|
||||
EntryKind::Message(message) => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert!(
|
||||
persisted.iter().any(|message| message.content == vec![ContentBlock::Text(String::new())]),
|
||||
"the empty reply stays in the record"
|
||||
);
|
||||
|
||||
let _ = action_tx.send(Action::Close).await;
|
||||
}
|
||||
16
crates/luxe-core/tests/engine/main.rs
Normal file
16
crates/luxe-core/tests/engine/main.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
//! Engine scenarios — the turn loop, interruption, models, review restore, remediation and
|
||||
//! compaction, driven end to end against doubles.
|
||||
//!
|
||||
//! One binary for the lot, since every top-level file under `tests/` links on its own. The shared
|
||||
//! doubles and the bounded event waiters live in [`fixtures`].
|
||||
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
mod carryover;
|
||||
mod compaction;
|
||||
mod fixtures;
|
||||
mod interruption;
|
||||
mod models;
|
||||
mod remediation;
|
||||
mod review_restore;
|
||||
mod turns;
|
||||
288
crates/luxe-core/tests/engine/models.rs
Normal file
288
crates/luxe-core/tests/engine/models.rs
Normal file
@@ -0,0 +1,288 @@
|
||||
//! Models — the engine sends the configured model, and refuses a turn with none.
|
||||
|
||||
use crate::fixtures::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::stream::{self, BoxStream};
|
||||
use luxe_core::engine::{Action, Engine, EngineConfig};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::provider::{
|
||||
CompletionRequest, ModelInfo, ModelRef, Provider, ProviderError, ProviderEvent, StopReason,
|
||||
};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// Provider that records the model id from each request.
|
||||
struct CapturingProvider {
|
||||
seen_model: Arc<Mutex<Option<String>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for CapturingProvider {
|
||||
fn name(&self) -> &str {
|
||||
"capturing"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
req: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
*self.seen_model.lock().await = Some(req.model.r#ref.id.clone());
|
||||
let events = vec![
|
||||
ProviderEvent::TextStart { index: 0 },
|
||||
ProviderEvent::TextDelta { index: 0, delta: "ok".to_string() },
|
||||
ProviderEvent::TextEnd { index: 0 },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
];
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Records the resolved model of every request, so a test can ask what the *wire* was told rather
|
||||
/// than what the engine believes.
|
||||
struct ModelRecordingProvider {
|
||||
models: Arc<Mutex<Vec<ModelInfo>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for ModelRecordingProvider {
|
||||
fn name(&self) -> &str {
|
||||
"model-recording"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
req: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
self.models.lock().await.push(req.model.clone());
|
||||
Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: "ok".to_string() },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])))
|
||||
}
|
||||
}
|
||||
|
||||
/// A mid-session model switch carries that model's compat flags to the adapter.
|
||||
///
|
||||
/// The seam the whole feature rests on, and the reason it was inert for so long: an adapter is built
|
||||
/// once per provider, so the *only* way a model's own request-shaping quirks can reach the wire is on
|
||||
/// the `ModelInfo` that travels with the request. Asserted here rather than at the adapter, because
|
||||
/// what could break is the engine dropping or rebuilding the model between `SetModel` and the call.
|
||||
#[tokio::test]
|
||||
async fn a_switched_models_compat_travels_with_the_request() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let models = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(ModelRecordingProvider { models: models.clone() });
|
||||
|
||||
let launch =
|
||||
ModelInfo { r#ref: ModelRef::new("lmstudio", "qwen3.8-27b"), ..Default::default() };
|
||||
let config = EngineConfig { model: launch, ..base_config(store, session_id, provider) };
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
|
||||
// The build with its own template: it takes thinking as a template argument, and its chat
|
||||
// template raises on a `developer` role.
|
||||
let sibling = ModelInfo {
|
||||
r#ref: ModelRef::new("lmstudio", "qwen3.6-27b-fable-fusion"),
|
||||
compat: Some(luxe_core::provider::CompatFlags {
|
||||
thinking_format: Some("chat_template".into()),
|
||||
supports_developer_role: false,
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
action_tx.send(Action::SetModel(sibling)).await.unwrap();
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "go".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
let seen = models.lock().await;
|
||||
let asked = seen.first().expect("one provider call");
|
||||
assert_eq!(asked.r#ref.id, "qwen3.6-27b-fable-fusion");
|
||||
let compat = asked.compat.as_ref().expect("the switched model's flags reached the request");
|
||||
assert_eq!(compat.thinking_format.as_deref(), Some("chat_template"));
|
||||
assert!(!compat.supports_developer_role);
|
||||
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn engine_announces_sessions_and_forwards_configured_model() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let expected_session_id = session_id.to_string();
|
||||
|
||||
let seen_model = Arc::new(Mutex::new(None));
|
||||
let provider = Arc::new(CapturingProvider { seen_model: seen_model.clone() });
|
||||
|
||||
let config = EngineConfig {
|
||||
model: ModelInfo {
|
||||
r#ref: ModelRef::new("openai_compat", "my-configured-model"),
|
||||
..Default::default()
|
||||
},
|
||||
..base_config(store, session_id, provider)
|
||||
};
|
||||
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
let Some(Event::SessionLoaded { session_id, messages }) = event_rx.recv().await else {
|
||||
panic!("engine must announce its initial session");
|
||||
};
|
||||
assert_eq!(session_id, expected_session_id);
|
||||
assert!(messages.is_empty());
|
||||
|
||||
// Prime the initial session so it carries a message; `New session` then forks a fresh session
|
||||
// rather than reusing the (empty) one.
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "prime".into(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
action_tx.send(Action::NewSession).await.unwrap();
|
||||
let new_session_id = loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::SessionLoaded { session_id, messages }) => {
|
||||
assert!(messages.is_empty());
|
||||
break session_id;
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("engine stopped before announcing the new session"),
|
||||
}
|
||||
};
|
||||
assert_ne!(new_session_id, expected_session_id);
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "hi".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
let seen = seen_model.lock().await.clone();
|
||||
assert_eq!(
|
||||
seen.as_deref(),
|
||||
Some("my-configured-model"),
|
||||
"engine must forward the configured model id to the provider"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn new_session_reuses_an_empty_session_instead_of_orphaning_it() {
|
||||
// Asking for a new session while the current one is still empty (header only) must reuse it,
|
||||
// not spawn an orphan `.jsonl`. This keeps the dashboard 'n' / `/new` flow from littering the
|
||||
// session store when the user hasn't typed anything yet.
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let sessions_root = tmp.path().join("sessions");
|
||||
let mut store = JsonlSessionStore::new(&sessions_root);
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let expected_session_id = session_id.to_string();
|
||||
|
||||
let config = base_config(
|
||||
store,
|
||||
session_id,
|
||||
Arc::new(CapturingProvider { seen_model: Arc::new(Mutex::new(None)) }),
|
||||
);
|
||||
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
assert!(matches!(event_rx.recv().await, Some(Event::SessionLoaded { .. })));
|
||||
|
||||
action_tx.send(Action::NewSession).await.unwrap();
|
||||
let reused = loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::SessionLoaded { session_id, messages }) => {
|
||||
assert!(messages.is_empty());
|
||||
break session_id;
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("engine must re-announce a session for a new-session request"),
|
||||
}
|
||||
};
|
||||
assert_eq!(reused, expected_session_id, "an empty session must be reused, not orphaned");
|
||||
|
||||
// The store must still hold exactly one session file (reuse, not a fresh orphan).
|
||||
fn count_jsonl(dir: &std::path::Path) -> usize {
|
||||
let mut count = 0;
|
||||
if let Ok(entries) = std::fs::read_dir(dir) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
count += count_jsonl(&path);
|
||||
} else if path.extension().is_some_and(|ext| ext == "jsonl") {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
assert_eq!(count_jsonl(&sessions_root), 1, "reuse must not create a second session file");
|
||||
}
|
||||
|
||||
/// A turn with no model chosen is refused, and the transcript is left alone.
|
||||
///
|
||||
/// Luxe resolves no default: with nothing chosen the front-end hands the engine a model with an
|
||||
/// empty id, and the gate at the top of every turn is what makes that safe. The provider is never
|
||||
/// reached, and the user's message is not committed — a refused turn must not leave a message in
|
||||
/// the history that would replay on every later request.
|
||||
#[tokio::test]
|
||||
async fn a_turn_with_no_model_is_refused_before_anything_is_committed() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let seen_model = Arc::new(Mutex::new(None));
|
||||
let provider = Arc::new(CapturingProvider { seen_model: seen_model.clone() });
|
||||
|
||||
// What `AgentSettings::launch_model` hands over when nobody has chosen: a provider, and no id.
|
||||
let unset = ModelInfo { r#ref: ModelRef::new("openai_compat", ""), ..Default::default() };
|
||||
assert!(unset.r#ref.is_unset());
|
||||
let config = EngineConfig { model: unset, ..base_config(store, session_id.clone(), provider) };
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "go".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let refusal =
|
||||
events_until_turn_end(&mut event_rx).await.into_iter().find_map(|event| match event {
|
||||
Event::Error(message) => Some(message),
|
||||
_ => None,
|
||||
});
|
||||
let refusal = refusal.expect("the refusal is reported, not swallowed");
|
||||
assert!(refusal.contains("no model selected"), "{refusal}");
|
||||
assert!(refusal.contains("/model"), "and says what to do about it: {refusal}");
|
||||
assert_eq!(*seen_model.lock().await, None, "the provider was never asked");
|
||||
|
||||
// A turn that never ran leaves no history behind it — the session's own header aside.
|
||||
let store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let entries = store.load(&session_id).unwrap();
|
||||
assert!(
|
||||
entries
|
||||
.iter()
|
||||
.all(|entry| matches!(entry.kind, luxe_core::session::EntryKind::Header { .. })),
|
||||
"the refused message was committed: {:?}",
|
||||
entries.iter().map(|entry| &entry.kind).collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
250
crates/luxe-core/tests/engine/remediation.rs
Normal file
250
crates/luxe-core/tests/engine/remediation.rs
Normal file
@@ -0,0 +1,250 @@
|
||||
//! Remediation — a request for fixes reaches the agent with its evidence, and a retry reuses it.
|
||||
|
||||
use crate::fixtures::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::stream::{self, BoxStream};
|
||||
use luxe_core::engine::{Action, Engine};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::provider::{CompletionRequest, Provider, ProviderError, ProviderEvent, StopReason};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
struct FeedbackRemediationProvider {
|
||||
requests: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
reply: String,
|
||||
failures_remaining: Mutex<usize>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for FeedbackRemediationProvider {
|
||||
fn name(&self) -> &str {
|
||||
"feedback-remediation"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
self.requests.lock().await.push(request);
|
||||
let mut failures_remaining = self.failures_remaining.lock().await;
|
||||
if *failures_remaining > 0 {
|
||||
*failures_remaining -= 1;
|
||||
return Err(ProviderError::Other("transient feedback failure".into()));
|
||||
}
|
||||
Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: self.reply.clone() },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn p32_request_fixes_reaches_agent_with_immutable_evidence_and_remediation() {
|
||||
use luxe_core::guided_review::{
|
||||
FindingAnchor, FindingId, FindingSeverity, ReviewFeedbackMode, ReviewId, SubmissionId,
|
||||
SubmittedFinding,
|
||||
};
|
||||
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let review_id = ReviewId::new();
|
||||
let source = review_source();
|
||||
let finding = SubmittedFinding {
|
||||
finding_id: FindingId::new(),
|
||||
label: "R1".into(),
|
||||
evidence: Default::default(),
|
||||
anchor: FindingAnchor::Review,
|
||||
severity: FindingSeverity::Issue,
|
||||
title: "Bound retries".into(),
|
||||
body: "Avoid an unbounded client wait.".into(),
|
||||
proposed_fix: Some("Return a timeout error.".into()),
|
||||
thread: Vec::new(),
|
||||
github_author: None,
|
||||
};
|
||||
let submission_id = SubmissionId::new();
|
||||
let reply = format!(
|
||||
"Fixed the timeout.\n```luxe-remediation\n{{\"review_id\":\"{}\",\"revision_id\":\"{}\",\"findings\":[{{\"finding_id\":\"{}\",\"status\":\"fixed\",\"note\":\"Added a bound.\"}}]}}\n```",
|
||||
review_id.as_str(),
|
||||
source.content_digest.as_str(),
|
||||
finding.finding_id.as_str(),
|
||||
);
|
||||
let requests = Arc::new(Mutex::new(Vec::new()));
|
||||
let config = base_config(
|
||||
store,
|
||||
session_id,
|
||||
Arc::new(FeedbackRemediationProvider {
|
||||
requests: requests.clone(),
|
||||
reply,
|
||||
failures_remaining: Mutex::new(0),
|
||||
}),
|
||||
);
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
assert!(matches!(event_rx.recv().await, Some(Event::SessionLoaded { .. })));
|
||||
|
||||
let feedback_action = Action::SubmitReviewFeedback {
|
||||
submission_id: submission_id.clone(),
|
||||
review_id: review_id.clone(),
|
||||
source: source.clone(),
|
||||
mode: ReviewFeedbackMode::RequestFixes,
|
||||
findings: vec![finding.clone()],
|
||||
};
|
||||
action_tx.send(feedback_action.clone()).await.unwrap();
|
||||
action_tx.send(feedback_action).await.unwrap();
|
||||
|
||||
let mut assistant_committed = false;
|
||||
let mut remediation_reported = false;
|
||||
let mut feedback_completed = false;
|
||||
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
while tokio::time::Instant::now() < deadline {
|
||||
match tokio::time::timeout(std::time::Duration::from_millis(200), event_rx.recv()).await {
|
||||
Ok(Some(Event::MessageCommitted { message, .. }))
|
||||
if message.role == luxe_core::message::Role::Assistant =>
|
||||
{
|
||||
assistant_committed = true;
|
||||
}
|
||||
Ok(Some(Event::RemediationReported { report, .. })) => {
|
||||
assert!(assistant_committed, "evidence follows the committed assistant message");
|
||||
assert_eq!(report.review_id, review_id);
|
||||
assert_eq!(report.revision_id, source.content_digest);
|
||||
assert_eq!(report.findings[0].finding_id, finding.finding_id);
|
||||
remediation_reported = true;
|
||||
}
|
||||
Ok(Some(Event::ReviewFeedbackTurnCompleted { submission_id: completed })) => {
|
||||
assert_eq!(completed, submission_id);
|
||||
feedback_completed = true;
|
||||
break;
|
||||
}
|
||||
Ok(Some(_)) | Err(_) => {}
|
||||
Ok(None) => break,
|
||||
}
|
||||
}
|
||||
|
||||
assert!(remediation_reported, "agent must report bounded remediation evidence");
|
||||
assert!(feedback_completed, "feedback turn must complete with its submission ID");
|
||||
|
||||
let requests = requests.lock().await;
|
||||
assert_eq!(
|
||||
requests.len(),
|
||||
1,
|
||||
"duplicate immutable submissions must still start only one agent turn"
|
||||
);
|
||||
let prompt = requests[0]
|
||||
.messages
|
||||
.iter()
|
||||
.find_map(|message| match (&message.role, message.content.first()) {
|
||||
(
|
||||
luxe_core::message::Role::User,
|
||||
Some(luxe_core::message::ContentBlock::Text(text)),
|
||||
) => Some(text),
|
||||
_ => None,
|
||||
})
|
||||
.expect("structured feedback user message");
|
||||
assert!(prompt.contains(review_id.as_str()));
|
||||
assert!(prompt.contains(source.content_digest.as_str()));
|
||||
assert!(prompt.contains(finding.finding_id.as_str()));
|
||||
assert!(prompt.contains("Do not rewrite unrelated code."));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn p32_retry_reuses_one_feedback_transcript_entry() {
|
||||
use luxe_core::guided_review::{
|
||||
FindingAnchor, FindingId, FindingSeverity, ReviewFeedbackMode, ReviewId, SubmissionId,
|
||||
SubmittedFinding,
|
||||
};
|
||||
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let sessions_root = tmp.path().join("sessions");
|
||||
let mut store = JsonlSessionStore::new(&sessions_root);
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let review_id = ReviewId::new();
|
||||
let source = review_source();
|
||||
let finding = SubmittedFinding {
|
||||
finding_id: FindingId::new(),
|
||||
label: "R1".into(),
|
||||
evidence: Default::default(),
|
||||
anchor: FindingAnchor::Review,
|
||||
severity: FindingSeverity::Issue,
|
||||
title: "Bound retries".into(),
|
||||
body: "Avoid an unbounded client wait.".into(),
|
||||
proposed_fix: None,
|
||||
thread: Vec::new(),
|
||||
github_author: None,
|
||||
};
|
||||
let submission_id = SubmissionId::new();
|
||||
let requests = Arc::new(Mutex::new(Vec::new()));
|
||||
// a compliant agent, so the only extra call is the retry itself
|
||||
let reply = format!(
|
||||
"Retry completed.\n```luxe-remediation\n{{\"review_id\":\"{}\",\"revision_id\":\"{}\",\"findings\":[{{\"finding_id\":\"{}\",\"status\":\"fixed\"}}]}}\n```",
|
||||
review_id.as_str(),
|
||||
source.content_digest.as_str(),
|
||||
finding.finding_id.as_str(),
|
||||
);
|
||||
let config = base_config(
|
||||
store,
|
||||
session_id.clone(),
|
||||
Arc::new(FeedbackRemediationProvider {
|
||||
requests: requests.clone(),
|
||||
reply,
|
||||
failures_remaining: Mutex::new(1),
|
||||
}),
|
||||
);
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
assert!(matches!(event_rx.recv().await, Some(Event::SessionLoaded { .. })));
|
||||
|
||||
let action = Action::SubmitReviewFeedback {
|
||||
submission_id: submission_id.clone(),
|
||||
review_id: review_id.clone(),
|
||||
source: source.clone(),
|
||||
mode: ReviewFeedbackMode::RequestFixes,
|
||||
findings: vec![finding],
|
||||
};
|
||||
action_tx.send(action.clone()).await.unwrap();
|
||||
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
loop {
|
||||
match tokio::time::timeout(std::time::Duration::from_millis(200), event_rx.recv()).await {
|
||||
Ok(Some(Event::ReviewFeedbackTurnFailed { submission_id: failed, .. })) => {
|
||||
assert_eq!(failed, submission_id);
|
||||
break;
|
||||
}
|
||||
Ok(Some(_)) | Err(_) => {}
|
||||
Ok(None) => panic!("engine stopped before the transient failure"),
|
||||
}
|
||||
assert!(tokio::time::Instant::now() < deadline, "feedback did not fail in time");
|
||||
}
|
||||
|
||||
action_tx.send(action).await.unwrap();
|
||||
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
loop {
|
||||
match tokio::time::timeout(std::time::Duration::from_millis(200), event_rx.recv()).await {
|
||||
Ok(Some(Event::ReviewFeedbackTurnCompleted { submission_id: completed })) => {
|
||||
assert_eq!(completed, submission_id);
|
||||
break;
|
||||
}
|
||||
Ok(Some(_)) | Err(_) => {}
|
||||
Ok(None) => panic!("engine stopped before completing retry"),
|
||||
}
|
||||
assert!(tokio::time::Instant::now() < deadline, "feedback retry did not finish in time");
|
||||
}
|
||||
|
||||
let persisted = JsonlSessionStore::new(sessions_root);
|
||||
let marker = format!("Submission: {}", submission_id.as_str());
|
||||
let entries = persisted.load(&session_id).unwrap();
|
||||
let feedback_entries = entries
|
||||
.iter()
|
||||
.filter(|entry| {
|
||||
matches!(&entry.kind, luxe_core::session::EntryKind::Message(message)
|
||||
if message.role == luxe_core::message::Role::User
|
||||
&& message.content.iter().any(|block| matches!(block,
|
||||
luxe_core::message::ContentBlock::Text(text)
|
||||
if text.lines().any(|line| line == marker))))
|
||||
})
|
||||
.count();
|
||||
assert_eq!(feedback_entries, 1, "same submission must reuse one transcript entry");
|
||||
assert_eq!(requests.lock().await.len(), 2, "retry must reach the provider once more");
|
||||
}
|
||||
|
||||
mod queued;
|
||||
mod reports;
|
||||
242
crates/luxe-core/tests/engine/remediation/queued.rs
Normal file
242
crates/luxe-core/tests/engine/remediation/queued.rs
Normal file
@@ -0,0 +1,242 @@
|
||||
//! Queued — feedback that arrives behind an active turn is cancelled or dispatched, never lost.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::stream::{self, BoxStream};
|
||||
use luxe_core::engine::{Action, Engine};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::provider::{CompletionRequest, Provider, ProviderError, ProviderEvent, StopReason};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
use tokio::sync::{Mutex, Notify};
|
||||
|
||||
use crate::fixtures::*;
|
||||
|
||||
struct BlockingFirstProvider {
|
||||
requests: Arc<Mutex<Vec<CompletionRequest>>>,
|
||||
started: Arc<Notify>,
|
||||
release: Arc<Notify>,
|
||||
calls: Mutex<usize>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for BlockingFirstProvider {
|
||||
fn name(&self) -> &str {
|
||||
"blocking-first"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
self.requests.lock().await.push(request);
|
||||
let first_call = {
|
||||
let mut calls = self.calls.lock().await;
|
||||
*calls += 1;
|
||||
*calls == 1
|
||||
};
|
||||
if first_call {
|
||||
self.started.notify_one();
|
||||
let release = self.release.clone();
|
||||
return Ok(Box::pin(stream::once(async move {
|
||||
release.notified().await;
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn }
|
||||
})));
|
||||
}
|
||||
Ok(Box::pin(stream::iter(vec![ProviderEvent::Done { reason: StopReason::EndTurn }])))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn p32_cancels_feedback_queued_behind_an_active_turn() {
|
||||
use luxe_core::guided_review::{
|
||||
FindingAnchor, FindingId, FindingSeverity, ReviewFeedbackMode, ReviewId, SubmissionId,
|
||||
SubmittedFinding,
|
||||
};
|
||||
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let sessions_root = tmp.path().join("sessions");
|
||||
let mut store = JsonlSessionStore::new(&sessions_root);
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let requests = Arc::new(Mutex::new(Vec::new()));
|
||||
let started = Arc::new(Notify::new());
|
||||
let release = Arc::new(Notify::new());
|
||||
let config = base_config(
|
||||
store,
|
||||
session_id.clone(),
|
||||
Arc::new(BlockingFirstProvider {
|
||||
requests: requests.clone(),
|
||||
started: started.clone(),
|
||||
release: release.clone(),
|
||||
calls: Mutex::new(0),
|
||||
}),
|
||||
);
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
assert!(matches!(event_rx.recv().await, Some(Event::SessionLoaded { .. })));
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "Keep the agent busy.".into(),
|
||||
images: Vec::new(),
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), started.notified())
|
||||
.await
|
||||
.expect("initial provider turn must start");
|
||||
|
||||
let submission_id = SubmissionId::new();
|
||||
let action = Action::SubmitReviewFeedback {
|
||||
submission_id: submission_id.clone(),
|
||||
review_id: ReviewId::new(),
|
||||
source: review_source(),
|
||||
mode: ReviewFeedbackMode::RequestFixes,
|
||||
findings: vec![SubmittedFinding {
|
||||
finding_id: FindingId::new(),
|
||||
label: "R1".into(),
|
||||
evidence: Default::default(),
|
||||
anchor: FindingAnchor::Review,
|
||||
severity: FindingSeverity::Issue,
|
||||
title: "Cancel me".into(),
|
||||
body: "This feedback must not run.".into(),
|
||||
proposed_fix: None,
|
||||
thread: Vec::new(),
|
||||
github_author: None,
|
||||
}],
|
||||
};
|
||||
action_tx.send(action).await.unwrap();
|
||||
action_tx
|
||||
.send(Action::CancelQueuedReviewFeedback { submission_id: submission_id.clone() })
|
||||
.await
|
||||
.unwrap();
|
||||
release.notify_one();
|
||||
|
||||
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
loop {
|
||||
match tokio::time::timeout(std::time::Duration::from_millis(100), event_rx.recv()).await {
|
||||
Ok(Some(Event::TurnEnded { .. })) => break,
|
||||
Ok(Some(_)) | Err(_) => {}
|
||||
Ok(None) => panic!("engine stopped before the active turn ended"),
|
||||
}
|
||||
assert!(tokio::time::Instant::now() < deadline, "active turn did not finish in time");
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||
|
||||
assert_eq!(requests.lock().await.len(), 1, "cancelled feedback must not reach the provider");
|
||||
let persisted = JsonlSessionStore::new(sessions_root);
|
||||
let marker = format!("Submission: {}", submission_id.as_str());
|
||||
let feedback_entries = persisted
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|entry| {
|
||||
matches!(&entry.kind, luxe_core::session::EntryKind::Message(message)
|
||||
if message.role == luxe_core::message::Role::User
|
||||
&& message.content.iter().any(|block| matches!(block,
|
||||
luxe_core::message::ContentBlock::Text(text)
|
||||
if text.lines().any(|line| line == marker))))
|
||||
})
|
||||
.count();
|
||||
assert_eq!(feedback_entries, 0, "cancelled feedback must not create a transcript entry");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn p32_dispatches_feedback_queued_behind_an_active_turn() {
|
||||
use luxe_core::guided_review::{
|
||||
FindingAnchor, FindingId, FindingSeverity, ReviewFeedbackMode, ReviewId, SubmissionId,
|
||||
SubmittedFinding,
|
||||
};
|
||||
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let sessions_root = tmp.path().join("sessions");
|
||||
let mut store = JsonlSessionStore::new(&sessions_root);
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let requests = Arc::new(Mutex::new(Vec::new()));
|
||||
let started = Arc::new(Notify::new());
|
||||
let release = Arc::new(Notify::new());
|
||||
let config = base_config(
|
||||
store,
|
||||
session_id.clone(),
|
||||
Arc::new(BlockingFirstProvider {
|
||||
requests: requests.clone(),
|
||||
started: started.clone(),
|
||||
release: release.clone(),
|
||||
calls: Mutex::new(0),
|
||||
}),
|
||||
);
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
assert!(matches!(event_rx.recv().await, Some(Event::SessionLoaded { .. })));
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "Keep the agent busy.".into(),
|
||||
images: Vec::new(),
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), started.notified())
|
||||
.await
|
||||
.expect("initial provider turn must start");
|
||||
|
||||
let submission_id = SubmissionId::new();
|
||||
action_tx
|
||||
.send(Action::SubmitReviewFeedback {
|
||||
submission_id: submission_id.clone(),
|
||||
review_id: ReviewId::new(),
|
||||
source: review_source(),
|
||||
mode: ReviewFeedbackMode::RequestFixes,
|
||||
findings: vec![SubmittedFinding {
|
||||
finding_id: FindingId::new(),
|
||||
label: "R1".into(),
|
||||
evidence: Default::default(),
|
||||
anchor: FindingAnchor::Review,
|
||||
severity: FindingSeverity::Issue,
|
||||
title: "Dispatch me".into(),
|
||||
body: "This feedback must run at the next boundary.".into(),
|
||||
proposed_fix: None,
|
||||
thread: Vec::new(),
|
||||
github_author: None,
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
release.notify_one();
|
||||
|
||||
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
let mut dispatched = false;
|
||||
loop {
|
||||
match tokio::time::timeout(std::time::Duration::from_millis(100), event_rx.recv()).await {
|
||||
Ok(Some(Event::ReviewFeedbackTurnDispatched { submission_id: boundary })) => {
|
||||
assert_eq!(boundary, submission_id);
|
||||
dispatched = true;
|
||||
}
|
||||
Ok(Some(Event::ReviewFeedbackTurnCompleted { submission_id: completed })) => {
|
||||
assert_eq!(completed, submission_id);
|
||||
break;
|
||||
}
|
||||
Ok(Some(_)) | Err(_) => {}
|
||||
Ok(None) => panic!("engine stopped before dispatching queued feedback"),
|
||||
}
|
||||
assert!(tokio::time::Instant::now() < deadline, "queued feedback did not dispatch in time");
|
||||
}
|
||||
|
||||
assert!(dispatched, "feedback completion must follow its dispatch-boundary event");
|
||||
assert_eq!(requests.lock().await.len(), 2, "feedback must dispatch after the active turn");
|
||||
let persisted = JsonlSessionStore::new(sessions_root);
|
||||
let marker = format!("Submission: {}", submission_id.as_str());
|
||||
let feedback_entries = persisted
|
||||
.load(&session_id)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|entry| {
|
||||
matches!(&entry.kind, luxe_core::session::EntryKind::Message(message)
|
||||
if message.role == luxe_core::message::Role::User
|
||||
&& message.content.iter().any(|block| matches!(block,
|
||||
luxe_core::message::ContentBlock::Text(text)
|
||||
if text.lines().any(|line| line == marker))))
|
||||
})
|
||||
.count();
|
||||
assert_eq!(feedback_entries, 1, "queued feedback must create one transcript entry");
|
||||
}
|
||||
493
crates/luxe-core/tests/engine/remediation/reports.rs
Normal file
493
crates/luxe-core/tests/engine/remediation/reports.rs
Normal file
@@ -0,0 +1,493 @@
|
||||
//! Reports — what the agent says about each comment, and what is said when it says nothing.
|
||||
|
||||
use std::process::Command;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::stream::{self, BoxStream};
|
||||
use luxe_core::engine::{Action, Engine};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::guided_review::{FindingAnchor, FindingSeverity, ReviewLoadRequestId, ReviewTarget};
|
||||
use luxe_core::provider::{CompletionRequest, Provider, ProviderError, ProviderEvent, StopReason};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::fixtures::*;
|
||||
|
||||
struct ToolIntermediateRemediationProvider {
|
||||
intermediate: String,
|
||||
calls: Mutex<usize>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for ToolIntermediateRemediationProvider {
|
||||
fn name(&self) -> &str {
|
||||
"tool-intermediate-remediation"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
_request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let first_call = {
|
||||
let mut calls = self.calls.lock().await;
|
||||
*calls += 1;
|
||||
*calls == 1
|
||||
};
|
||||
let events = if first_call {
|
||||
vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: self.intermediate.clone() },
|
||||
ProviderEvent::ToolCallStart {
|
||||
index: 0,
|
||||
id: "call_1".into(),
|
||||
name: "unknown".into(),
|
||||
},
|
||||
ProviderEvent::ToolCallDelta { index: 0, json_delta: "{}".into() },
|
||||
ProviderEvent::ToolCallEnd { index: 0 },
|
||||
ProviderEvent::Done { reason: StopReason::ToolCall },
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
ProviderEvent::TextDelta {
|
||||
index: 0,
|
||||
delta: "Finished after the tool result.".into(),
|
||||
},
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
]
|
||||
};
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn remediation_report_emits_after_its_assistant_message_commits() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let review_id = luxe_core::guided_review::ReviewId::new();
|
||||
let revision_id = luxe_core::guided_review::ReviewRevisionId::from_canonical_bytes(b"review");
|
||||
let finding_id = luxe_core::guided_review::FindingId::new();
|
||||
let reply = format!(
|
||||
"Addressed the retry path.\n```luxe-remediation\n{{\"review_id\":\"{}\",\"revision_id\":\"{}\",\"findings\":[{{\"finding_id\":\"{}\",\"status\":\"fixed\",\"note\":\"Added the missing bound.\"}}]}}\n```",
|
||||
review_id.as_str(),
|
||||
revision_id.as_str(),
|
||||
finding_id.as_str(),
|
||||
);
|
||||
let config = base_config(store, session_id, Arc::new(RemediationProvider { reply }));
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
assert!(matches!(event_rx.recv().await, Some(Event::SessionLoaded { .. })));
|
||||
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "Please address R1.".into(),
|
||||
images: Vec::new(),
|
||||
remediation: Some(luxe_core::engine::RemediationTurnContext {
|
||||
submission_id: None,
|
||||
review_id: review_id.clone(),
|
||||
revision_id: revision_id.clone(),
|
||||
finding_ids: vec![finding_id.clone()],
|
||||
answers_only: false,
|
||||
reports: true,
|
||||
}),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut committed_entry = None;
|
||||
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
while tokio::time::Instant::now() < deadline {
|
||||
match tokio::time::timeout(std::time::Duration::from_millis(200), event_rx.recv()).await {
|
||||
Ok(Some(Event::MessageCommitted { entry_id, message, .. }))
|
||||
if message.role == luxe_core::message::Role::Assistant =>
|
||||
{
|
||||
committed_entry = Some(entry_id);
|
||||
}
|
||||
Ok(Some(Event::RemediationReported { entry_id, report })) => {
|
||||
assert_eq!(committed_entry.as_ref(), Some(&entry_id));
|
||||
assert_eq!(report.review_id, review_id);
|
||||
assert_eq!(report.revision_id, revision_id);
|
||||
assert_eq!(report.findings[0].finding_id, finding_id);
|
||||
return;
|
||||
}
|
||||
Ok(Some(_)) | Err(_) => {}
|
||||
Ok(None) => break,
|
||||
}
|
||||
}
|
||||
panic!("engine did not emit the committed remediation report");
|
||||
}
|
||||
|
||||
/// A report written beside a tool call counts, and is read once, when the turn ends.
|
||||
///
|
||||
/// A model that reports and then runs one last check before a one-line sign-off used to lose its
|
||||
/// report: only the final message was read.
|
||||
#[tokio::test]
|
||||
async fn a_report_beside_a_tool_call_is_read_when_the_turn_ends() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let review_id = luxe_core::guided_review::ReviewId::new();
|
||||
let revision_id = luxe_core::guided_review::ReviewRevisionId::from_canonical_bytes(b"review");
|
||||
let finding_id = luxe_core::guided_review::FindingId::new();
|
||||
let intermediate = format!(
|
||||
"```luxe-remediation\n{{\"review_id\":\"{}\",\"revision_id\":\"{}\",\"findings\":[{{\"finding_id\":\"{}\",\"status\":\"fixed\"}}]}}\n```",
|
||||
review_id.as_str(),
|
||||
revision_id.as_str(),
|
||||
finding_id.as_str(),
|
||||
);
|
||||
let config = base_config(
|
||||
store,
|
||||
session_id,
|
||||
Arc::new(ToolIntermediateRemediationProvider { intermediate, calls: Mutex::new(0) }),
|
||||
);
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
assert!(matches!(event_rx.recv().await, Some(Event::SessionLoaded { .. })));
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "Please address R1.".into(),
|
||||
images: Vec::new(),
|
||||
remediation: Some(luxe_core::engine::RemediationTurnContext {
|
||||
submission_id: None,
|
||||
review_id,
|
||||
revision_id,
|
||||
finding_ids: vec![finding_id.clone()],
|
||||
answers_only: false,
|
||||
reports: true,
|
||||
}),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut last_reply = None;
|
||||
let mut reported = Vec::new();
|
||||
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
while tokio::time::Instant::now() < deadline {
|
||||
match tokio::time::timeout(std::time::Duration::from_millis(200), event_rx.recv()).await {
|
||||
Ok(Some(Event::MessageCommitted { entry_id, message, .. }))
|
||||
if message.role == luxe_core::message::Role::Assistant =>
|
||||
{
|
||||
last_reply = Some(entry_id);
|
||||
}
|
||||
Ok(Some(Event::RemediationReported { entry_id, report })) => {
|
||||
reported.push((entry_id, report));
|
||||
}
|
||||
Ok(Some(Event::TurnEnded { .. })) => {
|
||||
assert_eq!(reported.len(), 1, "read once, at the end");
|
||||
let (entry_id, report) = &reported[0];
|
||||
assert_eq!(Some(entry_id), last_reply.as_ref(), "against the reply that ended it");
|
||||
assert_eq!(report.findings[0].finding_id, finding_id);
|
||||
return;
|
||||
}
|
||||
Ok(Some(_)) | Err(_) => {}
|
||||
Ok(None) => break,
|
||||
}
|
||||
}
|
||||
panic!("engine did not complete the scripted tool turn");
|
||||
}
|
||||
|
||||
/// A Guide like [`GuideManifestProvider`]'s, and fix turns answered from a script.
|
||||
///
|
||||
/// Every call a fix turn makes — the feedback itself, or Luxe asking for the report — takes the
|
||||
/// next reply, with `{review}`, `{revision}`, `{first}` and `{second}` filled in from the feedback.
|
||||
/// [`UNTIL_CANCELLED`] answers nothing until the turn is cancelled, and [`CUT_OFF`] stops partway
|
||||
/// through a tool call's arguments, as a reply that hit its token limit does.
|
||||
#[derive(Default)]
|
||||
struct ScriptedFixes {
|
||||
guide: GuideManifestProvider,
|
||||
replies: std::sync::Mutex<std::collections::VecDeque<&'static str>>,
|
||||
/// The review, the revision, then each finding, as the feedback named them
|
||||
ids: std::sync::Mutex<Vec<String>>,
|
||||
/// What each fix-turn call was sent last
|
||||
asked: std::sync::Mutex<Vec<String>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for ScriptedFixes {
|
||||
fn name(&self) -> &str {
|
||||
"scripted-fixes"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
request: CompletionRequest,
|
||||
cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let last = request.messages.last().and_then(|message| {
|
||||
message.content.iter().find_map(|block| match block {
|
||||
luxe_core::message::ContentBlock::Text(text) => Some(text.clone()),
|
||||
_ => None,
|
||||
})
|
||||
});
|
||||
let Some(text) = last.filter(|text| text.starts_with("Guided review ")) else {
|
||||
return self.guide.stream(request, cancel).await;
|
||||
};
|
||||
if text.starts_with("Guided review feedback") {
|
||||
let field = |prefix: &str| -> Vec<String> {
|
||||
text.lines()
|
||||
.filter_map(|line| line.trim_start().strip_prefix(prefix).map(str::to_owned))
|
||||
.collect()
|
||||
};
|
||||
let mut ids = field("Review: ");
|
||||
ids.extend(field("Source revision: "));
|
||||
ids.extend(field("Finding ID: "));
|
||||
*self.ids.lock().unwrap() = ids;
|
||||
}
|
||||
self.asked.lock().unwrap().push(text);
|
||||
let ids = self.ids.lock().unwrap().clone();
|
||||
let next = self.replies.lock().unwrap().pop_front();
|
||||
let next = next.expect("a scripted reply for every fix-turn call");
|
||||
if next == CUT_OFF {
|
||||
return Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::ToolCallStart {
|
||||
index: 0,
|
||||
id: "call_1".into(),
|
||||
name: "write".into(),
|
||||
},
|
||||
ProviderEvent::ToolCallDelta { index: 0, json_delta: "{\"path\": \"stat".into() },
|
||||
ProviderEvent::Done { reason: StopReason::Length },
|
||||
])));
|
||||
}
|
||||
if next == UNTIL_CANCELLED {
|
||||
let mut cancel = cancel;
|
||||
return Ok(Box::pin(stream::once(async move {
|
||||
while !*cancel.borrow() {
|
||||
if cancel.changed().await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn }
|
||||
})));
|
||||
}
|
||||
let reply = next
|
||||
.replace("{review}", &ids[0])
|
||||
.replace("{revision}", &ids[1])
|
||||
.replace("{first}", &ids[2])
|
||||
.replace("{second}", &ids[3]);
|
||||
Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: reply },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])))
|
||||
}
|
||||
}
|
||||
|
||||
/// A scripted reply that waits for the turn to be cancelled.
|
||||
const UNTIL_CANCELLED: &str = "<until cancelled>";
|
||||
|
||||
/// A scripted reply cut off in the middle of a tool call.
|
||||
const CUT_OFF: &str = "<cut off>";
|
||||
|
||||
/// Sends two comments to be fixed by an agent that answers with `replies`, and returns the
|
||||
/// session once the turn is done, with what each of the turn's calls was sent. A reply of
|
||||
/// [`UNTIL_CANCELLED`] is cancelled, the way a reader stops a turn, once the turn starts.
|
||||
async fn fix_two_comments(
|
||||
replies: &[&'static str],
|
||||
) -> (luxe_core::guided_review::ReviewSession, Vec<String>) {
|
||||
use luxe_core::guided_review::{
|
||||
ReviewFeedbackMode, ReviewSessionSaveRequestId, SubmissionDispatch,
|
||||
};
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let project_root = temp_dir.path();
|
||||
assert!(Command::new("git").arg("init").current_dir(project_root).status().unwrap().success());
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn original() {}\n").unwrap();
|
||||
for arguments in [
|
||||
["config", "user.email", "tests@example.invalid"].as_slice(),
|
||||
["config", "user.name", "Luxe Tests"].as_slice(),
|
||||
["add", "tracked.rs"].as_slice(),
|
||||
["commit", "-m", "initial fixture"].as_slice(),
|
||||
] {
|
||||
assert!(Command::new("git")
|
||||
.args(arguments)
|
||||
.current_dir(project_root)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success());
|
||||
}
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn changed() {}\n").unwrap();
|
||||
let session_id = JsonlSessionStore::new(project_root.to_path_buf())
|
||||
.create(&project_root.display().to_string())
|
||||
.unwrap();
|
||||
let provider = Arc::new(ScriptedFixes {
|
||||
replies: std::sync::Mutex::new(replies.iter().copied().collect()),
|
||||
..Default::default()
|
||||
});
|
||||
let mut config = review_engine_config(project_root, session_id);
|
||||
config.provider = provider.clone();
|
||||
let (actions, mut events) = Engine::spawn(config);
|
||||
|
||||
let request_id = ReviewLoadRequestId::new();
|
||||
actions
|
||||
.send(Action::LoadGuidedReview {
|
||||
request_id: request_id.clone(),
|
||||
target: ReviewTarget::WorkingTree,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let review = first(&mut events, "the review", |event| match event {
|
||||
Event::GuidedReviewLoaded { request_id: loaded, review } if loaded == request_id => {
|
||||
Some(review)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
let mut finding_ids = Vec::new();
|
||||
for title in ["Keep the function focused", "Name it for what it does"] {
|
||||
let request_id = ReviewSessionSaveRequestId::new();
|
||||
actions
|
||||
.send(Action::EditReview {
|
||||
request_id: request_id.clone(),
|
||||
review_id: review.id.clone(),
|
||||
edit: luxe_core::engine::ReviewEdit::Comment {
|
||||
draft: luxe_core::engine::CommentDraft {
|
||||
anchor: FindingAnchor::Review,
|
||||
severity: FindingSeverity::Issue,
|
||||
title: title.into(),
|
||||
body: format!("{title}, please."),
|
||||
proposed_fix: None,
|
||||
},
|
||||
send: false,
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let saved = first(&mut events, "the comment", |event| match event {
|
||||
Event::ReviewSessionSaved { request_id: saved, session } if saved == request_id => {
|
||||
Some(session)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
finding_ids.push(saved.findings.last().unwrap().id.clone());
|
||||
}
|
||||
actions
|
||||
.send(Action::EditReview {
|
||||
request_id: ReviewSessionSaveRequestId::new(),
|
||||
review_id: review.id.clone(),
|
||||
edit: luxe_core::engine::ReviewEdit::Send {
|
||||
finding_ids,
|
||||
mode: Some(ReviewFeedbackMode::RequestFixes),
|
||||
outcome: None,
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let stops = replies.contains(&UNTIL_CANCELLED);
|
||||
let finished = first(&mut events, "the finished fix turn", |event| match event {
|
||||
Event::ReviewFeedbackTurnDispatched { .. } if stops => {
|
||||
let actions = actions.clone();
|
||||
tokio::spawn(async move { actions.send(Action::Cancel).await });
|
||||
None
|
||||
}
|
||||
Event::ReviewSessionSaved { session, .. }
|
||||
if session.submissions.first().map(|s| s.dispatch)
|
||||
== Some(SubmissionDispatch::Completed) =>
|
||||
{
|
||||
Some(session)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
actions.send(Action::Close).await.unwrap();
|
||||
tokio::time::timeout(EVENT_WAIT, async { while events.recv().await.is_some() {} })
|
||||
.await
|
||||
.expect("the engine shuts down on close");
|
||||
let asked = provider.asked.lock().unwrap().clone();
|
||||
(*finished, asked)
|
||||
}
|
||||
|
||||
/// What Luxe said on a comment's thread, in its own name.
|
||||
fn luxe_notes(finding: &luxe_core::guided_review::ReviewFinding) -> Vec<&str> {
|
||||
use luxe_core::guided_review::FindingMessageAuthor;
|
||||
let notes = finding.thread.iter().filter(|m| m.author == FindingMessageAuthor::System);
|
||||
notes.map(|m| m.body.as_str()).collect()
|
||||
}
|
||||
|
||||
/// A fix turn that ends without a report is asked for it once, and the answer counts — even in a
|
||||
/// `json` fence, which is how a correct report used to be thrown away.
|
||||
#[tokio::test]
|
||||
async fn a_missing_report_is_asked_for_once_and_the_answer_counts() {
|
||||
use luxe_core::guided_review::FindingState;
|
||||
let (session, asked) = fix_two_comments(&[
|
||||
"Fixed both comments.",
|
||||
"```json\n{\"review_id\":\"{review}\",\"revision_id\":\"{revision}\",\"findings\":[\
|
||||
{\"finding_id\":\"{first}\",\"status\":\"fixed\",\"note\":\"Split it.\"},\
|
||||
{\"finding_id\":\"{second}\",\"status\":\"Fixed\",\"note\":\"Renamed it.\"}]}\n```",
|
||||
])
|
||||
.await;
|
||||
assert_eq!(asked.len(), 2, "the feedback, then one ask");
|
||||
assert!(asked[1].starts_with("Guided review report\n"), "{}", asked[1]);
|
||||
assert!(asked[1].contains("it wrote no report"), "the ask says what was wrong: {}", asked[1]);
|
||||
assert!(asked[1].contains("```luxe-remediation\n"), "and carries the template: {}", asked[1]);
|
||||
assert!(!asked[1].contains("Submission: "), "a retry must not mistake it for the feedback");
|
||||
assert_eq!(luxe_core::briefs::label(&asked[1]), Some("Asked the agent for its report"));
|
||||
for (finding, note) in session.findings.iter().zip(["Split it.", "Renamed it."]) {
|
||||
assert_eq!(finding.state, FindingState::NeedsVerification, "{}", finding.title);
|
||||
assert!(finding.thread.iter().any(|m| m.body.contains(note)), "{:?}", finding.thread);
|
||||
assert!(luxe_notes(finding).is_empty(), "nothing went unreported: {:?}", finding.thread);
|
||||
}
|
||||
}
|
||||
|
||||
/// An agent that cannot produce a usable report is asked once, not again and again, and then
|
||||
/// each comment says so, and why, in Luxe's name; the comments stay where the reviewer left them.
|
||||
#[tokio::test]
|
||||
async fn an_agent_that_never_reports_leaves_word_on_each_comment() {
|
||||
use luxe_core::guided_review::FindingState;
|
||||
let (session, asked) = fix_two_comments(&[
|
||||
"Fixed both comments.",
|
||||
"```luxe-remediation\n{\"review_id\":\"{review}\",\"revision_id\":\"revision:stale\",\
|
||||
\"findings\":[{\"finding_id\":\"{first}\",\"status\":\"fixed\"}]}\n```",
|
||||
])
|
||||
.await;
|
||||
assert_eq!(asked.len(), 2, "asked once, then the turn ends");
|
||||
for finding in &session.findings {
|
||||
assert_eq!(finding.state, FindingState::Open, "only the reviewer moves a comment");
|
||||
assert_eq!(
|
||||
luxe_notes(finding),
|
||||
["No word from the agent on this comment: its report was for a different version of \
|
||||
the code. Refresh the review to see the code as it is now."],
|
||||
"{}",
|
||||
finding.title
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A report that speaks for some of the comments counts for those, without an ask, and the one it
|
||||
/// left out says so.
|
||||
#[tokio::test]
|
||||
async fn a_report_that_leaves_a_comment_out_says_so_on_that_comment() {
|
||||
use luxe_core::guided_review::FindingState;
|
||||
let (session, asked) = fix_two_comments(&[
|
||||
"Split it.\n```luxe-remediation\n{\"review_id\":\"{review}\",\"revision_id\":\"{revision}\",\
|
||||
\"findings\":[{\"finding_id\":\"{first}\",\"status\":\"fixed\"}]}\n```",
|
||||
])
|
||||
.await;
|
||||
assert_eq!(asked.len(), 1, "a usable report is not asked for again");
|
||||
let (reported, left_out) = (&session.findings[0], &session.findings[1]);
|
||||
assert_eq!(reported.state, FindingState::NeedsVerification);
|
||||
assert!(luxe_notes(reported).is_empty());
|
||||
assert_eq!(left_out.state, FindingState::Open);
|
||||
assert_eq!(luxe_notes(left_out).len(), 1);
|
||||
assert!(luxe_notes(left_out)[0].contains("its report left this comment out"));
|
||||
}
|
||||
|
||||
/// A reader who stops a fix turn is told, on each comment, that it stopped before the agent said
|
||||
/// anything, not that the agent wrote no report; and a stopped turn is not asked for one.
|
||||
#[tokio::test]
|
||||
async fn a_stopped_fix_turn_says_so_on_each_comment() {
|
||||
let (session, asked) = fix_two_comments(&[UNTIL_CANCELLED]).await;
|
||||
assert_eq!(asked.len(), 1, "a stopped turn is not asked for its report");
|
||||
for finding in &session.findings {
|
||||
assert_eq!(luxe_notes(finding).len(), 1, "{:?}", finding.thread);
|
||||
assert!(luxe_notes(finding)[0].contains("the turn was stopped before it reported"));
|
||||
}
|
||||
}
|
||||
|
||||
/// A fix turn whose reply was cut off says so on each comment, rather than blaming the agent for
|
||||
/// writing no report; a turn that cannot go on is not asked for one.
|
||||
#[tokio::test]
|
||||
async fn a_cut_off_fix_turn_says_so_on_each_comment() {
|
||||
let (session, asked) = fix_two_comments(&[CUT_OFF]).await;
|
||||
assert_eq!(asked.len(), 1);
|
||||
for finding in &session.findings {
|
||||
assert_eq!(luxe_notes(finding).len(), 1, "{:?}", finding.thread);
|
||||
assert!(luxe_notes(finding)[0].contains("its reply was cut off before the report"));
|
||||
}
|
||||
}
|
||||
555
crates/luxe-core/tests/engine/review_restore.rs
Normal file
555
crates/luxe-core/tests/engine/review_restore.rs
Normal file
@@ -0,0 +1,555 @@
|
||||
//! Review restore — the engine owns a guided review through load, refresh, close and restart.
|
||||
|
||||
use std::process::Command;
|
||||
use std::sync::Arc;
|
||||
|
||||
use luxe_core::engine::{Action, Engine, EngineConfig};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::guided_review::{
|
||||
ActiveReviewRecordStore, AgentTurnId, FindingAnchor, FindingId, FindingSeverity,
|
||||
LastTurnRecord, LastTurnRecordStore, LocalGit, ReviewFeedbackMode, ReviewLoadRequestId,
|
||||
ReviewTarget, SubmissionId, SubmittedFinding, UntrackedSourcePolicy,
|
||||
};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
|
||||
use crate::fixtures::*;
|
||||
|
||||
/// A review of the last turn loads from the baseline the turn advertised, then refreshes in place
|
||||
/// and refuses a turn it never retained.
|
||||
///
|
||||
/// Two turns run first so the review has a real baseline to name; what those turns do is
|
||||
/// `turns`'s business.
|
||||
#[tokio::test]
|
||||
async fn a_last_turn_review_loads_the_turn_that_ran_and_refreshes_in_place() {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let tmp_dir = temp_dir.path();
|
||||
assert!(Command::new("git").arg("init").current_dir(tmp_dir).status().unwrap().success());
|
||||
|
||||
// Create a test file for the read tool.
|
||||
std::fs::write(tmp_dir.join("test.txt"), "Hello, World!").unwrap();
|
||||
for arguments in [
|
||||
["config", "user.email", "tests@example.invalid"].as_slice(),
|
||||
["config", "user.name", "Luxe Tests"].as_slice(),
|
||||
["add", "test.txt"].as_slice(),
|
||||
["commit", "-m", "initial fixture"].as_slice(),
|
||||
] {
|
||||
assert!(Command::new("git")
|
||||
.args(arguments)
|
||||
.current_dir(tmp_dir)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success());
|
||||
}
|
||||
|
||||
// Luxe's own planning artifact, not the harness it happens to be developed with.
|
||||
std::fs::write(tmp_dir.join("PLAN.md"), "review plan evidence").unwrap();
|
||||
|
||||
// Setup session store.
|
||||
let mut store = JsonlSessionStore::new(tmp_dir.to_path_buf());
|
||||
let session_id = store.create(&tmp_dir.display().to_string()).unwrap();
|
||||
|
||||
let config = EngineConfig {
|
||||
guided_review_project_root: Some(tmp_dir.to_path_buf()),
|
||||
..base_config(store, session_id.clone(), Arc::new(MockProvider::new()))
|
||||
};
|
||||
|
||||
// Spawn the engine.
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
|
||||
// Submit a user message.
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::SubmitUserMessage {
|
||||
content: "Read test.txt".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
// Submit another message for second turn.
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::SubmitUserMessage {
|
||||
content: "Edit test.txt".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let events_turn2 = events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
let (baseline_session_id, baseline_turn_id) = events_turn2
|
||||
.iter()
|
||||
.find_map(|event| match event {
|
||||
Event::LastTurnAvailable { session_id, turn_id } => {
|
||||
Some((session_id.clone(), turn_id.clone()))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.expect("successful pre-turn capture should advertise its exact LastTurn identity");
|
||||
let request_id = luxe_core::guided_review::ReviewLoadRequestId::new();
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::LoadGuidedReview {
|
||||
request_id: request_id.clone(),
|
||||
target: luxe_core::guided_review::ReviewTarget::LastTurn {
|
||||
session_id: baseline_session_id,
|
||||
turn_id: baseline_turn_id,
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let mut saw_loading = false;
|
||||
let loaded_review = loop {
|
||||
match tokio::time::timeout(EVENT_WAIT, event_rx.recv())
|
||||
.await
|
||||
.expect("matching LastTurn request should resolve")
|
||||
{
|
||||
Some(Event::GuidedReviewLoading { request_id: loading, .. })
|
||||
if loading == request_id =>
|
||||
{
|
||||
saw_loading = true
|
||||
}
|
||||
Some(Event::GuidedReviewLoaded { request_id: loaded, review })
|
||||
if loaded == request_id =>
|
||||
{
|
||||
assert!(saw_loading, "LastTurn review should report its loading state first");
|
||||
break review;
|
||||
}
|
||||
Some(Event::GuidedReviewFailed { error, .. }) => {
|
||||
panic!("matching retained LastTurn request failed: {}", error.message)
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("Event channel closed unexpectedly"),
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(loaded_review.context.goal.as_deref(), Some("Edit test.txt"));
|
||||
assert_eq!(loaded_review.context.planning_excerpt.as_deref(), Some("review plan evidence"));
|
||||
let expected_revision = loaded_review.revision_id.clone();
|
||||
let expected_review_id = loaded_review.id.clone();
|
||||
let refresh_request = luxe_core::guided_review::ReviewLoadRequestId::new();
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::RefreshGuidedReview {
|
||||
request_id: refresh_request.clone(),
|
||||
review_id: loaded_review.id.clone(),
|
||||
expected_revision,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let mut saw_refresh_loading = false;
|
||||
loop {
|
||||
match tokio::time::timeout(EVENT_WAIT, event_rx.recv())
|
||||
.await
|
||||
.expect("refresh request should resolve")
|
||||
{
|
||||
Some(Event::GuidedReviewLoading { request_id, .. })
|
||||
if request_id == refresh_request =>
|
||||
{
|
||||
saw_refresh_loading = true;
|
||||
}
|
||||
Some(Event::GuidedReviewLoaded { request_id, review })
|
||||
if request_id == refresh_request =>
|
||||
{
|
||||
assert!(saw_refresh_loading, "refresh should report its loading state first");
|
||||
assert_eq!(review.id, expected_review_id);
|
||||
break;
|
||||
}
|
||||
Some(Event::GuidedReviewFailed { error, .. }) => {
|
||||
panic!("refreshing the pinned review failed: {}", error.message)
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("Event channel closed unexpectedly"),
|
||||
}
|
||||
}
|
||||
|
||||
let request_id = luxe_core::guided_review::ReviewLoadRequestId::new();
|
||||
let target = luxe_core::guided_review::ReviewTarget::LastTurn {
|
||||
session_id: session_id.to_string(),
|
||||
turn_id: "unretained-turn".into(),
|
||||
};
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::LoadGuidedReview {
|
||||
request_id: request_id.clone(),
|
||||
target: target.clone(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
// A background Guide pass for the earlier review may still report in, so wait for the
|
||||
// failure rather than assuming it is the very next event.
|
||||
let (failed_request, failed_target, error) = tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::GuidedReviewFailed { request_id, target, error }) => {
|
||||
break (request_id, target, error)
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("expected typed unavailable failure for unretained LastTurn"),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("unretained LastTurn request should resolve");
|
||||
assert_eq!(failed_request, request_id);
|
||||
assert_eq!(failed_target, target);
|
||||
assert_eq!(error.kind, luxe_core::event::GuidedReviewFailureKind::Unavailable);
|
||||
|
||||
// Cancel and close.
|
||||
action_tx.send(luxe_core::engine::Action::Cancel).await.unwrap();
|
||||
action_tx.send(luxe_core::engine::Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn p32_guided_review_refresh_persists_a_fresh_artifact_after_worktree_mutation() {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let project_root = temp_dir.path();
|
||||
assert!(Command::new("git").arg("init").current_dir(project_root).status().unwrap().success());
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn original() {}\n").unwrap();
|
||||
for arguments in [
|
||||
["config", "user.email", "tests@example.invalid"].as_slice(),
|
||||
["config", "user.name", "Luxe Tests"].as_slice(),
|
||||
["add", "tracked.rs"].as_slice(),
|
||||
["commit", "-m", "initial fixture"].as_slice(),
|
||||
] {
|
||||
assert!(Command::new("git")
|
||||
.args(arguments)
|
||||
.current_dir(project_root)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success());
|
||||
}
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn changed() {}\n").unwrap();
|
||||
|
||||
let mut store = JsonlSessionStore::new(project_root.to_path_buf());
|
||||
let session_id = store.create(&project_root.display().to_string()).unwrap();
|
||||
let provider = Arc::new(GuideManifestProvider::default());
|
||||
let config = EngineConfig {
|
||||
guided_review_project_root: Some(project_root.to_path_buf()),
|
||||
..base_config(store, session_id.clone(), provider.clone())
|
||||
};
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
let request_id = ReviewLoadRequestId::new();
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::LoadGuidedReview {
|
||||
request_id: request_id.clone(),
|
||||
target: ReviewTarget::WorkingTree,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let review = tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::GuidedReviewLoaded { request_id: loaded_request, review })
|
||||
if loaded_request == request_id =>
|
||||
{
|
||||
break review
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("engine event channel closed before review loaded"),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("guided review load timed out");
|
||||
|
||||
// The review opens on deterministic chapters so the diff never waits on a model...
|
||||
assert_eq!(review.chapters[0].title, "Core implementation");
|
||||
let record = ActiveReviewRecordStore::new(project_root).load().unwrap().unwrap();
|
||||
assert_eq!(record.review_id, review.id);
|
||||
assert_eq!(record.revision_id, review.revision_id);
|
||||
|
||||
// ...and the model-authored Guide lands afterwards as an in-place upgrade for that revision.
|
||||
let (chapters, provenance) = tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::GuidedReviewGuideUpdated {
|
||||
review_id,
|
||||
revision_id,
|
||||
chapters,
|
||||
provenance,
|
||||
..
|
||||
}) if review_id == review.id && revision_id == review.revision_id => {
|
||||
break (chapters, provenance)
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("engine event channel closed before the Guide arrived"),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("guide generation timed out");
|
||||
assert_eq!(chapters[0].title, "Model Guide");
|
||||
assert!(matches!(provenance, luxe_core::guided_review::GuideProvenance::Model { .. }));
|
||||
// A restart reopens the chaptered Guide, not the structural placeholder it started from.
|
||||
let record = ActiveReviewRecordStore::new(project_root).load().unwrap().unwrap();
|
||||
assert_eq!(record.chapters[0].title, "Model Guide");
|
||||
|
||||
// The same change again gets the guide it already has, without asking the model twice
|
||||
let asked = provider.guides.load(std::sync::atomic::Ordering::SeqCst);
|
||||
assert!(asked > 0);
|
||||
let again = ReviewLoadRequestId::new();
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::LoadGuidedReview {
|
||||
request_id: again.clone(),
|
||||
target: ReviewTarget::WorkingTree,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let (chapters, provenance) = tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::GuidedReviewGuideUpdated {
|
||||
request_id, chapters, provenance, ..
|
||||
}) if request_id == again => break (chapters, provenance),
|
||||
Some(_) => {}
|
||||
None => panic!("engine event channel closed before the reused Guide arrived"),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("the reused guide arrives");
|
||||
assert_eq!(chapters[0].title, "Model Guide");
|
||||
assert!(matches!(provenance, luxe_core::guided_review::GuideProvenance::Model { .. }));
|
||||
assert_eq!(provider.guides.load(std::sync::atomic::Ordering::SeqCst), asked, "no second pass");
|
||||
// The feedback below goes to the review the engine now holds, which is this second one
|
||||
let review = Box::new(
|
||||
ActiveReviewRecordStore::new(project_root)
|
||||
.load()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.restore(project_root, &luxe_core::guided_review::ReviewExclusions::default())
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let finding = SubmittedFinding {
|
||||
finding_id: FindingId::new(),
|
||||
label: "R1".into(),
|
||||
evidence: Default::default(),
|
||||
anchor: FindingAnchor::Review,
|
||||
severity: FindingSeverity::Issue,
|
||||
title: "Keep the tracked function focused".into(),
|
||||
body: "The reviewed source needs the bounded follow-up change.".into(),
|
||||
proposed_fix: None,
|
||||
thread: Vec::new(),
|
||||
github_author: None,
|
||||
};
|
||||
let submission_id = SubmissionId::new();
|
||||
action_tx
|
||||
.send(Action::SubmitReviewFeedback {
|
||||
submission_id: submission_id.clone(),
|
||||
review_id: review.id.clone(),
|
||||
source: review.source.clone(),
|
||||
mode: ReviewFeedbackMode::RequestFixes,
|
||||
findings: vec![finding.clone()],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let mut remediation_seen = false;
|
||||
let feedback_deadline = tokio::time::Instant::now() + EVENT_WAIT;
|
||||
while tokio::time::Instant::now() < feedback_deadline {
|
||||
match tokio::time::timeout(std::time::Duration::from_millis(200), event_rx.recv()).await {
|
||||
Ok(Some(Event::RemediationReported { report, .. })) => {
|
||||
assert_eq!(report.review_id, review.id);
|
||||
assert_eq!(report.revision_id, review.source.content_digest);
|
||||
assert_eq!(report.findings[0].finding_id, finding.finding_id);
|
||||
remediation_seen = true;
|
||||
}
|
||||
Ok(Some(Event::ReviewFeedbackTurnCompleted { submission_id: completed })) => {
|
||||
assert_eq!(completed, submission_id);
|
||||
break;
|
||||
}
|
||||
Ok(Some(_)) | Err(_) => {}
|
||||
Ok(None) => panic!("engine event channel closed before feedback completion"),
|
||||
}
|
||||
}
|
||||
assert!(remediation_seen, "the pinned feedback emits remediation evidence");
|
||||
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn later_mutation() {}\n").unwrap();
|
||||
let refresh_request = ReviewLoadRequestId::new();
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::RefreshGuidedReview {
|
||||
request_id: refresh_request.clone(),
|
||||
expected_revision: review.revision_id.clone(),
|
||||
review_id: review.id.clone(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let refreshed = tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::GuidedReviewLoaded { request_id: loaded_request, review })
|
||||
if loaded_request == refresh_request =>
|
||||
{
|
||||
break review
|
||||
}
|
||||
Some(Event::GuidedReviewFailed { error, .. }) => {
|
||||
panic!("fresh review failed: {}", error.message)
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("engine event channel closed before refreshed review loaded"),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("guided review refresh timed out");
|
||||
assert_eq!(refreshed.id, review.id);
|
||||
assert_ne!(refreshed.revision_id, review.revision_id);
|
||||
assert_ne!(refreshed.source.content_digest, review.source.content_digest);
|
||||
let refreshed_lines = refreshed
|
||||
.files
|
||||
.iter()
|
||||
.flat_map(|file| &file.hunks)
|
||||
.flat_map(|hunk| &hunk.lines)
|
||||
.map(|line| line.text.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
assert!(refreshed_lines.iter().any(|line| line.contains("fn later_mutation()")));
|
||||
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn final_mutation() {}\n").unwrap();
|
||||
drop(action_tx);
|
||||
|
||||
let resumed_config = EngineConfig {
|
||||
guided_review_project_root: Some(project_root.to_path_buf()),
|
||||
..base_config(
|
||||
JsonlSessionStore::new(project_root.to_path_buf()),
|
||||
session_id,
|
||||
Arc::new(MockProvider::new()),
|
||||
)
|
||||
};
|
||||
let (_resumed_actions, mut resumed_events) = Engine::spawn(resumed_config);
|
||||
let restored = tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match resumed_events.recv().await {
|
||||
Some(Event::GuidedReviewLoaded { review, .. }) => break review,
|
||||
Some(_) => {}
|
||||
None => panic!("resumed engine event channel closed before review loaded"),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("persisted guided review restore timed out");
|
||||
|
||||
assert_eq!(restored.id, refreshed.id);
|
||||
assert_eq!(restored.revision_id, refreshed.revision_id);
|
||||
let restored_lines = restored
|
||||
.files
|
||||
.iter()
|
||||
.flat_map(|file| &file.hunks)
|
||||
.flat_map(|hunk| &hunk.lines)
|
||||
.map(|line| line.text.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
assert!(restored_lines.iter().any(|line| line.contains("fn later_mutation()")));
|
||||
assert!(!restored_lines.iter().any(|line| line.contains("fn final_mutation()")));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guided_review_last_turn_loads_a_retained_baseline_after_restart() {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let project_root = temp_dir.path();
|
||||
assert!(Command::new("git").arg("init").current_dir(project_root).status().unwrap().success());
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn original() {}\n").unwrap();
|
||||
for arguments in [
|
||||
["config", "user.email", "tests@example.invalid"].as_slice(),
|
||||
["config", "user.name", "Luxe Tests"].as_slice(),
|
||||
["add", "tracked.rs"].as_slice(),
|
||||
["commit", "-m", "initial fixture"].as_slice(),
|
||||
] {
|
||||
assert!(Command::new("git")
|
||||
.args(arguments)
|
||||
.current_dir(project_root)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success());
|
||||
}
|
||||
|
||||
let mut store = JsonlSessionStore::new(project_root.to_path_buf());
|
||||
let session_id = store.create(&project_root.display().to_string()).unwrap();
|
||||
let turn_id = AgentTurnId::new();
|
||||
let git = LocalGit::new(project_root);
|
||||
git.capture_and_persist_turn_baseline(
|
||||
&session_id.to_string(),
|
||||
turn_id.as_str(),
|
||||
UntrackedSourcePolicy::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn changed_after_turn() {}\n").unwrap();
|
||||
git.capture_and_persist_post_turn_snapshot(
|
||||
&session_id.to_string(),
|
||||
turn_id.as_str(),
|
||||
UntrackedSourcePolicy::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
LastTurnRecordStore::new(project_root)
|
||||
.save(&LastTurnRecord::new(session_id.to_string(), turn_id.as_str()).unwrap())
|
||||
.unwrap();
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn mutated_after_turn() {}\n").unwrap();
|
||||
|
||||
let config = EngineConfig {
|
||||
guided_review_project_root: Some(project_root.to_path_buf()),
|
||||
..base_config(store, session_id.clone(), Arc::new(MockProvider::new()))
|
||||
};
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
let restored_turn = tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::LastTurnAvailable {
|
||||
session_id: restored_session,
|
||||
turn_id: restored_turn,
|
||||
}) => {
|
||||
break (restored_session, restored_turn);
|
||||
}
|
||||
Some(_) => {}
|
||||
None => {
|
||||
panic!("engine event channel closed before Last Turn availability restored")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("Last Turn availability restore timed out");
|
||||
assert_eq!(restored_turn.0, session_id.to_string());
|
||||
assert_eq!(restored_turn.1, turn_id.as_str());
|
||||
|
||||
let request_id = ReviewLoadRequestId::new();
|
||||
action_tx
|
||||
.send(luxe_core::engine::Action::LoadGuidedReview {
|
||||
request_id: request_id.clone(),
|
||||
target: ReviewTarget::LastTurn {
|
||||
session_id: session_id.to_string(),
|
||||
turn_id: turn_id.as_str().to_owned(),
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let review = tokio::time::timeout(EVENT_WAIT, async {
|
||||
loop {
|
||||
match event_rx.recv().await {
|
||||
Some(Event::GuidedReviewLoaded { request_id: loaded_request, review })
|
||||
if loaded_request == request_id =>
|
||||
{
|
||||
break review
|
||||
}
|
||||
Some(_) => {}
|
||||
None => panic!("engine event channel closed before Last Turn review loaded"),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("Last Turn review load timed out");
|
||||
|
||||
assert!(matches!(review.target, ReviewTarget::LastTurn { .. }));
|
||||
let lines = review
|
||||
.files
|
||||
.iter()
|
||||
.flat_map(|file| &file.hunks)
|
||||
.flat_map(|hunk| &hunk.lines)
|
||||
.map(|line| line.text.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
assert!(lines.iter().any(|line| line.contains("fn changed_after_turn()")));
|
||||
assert!(!lines.iter().any(|line| line.contains("fn mutated_after_turn()")));
|
||||
}
|
||||
|
||||
mod ownership;
|
||||
297
crates/luxe-core/tests/engine/review_restore/ownership.rs
Normal file
297
crates/luxe-core/tests/engine/review_restore/ownership.rs
Normal file
@@ -0,0 +1,297 @@
|
||||
//! Ownership — the engine holds a review from comment to report, through close and a restart.
|
||||
|
||||
use std::process::Command;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::stream::{self, BoxStream};
|
||||
use luxe_core::engine::{Action, Engine};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::guided_review::{FindingAnchor, FindingSeverity, ReviewLoadRequestId, ReviewTarget};
|
||||
use luxe_core::provider::{CompletionRequest, Provider, ProviderError, ProviderEvent, StopReason};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
|
||||
use crate::fixtures::*;
|
||||
|
||||
/// The engine owns a review from the first comment to the agent's report, and through a restart.
|
||||
///
|
||||
/// The whole loop the TUI used to run by hand, with nothing but intents going in: a comment sent
|
||||
/// with `send`, the feedback turn it starts, the report that turn writes, each stored as it
|
||||
/// happens, and all of it back after a restart. The restart half is the defect this replaces: the
|
||||
/// restored session reached the front end under a request it never sent, and was dropped.
|
||||
#[tokio::test]
|
||||
async fn the_engine_owns_a_review_from_comment_to_report_and_through_a_restart() {
|
||||
use luxe_core::guided_review::{FindingState, ReviewSessionSaveRequestId, SubmissionDispatch};
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let project_root = temp_dir.path();
|
||||
assert!(Command::new("git").arg("init").current_dir(project_root).status().unwrap().success());
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn original() {}\n").unwrap();
|
||||
for arguments in [
|
||||
["config", "user.email", "tests@example.invalid"].as_slice(),
|
||||
["config", "user.name", "Luxe Tests"].as_slice(),
|
||||
["add", "tracked.rs"].as_slice(),
|
||||
["commit", "-m", "initial fixture"].as_slice(),
|
||||
] {
|
||||
assert!(Command::new("git")
|
||||
.args(arguments)
|
||||
.current_dir(project_root)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success());
|
||||
}
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn changed() {}\n").unwrap();
|
||||
let session_id = JsonlSessionStore::new(project_root.to_path_buf())
|
||||
.create(&project_root.display().to_string())
|
||||
.unwrap();
|
||||
|
||||
let (actions, mut events) =
|
||||
Engine::spawn(review_engine_config(project_root, session_id.clone()));
|
||||
let request_id = ReviewLoadRequestId::new();
|
||||
actions
|
||||
.send(Action::LoadGuidedReview {
|
||||
request_id: request_id.clone(),
|
||||
target: ReviewTarget::WorkingTree,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let review = first(&mut events, "the review", |event| match event {
|
||||
Event::GuidedReviewLoaded { request_id: loaded, review } if loaded == request_id => {
|
||||
Some(review)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
// The next review event is its session: always, so a front end never has a review without one
|
||||
let bound = first(&mut events, "the review's session", |event| match event {
|
||||
Event::ReviewSessionRestored { session } => Some(session),
|
||||
Event::GuidedReviewLoaded { .. } => panic!("a second review arrived before the session"),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(bound.id, review.id);
|
||||
assert!(bound.findings.is_empty());
|
||||
|
||||
let comment = ReviewSessionSaveRequestId::new();
|
||||
actions
|
||||
.send(Action::EditReview {
|
||||
request_id: comment.clone(),
|
||||
review_id: review.id.clone(),
|
||||
edit: luxe_core::engine::ReviewEdit::Comment {
|
||||
draft: luxe_core::engine::CommentDraft {
|
||||
anchor: FindingAnchor::Review,
|
||||
severity: FindingSeverity::Issue,
|
||||
title: "Keep the tracked function focused".into(),
|
||||
body: "The reviewed source needs the bounded follow-up change.".into(),
|
||||
proposed_fix: None,
|
||||
},
|
||||
send: true,
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let saved = first(&mut events, "the comment's save", |event| match event {
|
||||
Event::ReviewSessionSaved { request_id, session } if request_id == comment => Some(session),
|
||||
Event::ReviewSessionSaveFailed { message, .. } => {
|
||||
panic!("the comment was refused: {message}")
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(saved.findings[0].label, "R1");
|
||||
assert_eq!(saved.submissions[0].dispatch, SubmissionDispatch::Queued);
|
||||
|
||||
// The turn it started runs, reports, and finishes, and the desk stores each step
|
||||
let finished = first(&mut events, "the stored completion", |event| match event {
|
||||
Event::ReviewSessionSaved { session, .. }
|
||||
if session.submissions.first().map(|s| s.dispatch)
|
||||
== Some(SubmissionDispatch::Completed) =>
|
||||
{
|
||||
Some(session)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(finished.findings[0].state, FindingState::NeedsVerification, "the agent says fixed");
|
||||
assert!(finished.findings[0]
|
||||
.thread
|
||||
.iter()
|
||||
.any(|m| m.body.contains("Updated the tracked source.")));
|
||||
// Let the first engine finish writing before another one opens the project
|
||||
actions.send(Action::Close).await.unwrap();
|
||||
tokio::time::timeout(EVENT_WAIT, async { while events.recv().await.is_some() {} })
|
||||
.await
|
||||
.expect("the engine and its review desk shut down on close");
|
||||
|
||||
let (_resumed, mut resumed_events) =
|
||||
Engine::spawn(review_engine_config(project_root, session_id));
|
||||
let restored_review = first(&mut resumed_events, "the restored review", |event| match event {
|
||||
Event::GuidedReviewLoaded { review, .. } => Some(review),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(restored_review.id, review.id);
|
||||
let restored = first(&mut resumed_events, "the restored session", |event| match event {
|
||||
Event::ReviewSessionRestored { session } => Some(session),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(*restored, *finished, "the comment, its turn and its report all came back");
|
||||
}
|
||||
|
||||
/// A comment saved just before the front end closes is on disk by the time the engine has stopped.
|
||||
///
|
||||
/// The desk takes edits as they arrive, so `Close` can reach the engine while the write is still
|
||||
/// queued; a front end awaits the engine's task and then exits, which drops anything unfinished.
|
||||
/// Shutdown waits for the desk, so the last thing the reader wrote is not the thing they lose.
|
||||
#[tokio::test]
|
||||
async fn a_comment_saved_just_before_close_is_on_disk_when_the_engine_stops() {
|
||||
use luxe_core::guided_review::{ReviewSessionSaveRequestId, ReviewSessionStore};
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let project_root = temp_dir.path();
|
||||
assert!(Command::new("git").arg("init").current_dir(project_root).status().unwrap().success());
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn original() {}\n").unwrap();
|
||||
for arguments in [
|
||||
["config", "user.email", "tests@example.invalid"].as_slice(),
|
||||
["config", "user.name", "Luxe Tests"].as_slice(),
|
||||
["add", "tracked.rs"].as_slice(),
|
||||
["commit", "-m", "initial fixture"].as_slice(),
|
||||
] {
|
||||
assert!(Command::new("git")
|
||||
.args(arguments)
|
||||
.current_dir(project_root)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success());
|
||||
}
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn changed() {}\n").unwrap();
|
||||
let session_id = JsonlSessionStore::new(project_root.to_path_buf())
|
||||
.create(&project_root.display().to_string())
|
||||
.unwrap();
|
||||
|
||||
let (actions, mut events, task) =
|
||||
Engine::spawn_task(review_engine_config(project_root, session_id));
|
||||
let request_id = ReviewLoadRequestId::new();
|
||||
actions
|
||||
.send(Action::LoadGuidedReview {
|
||||
request_id: request_id.clone(),
|
||||
target: ReviewTarget::WorkingTree,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let review = first(&mut events, "the review", |event| match event {
|
||||
Event::GuidedReviewLoaded { request_id: loaded, review } if loaded == request_id => {
|
||||
Some(review)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
actions
|
||||
.send(Action::EditReview {
|
||||
request_id: ReviewSessionSaveRequestId::new(),
|
||||
review_id: review.id.clone(),
|
||||
edit: luxe_core::engine::ReviewEdit::Comment {
|
||||
draft: luxe_core::engine::CommentDraft {
|
||||
anchor: FindingAnchor::Review,
|
||||
severity: FindingSeverity::Issue,
|
||||
title: "Written last".into(),
|
||||
body: "Saved a moment before quitting.".into(),
|
||||
proposed_fix: None,
|
||||
},
|
||||
send: false,
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
actions.send(Action::Close).await.unwrap();
|
||||
// Drain so the desk is never stuck behind a full event channel, then wait for the engine
|
||||
let drained = tokio::spawn(async move { while events.recv().await.is_some() {} });
|
||||
tokio::time::timeout(EVENT_WAIT, task).await.expect("the engine stops").unwrap();
|
||||
|
||||
let stored = ReviewSessionStore::new(project_root).load(&review.id).unwrap();
|
||||
let titles: Vec<String> =
|
||||
stored.iter().flat_map(|session| &session.findings).map(|f| f.title.clone()).collect();
|
||||
assert_eq!(titles, ["Written last"], "the last comment survived the close");
|
||||
drained.abort();
|
||||
}
|
||||
|
||||
/// A provider whose every answer waits until it is cancelled: a Guide pass that would otherwise
|
||||
/// run for minutes on a large review.
|
||||
struct UntilCancelled;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for UntilCancelled {
|
||||
fn name(&self) -> &str {
|
||||
"until-cancelled"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
_request: CompletionRequest,
|
||||
mut cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
Ok(Box::pin(stream::once(async move {
|
||||
while !*cancel.borrow() {
|
||||
if cancel.changed().await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn }
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
/// Closing the engine ends the event stream promptly, even with a Guide pass still being written.
|
||||
///
|
||||
/// A front end waits for the stream to end before it exits, and every Guide pass holds a sender:
|
||||
/// `luxe --rpc` given `close` mid-guide on a large review sat there for minutes. Shutdown cancels
|
||||
/// the pass instead of waiting it out.
|
||||
#[tokio::test]
|
||||
async fn closing_does_not_wait_for_a_guide_being_written() {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let project_root = temp_dir.path();
|
||||
assert!(Command::new("git").arg("init").current_dir(project_root).status().unwrap().success());
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn original() {}\n").unwrap();
|
||||
for arguments in [
|
||||
["config", "user.email", "tests@example.invalid"].as_slice(),
|
||||
["config", "user.name", "Luxe Tests"].as_slice(),
|
||||
["add", "tracked.rs"].as_slice(),
|
||||
["commit", "-m", "initial fixture"].as_slice(),
|
||||
] {
|
||||
assert!(Command::new("git")
|
||||
.args(arguments)
|
||||
.current_dir(project_root)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success());
|
||||
}
|
||||
std::fs::write(project_root.join("tracked.rs"), "fn changed() {}\n").unwrap();
|
||||
let session_id = JsonlSessionStore::new(project_root.to_path_buf())
|
||||
.create(&project_root.display().to_string())
|
||||
.unwrap();
|
||||
let mut config = review_engine_config(project_root, session_id);
|
||||
config.provider = Arc::new(UntilCancelled);
|
||||
config.model = luxe_core::provider::ModelInfo {
|
||||
r#ref: luxe_core::provider::ModelRef::new("until-cancelled", "slow"),
|
||||
..Default::default()
|
||||
};
|
||||
let (actions, mut events) = Engine::spawn(config);
|
||||
let request_id = ReviewLoadRequestId::new();
|
||||
actions
|
||||
.send(Action::LoadGuidedReview {
|
||||
request_id: request_id.clone(),
|
||||
target: ReviewTarget::WorkingTree,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
first(&mut events, "the review", |event| match event {
|
||||
Event::GuidedReviewLoaded { request_id: loaded, .. } if loaded == request_id => Some(()),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
|
||||
actions.send(Action::Close).await.unwrap();
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
||||
while events.recv().await.is_some() {}
|
||||
})
|
||||
.await
|
||||
.expect("the stream ends on close, without waiting for the guide");
|
||||
}
|
||||
247
crates/luxe-core/tests/engine/turns.rs
Normal file
247
crates/luxe-core/tests/engine/turns.rs
Normal file
@@ -0,0 +1,247 @@
|
||||
//! Turns — a tool call streams, runs, and its result reaches the next request.
|
||||
|
||||
use futures::StreamExt;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::stream::{self, BoxStream};
|
||||
use luxe_core::engine::{Action, Engine, EngineConfig};
|
||||
use luxe_core::event::Event;
|
||||
use luxe_core::hook::{HookPoint, HookRegistry};
|
||||
use luxe_core::provider::{CompletionRequest, Provider, ProviderError, ProviderEvent, StopReason};
|
||||
use luxe_core::session::{JsonlSessionStore, SessionStore};
|
||||
use luxe_core::tool::ToolRegistry;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::fixtures::*;
|
||||
|
||||
/// Streams a `read` and a `write` the way a provider does: the name up front, then the arguments
|
||||
/// in fragments, with the file's content dwarfing everything else.
|
||||
struct SlowWriteProvider {
|
||||
turns: Arc<RwLock<usize>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Provider for SlowWriteProvider {
|
||||
fn name(&self) -> &str {
|
||||
"slow-write"
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
_request: CompletionRequest,
|
||||
_cancel: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<BoxStream<'static, ProviderEvent>, ProviderError> {
|
||||
let mut turns = self.turns.write().await;
|
||||
*turns += 1;
|
||||
if *turns > 1 {
|
||||
return Ok(Box::pin(stream::iter(vec![
|
||||
ProviderEvent::TextDelta { index: 0, delta: "written".into() },
|
||||
ProviderEvent::Done { reason: StopReason::EndTurn },
|
||||
])));
|
||||
}
|
||||
// `(event, pause before it)`. The body is rate-limited, so the fragments carrying the
|
||||
// file are spaced past that window — which is also how a real model delivers them.
|
||||
let mut script: Vec<(ProviderEvent, u64)> = vec![
|
||||
// A lookup, whose card must *not* open early: three of them fold into one line, and
|
||||
// the fold is declared by the committed message.
|
||||
(
|
||||
ProviderEvent::ToolCallStart {
|
||||
index: 0,
|
||||
id: "call-read".into(),
|
||||
name: "read".into(),
|
||||
},
|
||||
0,
|
||||
),
|
||||
(
|
||||
ProviderEvent::ToolCallDelta {
|
||||
index: 0,
|
||||
json_delta: r#"{"path":"notes.txt"}"#.into(),
|
||||
},
|
||||
0,
|
||||
),
|
||||
(
|
||||
ProviderEvent::ToolCallStart {
|
||||
index: 1,
|
||||
id: "call-write".into(),
|
||||
name: "write".into(),
|
||||
},
|
||||
0,
|
||||
),
|
||||
];
|
||||
for (fragment, pause) in [
|
||||
(r#"{"pa"#, 0),
|
||||
(r#"th":"notes"#, 0),
|
||||
(r#".txt","#, 0),
|
||||
(r#""content":"one\n"#, 0),
|
||||
(r#"two\n"#, 150),
|
||||
(r#"three\n"}"#, 150),
|
||||
] {
|
||||
script.push((
|
||||
ProviderEvent::ToolCallDelta { index: 1, json_delta: fragment.into() },
|
||||
pause,
|
||||
));
|
||||
}
|
||||
script.push((ProviderEvent::Done { reason: StopReason::ToolCall }, 0));
|
||||
Ok(Box::pin(stream::iter(script).then(|(event, pause)| async move {
|
||||
if pause > 0 {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(pause)).await;
|
||||
}
|
||||
event
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
/// A `write`'s content *is* its argument, so the model spends far longer writing the call than the
|
||||
/// tool spends running it. Nothing used to reach a front-end for that whole stretch, so the card
|
||||
/// landed at the moment the work finished — which reads as a tool that never ran.
|
||||
#[tokio::test]
|
||||
async fn a_tool_call_announces_itself_while_the_model_is_still_writing_it() {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(temp_dir.path().to_path_buf());
|
||||
let session_id = store.create(&temp_dir.path().display().to_string()).unwrap();
|
||||
|
||||
let config = EngineConfig {
|
||||
guided_review_project_root: Some(temp_dir.path().to_path_buf()),
|
||||
..base_config(
|
||||
store,
|
||||
session_id,
|
||||
Arc::new(SlowWriteProvider { turns: Arc::new(RwLock::new(0)) }),
|
||||
)
|
||||
};
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "write notes.txt".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut lifecycle = Vec::new();
|
||||
loop {
|
||||
match tokio::time::timeout(EVENT_WAIT, event_rx.recv()).await {
|
||||
Ok(Some(Event::ToolCallComposing { call_id, name, subject, body })) => {
|
||||
lifecycle.push(format!(
|
||||
"composing {call_id} {name} {} {}",
|
||||
subject.unwrap_or_else(|| "-".into()),
|
||||
body.map_or_else(|| "-".to_string(), |body| body.total_lines.to_string()),
|
||||
));
|
||||
}
|
||||
Ok(Some(Event::ToolStarted { call_id, name })) => {
|
||||
lifecycle.push(format!("started {call_id} {name}"));
|
||||
}
|
||||
Ok(Some(Event::ToolEnded { call_id, .. })) => {
|
||||
lifecycle.push(format!("ended {call_id}"));
|
||||
if call_id == "call-write" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(Some(_)) => {}
|
||||
Ok(None) => panic!("the engine closed the stream before the call ran"),
|
||||
Err(_) => panic!("timed out waiting for the tool lifecycle; got {lifecycle:#?}"),
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
lifecycle,
|
||||
[
|
||||
// The `write` is watched as it is written — opened, named once its path closes, then
|
||||
// filling in. The `read` in the same message says nothing until it is dispatched:
|
||||
// composing a lookup would put a card on screen that the message's own declared run
|
||||
// then takes straight back.
|
||||
"composing call-write write - -",
|
||||
"composing call-write write notes.txt -",
|
||||
"composing call-write write - 2",
|
||||
"composing call-write write - 3",
|
||||
// Dispatch is after the whole message, which is why watching it arrive was worth an
|
||||
// event of its own.
|
||||
"started call-read read",
|
||||
"started call-write write",
|
||||
"ended call-read",
|
||||
"ended call-write",
|
||||
],
|
||||
"the card opens on the first fragment, is named as soon as the path closes, and fills in \
|
||||
as the file is written",
|
||||
);
|
||||
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// A tool call the model makes runs the tool, and what it returned is in the next request.
|
||||
///
|
||||
/// Three claims, asserted apart so a failure says which link broke: dispatch ran the registered
|
||||
/// tool (it counted the call), the call and its result were paired and committed as one unit (the
|
||||
/// lifecycle events and the second request agree), and the answer the tool gave is what the model
|
||||
/// reads next. Unregister the tool and the first fails; drop the result from the transcript and
|
||||
/// the last does.
|
||||
#[tokio::test]
|
||||
async fn a_tool_call_runs_and_its_result_reaches_the_next_request() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut store = JsonlSessionStore::new(tmp.path().to_path_buf());
|
||||
let session_id = store.create(&tmp.path().display().to_string()).unwrap();
|
||||
|
||||
let witness = WitnessReadTool::new();
|
||||
let mut tool_registry = ToolRegistry::new();
|
||||
tool_registry.register(witness.clone());
|
||||
let (recording_hook, recorded_points) = RecordingHook::new();
|
||||
let mut hook_registry = HookRegistry::new();
|
||||
hook_registry.register(Arc::new(recording_hook));
|
||||
let provider = Arc::new(MockProvider::new());
|
||||
let requests = provider.requests();
|
||||
|
||||
let config =
|
||||
EngineConfig { tool_registry, hook_registry, ..base_config(store, session_id, provider) };
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config);
|
||||
action_tx
|
||||
.send(Action::SubmitUserMessage {
|
||||
content: "Read test.txt".to_string(),
|
||||
images: vec![],
|
||||
remediation: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let events = events_until_turn_end(&mut event_rx).await;
|
||||
|
||||
// dispatch ran the tool, once, with the arguments the model streamed
|
||||
assert_eq!(witness.invocations(), [serde_json::json!({"path": "test.txt"})]);
|
||||
|
||||
// the call started and ended without an error
|
||||
let lifecycle: Vec<String> = events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::ToolStarted { call_id, name } => Some(format!("started {call_id} {name}")),
|
||||
Event::ToolEnded { call_id, is_error } => {
|
||||
Some(format!("ended {call_id} error={is_error}"))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(lifecycle, ["started call_1 read", "ended call_1 error=false"]);
|
||||
|
||||
// the next request carries the call and its answer, paired, with the tool's own words
|
||||
let requests = requests.read().await;
|
||||
assert_eq!(requests.len(), 2, "the result sent the model back for another reply");
|
||||
let (calls, results) = pairing(&requests[1].messages);
|
||||
assert_eq!((calls, results), (vec!["call_1".to_string()], vec!["call_1".to_string()]));
|
||||
assert_eq!(result_text(&requests[1].messages, "call_1"), WITNESS_OUTPUT);
|
||||
let first_request_results = pairing(&requests[0].messages).1;
|
||||
assert!(first_request_results.is_empty(), "the first request had nothing to answer");
|
||||
|
||||
// the hooks around the call fired; `AgentEnd` comes after the turn's last event, so close the
|
||||
// engine and wait for its stream to end before reading them
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
tokio::time::timeout(EVENT_WAIT, async { while event_rx.recv().await.is_some() {} })
|
||||
.await
|
||||
.expect("the engine closes its stream after Close");
|
||||
let points = recorded_points.read().await;
|
||||
for point in [
|
||||
HookPoint::SessionStart,
|
||||
HookPoint::UserPromptSubmit,
|
||||
HookPoint::BeforeToolUse,
|
||||
HookPoint::AfterToolUse,
|
||||
HookPoint::AgentEnd,
|
||||
] {
|
||||
assert!(points.contains(&point), "{point:?} should have fired: {points:?}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user