huskies: merge 1248 bug Duplicate Working line: obsolete digging-in watcher survives alongside 1240 placeholder

This commit is contained in:
Huskies Agent
2026-07-21 13:46:43 +00:00
parent 1de05b480b
commit 5eeb036875
8 changed files with 2 additions and 204 deletions
@@ -130,10 +130,6 @@ pub struct BotContext {
/// Minimum seconds between repeated `compact` suggestions for the same
/// room. From `bot.toml`'s `compact_suggest_cooldown_secs`.
pub compact_suggest_cooldown_secs: i64,
/// Seconds a turn may spend on tool calls before emitting any
/// user-facing text before the bot posts a "digging in" notice. From
/// `bot.toml`'s `digging_in_threshold_secs`.
pub digging_in_threshold_secs: u64,
}
impl BotContext {
@@ -365,7 +361,6 @@ mod tests {
compact_seed_max_bytes: 8_000,
cache_read_suggest_threshold: 50_000,
compact_suggest_cooldown_secs: 3_600,
digging_in_threshold_secs: 15,
}
}
@@ -9,7 +9,6 @@ use matrix_sdk::ruma::{OwnedEventId, OwnedRoomId};
use std::collections::HashSet;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use tokio::sync::Mutex as TokioMutex;
use tokio::sync::watch;
@@ -19,34 +18,10 @@ use super::super::history::{ConversationEntry, ConversationRole, save_history};
use super::format_user_prompt;
/// Text posted to the room by [`spawn_digging_in_watcher`] when a turn runs
/// long without emitting any user-facing text, and by `on_room_message` as
/// an immediate acknowledgement when a message is first received (story 1239).
/// Text posted by `on_room_message` as an immediate acknowledgement when a
/// message is first received (story 1239).
pub(super) const DIGGING_IN_MESSAGE: &str = "Working...";
/// Spawns a background watcher that posts a single "digging in" notice to
/// `room_id` if `threshold` elapses before `sent_any_text` becomes `true`.
///
/// Callers must abort the returned [`tokio::task::JoinHandle`] once the turn
/// completes so a turn that finishes under the threshold — with or without
/// text — never triggers the notice after the fact.
pub(in crate::chat::transport::matrix::bot) fn spawn_digging_in_watcher(
transport: Arc<dyn ChatTransport>,
room_id: String,
sent_any_text: Arc<AtomicBool>,
threshold: Duration,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
tokio::time::sleep(threshold).await;
if !sent_any_text.load(Ordering::Relaxed) {
let html = markdown_to_html(DIGGING_IN_MESSAGE);
let _ = transport
.send_message(&room_id, DIGGING_IN_MESSAGE, &html)
.await;
}
})
}
/// One live-progress update to apply to the room's placeholder message
/// while a turn runs (story 1240).
enum ProgressUpdate {
@@ -248,21 +223,10 @@ pub(in crate::chat::transport::matrix::bot) async fn handle_message(
);
tokio::pin!(chat_fut);
// Notify the room if this turn spends longer than the configured
// threshold on tool calls before emitting any user-facing text, so a
// long silent turn doesn't look hung. Aborted below once the turn ends.
let digging_in_task = spawn_digging_in_watcher(
Arc::clone(&ctx.transport),
room_id_str.clone(),
Arc::clone(&sent_any_chunk),
Duration::from_secs(ctx.digging_in_threshold_secs),
);
// Permission requests are handled by the persistent permission_listener
// task spawned at bot startup (story 884) — they no longer route through
// per-message handlers. Just await chat_fut.
let result = (&mut chat_fut).await;
digging_in_task.abort();
// Flush any remaining text that didn't end with a paragraph boundary.
let remaining = buffer.lock().unwrap().trim().to_string();
@@ -503,107 +467,6 @@ mod tests {
}
}
/// AC 1: a turn that runs longer than the threshold without sending any
/// text gets exactly one "Working..." notice.
#[tokio::test]
async fn digging_in_fires_after_threshold_when_no_text_sent() {
let transport = Arc::new(CapturingTransport::new());
let sent_any_text = Arc::new(AtomicBool::new(false));
let handle = spawn_digging_in_watcher(
transport.clone() as Arc<dyn ChatTransport>,
"!room:example.com".to_string(),
Arc::clone(&sent_any_text),
Duration::from_millis(30),
);
handle.await.unwrap();
assert_eq!(transport.sent_count(), 1);
assert_eq!(transport.last_message().unwrap(), "Working...");
}
/// AC 2: if text is sent before the threshold elapses, the watcher must
/// not post anything.
#[tokio::test]
async fn digging_in_does_not_fire_when_text_sent_before_threshold() {
let transport = Arc::new(CapturingTransport::new());
let sent_any_text = Arc::new(AtomicBool::new(false));
let handle = spawn_digging_in_watcher(
transport.clone() as Arc<dyn ChatTransport>,
"!room:example.com".to_string(),
Arc::clone(&sent_any_text),
Duration::from_millis(30),
);
sent_any_text.store(true, Ordering::Relaxed);
handle.await.unwrap();
assert_eq!(transport.sent_count(), 0);
}
/// AC 2: a fast turn that completes (and is aborted by its caller)
/// before the threshold elapses must not post anything, even if it
/// never sent any text either.
#[tokio::test]
async fn digging_in_does_not_fire_when_aborted_before_threshold() {
let transport = Arc::new(CapturingTransport::new());
let sent_any_text = Arc::new(AtomicBool::new(false));
let handle = spawn_digging_in_watcher(
transport.clone() as Arc<dyn ChatTransport>,
"!room:example.com".to_string(),
Arc::clone(&sent_any_text),
Duration::from_millis(200),
);
handle.abort();
tokio::time::sleep(Duration::from_millis(250)).await;
assert_eq!(transport.sent_count(), 0);
}
/// AC 3: the notice fires at most once per turn — even waiting well past
/// the threshold never produces a second message.
#[tokio::test]
async fn digging_in_fires_at_most_once_even_after_extra_wait() {
let transport = Arc::new(CapturingTransport::new());
let sent_any_text = Arc::new(AtomicBool::new(false));
let handle = spawn_digging_in_watcher(
transport.clone() as Arc<dyn ChatTransport>,
"!room:example.com".to_string(),
Arc::clone(&sent_any_text),
Duration::from_millis(20),
);
handle.await.unwrap();
tokio::time::sleep(Duration::from_millis(100)).await;
assert_eq!(transport.sent_count(), 1);
}
/// AC 5: state is fresh per call — spawning a second, independent
/// watcher (as a new turn would) does not inherit any state from a
/// prior watcher's `sent_any_text` flag.
#[tokio::test]
async fn digging_in_state_does_not_leak_across_turns() {
let transport = Arc::new(CapturingTransport::new());
// First "turn": text sent before threshold, so no notice.
let first_sent_any_text = Arc::new(AtomicBool::new(false));
let first_handle = spawn_digging_in_watcher(
transport.clone() as Arc<dyn ChatTransport>,
"!room:example.com".to_string(),
Arc::clone(&first_sent_any_text),
Duration::from_millis(30),
);
first_sent_any_text.store(true, Ordering::Relaxed);
first_handle.await.unwrap();
assert_eq!(transport.sent_count(), 0);
// Second "turn": fresh flag, no text sent — must fire independently
// of the first turn's outcome.
let second_sent_any_text = Arc::new(AtomicBool::new(false));
let second_handle = spawn_digging_in_watcher(
transport.clone() as Arc<dyn ChatTransport>,
"!room:example.com".to_string(),
Arc::clone(&second_sent_any_text),
Duration::from_millis(30),
);
second_handle.await.unwrap();
assert_eq!(transport.sent_count(), 1);
}
// ── run_progress_updates (story 1240) ─────────────────────────────────
fn spawn_progress(
@@ -2092,7 +2092,6 @@ mod tests {
compact_seed_max_bytes: 8_000,
cache_read_suggest_threshold: 50_000,
compact_suggest_cooldown_secs: 3_600,
digging_in_threshold_secs: 15,
}
}
@@ -343,7 +343,6 @@ pub async fn run_bot(
compact_seed_max_bytes: config.compact_seed_max_bytes,
cache_read_suggest_threshold: config.cache_read_suggest_threshold,
compact_suggest_cooldown_secs: config.compact_suggest_cooldown_secs,
digging_in_threshold_secs: config.digging_in_threshold_secs,
};
slog!(
@@ -172,49 +172,6 @@ history_size = 50
assert_eq!(config.history_size, 50);
}
/// AC4: `digging_in_threshold_secs` defaults to 15 when unset in bot.toml.
#[test]
fn load_uses_default_digging_in_threshold_secs() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".huskies");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"
homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
"#,
)
.unwrap();
let config = BotConfig::load(tmp.path()).unwrap();
assert_eq!(config.digging_in_threshold_secs, 15);
}
/// AC4: `digging_in_threshold_secs` can be overridden in bot.toml.
#[test]
fn load_respects_custom_digging_in_threshold_secs() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".huskies");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"
homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
digging_in_threshold_secs = 30
"#,
)
.unwrap();
let config = BotConfig::load(tmp.path()).unwrap();
assert_eq!(config.digging_in_threshold_secs, 30);
}
#[test]
fn load_reads_display_name() {
let tmp = tempfile::tempdir().unwrap();
@@ -31,12 +31,6 @@ pub(super) fn default_compact_suggest_cooldown_secs() -> i64 {
3_600
}
/// Default threshold (seconds) a turn may spend on tool calls before
/// emitting any user-facing text before the bot posts a "digging in" notice.
pub(super) fn default_digging_in_threshold_secs() -> u64 {
15
}
pub(super) fn default_transport() -> String {
"matrix".to_string()
}
@@ -229,10 +223,4 @@ pub struct BotConfig {
/// room, so a busy room isn't spammed every turn. Defaults to 3600 (1h).
#[serde(default = "default_compact_suggest_cooldown_secs")]
pub compact_suggest_cooldown_secs: i64,
/// Seconds a turn may spend on tool calls before emitting any
/// user-facing text before the bot posts a "digging in" notice to the
/// room, so a silent long-running turn doesn't look hung. Defaults to 15.
#[serde(default = "default_digging_in_threshold_secs")]
pub digging_in_threshold_secs: u64,
}
@@ -959,7 +959,6 @@ mod tests {
compact_seed_max_bytes: 8_000,
cache_read_suggest_threshold: 50_000,
compact_suggest_cooldown_secs: 3_600,
digging_in_threshold_secs: 15,
}
}
}
@@ -102,7 +102,6 @@ mod tests {
compact_seed_max_bytes: 8_000,
cache_read_suggest_threshold: 50_000,
compact_suggest_cooldown_secs: 3_600,
digging_in_threshold_secs: 15,
};
run_projects_list(&ctx).await
}
@@ -225,7 +224,6 @@ mod tests {
compact_seed_max_bytes: 8_000,
cache_read_suggest_threshold: 50_000,
compact_suggest_cooldown_secs: 3_600,
digging_in_threshold_secs: 15,
};
let response = run_projects_list(&ctx).await;
assert!(