Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test: cover the compaction status end to end

Sanil27d6a9c8d61c
diff --git a/app/src/features/conversations/aui/AgentRunningStatus.test.tsx b/app/src/features/conversations/aui/AgentRunningStatus.test.tsx
index 6cef2abe59..4df546f6ad 100644
--- a/app/src/features/conversations/aui/AgentRunningStatus.test.tsx
+++ b/app/src/features/conversations/aui/AgentRunningStatus.test.tsx
@@ -1,5 +1,5 @@
import { combineReducers, configureStore } from '@reduxjs/toolkit';
-import { render, screen } from '@testing-library/react';
+import { act, render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { describe, expect, it, vi } from 'vitest';
@@ -117,6 +117,25 @@ describe('AgentRunningStatus while compacting', () => {
);
});
+ it('counts the elapsed time once a second has passed', () => {
+ vi.useFakeTimers();
+ try {
+ renderStatus({
+ inferenceStatusByThread: {
+ [THREAD_ID]: { phase: 'compacting', iteration: 1, maxIterations: 0 },
+ },
+ });
+ act(() => {
+ vi.advanceTimersByTime(2500);
+ });
+ expect(screen.getByTestId('agent-running-status-thinking')).toHaveTextContent(
+ /Summarizing earlier conversation · \d/
+ );
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('thinks again once the next model call starts', () => {
renderStatus({
inferenceStatusByThread: {
diff --git a/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx b/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
index 35c2044347..e6ffb5fe51 100644
--- a/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
+++ b/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
@@ -581,6 +581,48 @@ describe('ChatRuntimeProvider — dedupe, proactive resolution, mid-turn invaria
expect(store.getState().chatRuntime.inferenceHeartbeatByThread['t-par']).toBe(2);
});
+ it('shows a primary turn compacting until its next iteration starts', () => {
+ const listeners = renderProvider();
+ const phase = () => store.getState().chatRuntime.inferenceStatusByThread['t-cmp']?.phase;
+
+ act(() => {
+ listeners.onContextCompacting?.({ thread_id: 't-cmp', request_id: 'primary', round: 1 });
+ });
+ expect(phase()).toBe('compacting');
+
+ act(() => {
+ listeners.onIterationStart?.({
+ thread_id: 't-cmp',
+ request_id: 'primary',
+ round: 1,
+ message: 'Iteration 1/200',
+ });
+ });
+ expect(phase()).toBe('thinking');
+ });
+
+ it('ignores a superseded turn compacting', () => {
+ const listeners = renderProvider();
+
+ act(() => {
+ listeners.onInferenceStart?.({ thread_id: 't-cmp-old', request_id: 'newer' });
+ listeners.onContextCompacting?.({ thread_id: 't-cmp-old', request_id: 'older', round: 1 });
+ });
+ expect(store.getState().chatRuntime.inferenceStatusByThread['t-cmp-old']?.phase).toBe(
+ 'thinking'
+ );
+ });
+
+ it('ignores a parallel branch compacting', () => {
+ const listeners = renderProvider();
+
+ act(() => {
+ store.dispatch(registerParallelRequest({ threadId: 't-cmp-par', requestId: 'branch' }));
+ listeners.onContextCompacting?.({ thread_id: 't-cmp-par', request_id: 'branch', round: 1 });
+ });
+ expect(store.getState().chatRuntime.inferenceStatusByThread['t-cmp-par']).toBeUndefined();
+ });
+
it('drops duplicate chat_done events with the same thread/request', async () => {
const listeners = renderProvider();
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_exec_tests.rs b/crates/openhuman-core/src/agent/tinyagents/turn_exec_tests.rs
index 1b44141e27..691b05a033 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_exec_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec_tests.rs
@@ -604,3 +604,73 @@ async fn session_screens_the_new_input_but_not_a_replayed_tool_results_row() {
"a blocked input never reaches the model"
);
}
+
+/// A root turn over its compaction trigger says so before the model call the
+/// summary is for: on a long chat the summary can hold that call for minutes.
+#[tokio::test]
+async fn a_root_turn_reports_its_compaction_before_the_model_call() {
+ use crate::progress::AgentProgress;
+
+ let (progress, mut events) = tokio::sync::mpsc::channel(256);
+ let model: Arc<dyn ChatModel<()>> =
+ Arc::new(tinyagents_harness::testkit::ScriptedModel::replies(vec![
+ "summary", "done", "done",
+ ]));
+ let mut history = vec![TranscriptMessage::system("system-compacting")];
+ for round in 0..6 {
+ history.push(TranscriptMessage::user(format!(
+ "request {round}: {}",
+ "earlier detail ".repeat(40)
+ )));
+ history.push(TranscriptMessage::assistant(format!(
+ "answer {round}: {}",
+ "earlier result ".repeat(40)
+ )));
+ }
+ history.push(TranscriptMessage::user("continue"));
+
+ TurnSpec {
+ run_context: root_context("compacting", "/tmp/compacting", progress),
+ turn_models: TurnModelSource::from_model(model)
+ .build("root-test-model", 0.0, None, None)
+ .expect("scripted turn models build"),
+ provider_id: "test".to_string(),
+ model: "root-test-model",
+ history,
+ tool_sets: vec![Arc::new(Vec::new())],
+ allowed: Some(Default::default()),
+ max_iterations: 2,
+ context_window: None,
+ run_queue: None,
+ message_run_queue: None,
+ early_exit_tools: &[],
+ pause_at_cap: false,
+ max_output_tokens: None,
+ context_mw: TurnContextMiddleware {
+ autocompact_enabled: true,
+ compaction: crate::config::CompactionSettings {
+ trigger_tokens: Some(200),
+ strategy: crate::config::CompactionStrategy::Summary,
+ },
+ ..TurnContextMiddleware::default()
+ },
+ tool_policy: None,
+ defer_turn_completed_to_caller: true,
+ subagent_scope: None,
+ deterministic_cacheable: false,
+ }
+ .run_hosted(hosted_base(), "main".to_string())
+ .await
+ .expect("hosted root succeeds");
+
+ let mut kinds = Vec::new();
+ while let Ok(event) = events.try_recv() {
+ match event {
+ AgentProgress::ContextCompacting => kinds.push("compacting"),
+ AgentProgress::IterationStarted { .. } => kinds.push("iteration"),
+ _ => {}
+ }
+ }
+ assert_eq!(kinds.first(), Some(&"compacting"), "{kinds:?}");
+ assert!(kinds.contains(&"iteration"), "{kinds:?}");
+}
diff --git a/crates/openhuman-core/src/skills/run_log_tests.rs b/crates/openhuman-core/src/skills/run_log_tests.rs
index 1fcf6e0b4c..dc8bc7e5e4 100644
--- a/crates/openhuman-core/src/skills/run_log_tests.rs
+++ b/crates/openhuman-core/src/skills/run_log_tests.rs
@@ -99,3 +99,12 @@ async fn host_written_log_parses_like_legacy_layout() {
assert_eq!(old[0].status, "FAILED");
assert_eq!(old[0].duration_ms, Some(5));
}
+
+#[test]
+fn a_compaction_is_logged_as_a_step() {
+ let line = format_event(&AgentProgress::ContextCompacting).expect("a step line");
+ assert!(
+ line.ends_with("· summarizing earlier conversation"),
+ "{line}"
+ );
+}
diff --git a/crates/openhuman-core/src/threads/turn_state/mirror_observe_tests.rs b/crates/openhuman-core/src/threads/turn_state/mirror_observe_tests.rs
index 82cee92e99..de456e2b37 100644
--- a/crates/openhuman-core/src/threads/turn_state/mirror_observe_tests.rs
+++ b/crates/openhuman-core/src/threads/turn_state/mirror_observe_tests.rs
@@ -738,3 +738,14 @@ fn subagent_completed_persists_capped_output_on_the_activity() {
assert_eq!(activity.output.as_deref(), Some("final answer"));
assert_eq!(activity.parent_call_id.as_deref(), Some("call-3"));
}
+
+#[test]
+fn compacting_neither_flushes_nor_changes_the_phase() {
+ let (_d, mut m) = fresh("t");
+ m.observe(&AgentProgress::IterationStarted {
+ iteration: 1,
+ max_iterations: 25,
+ });
+ assert!(!m.observe(&AgentProgress::ContextCompacting));
+ assert_eq!(m.snapshot().phase, Some(TurnPhase::Thinking));
+}
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs b/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
index 559145572f..b3dbdc614b 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
@@ -644,3 +644,25 @@ mod tool_progress_tests;
#[path = "progress_bridge_subagent_tests.rs"]
mod subagent_tests;
+
+/// A compaction reaches the client as `context_compacting`, for the status
+/// line to say what a long pause before the next model call is.
+#[tokio::test]
+async fn forwards_a_compaction_as_context_compacting() {
+ let mut events = super::super::event_bus::subscribe_web_channel_events();
+ let thread_id = "thread-context-compacting";
+ let request_id = "req-context-compacting";
+ let tx = spawn_test_bridge(thread_id, request_id);
+
+ tx.send(AgentProgress::TurnStarted).await.unwrap();
+ tx.send(AgentProgress::ContextCompacting).await.unwrap();
+
+ let compacting = loop {
+ let event = recv_for_thread(&mut events, thread_id).await;
+ if event.event == "context_compacting" {
+ break event;
+ }
+ };
+ assert_eq!(compacting.request_id, request_id);
+ drop(tx);
+}
diff --git a/crates/openhuman-core/src/web_chat/turn_activity_tests.rs b/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
index ad55b85e86..1a6327c6d1 100644
--- a/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
+++ b/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
@@ -307,3 +307,11 @@ async fn a_long_call_that_reports_progress_stays_active_past_the_expiry() {
assert!(!watchdog.is_finished(), "an hour of progress beats");
watchdog.abort();
}
+
+#[test]
+fn a_compaction_names_the_step_the_turn_stopped_after() {
+ assert_eq!(
+ step_of(&AgentProgress::ContextCompacting).as_deref(),
+ Some("it began summarizing earlier conversation")
+ );
+}