use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OutcomeClass {
Found,
NotFound,
Ambiguous,
InvalidRequest,
EmptyResult,
InternalFailure,
}
impl OutcomeClass {
pub const ALL: [Self; 6] = [
Self::Found,
Self::NotFound,
Self::Ambiguous,
Self::InvalidRequest,
Self::EmptyResult,
Self::InternalFailure,
];
pub const fn as_str(self) -> &'static str {
match self {
Self::Found => "found",
Self::NotFound => "not_found",
Self::Ambiguous => "ambiguous",
Self::InvalidRequest => "invalid_request",
Self::EmptyResult => "empty_result",
Self::InternalFailure => "internal_failure",
}
}
pub const fn is_error(self) -> bool {
matches!(self, Self::InvalidRequest | Self::InternalFailure)
}
}
pub(crate) fn is_index_unavailable_output(text: &str) -> bool {
text.starts_with("Index not loaded.")
|| text.starts_with("Index is loading")
|| text.starts_with("Index degraded:")
|| text.starts_with("Index refresh interrupted:")
}
pub(crate) fn is_error_output(text: &str) -> bool {
text.starts_with("Error:")
|| text.starts_with("Error in ")
|| is_admission_refusal(text)
|| is_foreign_project_refusal(text)
|| is_local_cross_project_refusal(text)
}
pub(crate) fn is_admission_refusal(text: &str) -> bool {
text.starts_with("Content withheld by admission policy:")
}
pub(crate) fn is_foreign_project_refusal(text: &str) -> bool {
(text.starts_with("Error: project_routing: project '") || text.starts_with("project '"))
&& text.contains("is not available on this connection")
}
pub(crate) fn is_local_cross_project_refusal(text: &str) -> bool {
text.starts_with("Error: project_routing: cross-project queries")
|| text.starts_with("Cross-project queries (project/projects) require the daemon")
}
pub(crate) fn classify_get_symbol_output(text: &str) -> OutcomeClass {
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text) {
OutcomeClass::InvalidRequest
} else if text.starts_with("Ambiguous:") {
OutcomeClass::Ambiguous
} else if text.starts_with("File not found:") || text.starts_with("No symbol ") {
OutcomeClass::NotFound
} else {
OutcomeClass::Found
}
}
pub(crate) fn classify_get_symbol_context_output(text: &str) -> OutcomeClass {
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text) {
OutcomeClass::InvalidRequest
} else if text.starts_with("Ambiguous symbol selector") || text.starts_with("Ambiguous:") {
OutcomeClass::Ambiguous
} else if text.starts_with("Symbol \"") || text.starts_with("Symbol '") {
OutcomeClass::NotFound
} else {
OutcomeClass::Found
}
}
pub(crate) fn strip_mode_annotation(text: &str) -> &str {
text.strip_prefix("── mode: ")
.and_then(|rest| rest.split_once(" ──\n"))
.map(|(_, body)| body)
.unwrap_or(text)
}
pub(crate) fn classify_get_file_content_output(text: &str) -> OutcomeClass {
let body = strip_mode_annotation(text);
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text)
|| text.starts_with("Invalid get_file_content request:")
|| text.starts_with("mode=")
|| text.contains("[error:")
|| body.starts_with("Path is outside the repository root:")
|| (body.starts_with("Chunk ") && body.contains(" out of range for "))
{
OutcomeClass::InvalidRequest
} else if text.starts_with("Ambiguous symbol selector") {
OutcomeClass::Ambiguous
} else if text.starts_with("File not found:")
|| text.starts_with("No symbol ")
|| text.starts_with("Symbol not found in ")
|| body.starts_with("No matches for '")
|| body.starts_with("Match occurrence ")
{
OutcomeClass::NotFound
} else {
OutcomeClass::Found
}
}
pub(crate) fn classify_search_symbols_output(text: &str) -> OutcomeClass {
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text) || text.starts_with("search_symbols requires") {
OutcomeClass::InvalidRequest
} else if text.starts_with("No symbols matching") {
OutcomeClass::EmptyResult
} else {
OutcomeClass::Found
}
}
pub(crate) fn classify_search_text_output(text: &str) -> OutcomeClass {
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text)
|| text.starts_with("Regex search requires")
|| text.starts_with("Search requires")
|| text.starts_with("Invalid regex")
|| text.starts_with("Invalid glob")
|| text.starts_with("whole_word is not supported")
{
OutcomeClass::InvalidRequest
} else if text.starts_with("No matches") || text.starts_with("No AST matches") {
OutcomeClass::EmptyResult
} else {
OutcomeClass::Found
}
}
pub(crate) fn classify_search_knowledge_output(text: &str) -> OutcomeClass {
if text.starts_with("Error:") {
OutcomeClass::InvalidRequest
} else if text.contains("\nNo match:") {
OutcomeClass::EmptyResult
} else if text.starts_with("Readiness:") {
OutcomeClass::InternalFailure
} else {
OutcomeClass::Found
}
}
pub(crate) fn classify_search_files_output(text: &str) -> OutcomeClass {
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text)
|| text.starts_with("Path search requires")
|| text.starts_with("Path hint must not be empty")
|| text.starts_with("search_files")
{
OutcomeClass::InvalidRequest
} else if text.contains("Ambiguous path hint") {
OutcomeClass::Ambiguous
} else if text.starts_with("No indexed source files matching")
|| text.starts_with("No indexed source path matched")
|| text.starts_with("No git history found")
|| text.starts_with("'") && text.contains("not found in git history")
{
OutcomeClass::NotFound
} else if text.starts_with("No high-confidence co-change data") {
OutcomeClass::EmptyResult
} else {
OutcomeClass::Found
}
}
pub(crate) fn classify_find_references_output(text: &str) -> OutcomeClass {
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text) {
OutcomeClass::InvalidRequest
} else if text.starts_with("Ambiguous symbol selector") {
OutcomeClass::Ambiguous
} else if text.starts_with("File not found:")
|| text.starts_with("Symbol not found")
|| text.contains("this symbol is not defined in the indexed project")
{
OutcomeClass::NotFound
} else if text.starts_with("No references found")
|| text.starts_with("No implementations found")
{
OutcomeClass::EmptyResult
} else {
OutcomeClass::Found
}
}
pub(crate) fn classify_get_file_context_output(text: &str) -> OutcomeClass {
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text) || text.starts_with("Invalid get_file_context") {
OutcomeClass::InvalidRequest
} else if text.starts_with("File not found:")
|| text.starts_with("File not found on disk:")
|| text.starts_with("No symbol ")
{
OutcomeClass::NotFound
} else if text.starts_with("Ambiguous") {
OutcomeClass::Ambiguous
} else {
OutcomeClass::Found
}
}
pub(crate) fn classify_compact_tool_output(tool: &str, text: &str) -> OutcomeClass {
match tool {
"get_symbol" => classify_get_symbol_output(text),
"get_symbol_context" => classify_get_symbol_context_output(text),
"get_file_content" => classify_get_file_content_output(text),
"get_file_context" => classify_get_file_context_output(text),
"search_symbols" => classify_search_symbols_output(text),
"search_text" => classify_search_text_output(text),
"search_knowledge" => classify_search_knowledge_output(text),
"search_files" => classify_search_files_output(text),
"find_references" => classify_find_references_output(text),
_ => {
if is_index_unavailable_output(text) {
OutcomeClass::InternalFailure
} else if is_error_output(text) || text.starts_with("Invalid") {
OutcomeClass::InvalidRequest
} else {
OutcomeClass::Found
}
}
}
}
pub(crate) fn compact_tool_output_is_success(tool: &str, text: &str) -> bool {
classify_compact_tool_output(tool, text) == OutcomeClass::Found
}