fix: add --all to cargo fmt in script/test and autoformat codebase
cargo fmt without --all fails with "Failed to find targets" in workspace repos. This was blocking every story's gates. Also ran cargo fmt --all to fix all existing formatting issues. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
//! Slack incoming message dispatch and slash command handling.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::sync::{Mutex as TokioMutex, oneshot};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::format::markdown_to_slack;
|
||||
use super::history::{SlackConversationHistory, save_slack_history};
|
||||
use super::meta::SlackTransport;
|
||||
use crate::agents::AgentPool;
|
||||
use crate::chat::ChatTransport;
|
||||
use crate::chat::transport::matrix::{ConversationEntry, ConversationRole, RoomConversation};
|
||||
use crate::chat::util::is_permission_approval;
|
||||
use crate::slog;
|
||||
use crate::chat::ChatTransport;
|
||||
use crate::http::context::{PermissionDecision, PermissionForward};
|
||||
use super::meta::SlackTransport;
|
||||
use super::history::{SlackConversationHistory, save_slack_history};
|
||||
use super::format::markdown_to_slack;
|
||||
use crate::slog;
|
||||
|
||||
// ── Slash command types ─────────────────────────────────────────────────
|
||||
|
||||
@@ -81,8 +81,7 @@ pub struct SlackWebhookContext {
|
||||
/// Permission requests from the MCP `prompt_permission` tool arrive here.
|
||||
pub perm_rx: Arc<TokioMutex<tokio::sync::mpsc::UnboundedReceiver<PermissionForward>>>,
|
||||
/// Pending permission replies keyed by channel ID.
|
||||
pub pending_perm_replies:
|
||||
Arc<TokioMutex<HashMap<String, oneshot::Sender<PermissionDecision>>>>,
|
||||
pub pending_perm_replies: Arc<TokioMutex<HashMap<String, oneshot::Sender<PermissionDecision>>>>,
|
||||
/// Seconds before an unanswered permission prompt is auto-denied.
|
||||
pub permission_timeout_secs: u64,
|
||||
}
|
||||
@@ -154,8 +153,11 @@ pub(super) async fn handle_incoming_message(
|
||||
}
|
||||
HtopCommand::Start { duration_secs } => {
|
||||
// On Slack, htop uses native message editing for live updates.
|
||||
let snapshot =
|
||||
crate::chat::transport::matrix::htop::build_htop_message(&ctx.agents, 0, duration_secs);
|
||||
let snapshot = crate::chat::transport::matrix::htop::build_htop_message(
|
||||
&ctx.agents,
|
||||
0,
|
||||
duration_secs,
|
||||
);
|
||||
let snapshot = markdown_to_slack(&snapshot);
|
||||
let msg_id = match ctx.transport.send_message(channel, &snapshot, "").await {
|
||||
Ok(id) => id,
|
||||
@@ -179,9 +181,7 @@ pub(super) async fn handle_incoming_message(
|
||||
duration_secs,
|
||||
);
|
||||
let updated = markdown_to_slack(&updated);
|
||||
if let Err(e) =
|
||||
transport.edit_message(&ch, &msg_id, &updated, "").await
|
||||
{
|
||||
if let Err(e) = transport.edit_message(&ch, &msg_id, &updated, "").await {
|
||||
slog!("[slack] Failed to edit htop message: {e}");
|
||||
break;
|
||||
}
|
||||
@@ -245,7 +245,9 @@ pub(super) async fn handle_incoming_message(
|
||||
) {
|
||||
let response = match rmtree_cmd {
|
||||
crate::chat::transport::matrix::rmtree::RmtreeCommand::Rmtree { story_number } => {
|
||||
slog!("[slack] Handling rmtree command from {user} in {channel}: story {story_number}");
|
||||
slog!(
|
||||
"[slack] Handling rmtree command from {user} in {channel}: story {story_number}"
|
||||
);
|
||||
crate::chat::transport::matrix::rmtree::handle_rmtree(
|
||||
&ctx.bot_name,
|
||||
&story_number,
|
||||
@@ -273,7 +275,9 @@ pub(super) async fn handle_incoming_message(
|
||||
slog!("[slack] Handling reset command from {user} in {channel}");
|
||||
{
|
||||
let mut guard = ctx.history.lock().await;
|
||||
let conv = guard.entry(channel.to_string()).or_insert_with(RoomConversation::default);
|
||||
let conv = guard
|
||||
.entry(channel.to_string())
|
||||
.or_insert_with(RoomConversation::default);
|
||||
conv.session_id = None;
|
||||
conv.entries.clear();
|
||||
save_slack_history(&ctx.project_root, &guard);
|
||||
@@ -295,7 +299,9 @@ pub(super) async fn handle_incoming_message(
|
||||
story_number,
|
||||
agent_hint,
|
||||
} => {
|
||||
slog!("[slack] Handling start command from {user} in {channel}: story {story_number}");
|
||||
slog!(
|
||||
"[slack] Handling start command from {user} in {channel}: story {story_number}"
|
||||
);
|
||||
crate::chat::transport::matrix::start::handle_start(
|
||||
&ctx.bot_name,
|
||||
&story_number,
|
||||
@@ -320,8 +326,13 @@ pub(super) async fn handle_incoming_message(
|
||||
&ctx.bot_user_id,
|
||||
) {
|
||||
let response = match assign_cmd {
|
||||
crate::chat::transport::matrix::assign::AssignCommand::Assign { story_number, model } => {
|
||||
slog!("[slack] Handling assign command from {user} in {channel}: story {story_number} model {model}");
|
||||
crate::chat::transport::matrix::assign::AssignCommand::Assign {
|
||||
story_number,
|
||||
model,
|
||||
} => {
|
||||
slog!(
|
||||
"[slack] Handling assign command from {user} in {channel}: story {story_number} model {model}"
|
||||
);
|
||||
crate::chat::transport::matrix::assign::handle_assign(
|
||||
&ctx.bot_name,
|
||||
&story_number,
|
||||
@@ -352,17 +363,15 @@ async fn handle_llm_message(
|
||||
user: &str,
|
||||
user_message: &str,
|
||||
) {
|
||||
use crate::llm::providers::claude_code::{ClaudeCodeProvider, ClaudeCodeResult};
|
||||
use crate::chat::util::drain_complete_paragraphs;
|
||||
use crate::llm::providers::claude_code::{ClaudeCodeProvider, ClaudeCodeResult};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tokio::sync::watch;
|
||||
|
||||
// Look up existing session ID for this channel.
|
||||
let resume_session_id: Option<String> = {
|
||||
let guard = ctx.history.lock().await;
|
||||
guard
|
||||
.get(channel)
|
||||
.and_then(|conv| conv.session_id.clone())
|
||||
guard.get(channel).and_then(|conv| conv.session_id.clone())
|
||||
};
|
||||
|
||||
let bot_name = &ctx.bot_name;
|
||||
@@ -383,7 +392,9 @@ async fn handle_llm_message(
|
||||
let post_task = tokio::spawn(async move {
|
||||
while let Some(chunk) = msg_rx.recv().await {
|
||||
let formatted = markdown_to_slack(&chunk);
|
||||
let _ = post_transport.send_message(&post_channel, &formatted, "").await;
|
||||
let _ = post_transport
|
||||
.send_message(&post_channel, &formatted, "")
|
||||
.await;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -472,9 +483,7 @@ async fn handle_llm_message(
|
||||
let last_text = messages
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|m| {
|
||||
m.role == crate::llm::types::Role::Assistant && !m.content.is_empty()
|
||||
})
|
||||
.find(|m| m.role == crate::llm::types::Role::Assistant && !m.content.is_empty())
|
||||
.map(|m| m.content.clone())
|
||||
.unwrap_or_default();
|
||||
if !last_text.is_empty() {
|
||||
@@ -559,7 +568,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn slash_command_maps_status() {
|
||||
assert_eq!(slash_command_to_bot_keyword("/huskies-status"), Some("status"));
|
||||
assert_eq!(
|
||||
slash_command_to_bot_keyword("/huskies-status"),
|
||||
Some("status")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -600,9 +612,8 @@ mod tests {
|
||||
response_type: "ephemeral",
|
||||
text: "hello".to_string(),
|
||||
};
|
||||
let json: serde_json::Value = serde_json::from_str(
|
||||
&serde_json::to_string(&resp).unwrap()
|
||||
).unwrap();
|
||||
let json: serde_json::Value =
|
||||
serde_json::from_str(&serde_json::to_string(&resp).unwrap()).unwrap();
|
||||
assert_eq!(json["response_type"], "ephemeral");
|
||||
assert_eq!(json["text"], "hello");
|
||||
}
|
||||
@@ -642,7 +653,10 @@ mod tests {
|
||||
};
|
||||
|
||||
let result = try_handle_command(&dispatch, &synthetic);
|
||||
assert!(result.is_some(), "status slash command should produce output via registry");
|
||||
assert!(
|
||||
result.is_some(),
|
||||
"status slash command should produce output via registry"
|
||||
);
|
||||
assert!(result.unwrap().contains("Pipeline Status"));
|
||||
}
|
||||
|
||||
@@ -671,7 +685,10 @@ mod tests {
|
||||
let result = try_handle_command(&dispatch, &synthetic);
|
||||
assert!(result.is_some(), "show slash command should produce output");
|
||||
let output = result.unwrap();
|
||||
assert!(output.contains("999"), "show output should reference the story number: {output}");
|
||||
assert!(
|
||||
output.contains("999"),
|
||||
"show output should reference the story number: {output}"
|
||||
);
|
||||
}
|
||||
|
||||
// ── rebuild command extraction ─────────────────────────────────────
|
||||
@@ -704,7 +721,10 @@ mod tests {
|
||||
"Huskies",
|
||||
"slack-bot",
|
||||
);
|
||||
assert!(result.is_none(), "'status' should not be recognised as rebuild");
|
||||
assert!(
|
||||
result.is_none(),
|
||||
"'status' should not be recognised as rebuild"
|
||||
);
|
||||
}
|
||||
|
||||
// ── reset command extraction ───────────────────────────────────────
|
||||
@@ -731,21 +751,26 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn reset_command_clears_slack_session() {
|
||||
use crate::chat::transport::matrix::{
|
||||
ConversationEntry, ConversationRole, RoomConversation,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex as TokioMutex;
|
||||
use crate::chat::transport::matrix::{ConversationEntry, ConversationRole, RoomConversation};
|
||||
|
||||
let channel = "C01ABCDEF";
|
||||
let history: SlackConversationHistory = Arc::new(TokioMutex::new({
|
||||
let mut m = HashMap::new();
|
||||
m.insert(channel.to_string(), RoomConversation {
|
||||
session_id: Some("old-session".to_string()),
|
||||
entries: vec![ConversationEntry {
|
||||
role: ConversationRole::User,
|
||||
sender: "U01GHIJKL".to_string(),
|
||||
content: "previous message".to_string(),
|
||||
}],
|
||||
});
|
||||
m.insert(
|
||||
channel.to_string(),
|
||||
RoomConversation {
|
||||
session_id: Some("old-session".to_string()),
|
||||
entries: vec![ConversationEntry {
|
||||
role: ConversationRole::User,
|
||||
sender: "U01GHIJKL".to_string(),
|
||||
content: "previous message".to_string(),
|
||||
}],
|
||||
},
|
||||
);
|
||||
m
|
||||
}));
|
||||
|
||||
@@ -755,7 +780,9 @@ mod tests {
|
||||
|
||||
{
|
||||
let mut guard = history.lock().await;
|
||||
let conv = guard.entry(channel.to_string()).or_insert_with(RoomConversation::default);
|
||||
let conv = guard
|
||||
.entry(channel.to_string())
|
||||
.or_insert_with(RoomConversation::default);
|
||||
conv.session_id = None;
|
||||
conv.entries.clear();
|
||||
save_slack_history(tmp.path(), &guard);
|
||||
@@ -862,6 +889,9 @@ mod tests {
|
||||
"Timmy",
|
||||
"@timmy:home.local",
|
||||
);
|
||||
assert!(result.is_none(), "'status' should not be recognised as assign on Slack");
|
||||
assert!(
|
||||
result.is_none(),
|
||||
"'status' should not be recognised as assign on Slack"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +20,8 @@ pub fn markdown_to_slack(text: &str) -> String {
|
||||
LazyLock::new(|| Regex::new(r"(?m)^#{1,6}\s+(.+)$").unwrap());
|
||||
static RE_BOLD_ITALIC: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"\*\*\*(.+?)\*\*\*").unwrap());
|
||||
static RE_BOLD: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"\*\*(.+?)\*\*").unwrap());
|
||||
static RE_STRIKETHROUGH: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"~~(.+?)~~").unwrap());
|
||||
static RE_BOLD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\*\*(.+?)\*\*").unwrap());
|
||||
static RE_STRIKETHROUGH: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"~~(.+?)~~").unwrap());
|
||||
static RE_LINK: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"\[([^\]]+)\]\(([^)]+)\)").unwrap());
|
||||
|
||||
@@ -105,8 +103,14 @@ mod tests {
|
||||
fn slack_fenced_code_block_preserved() {
|
||||
let input = "```rust\nlet x = 1;\n```";
|
||||
let output = markdown_to_slack(input);
|
||||
assert!(output.contains("let x = 1;"), "code block content must be preserved");
|
||||
assert!(output.contains("```"), "fenced code delimiters must be preserved");
|
||||
assert!(
|
||||
output.contains("let x = 1;"),
|
||||
"code block content must be preserved"
|
||||
);
|
||||
assert!(
|
||||
output.contains("```"),
|
||||
"fenced code delimiters must be preserved"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -104,9 +104,8 @@ impl ChatTransport for SlackTransport {
|
||||
return Err(format!("Slack API returned {status}: {resp_text}"));
|
||||
}
|
||||
|
||||
let parsed: SlackApiResponse = serde_json::from_str(&resp_text).map_err(|e| {
|
||||
format!("Failed to parse Slack API response: {e} — body: {resp_text}")
|
||||
})?;
|
||||
let parsed: SlackApiResponse = serde_json::from_str(&resp_text)
|
||||
.map_err(|e| format!("Failed to parse Slack API response: {e} — body: {resp_text}"))?;
|
||||
|
||||
if !parsed.ok {
|
||||
return Err(format!(
|
||||
@@ -190,10 +189,7 @@ mod tests {
|
||||
.create_async()
|
||||
.await;
|
||||
|
||||
let transport = SlackTransport::with_api_base(
|
||||
"xoxb-test-token".to_string(),
|
||||
server.url(),
|
||||
);
|
||||
let transport = SlackTransport::with_api_base("xoxb-test-token".to_string(), server.url());
|
||||
|
||||
let result = transport
|
||||
.send_message("C01ABCDEF", "hello", "<p>hello</p>")
|
||||
@@ -212,14 +208,9 @@ mod tests {
|
||||
.create_async()
|
||||
.await;
|
||||
|
||||
let transport = SlackTransport::with_api_base(
|
||||
"xoxb-test-token".to_string(),
|
||||
server.url(),
|
||||
);
|
||||
let transport = SlackTransport::with_api_base("xoxb-test-token".to_string(), server.url());
|
||||
|
||||
let result = transport
|
||||
.send_message("C_INVALID", "hello", "")
|
||||
.await;
|
||||
let result = transport.send_message("C_INVALID", "hello", "").await;
|
||||
assert!(result.is_err());
|
||||
assert!(
|
||||
result.unwrap_err().contains("channel_not_found"),
|
||||
@@ -237,10 +228,7 @@ mod tests {
|
||||
.create_async()
|
||||
.await;
|
||||
|
||||
let transport = SlackTransport::with_api_base(
|
||||
"xoxb-test-token".to_string(),
|
||||
server.url(),
|
||||
);
|
||||
let transport = SlackTransport::with_api_base("xoxb-test-token".to_string(), server.url());
|
||||
|
||||
let result = transport
|
||||
.edit_message("C01ABCDEF", "1234567890.123456", "updated", "")
|
||||
@@ -258,10 +246,7 @@ mod tests {
|
||||
.create_async()
|
||||
.await;
|
||||
|
||||
let transport = SlackTransport::with_api_base(
|
||||
"xoxb-test-token".to_string(),
|
||||
server.url(),
|
||||
);
|
||||
let transport = SlackTransport::with_api_base("xoxb-test-token".to_string(), server.url());
|
||||
|
||||
let result = transport
|
||||
.edit_message("C01ABCDEF", "bad-ts", "updated", "")
|
||||
@@ -287,10 +272,7 @@ mod tests {
|
||||
.create_async()
|
||||
.await;
|
||||
|
||||
let transport = SlackTransport::with_api_base(
|
||||
"xoxb-test-token".to_string(),
|
||||
server.url(),
|
||||
);
|
||||
let transport = SlackTransport::with_api_base("xoxb-test-token".to_string(), server.url());
|
||||
|
||||
let result = transport.send_message("C01ABCDEF", "hello", "").await;
|
||||
assert!(result.is_err());
|
||||
|
||||
@@ -12,15 +12,15 @@ pub mod history;
|
||||
pub mod meta;
|
||||
pub mod verify;
|
||||
|
||||
pub use commands::SlackWebhookContext;
|
||||
pub use format::markdown_to_slack;
|
||||
pub use history::load_slack_history;
|
||||
pub use meta::SlackTransport;
|
||||
pub use format::markdown_to_slack;
|
||||
pub use commands::SlackWebhookContext;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use poem::{Request, Response, handler, http::StatusCode};
|
||||
use crate::slog;
|
||||
use poem::{Request, Response, handler, http::StatusCode};
|
||||
|
||||
// ── Slack Events API types ──────────────────────────────────────────────
|
||||
|
||||
@@ -71,10 +71,7 @@ pub async fn webhook_receive(
|
||||
.header("X-Slack-Request-Timestamp")
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let signature = req
|
||||
.header("X-Slack-Signature")
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let signature = req.header("X-Slack-Signature").unwrap_or("").to_string();
|
||||
|
||||
let bytes = match body.into_bytes().await {
|
||||
Ok(b) => b,
|
||||
@@ -98,9 +95,7 @@ pub async fn webhook_receive(
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
slog!("[slack] Failed to parse webhook payload: {e}");
|
||||
return Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body("ok");
|
||||
return Response::builder().status(StatusCode::OK).body("ok");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -124,8 +119,7 @@ pub async fn webhook_receive(
|
||||
&& event.r#type.as_deref() == Some("message")
|
||||
&& event.subtype.is_none()
|
||||
&& event.bot_id.is_none()
|
||||
&& let (Some(channel), Some(user), Some(text)) =
|
||||
(event.channel, event.user, event.text)
|
||||
&& let (Some(channel), Some(user), Some(text)) = (event.channel, event.user, event.text)
|
||||
&& ctx.channel_ids.contains(&channel)
|
||||
{
|
||||
let ctx = Arc::clone(*ctx);
|
||||
@@ -135,9 +129,7 @@ pub async fn webhook_receive(
|
||||
});
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body("ok")
|
||||
Response::builder().status(StatusCode::OK).body("ok")
|
||||
}
|
||||
|
||||
/// POST /webhook/slack/command — receive incoming Slack slash commands.
|
||||
@@ -155,10 +147,7 @@ pub async fn slash_command_receive(
|
||||
.header("X-Slack-Request-Timestamp")
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let signature = req
|
||||
.header("X-Slack-Signature")
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let signature = req.header("X-Slack-Signature").unwrap_or("").to_string();
|
||||
|
||||
let bytes = match body.into_bytes().await {
|
||||
Ok(b) => b,
|
||||
@@ -178,16 +167,15 @@ pub async fn slash_command_receive(
|
||||
.body("Invalid signature");
|
||||
}
|
||||
|
||||
let payload: commands::SlackSlashCommandPayload =
|
||||
match serde_urlencoded::from_bytes(&bytes) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
slog!("[slack] Failed to parse slash command payload: {e}");
|
||||
return Response::builder()
|
||||
.status(StatusCode::BAD_REQUEST)
|
||||
.body("Bad request");
|
||||
}
|
||||
};
|
||||
let payload: commands::SlackSlashCommandPayload = match serde_urlencoded::from_bytes(&bytes) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
slog!("[slack] Failed to parse slash command payload: {e}");
|
||||
return Response::builder()
|
||||
.status(StatusCode::BAD_REQUEST)
|
||||
.body("Bad request");
|
||||
}
|
||||
};
|
||||
|
||||
slog!(
|
||||
"[slack] Slash command from {}: {} {}",
|
||||
|
||||
@@ -215,7 +215,12 @@ mod tests {
|
||||
let body = b"test body";
|
||||
|
||||
let sig = compute_test_signature("correct-secret", timestamp, body);
|
||||
assert!(!verify_slack_signature("wrong-secret", timestamp, body, &sig));
|
||||
assert!(!verify_slack_signature(
|
||||
"wrong-secret",
|
||||
timestamp,
|
||||
body,
|
||||
&sig
|
||||
));
|
||||
}
|
||||
|
||||
/// Helper to compute a test signature using our sha256 + HMAC implementation.
|
||||
|
||||
Reference in New Issue
Block a user