Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

reject empty compaction summaries, retry length-capped ones once (#3529)

Sebastian Müllerf87fa01c5d72
diff --git a/crates/pa-core/src/session_engine/compact_session.rs b/crates/pa-core/src/session_engine/compact_session.rs
index 270d639a7a..5a171a4520 100644
--- a/crates/pa-core/src/session_engine/compact_session.rs
+++ b/crates/pa-core/src/session_engine/compact_session.rs
@@ -1,14 +1,16 @@
//! `/compact` execution: resolve the cut over session entries, run the
//! summarizer, persist the compaction entry, and rebuild the agent context.
+use pa_types::ai::{TextContent, UserContent, UserContentBlock};
use pa_types::session::{AgentMessage, FileEntry};
use super::compaction::{estimate_context_tokens, find_cut_point, CutPointResult};
use super::compaction_exec;
use super::compaction_exec::{
- build_summarization_request, build_turn_prefix_request, compaction_entry_for,
- complete_summary_call, details_for, file_ops_block, split_summary, summed_usage,
- CompactionDetails, CompactionResult, SummaryDeltaSink, SummarySlice, NO_PRIOR_HISTORY,
+ add_assistant_usage, build_summarization_request, build_turn_prefix_request,
+ compaction_entry_for, complete_summary_call, details_for, file_ops_block, split_summary,
+ summed_usage, CompactionDetails, CompactionResult, SummaryDeltaSink, SummarySlice,
+ NO_PRIOR_HISTORY,
};
use super::compaction_utils;
use crate::session::manager::SessionManager;
@@ -219,6 +221,85 @@ pub(crate) fn prepare_attempt(
})
}
+/// One summary wire call with the length-capped retry — the shared
+/// `complete_summary_call` wrapper for both the history and the turn-prefix
+/// calls. Each attempt carries its own request id under the compaction's
+/// ledger guard (`semantic_edges::summary_slice_call`; TS `summaryCall`: the
+/// id's headers merge over the routed model's). A reply the output cap cut
+/// off retries ONCE with the shorter-summary instruction appended as one
+/// more text block on the request — no loop, no counter. A twice-capped
+/// retry still commits (the alternative is the amnesiac transcript), with
+/// the truncation marker line in front. The retry never streams live: the
+/// live block is transient, and the committed row replaces it.
+///
+/// # Errors
+///
+/// Returns an error when either wire call fails; `complete_summary_call`
+/// labels the unusable replies.
+#[allow(clippy::too_many_arguments)] // the call mirrors complete_summary_call's shape
+async fn summary_call_with_retry(
+ compaction: Option<&super::semantic_edges::SemanticCompaction>,
+ summary_headers: Option<std::collections::BTreeMap<String, String>>,
+ model: &pa_types::ai::Model,
+ api_key: Option<String>,
+ max_tokens: u64,
+ mut request: Vec<AgentMessage>,
+ on_delta: Option<SummaryDeltaSink>,
+ failure: &'static str,
+) -> anyhow::Result<SummarySlice> {
+ let first =
+ super::semantic_edges::summary_slice_call(compaction, summary_headers.clone(), |headers| {
+ complete_summary_call(
+ model,
+ api_key.clone(),
+ headers,
+ max_tokens,
+ request.clone(),
+ on_delta.clone(),
+ failure,
+ )
+ })
+ .await?;
+ if !first.truncated {
+ return Ok(first);
+ }
+ super::compaction_trace::trace("compact.summarizer_retry", &serde_json::Value::Null);
+ // The retry request: the same single-user-message request with the
+ // shorter-summary instruction as one more text block.
+ if let Some(AgentMessage::User(user)) = request.first_mut() {
+ if let UserContent::Blocks(blocks) = &mut user.content {
+ blocks.push(UserContentBlock::Text(TextContent {
+ text: super::compaction::SHORTER_SUMMARY_INSTRUCTION.to_string(),
+ text_signature: None,
+ rest: serde_json::Map::default(),
+ }));
+ }
+ }
+ let retry = super::semantic_edges::summary_slice_call(compaction, summary_headers, |headers| {
+ complete_summary_call(model, api_key, headers, max_tokens, request, None, failure)
+ })
+ .await?;
+ // Both attempts are wire calls, so both bill: the slice's usage is their sum.
+ let mut usage = first.usage.unwrap_or_default();
+ if let Some(retry_usage) = &retry.usage {
+ add_assistant_usage(&mut usage, retry_usage);
+ }
+ let summary = if retry.truncated {
+ format!(
+ "{}\n\n{}",
+ super::compaction::SUMMARY_TRUNCATED_MARKER,
+ retry.summary
+ )
+ } else {
+ retry.summary
+ };
+ Ok(SummarySlice {
+ summary,
+ usage: Some(usage),
+ truncated: retry.truncated,
+ })
+}
+
/// SUMMARIZE: run the summarizer calls with no session lock held.
pub(crate) async fn summarize_attempt(
attempt: &CompactionAttempt,
@@ -299,6 +380,7 @@ pub(crate) async fn summarize_attempt(
return Ok(SummarySlice {
summary: NO_PRIOR_HISTORY.to_string(),
usage: None,
+ truncated: false,
});
}
let request = build_summarization_request(
@@ -308,23 +390,15 @@ pub(crate) async fn summarize_attempt(
attempt.recent_state_anchor.as_deref(),
options.settings.reserve_tokens,
);
- // Each summary wire call carries its own request id under the
- // compaction's guard (TS `summaryCall`): the id's headers merge
- // over the routed model's.
- super::semantic_edges::summary_slice_call(
+ summary_call_with_retry(
attempt.semantic_compaction.as_ref(),
summary_headers.clone(),
- |headers| {
- complete_summary_call(
- &model,
- api_key.clone(),
- headers,
- history_max_tokens,
- request,
- options.summary_delta.clone(),
- "Summarization failed",
- )
- },
+ &model,
+ api_key.clone(),
+ history_max_tokens,
+ request,
+ options.summary_delta.clone(),
+ "Summarization failed",
)
.await
};
@@ -333,27 +407,21 @@ pub(crate) async fn summarize_attempt(
return Ok::<Option<SummarySlice>, anyhow::Error>(None);
}
let request = build_turn_prefix_request(&attempt.turn_prefix_messages);
- let slice = super::semantic_edges::summary_slice_call(
+ // The turn-prefix call never streams live: the split join runs it
+ // concurrently with the history call, and its chunks interleaved
+ // into the live sink would land out of the final order (the
+ // committed summary is history, split marker, prefix). The
+ // completed prefix flushes through the sink after the join, so
+ // the live block converges to the exact committed summary.
+ let slice = summary_call_with_retry(
attempt.semantic_compaction.as_ref(),
summary_headers.clone(),
- |headers| {
- complete_summary_call(
- &model,
- api_key.clone(),
- headers,
- turn_prefix_max_tokens,
- request,
- // The turn-prefix call never streams live: the split join
- // runs it concurrently with the history call, and its chunks
- // interleaved into the live sink would land out of the
- // final order (the committed summary is history, split
- // marker, prefix). The completed prefix flushes through the
- // sink after the join, so the live block converges to the
- // exact committed summary.
- None,
- "Turn prefix summarization failed",
- )
- },
+ &model,
+ api_key.clone(),
+ turn_prefix_max_tokens,
+ request,
+ None,
+ "Turn prefix summarization failed",
)
.await?;
Ok(Some(slice))
diff --git a/crates/pa-core/src/session_engine/compact_session/tests/execute_compaction.rs b/crates/pa-core/src/session_engine/compact_session/tests/execute_compaction.rs
index ebeed376ea..bb354db29c 100644
--- a/crates/pa-core/src/session_engine/compact_session/tests/execute_compaction.rs
+++ b/crates/pa-core/src/session_engine/compact_session/tests/execute_compaction.rs
@@ -537,6 +537,155 @@ async fn execute_compaction_fails_on_an_error_summarizer_response() {
registration.unregister();
}
+/// A whitespace-only summarizer reply (a blank-but-billed HTTP success)
+/// fails the compaction instead of committing an empty summary that would
+/// replace the transcript.
+#[tokio::test]
+async fn execute_compaction_fails_on_an_empty_summarizer_response() {
+ let registration = faux_registration();
+ let model = registration.get_model();
+ registration.set_responses(vec![pa_ai::faux::FauxResponseStep::Message(
+ pa_ai::faux::faux_assistant_text_message(
+ " \n",
+ pa_ai::faux::FauxAssistantMessageOptions::default(),
+ ),
+ )]);
+ let tmp = tempfile::tempdir().unwrap();
+ let mut session = session_with_turns(tmp.path(), 3);
+ let before = session.get_entries();
+ let error = execute_compaction(
+ &mut session,
+ CompactOptions {
+ model,
+ api_key: None,
+ custom_instructions: None,
+ settings: super::super::compaction::CompactionSettings {
+ keep_recent_tokens: 20,
+ ..Default::default()
+ },
+ abort: None,
+ harness_digest: None,
+ auxiliary: None,
+ semantic_edges: None,
+ summary_delta: None,
+ },
+ )
+ .await
+ .unwrap_err();
+ assert_eq!(
+ error.to_string(),
+ "Summarization failed: the summary was empty"
+ );
+ assert_eq!(session.get_entries(), before);
+ registration.unregister();
+}
+
+/// A length-capped summarizer reply retries once with the shorter-summary
+/// instruction: the committed row is the retry's text, with no marker.
+#[tokio::test]
+async fn execute_compaction_retries_a_length_capped_summarizer_response() {
+ let registration = faux_registration();
+ let model = registration.get_model();
+ registration.set_responses(vec![
+ pa_ai::faux::FauxResponseStep::Message(pa_ai::faux::faux_assistant_text_message(
+ "cut-off summary",
+ pa_ai::faux::FauxAssistantMessageOptions {
+ stop_reason: Some(pa_types::ai::StopReason::Length),
+ ..Default::default()
+ },
+ )),
+ pa_ai::faux::FauxResponseStep::Message(pa_ai::faux::faux_assistant_text_message(
+ "shorter summary",
+ pa_ai::faux::FauxAssistantMessageOptions::default(),
+ )),
+ ]);
+ let tmp = tempfile::tempdir().unwrap();
+ let mut session = session_with_turns(tmp.path(), 3);
+ let outcome = execute_compaction(
+ &mut session,
+ CompactOptions {
+ model,
+ api_key: None,
+ custom_instructions: None,
+ settings: super::super::compaction::CompactionSettings {
+ keep_recent_tokens: 20,
+ ..Default::default()
+ },
+ abort: None,
+ harness_digest: None,
+ auxiliary: None,
+ semantic_edges: None,
+ summary_delta: None,
+ },
+ )
+ .await
+ .unwrap();
+ let CompactOutcome::Ran(run) = outcome else {
+ panic!("expected the compaction to run");
+ };
+ assert_eq!(run.entry.summary, "shorter summary");
+ // Exactly one retry: the capped reply plus its shorter-summary retry.
+ assert_eq!(registration.call_count(), 2);
+ registration.unregister();
+}
+
+/// A twice-capped summarizer pair still commits: the retry's text rides
+/// behind the truncation marker, and the queue's two responses prove the
+/// retry never loops (a third call would hit the empty queue and fail).
+#[tokio::test]
+async fn execute_compaction_accepts_a_twice_capped_summary_behind_the_marker() {
+ let registration = faux_registration();
+ let model = registration.get_model();
+ let capped = |text: &str| {
+ pa_ai::faux::FauxResponseStep::Message(pa_ai::faux::faux_assistant_text_message(
+ text,
+ pa_ai::faux::FauxAssistantMessageOptions {
+ stop_reason: Some(pa_types::ai::StopReason::Length),
+ ..Default::default()
+ },
+ ))
+ };
+ registration.set_responses(vec![capped("partial one"), capped("partial two")]);
+ let tmp = tempfile::tempdir().unwrap();
+ let mut session = session_with_turns(tmp.path(), 3);
+ let outcome = execute_compaction(
+ &mut session,
+ CompactOptions {
+ model,
+ api_key: None,
+ custom_instructions: None,
+ settings: super::super::compaction::CompactionSettings {
+ keep_recent_tokens: 20,
+ ..Default::default()
+ },
+ abort: None,
+ harness_digest: None,
+ auxiliary: None,
+ semantic_edges: None,
+ summary_delta: None,
+ },
+ )
+ .await
+ .unwrap();
+ let CompactOutcome::Ran(run) = outcome else {
+ panic!("expected the compaction to run");
+ };
+ assert_eq!(
+ run.entry.summary,
+ format!(
+ "{}\n\npartial two",
+ super::super::compaction::SUMMARY_TRUNCATED_MARKER
+ )
+ );
+ assert_eq!(registration.call_count(), 2);
+ // The session continues: the twice-capped retry still commits the row.
+ assert!(session
+ .get_entries()
+ .iter()
+ .any(|entry| matches!(entry, FileEntry::Compaction { .. })));
+ registration.unregister();
+}
+
/// The durable row carries the full record — `fromHook: false`, the usage,
/// and the file-operation details — not just the summary boundary.
#[tokio::test]
diff --git a/crates/pa-core/src/session_engine/compaction.rs b/crates/pa-core/src/session_engine/compaction.rs
index afff07ced4..0c76f5d44e 100644
--- a/crates/pa-core/src/session_engine/compaction.rs
+++ b/crates/pa-core/src/session_engine/compaction.rs
@@ -462,6 +462,15 @@ pub const TURN_PREFIX_SUMMARIZATION_PROMPT: &str = "This is the PREFIX of a turn
const UPDATE_SUMMARIZATION_PROMPT: &str = "The messages above are NEW conversation messages to incorporate into the existing summary provided in <previous-summary> tags.\n\nUpdate the existing structured summary with new information. RULES:\n- PRESERVE all existing information from the previous summary\n- ADD new progress, decisions, and context from the new messages\n- UPDATE the Progress section: move items from \"In Progress\" to \"Done\" when completed\n- UPDATE \"Next Steps\" based on what was accomplished\n- PRESERVE exact file paths, function names, and error messages\n- If something is no longer relevant, you may remove it\n\nUse this EXACT format:\n\n## Goal\n[Preserve existing goals, add new ones if the task expanded]\n\n## Constraints & Preferences\n- [Preserve existing, add new ones discovered]\n\n## Progress\n### Done\n- [x] [Include previously done items AND newly completed items]\n\n### In Progress\n- [ ] [Current work - update based on progress]\n\n### Blocked\n- [Current blockers - remove if resolved]\n\n## Key Decisions\n- **[Decision]**: [Brief rationale] (preserve all previous, add new)\n\n## Next Steps\n1. [Update based on current state]\n\n## Critical Context\n- [Preserve important context, add new if needed]\n\nKeep each section concise. Preserve exact file paths, function names, and error messages.";
+/// The retry instruction for a summary the output cap cut off: the retry
+/// wire call appends it as one more text block on the summarization request.
+pub(crate) const SHORTER_SUMMARY_INSTRUCTION: &str = "Your previous summary was cut off at the output limit. Write a shorter summary that fits: keep every required section and compress the detail.";
+
+/// The line a twice-capped summary commits behind: the retry's text still
+/// replaces the transcript, so it rides in front of this disclosure.
+pub(crate) const SUMMARY_TRUNCATED_MARKER: &str =
+ "[summary truncated: earlier detail may be missing]";
+
/// The instruction part of the summarization prompt (initial or update
/// template, user instructions, kernel persistence note).
#[must_use]
diff --git a/crates/pa-core/src/session_engine/compaction_exec.rs b/crates/pa-core/src/session_engine/compaction_exec.rs
index 1955eedb9e..fe89bd46a6 100644
--- a/crates/pa-core/src/session_engine/compaction_exec.rs
+++ b/crates/pa-core/src/session_engine/compaction_exec.rs
@@ -78,12 +78,15 @@ pub fn build_summarization_request(
})]
}
-/// One resolved summarizer wire call: the summary text and what it
-/// billed; the no-history split arm has no wire call (`usage: None`).
+/// One resolved summarizer wire call: the summary text, what it billed, and
+/// whether the reply stopped at the output cap (`Length` — the caller
+/// retries a truncated summary once with a shorter-summary instruction).
+/// The no-history split arm has no wire call (`usage: None`).
#[derive(Debug, Clone, PartialEq)]
pub struct SummarySlice {
pub summary: String,
pub usage: Option<pa_types::ai::Usage>,
+ pub truncated: bool,
}
/// The literal history stand-in for a split turn whose kept cut leaves
@@ -125,12 +128,17 @@ pub type SummaryDeltaSink = std::sync::Arc<dyn Fn(&str) + Send + Sync>;
/// Run one summarizer wire call through `pa_ai::complete_simple`: `headers`
/// are the routed model's merged request headers; `on_delta` is the live sink
-/// ([`SummaryDeltaSink`]); `failure` labels the error-stop bail.
+/// ([`SummaryDeltaSink`]); `failure` labels the unusable-reply bails. Only a
+/// reply with non-empty text is a usable summary — anything less would be
+/// committed as the compaction row and replace the transcript. A reply
+/// stopped at the output cap resolves with `truncated: true`; the caller
+/// decides the retry.
///
/// # Errors
///
-/// Returns an error when the summarizer wire call fails, or when its reply
-/// stops with an error (labeled with `failure`).
+/// Returns an error when the summarizer wire call fails, when its reply
+/// stops with an error, a tool call, or an abort, or when its joined text
+/// is empty (all labeled with `failure`).
pub async fn complete_summary_call(
model: &pa_types::ai::Model,
api_key: Option<String>,
@@ -181,15 +189,24 @@ pub async fn complete_summary_call(
stream.result().await
}
};
- if assistant.stop_reason == pa_types::ai::StopReason::Error {
- anyhow::bail!(
+ let stop_reason = assistant.stop_reason;
+ let truncated = match stop_reason {
+ pa_types::ai::StopReason::Stop => false,
+ pa_types::ai::StopReason::Length => true,
+ pa_types::ai::StopReason::Error => anyhow::bail!(
"{failure}: {}",
assistant
.error_message
.as_deref()
.unwrap_or("Unknown error")
- );
- }
+ ),
+ // A tool-call or aborted reply is not a usable summary: committing it
+ // as the compaction row would irreversibly replace the transcript
+ // with a fragment.
+ pa_types::ai::StopReason::ToolUse | pa_types::ai::StopReason::Aborted => {
+ anyhow::bail!("{failure}: the summary stopped with reason {stop_reason:?}");
+ }
+ };
let summary = assistant
.content
.iter()
@@ -199,9 +216,13 @@ pub async fn complete_summary_call(
})
.collect::<Vec<_>>()
.join("\n");
+ if summary.trim().is_empty() {
+ anyhow::bail!("{failure}: the summary was empty");
+ }
Ok(SummarySlice {
summary,
usage: Some(assistant.usage),
+ truncated,
})
}
@@ -649,6 +670,7 @@ mod tests {
let slice = |summary: &str, usage: Option<pa_types::ai::Usage>| SummarySlice {
summary: summary.to_string(),
usage,
+ truncated: false,
};
let summed = summed_usage(&[
slice("a", Some(usage(10, 5, first_cost))),
diff --git a/crates/pa-core/src/session_engine/messages.rs b/crates/pa-core/src/session_engine/messages.rs
index 8913bb8335..0a4da4695e 100644
--- a/crates/pa-core/src/session_engine/messages.rs
+++ b/crates/pa-core/src/session_engine/messages.rs
@@ -9,7 +9,7 @@ use pa_types::ai::{
use pa_types::session::AgentMessage;
use std::fmt::Write as _;
-pub const COMPACTION_SUMMARY_PREFIX: &str = "[compaction-summary]\n\nThe conversation history before this point was compacted into the following summary.\nThe retained messages below are authoritative; this summary may lag behind them.\n\n<summary>\n";
+pub const COMPACTION_SUMMARY_PREFIX: &str = "[compaction-summary]\n\nThe conversation history before this point was compacted into the following summary.\nThe retained messages below are authoritative; this summary may lag behind them.\nBackground work may have progressed during compaction; treat resource statuses as last-observed and refresh them before relying on them.\n\n<summary>\n";
pub const COMPACTION_SUMMARY_SUFFIX: &str = "\n</summary>";
pub const BRANCH_SUMMARY_PREFIX: &str = "[branch-summary]\n\nThe following is a summary of a branch that this conversation came back from:\n\n<summary>\n";
pub const BRANCH_SUMMARY_SUFFIX: &str = "\n</summary>";
diff --git a/crates/pa-daemon/tests/compaction_outcome_e2e.rs b/crates/pa-daemon/tests/compaction_outcome_e2e.rs
index ad78575b4f..8babd7951b 100644
--- a/crates/pa-daemon/tests/compaction_outcome_e2e.rs
+++ b/crates/pa-daemon/tests/compaction_outcome_e2e.rs
@@ -26,7 +26,6 @@ use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
-use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
@@ -48,36 +47,47 @@ impl Drop for Supervisor {
}
}
-/// An OpenAI-compatible SSE mock with a switchable failure mode: while `fail_summarizer`
-/// is set, any request carrying the summarization prompt gets a 500; every other request
-/// the fixed reply. The usage list makes the second turn cross (f14-auto shape).
+/// What the mock answers a summarizer request with: the fixed turn reply,
+/// a 500, or a blank-but-billed HTTP 200 stream (the glm-ht failure shape:
+/// a successful reply whose only content delta is empty, with usage billed).
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum SummarizerMode {
+ TurnReply,
+ Unavailable,
+ Blank,
+}
+
+/// An OpenAI-compatible SSE mock with a switchable summarizer mode: any
+/// request carrying the summarization prompt is answered per
+/// [`SummarizerMode`]; every other request gets the fixed reply. The usage
+/// list makes the second turn cross (f14-auto shape).
struct CompactionMock {
requests: Arc<Mutex<Vec<Value>>>,
- fail_summarizer: Arc<AtomicBool>,
+ summarizer_mode: Arc<Mutex<SummarizerMode>>,
port: u16,
}
impl CompactionMock {
fn start() -> CompactionMock {
let requests = Arc::new(Mutex::new(Vec::new()));
- let fail_summarizer = Arc::new(AtomicBool::new(false));
+ let summarizer_mode = Arc::new(Mutex::new(SummarizerMode::TurnReply));
let listener = TcpListener::bind("127.0.0.1:0").expect("bind mock");
let port = listener.local_addr().expect("mock addr").port();
let requests_for_thread = Arc::clone(&requests);
- let fail_for_thread = Arc::clone(&fail_summarizer);
+ let mode_for_thread = Arc::clone(&summarizer_mode);
std::thread::spawn(move || {
for stream in listener.incoming() {
let Ok(stream) = stream else { continue };
let requests = Arc::clone(&requests_for_thread);
- let fail = Arc::clone(&fail_for_thread);
+ let mode = Arc::clone(&mode_for_thread);
std::thread::spawn(move || {
- let _ = serve(stream, &requests, &fail);
+ let _ = serve(stream, &requests, &mode);
});
}
});
CompactionMock {
requests,
- fail_summarizer,
+ summarizer_mode,
port,
}
}
@@ -89,6 +99,10 @@ impl CompactionMock {
fn request_count(&self) -> usize {
self.requests.lock().expect("mock lock").len()
}
+
+ fn set_summarizer_mode(&self, mode: SummarizerMode) {
+ *self.summarizer_mode.lock().expect("mode lock") = mode;
+ }
}
fn chunk(delta: &Value, finish_reason: Option<&str>, usage: &Value) -> String {
@@ -142,10 +156,37 @@ fn is_summarizer_request(body: &Value) -> bool {
})
}
+/// The OpenAI-compatible SSE stream for one assistant reply: a content
+/// delta, the finish chunk, and the terminal usage frame.
+fn sse_stream(content: &str, usage: &Value) -> String {
+ let mut payload = String::new();
+ for data in [
+ chunk(
+ &json!({"role": "assistant", "content": content}),
+ None,
+ &small_usage(),
+ ),
+ chunk(&json!({}), Some("stop"), usage),
+ json!({
+ "id": "chatcmpl-test",
+ "object": "chat.completion.chunk",
+ "created": 1_750_000_000,
+ "model": "mock-1",
+ "choices": [],
+ "usage": usage,
+ })
+ .to_string(),
+ ] {
+ write!(payload, "data: {data}\n\n").expect("write to String");
+ }
+ payload.push_str("data: [DONE]\n\n");
+ payload
+}
+
fn serve(
mut stream: TcpStream,
requests: &Arc<Mutex<Vec<Value>>>,
- fail_summarizer: &Arc<AtomicBool>,
+ summarizer_mode: &Arc<Mutex<SummarizerMode>>,
) -> std::io::Result<()> {
let mut reader = BufReader::new(stream.try_clone()?);
let mut head = String::new();
@@ -173,17 +214,6 @@ fn serve(
let body: Value = serde_json::from_slice(&body_bytes).unwrap_or(Value::Null);
let index = requests.lock().expect("mock lock").len();
requests.lock().expect("mock lock").push(body.clone());
- if fail_summarizer.load(Ordering::SeqCst) && is_summarizer_request(&body) {
- let error = json!({"error": {"message": "summarizer unavailable"}}).to_string();
- return stream.write_all(
- format!(
- "HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\nConnection: close\r\nContent-Length: {}\r\n\r\n{}",
- error.len(),
- error
- )
- .as_bytes(),
- );
- }
// Turn 2 reports the over-threshold usage; every other request reports the small
// usage so the session does not re-cross.
let usage = if index == 1 {
@@ -191,30 +221,37 @@ fn serve(
} else {
small_usage()
};
- let mut payload = String::new();
- for data in [
- chunk(
- &json!({"role": "assistant", "content": "parity reply"}),
- None,
- &small_usage(),
- ),
- chunk(&json!({}), Some("stop"), &usage),
- json!({
- "id": "chatcmpl-test",
- "object": "chat.completion.chunk",
- "created": 1_750_000_000,
- "model": "mock-1",
- "choices": [],
- "usage": usage,
- })
- .to_string(),
- ] {
- write!(payload, "data: {data}\n\n").expect("write to String");
+ if is_summarizer_request(&body) {
+ match *summarizer_mode.lock().expect("mode lock") {
+ SummarizerMode::TurnReply => {}
+ SummarizerMode::Unavailable => {
+ let error = json!({"error": {"message": "summarizer unavailable"}}).to_string();
+ return stream.write_all(
+ format!(
+ "HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\nConnection: close\r\nContent-Length: {}\r\n\r\n{}",
+ error.len(),
+ error
+ )
+ .as_bytes(),
+ );
+ }
+ // The blank-but-billed success: an empty content delta, a "stop"
+ // finish, and usage the provider still bills.
+ SummarizerMode::Blank => {
+ return stream.write_all(
+ format!(
+ "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n{}",
+ sse_stream("", &usage)
+ )
+ .as_bytes(),
+ );
+ }
+ }
}
- payload.push_str("data: [DONE]\n\n");
stream.write_all(
format!(
- "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n{payload}"
+ "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n{}",
+ sse_stream("parity reply", &usage)
)
.as_bytes(),
)
@@ -352,8 +389,24 @@ impl Client {
}
}
-#[test]
-fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
+/// The live session right after a failed threshold auto-compaction: the
+/// broadcast events ride the client, and the harness (client, mock,
+/// supervisor, dir) stays alive for follow-up turns.
+struct FailedCompactionRun {
+ client: Client,
+ mock: CompactionMock,
+ session_id: String,
+ session_dir: PathBuf,
+ #[allow(dead_code)]
+ _supervisor: Supervisor,
+ #[allow(dead_code)]
+ _dir: tempfile::TempDir,
+}
+
+/// Drive the f14-auto shape once: seed a turn over the mock, flip its
+/// summarizer mode to `mode`, and send the crossing turn that fires the
+/// threshold auto-compaction against that mode.
+fn drive_failed_auto_compaction(mode: SummarizerMode) -> FailedCompactionRun {
let dir = tempfile::tempdir().expect("temp dir");
let agent_dir = dir.path().join("agent");
let session_dir = agent_dir.join("sessions");
@@ -391,7 +444,7 @@ fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
)
.expect("write settings.json");
let socket = dir.path().join("daemon.sock");
- let _supervisor = spawn_supervisor(&socket, &agent_dir);
+ let supervisor = spawn_supervisor(&socket, &agent_dir);
let mut client = Client::connect(&socket);
client.send_command(
@@ -430,8 +483,9 @@ fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
assert_eq!(seeded["success"], true, "seed prompt failed: {seeded}");
// The crossing turn reports 126010 tokens (over the 500-token headroom): the
- // threshold check fires a compaction, and the mock fails its summarizer request.
- mock.fail_summarizer.store(true, Ordering::SeqCst);
+ // threshold check fires a compaction, and the mock answers its summarizer
+ // request per `mode`.
+ mock.set_summarizer_mode(mode);
client.send_command(
"p2",
&json!({"type": "prompt_and_wait", "activeSessionId": session_id, "message": "crossing turn"}),
@@ -451,7 +505,23 @@ fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
"the compaction summarizer request never arrived"
);
client.drain_events(500);
+ FailedCompactionRun {
+ client,
+ mock,
+ session_id,
+ session_dir,
+ _supervisor: supervisor,
+ _dir: dir,
+ }
+}
+/// The failure-path observables shared by every failure mode: the durable
+/// `compaction_outcome` row broadcasts before the settled unsuccessful
+/// `compaction_end`, and the session file carries exactly one durable row
+/// with the same failure message. Returns the failure message and the
+/// persisted session file content for mode-specific follow-ups.
+fn assert_unsuccessful_compaction_observed(run: &FailedCompactionRun) -> (String, String) {
+ let client = &run.client;
// The broadcast: the durable row's message pair, then the settled end carrying
// the same failure message (TS `_endCompactionUnsuccessfully` order).
let row_start = client
@@ -465,7 +535,7 @@ fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
panic!(
"the outcome row's message_start broadcast; events: {:#?}; requests: {:#?}",
client.events,
- mock.requests.lock().expect("mock lock").len()
+ run.mock.requests.lock().expect("mock lock").len()
)
});
let row_end = client
@@ -478,7 +548,7 @@ fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
assert!(row_end > row_start, "the row's pair stays in order");
let row = client.events[row_start]["message"].clone();
assert_eq!(row["role"], "custom");
- let failure_message = row["content"].as_str().expect("row content");
+ let failure_message = row["content"].as_str().expect("row content").to_string();
assert!(
failure_message.starts_with("Auto-compaction failed:"),
"the row carries the failure message: {failure_message}"
@@ -511,7 +581,7 @@ fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
// The session file carries the row (TS `appendCustomMessageEntryWithRollback` shape)
// and a session UUID, not the worker's active id.
- let session_file = std::fs::read_dir(&session_dir)
+ let session_file = std::fs::read_dir(&run.session_dir)
.expect("list session dir")
.flatten()
.map(|entry| entry.path())
@@ -537,19 +607,26 @@ fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
durable["details"],
json!({"reason": "threshold", "outcome": "failed"})
);
+ (failure_message, persisted)
+}
+
+#[test]
+fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
+ let mut run = drive_failed_auto_compaction(SummarizerMode::Unavailable);
+ let (failure_message, _) = assert_unsuccessful_compaction_observed(&run);
// The next turn: recovery compaction succeeds and the provider request NEVER contains
// the disclosure — the exclusion that keeps the KV-cacheable prefix unaffected.
- mock.fail_summarizer.store(false, Ordering::SeqCst);
- let before_next = mock.request_count();
- client.send_command(
+ run.mock.set_summarizer_mode(SummarizerMode::TurnReply);
+ let before_next = run.mock.request_count();
+ run.client.send_command(
"p3",
- &json!({"type": "prompt_and_wait", "activeSessionId": session_id, "message": "next turn"}),
+ &json!({"type": "prompt_and_wait", "activeSessionId": run.session_id, "message": "next turn"}),
);
- let next = client.read_response("p3");
+ let next = run.client.read_response("p3");
assert_eq!(next["success"], true, "next prompt failed: {next}");
- client.drain_events(500);
- let requests = mock.requests.lock().expect("mock lock").clone();
+ run.client.drain_events(500);
+ let requests = run.mock.requests.lock().expect("mock lock").clone();
let next_turn_request = requests[before_next..]
.iter()
.find(|body| !is_summarizer_request(body))
@@ -557,7 +634,36 @@ fn forced_failed_auto_compaction_records_the_durable_outcome_row() {
.clone();
let serialized = serde_json::to_string(&next_turn_request).expect("serialize request");
assert!(
- !serialized.contains("compaction_outcome") && !serialized.contains(failure_message),
+ !serialized.contains("compaction_outcome") && !serialized.contains(&failure_message),
"the disclosure never reaches the provider request"
);
}
+
+/// The live failure shape behind the empty-summary bug: an HTTP-success
+/// summarizer reply that is blank-but-billed lands on the SAME failure path
+/// as a 500 — a durable failed outcome row, no compaction row, and the
+/// transcript intact.
+#[test]
+fn blank_summarizer_reply_fails_the_compaction_and_keeps_the_transcript() {
+ let run = drive_failed_auto_compaction(SummarizerMode::Blank);
+ let (_, session_file) = assert_unsuccessful_compaction_observed(&run);
+
+ // The blank summary is never committed: no compaction row lands, and the
+ // message rows from both turns stay in the transcript.
+ let rows: Vec<Value> = session_file
+ .lines()
+ .filter_map(|line| serde_json::from_str(line).ok())
+ .collect();
+ assert!(
+ rows.iter().all(|row| row["type"] != "compaction"),
+ "a blank summary must never become the compaction row: {session_file}"
+ );
+ let transcript: Vec<&Value> = rows.iter().filter(|row| row["type"] == "message").collect();
+ let serialized = serde_json::to_string(&transcript).expect("serialize transcript");
+ for text in ["seed turn", "crossing turn", "parity reply"] {
+ assert!(
+ serialized.contains(text),
+ "the transcript rows stay intact after the failed compaction"
+ );
+ }
+}