use crate::Workspace;
use crate::tools::Tool;
use anyhow::Result;
use async_trait::async_trait;
use serde_json::json;
use std::fmt::Write;
const RESULT_LIMIT: usize = 10;
pub struct SearchArchivedTicketsTool;
#[async_trait]
impl Tool for SearchArchivedTicketsTool {
fn name(&self) -> &'static str {
"search_archived_tickets"
}
fn parameters_schema(&self) -> serde_json::Value {
super::tool_params_schema(
&json!({
"query": {
"type": "string",
"description": "Search query — matched against ticket titles"
}
}),
&["query"],
)
}
fn side_effects(&self) -> bool {
false
}
async fn execute(&self, _ws: &Workspace, args: serde_json::Value) -> Result<String> {
let query = super::get_str(&args, "query")?;
let board = crate::pipeline::board::store();
let top_ids = board.search_archived_by_fts(query, RESULT_LIMIT).await?;
if top_ids.is_empty() {
return Ok("No archived tickets found matching the query.".to_string());
}
Self::format_results(board, &top_ids).await
}
}
impl SearchArchivedTicketsTool {
async fn format_results(
board: &crate::pipeline::board::BoardStore,
top_ids: &[String],
) -> Result<String> {
let tickets = board
.get_tickets_by_ids(top_ids, crate::pipeline::board::LoadComments::No)
.await?;
let mut output = format!(
"Search results (highest score first, {} of at most {RESULT_LIMIT}):\n",
top_ids.len()
);
for id in top_ids {
if let Some(ticket) = tickets.iter().find(|t| t.id == *id) {
let _ = writeln!(
output,
" [{}] [{}] {}: {}",
ticket.workspace_name, ticket.phase, id, ticket.title
);
}
}
Ok(output)
}
}
#[cfg(test)]
mod tests {
use crate::open_test_store;
use crate::pipeline::board::{BoardStore, TicketPhase};
use crate::util::test::make_ticket;
#[tokio::test]
async fn archived_search_ranks_by_score_and_is_not_crowded_out_by_live_tickets() {
let (store, _tmp) = open_test_store!(BoardStore, "board");
let ws = crate::workspace::test_ws("ws");
let mut archived_ids = Vec::new();
for title in ["migration one", "migration"] {
let id = make_ticket(&store, &ws, title, TicketPhase::Done).await;
store.set_archived(&id).await.expect("archive");
archived_ids.push(id);
}
for title in ["migration migration migration", "migration migration"] {
make_ticket(&store, &ws, title, TicketPhase::Backlog).await;
}
let ranked = store
.search_by_fts("migration", 50)
.await
.expect("gui search");
assert!(
!archived_ids.contains(&ranked[0].id),
"a live ticket must outrank every archived hit or the limit cannot bite"
);
let expected: Vec<String> = ranked
.into_iter()
.map(|ticket| ticket.id)
.filter(|id| archived_ids.contains(id))
.collect();
let found = store
.search_archived_by_fts("migration", 2)
.await
.expect("archive search");
assert_eq!(found, expected, "archive hits must keep the FTS order");
assert_ne!(
found, archived_ids,
"results must not come back in insertion (scan) order"
);
}
}