Skip to content

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

refactor(skills): delegate skill search ranking and projection to tinyskills

Steven Enamakel734789c3ea6a
diff --git a/crates/openhuman-core/src/skills/search.rs b/crates/openhuman-core/src/skills/search.rs
index 9c818e6798..47c566a15f 100644
--- a/crates/openhuman-core/src/skills/search.rs
+++ b/crates/openhuman-core/src/skills/search.rs
@@ -24,8 +24,8 @@
//!
//! # The seam
//!
-//! Ranking is [`tinytools::rank::Bm25Index`], which names nothing from this
-//! crate. The host-owned half is everything else in this file: which roots are
+//! Ranking and the capped projection live in `tinyskills::search`. The
+//! host-owned half is everything else in this file: which roots are
//! scanned and whether the workspace is trusted. That is the split to preserve
//! if this ever becomes a loadable module — a module can rank, but it cannot be
//! the thing that decides whose skills a caller may see.
@@ -34,12 +34,11 @@ use std::path::PathBuf;
use std::sync::Arc;
use async_trait::async_trait;
-use serde_json::{json, Value};
+use serde_json::{Value, json};
use super::ops_discover::{discover_workflows, is_workspace_trusted};
use crate::config::Config;
use tinyskills::Skill as Workflow;
-use tinytools::rank::Bm25Index;
use tinytools::{Tool, ToolResult};
/// How many matches a search returns when the caller does not say.
@@ -47,68 +46,12 @@ const DEFAULT_LIMIT: usize = 5;
/// Ceiling on `limit`. A search that can return everything is `list_workflows`
/// with extra steps, and would spend exactly what this tool saves.
const MAX_LIMIT: usize = 20;
-/// Cap on a returned description.
-///
-/// Skill descriptions are third-party metadata. The same 240 the orchestrator
-/// prompt's catalogue uses — see `render_installed_skills` — so a skill cannot
-/// present one size in the catalogue and a different one here.
-const MAX_DESCRIPTION: usize = 240;
-
pub const SKILL_SEARCH_NAME: &str = "skill_search";
-/// The text a skill is matched on.
-///
-/// `dir_name` is included as well as `name` because they diverge — the id the
-/// model must eventually type is `dir_name`, and a skill whose frontmatter
-/// display name differs would otherwise be unfindable by the name it is
-/// actually called by.
-fn searchable_text(workflow: &Workflow) -> String {
- format!(
- "{} {} {} {}",
- workflow.dir_name,
- workflow.name,
- workflow.description,
- workflow.tags.join(" ")
- )
-}
-
-fn id_of(workflow: &Workflow) -> &str {
- workflow.id()
-}
-
-/// Rank `workflows` against `query`, best first, at most `limit` results.
-///
-/// Split out from the tool so the ranking is testable without a workspace, a
-/// config or a filesystem.
-pub fn rank<'a>(workflows: &'a [Workflow], query: &str, limit: usize) -> Vec<&'a Workflow> {
- let texts: Vec<(String, String)> = workflows
- .iter()
- .map(|w| (id_of(w).to_string(), searchable_text(w)))
- .collect();
- let index = Bm25Index::build(texts.iter().map(|(id, text)| (id.as_str(), text.as_str())));
- index
- .search(query, limit)
- .into_iter()
- .map(|i| &workflows[i])
- .collect()
-}
-
-/// The compact per-result projection.
-fn project(workflow: &Workflow) -> Value {
- json!({
- "id": id_of(workflow),
- "name": workflow.name,
- // Sanitised for the same reason the prompt catalogue sanitises: this is
- // author-controlled text about to be read by a model as if the host had
- // said it. `sanitize_for_llm` strips control characters and instruction
- // fences and caps the length.
- "description": tinytools_std::sanitize::sanitize_for_llm(
- &workflow.description,
- MAX_DESCRIPTION,
- ),
- "tags": workflow.tags,
- "scope": workflow.scope,
- })
+/// Compact per-result projection, via the module's capped projection with the
+/// host's `sanitize_for_llm` supplied as the sanitiser.
+fn project(workflow: &Workflow) -> tinyskills::search::SkillHit {
+ tinyskills::search::project(workflow, tinytools_std::sanitize::sanitize_for_llm)
}
/// Search installed skills by capability.
@@ -202,7 +145,7 @@ impl Tool for SkillSearchTool {
.unwrap_or(DEFAULT_LIMIT);
let workflows = self.visible();
- let matches = rank(&workflows, query, limit);
+ let matches = tinyskills::search::rank(&workflows, query, limit);
tracing::debug!(
installed = workflows.len(),
matched = matches.len(),
diff --git a/crates/openhuman-core/src/skills/search_tests.rs b/crates/openhuman-core/src/skills/search_tests.rs
index eba72e9303..e1f8243ceb 100644
--- a/crates/openhuman-core/src/skills/search_tests.rs
+++ b/crates/openhuman-core/src/skills/search_tests.rs
@@ -35,60 +35,6 @@ fn corpus() -> Vec<Workflow> {
]
}
-#[test]
-fn a_capability_query_finds_the_skill_that_serves_it() {
- let all = corpus();
- let hits = rank(&all, "write a changelog from commits", 3);
- assert_eq!(id_of(hits[0]), "changelog");
-}
-
-#[test]
-fn a_skill_is_findable_by_its_directory_id_even_when_the_display_name_differs() {
- // The reason `dir_name` is in the searchable text. `changelog`'s display
- // name is "Release changelog"; the id the model must eventually type is
- // `changelog`, and a query using that id must not miss.
- let all = corpus();
- let hits = rank(&all, "ascii-art", 3);
- assert_eq!(id_of(hits[0]), "ascii-art");
-}
-
-#[test]
-fn tags_are_searchable() {
- let all = corpus();
- let hits = rank(&all, "pyfiglet fun", 3);
- assert_eq!(id_of(hits[0]), "ascii-art");
-}
-
-#[test]
-fn nothing_relevant_returns_nothing() {
- // The property that makes a miss legible. A ranker that always returns
- // `limit` entries would hand the model `ascii-art` for a database query
- // and invite it to run the thing.
- //
- // This query is also the stopword regression: before `Bm25Index::significant`
- // existed it matched `changelog`, on the strength of the word "a".
- let all = corpus();
- assert!(rank(&all, "provision a kubernetes cluster", 3).is_empty());
- assert!(rank(&all, "photosynthesis", 3).is_empty());
-}
-
-#[test]
-fn the_limit_caps_the_result_set() {
- let all = corpus();
- let query = "changelog ascii tinyflows";
- assert_eq!(
- rank(&all, query, 10).len(),
- 3,
- "all three must match unlimited"
- );
- assert_eq!(rank(&all, query, 1).len(), 1);
-}
-
-#[test]
-fn an_empty_corpus_is_safe() {
- assert!(rank(&[], "anything", 5).is_empty());
-}
-
#[test]
fn the_projection_omits_the_body_and_caps_the_description() {
// The whole point of projecting: `list_workflows` serialises frontmatter,
@@ -96,13 +42,13 @@ fn the_projection_omits_the_body_and_caps_the_description() {
// the tool has stopped saving anything.
let long = "x".repeat(1000);
let workflow = skill("s", "S", &long, &["t"]);
- let value = project(&workflow);
+ let value = serde_json::to_value(project(&workflow)).expect("json");
let obj = value.as_object().expect("object");
let mut keys: Vec<&str> = obj.keys().map(String::as_str).collect();
keys.sort();
assert_eq!(keys, ["description", "id", "name", "scope", "tags"]);
assert!(
- obj["description"].as_str().expect("str").len() <= MAX_DESCRIPTION + 8,
+ obj["description"].as_str().expect("str").len() <= tinyskills::search::MAX_DESCRIPTION + 8,
"description must be capped"
);
}
@@ -145,7 +91,7 @@ fn a_hit_reports_how_many_were_searched() {
// `installed` is what lets the model tell "nothing matched" from "you have
// three skills and none is close" without a second call.
let all = corpus();
- let hits = rank(&all, "changelog", 5);
+ let hits = tinyskills::search::rank(&all, "changelog", 5);
let value = render(&hits, all.len());
assert_eq!(value["matched"], 1);
assert_eq!(value["installed"], 3);