Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

[RSI] Relevance-ranked harness digest + harness.search query API (#2241)

Seth Kartenad4df9364daa
diff --git a/packages/coding-agent/.changes/eng-6096-harness-relevance-ranking.md b/packages/coding-agent/.changes/eng-6096-harness-relevance-ranking.md
new file mode 100644
index 0000000000..23c57ca54d
--- /dev/null
+++ b/packages/coding-agent/.changes/eng-6096-harness-relevance-ranking.md
@@ -0,0 +1 @@
+- Changed the continual harness digest from alphabetical truncation to relevance ranking: entries are selected by weighted term overlap with the active goal and recent messages (recency tiebreak), and a `harness.search(query, kind=None, limit=10)` kernel API returns ranked entries on demand.
diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.ts
index e11e325609..f834bed733 100644
--- a/packages/coding-agent/src/core/agent-session.ts
+++ b/packages/coding-agent/src/core/agent-session.ts
@@ -230,7 +230,9 @@ import {
getGlobalHarnessStateDir,
getLocalHarnessStateDir,
getRefinementHistory,
+ type HarnessQueryTerms,
type HarnessState,
+ harnessQueryTerms,
inferRefinementResultScope,
loadGlobalRefinementHistory,
loadHarnessState,
@@ -8855,9 +8857,49 @@ export class AgentSession {
includeIpythonExamples: hasIpython,
includeShellExamples: tools.includes("bash"),
includeRefineExamples: hasIpython && hasRefineSkill,
+ queryTerms: this._buildHarnessDigestQueryTerms(),
});
}
+ /**
+ * Relevance signal for the harness digest: terms from the active goal
+ * objective (strongest) and the last few user/assistant messages,
+ * newest first. Term overlap is cheap and capped so scoring stays trivial.
+ */
+ private _buildHarnessDigestQueryTerms(): HarnessQueryTerms {
+ const terms = new Map<string, number>();
+ const addText = (text: string | undefined, weight: number) => {
+ if (!text) return;
+ for (const raw of harnessQueryTerms(text)) {
+ if (terms.size >= 48 && !terms.has(raw)) return;
+ if (!terms.has(raw)) terms.set(raw, weight);
+ }
+ };
+ addText(this._goalState.objective, 3);
+ const recent = this.agent.state.messages
+ .filter(
+ (message): message is UserMessage | AssistantMessage =>
+ message.role === "user" || message.role === "assistant",
+ )
+ .slice(-4)
+ .reverse();
+ let recencyWeight = 2;
+ for (const message of recent) {
+ const text =
+ message.role === "assistant"
+ ? readAssistantText(message)
+ : typeof message.content === "string"
+ ? message.content
+ : message.content
+ .filter((block): block is TextContent => block.type === "text")
+ .map((block) => block.text)
+ .join(" ");
+ addText(text, recencyWeight);
+ recencyWeight = Math.max(1, recencyWeight - 0.5);
+ }
+ return terms;
+ }
+
/** Cold-boundary digest delivery: empty contexts defer to the first committed turn (untouched sessions must stay empty); non-empty contexts append only when the newest in-context digest mismatches disk. */
private _ensureHarnessDigestContext(): void {
if (this.agent.state.messages.length === 0) {
diff --git a/packages/coding-agent/src/core/refinement/refinement.ts b/packages/coding-agent/src/core/refinement/refinement.ts
index 5b1cb44205..7060c1606e 100644
--- a/packages/coding-agent/src/core/refinement/refinement.ts
+++ b/packages/coding-agent/src/core/refinement/refinement.ts
@@ -465,6 +465,89 @@ export function formatRefinementNoticeBody(result: RefinementResult): string {
return lines.join("\n");
}
+/**
+ * Query terms for relevance-ranked harness digests: term -> weight.
+ * Built by the caller from task signal (goal objective, recent
+ * messages). The ranking is a pure weighted-term overlap over the
+ * entry's searchable fields.
+ */
+export type HarnessQueryTerms = Map<string, number>;
+
+/** Lowercase a possibly malformed persisted field. */
+function searchableField(value: unknown): string {
+ return typeof value === "string" ? value.toLowerCase() : "";
+}
+
+/** CJK ideographs, kana, and Hangul: scripts that do not mark word
+ * boundaries with spaces. */
+const CJK_TERM_RANGES =
+ "\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uac00-\ud7af" +
+ "\u{20000}-\u{2a6df}\u{2a700}-\u{2b73f}\u{2b740}-\u{2b81f}" +
+ "\u{2b820}-\u{2ceaf}\u{2ceb0}-\u{2ebef}\u{2ebf0}-\u{2ee5f}" +
+ "\u{2f800}-\u{2fa1f}\u{30000}-\u{3134f}\u{31350}-\u{323af}\u{323b0}-\u{3347f}";
+const CJK_TERM_PATTERN = new RegExp(`[${CJK_TERM_RANGES}]`, "u");
+const CJK_TERM_SPLIT = new RegExp(`[${CJK_TERM_RANGES}]+|[^${CJK_TERM_RANGES}]+`, "gu");
+
+/**
+ * Tokenize text into lowercase query terms for harness relevance ranking.
+ * Letters, digits, and combining marks of any script form terms; punctuation only
+ * separate them, so a query like `worktree?` never ranks entries by their
+ * question marks. CJK runs carry no spaces between words, so each run
+ * becomes overlapping bigrams: `修复登录` yields 修复/复登/登录 and still
+ * matches an entry containing 登录故障. Each distinct term is returned once.
+ */
+export function harnessQueryTerms(text: string): string[] {
+ const terms: string[] = [];
+ // \p{M} keeps combining marks inside their run so mark-heavy scripts
+ // spell whole words (Devanagari किताब stays one run).
+ for (const run of text.toLowerCase().match(/[\p{L}\p{N}\p{M}]+/gu) ?? []) {
+ // Runs break only at CJK boundaries: accented Latin stays whole
+ // (naïve) while spacing-free CJK is cut from adjacent words.
+ for (const segment of run.match(CJK_TERM_SPLIT) ?? []) {
+ if (CJK_TERM_PATTERN.test(segment)) {
+ // Code points, not UTF-16 units, keep astral ideographs whole.
+ const chars = Array.from(segment);
+ if (chars.length === 1) terms.push(segment);
+ else for (let i = 0; i < chars.length - 1; i += 1) terms.push(chars[i] + chars[i + 1]);
+ } else if (segment.length >= 4) {
+ // Short runs are noise (the, and, ids) and are dropped.
+ terms.push(segment);
+ }
+ }
+ }
+ return [...new Set(terms)];
+}
+
+/** Score one harness entry against query terms: weighted term overlap. */
+export function scoreHarnessEntryForQuery(entry: HarnessEntry, terms: HarnessQueryTerms): number {
+ if (terms.size === 0) return 0;
+ const title = searchableField(entry.title);
+ const content = searchableField(entry.content);
+ const identifier = `${searchableField(entry.path)} ${searchableField(entry.id)}`;
+ let score = 0;
+ for (const [term, weight] of terms) {
+ // One match per field counts once per term: coverage over distinct
+ // fields matters more than repetition inside a single field. Path
+ // and id form a single identifier slot: the id is often embedded in
+ // the path, so matching both is one signal, not two.
+ let fields = 0;
+ if (title.includes(term)) fields += 1;
+ if (content.includes(term)) fields += 1;
+ if (identifier.includes(term)) fields += 1;
+ if (fields > 0) score += weight * (1 + (fields - 1) * 0.5);
+ }
+ return score;
+}
+
+function compareRankedHarnessEntries(a: HarnessEntry, b: HarnessEntry, terms: HarnessQueryTerms): number {
+ const scoreDifference = scoreHarnessEntryForQuery(b, terms) - scoreHarnessEntryForQuery(a, terms);
+ if (scoreDifference !== 0) return scoreDifference;
+ // Recency breaks ties; alphabetical order keeps selection deterministic.
+ const recencyDifference = (Date.parse(b.updated_at) || 0) - (Date.parse(a.updated_at) || 0);
+ if (recencyDifference !== 0) return recencyDifference;
+ return [a.path, a.title, a.id].join("\0").localeCompare([b.path, b.title, b.id].join("\0"));
+}
+
export function formatHarnessStateForPrompt(
state: HarnessState,
options: {
@@ -474,6 +557,9 @@ export function formatHarnessStateForPrompt(
includeIpythonExamples?: boolean;
includeShellExamples?: boolean;
includeRefineExamples?: boolean;
+ /** Select entries by relevance to these terms instead of
+ * alphabetical order. */
+ queryTerms?: HarnessQueryTerms;
} = {},
): string {
const maxEntriesPerKind = options.maxEntriesPerKind ?? DEFAULT_OVERVIEW_ENTRY_LIMIT;
@@ -501,10 +587,13 @@ export function formatHarnessStateForPrompt(
"",
];
+ const queryTerms = options.queryTerms;
let totalEntries = 0;
for (const kind of Object.keys(state.entries) as RefinementKind[]) {
const entries = Object.values(state.entries[kind]).sort((a, b) =>
- [a.path, a.title, a.id].join("\0").localeCompare([b.path, b.title, b.id].join("\0")),
+ queryTerms !== undefined && queryTerms.size > 0
+ ? compareRankedHarnessEntries(a, b, queryTerms)
+ : [a.path, a.title, a.id].join("\0").localeCompare([b.path, b.title, b.id].join("\0")),
);
totalEntries += entries.length;
// Render subagent specs as a task-shaped roster the model can match against — the
@@ -517,6 +606,9 @@ export function formatHarnessStateForPrompt(
} else {
lines.push(`${kind}: ${entries.length}`);
}
+ if (queryTerms !== undefined && queryTerms.size > 0 && entries.length > maxEntriesPerKind) {
+ lines.push("(entries ranked by relevance to the current task; see harness.search)");
+ }
for (const entry of entries.slice(0, maxEntriesPerKind)) {
const argumentsText =
entry.kind === "skill" && Object.keys(entry.arguments).length > 0
diff --git a/packages/coding-agent/test/refinement.test.ts b/packages/coding-agent/test/refinement.test.ts
index 26495159ed..01c0cdee6f 100644
--- a/packages/coding-agent/test/refinement.test.ts
+++ b/packages/coding-agent/test/refinement.test.ts
@@ -14,7 +14,9 @@ import {
getLocalHarnessStateDir,
getRefinementHistory,
getRefinementHistoryPath,
+ type HarnessEntry,
type HarnessState,
+ harnessQueryTerms,
inferRefinementResultScope,
loadGlobalRefinementHistory,
loadHarnessState,
@@ -27,6 +29,7 @@ import {
type RefinementResult,
refineHarness,
saveHarnessState,
+ scoreHarnessEntryForQuery,
} from "../src/core/refinement/index.js";
import type { CustomEntry } from "../src/core/session-manager.js";
@@ -1514,3 +1517,159 @@ describe("global refinement history", () => {
expect(plan.rollbackScope).toBe("global");
});
});
+
+describe("harness digest relevance ranking", () => {
+ function makeEntry(id: string, title: string, content: string, updatedAt: string): HarnessEntry {
+ return {
+ id,
+ kind: "memory",
+ title,
+ content,
+ path: "memory",
+ scope: "global",
+ reference: {},
+ arguments: {},
+ metadata: {},
+ source: "test",
+ created_at: updatedAt,
+ updated_at: updatedAt,
+ version: 1,
+ };
+ }
+
+ it("scores weighted term overlap with field coverage", () => {
+ const entry = makeEntry(
+ "repo",
+ "Repository facts",
+ "The checkout lives at ~/repo with worktrees.",
+ "2026-09-01T00:00:00.000Z",
+ );
+ const terms = new Map([
+ ["repository", 2],
+ ["worktree", 1],
+ ["absent-term", 5],
+ ]);
+ // "repository" matches the title only -> 2 * 1 = 2.
+ // "worktree" matches content only (1 field) -> 1 * 1 = 1. Total 3.
+ expect(scoreHarnessEntryForQuery(entry, terms)).toBe(3);
+ expect(scoreHarnessEntryForQuery(entry, new Map())).toBe(0);
+ });
+
+ it("selects top-k per kind by relevance instead of alphabetical order", () => {
+ const state = loadHarnessState(join(makeTempDir(), "h"), "local");
+ const irrelevant = makeEntry(
+ "aaa",
+ "Alphabetical first",
+ "Completely unrelated content about tea.",
+ "2026-08-01T00:00:00.000Z",
+ );
+ const relevant = makeEntry(
+ "zzz",
+ "Zebra note",
+ "The worktree workflow for rsi branches matters.",
+ "2026-08-02T00:00:00.000Z",
+ );
+ state.entries.memory.aaa = irrelevant;
+ state.entries.memory.zzz = relevant;
+
+ const ranked = formatHarnessStateForPrompt(state, {
+ maxEntriesPerKind: 1,
+ queryTerms: new Map([["worktree", 1]]),
+ });
+ expect(ranked).toContain("[global:zzz]");
+ expect(ranked).not.toContain("[global:aaa]");
+ expect(ranked).toContain("(entries ranked by relevance");
+
+ // Without query terms, alphabetical selection is unchanged.
+ const alphabetical = formatHarnessStateForPrompt(state, { maxEntriesPerKind: 1 });
+ expect(alphabetical).toContain("[global:aaa]");
+ expect(alphabetical).not.toContain("(entries ranked by relevance");
+ });
+
+ it("tolerates non-string persisted fields", () => {
+ const state = loadHarnessState(join(makeTempDir(), "h3"), "local");
+ const malformed = makeEntry("bad", "Worktree policy", "Worktree guidance.", "2026-08-01T00:00:00.000Z");
+ (malformed as unknown as { title: null }).title = null;
+ state.entries.memory.bad = malformed;
+ expect(() => formatHarnessStateForPrompt(state, { queryTerms: new Map([["worktree", 1]]) })).not.toThrow();
+ });
+
+ it("breaks score ties by recency", () => {
+ const state = loadHarnessState(join(makeTempDir(), "h2"), "local");
+ const older = makeEntry("older", "Worktree policy", "Same worktree signal.", "2026-08-01T00:00:00.000Z");
+ const newer = makeEntry("newer", "Worktree policy 2", "Same worktree signal.", "2026-09-01T00:00:00.000Z");
+ state.entries.memory.older = older;
+ state.entries.memory.newer = newer;
+ const ranked = formatHarnessStateForPrompt(state, {
+ maxEntriesPerKind: 1,
+ queryTerms: new Map([["worktree", 1]]),
+ });
+ const newerIndex = ranked.indexOf("[global:newer]");
+ const olderIndex = ranked.indexOf("[global:older]");
+ expect(newerIndex).toBeGreaterThanOrEqual(0);
+ expect(olderIndex).toBe(-1);
+ });
+
+ it("tokenizes queries without punctuation terms and with CJK bigrams", () => {
+ // Punctuation and symbols only separate terms, in any script.
+ expect(harnessQueryTerms("Worktree?")).toEqual(["worktree"]);
+ expect(harnessQueryTerms("path/to/skill")).toEqual(["path", "skill"]);
+ expect(harnessQueryTerms("harness_search")).toEqual(["harness", "search"]);
+ expect(harnessQueryTerms("??? / . ,")).toEqual([]);
+ // Short ASCII runs stay noise; other non-ASCII scripts stay whole.
+ expect(harnessQueryTerms("Fix the LOGIN bug")).toEqual(["login"]);
+ expect(harnessQueryTerms("Привет мир")).toEqual(["привет"]);
+ // Combining marks stay in their run: mark-heavy scripts spell
+ // whole words (Devanagari किताब).
+ expect(harnessQueryTerms("किताब notes")).toEqual(["किताब", "notes"]);
+ // Accented Latin stays whole; CJK is cut from adjacent words.
+ expect(harnessQueryTerms("naïve approach")).toEqual(["naïve", "approach"]);
+ expect(harnessQueryTerms("修复login")).toEqual(["修复", "login"]);
+ // Supplementary-plane ideographs count as CJK: lone chars become
+ // terms and multi-character runs take bigrams.
+ expect(harnessQueryTerms("𠀀")).toEqual(["𠀀"]);
+ expect(harnessQueryTerms("𠀀𠀁𠀂")).toEqual(["𠀀𠀁", "𠀁𠀂"]);
+ // CJK has no spaces between words: runs become overlapping bigrams,
+ // so partial matches stay findable and single characters count.
+ // Non-ASCII punctuation is a separator, not a term.
+ expect(harnessQueryTerms("修复登录")).toEqual(["修复", "复登", "登录"]);
+ expect(harnessQueryTerms("修复登录?")).toEqual(["修复", "复登", "登录"]);
+ expect(harnessQueryTerms("東京会議 login")).toEqual(["東京", "京会", "会議", "login"]);
+ expect(harnessQueryTerms("登")).toEqual(["登"]);
+ });
+
+ it("ranks whitespace-free CJK matches through bigram terms", () => {
+ const state = loadHarnessState(join(makeTempDir(), "h4"), "local");
+ const login = makeEntry("login", "Login fix", "登录故障排查记录。", "2026-08-01T00:00:00.000Z");
+ const tea = makeEntry("tea", "Tea notes", "All about oolong brewing.", "2026-08-02T00:00:00.000Z");
+ state.entries.memory.login = login;
+ state.entries.memory.tea = tea;
+
+ const ranked = formatHarnessStateForPrompt(state, {
+ maxEntriesPerKind: 1,
+ queryTerms: new Map(harnessQueryTerms("修复登录").map((term) => [term, 1])),
+ });
+ expect(ranked).toContain("[global:login]");
+ expect(ranked).not.toContain("[global:tea]");
+ });
+
+ it("does not rank entries by incidental punctuation in queries", () => {
+ const state = loadHarnessState(join(makeTempDir(), "h5"), "local");
+ const worktree = makeEntry(
+ "worktree",
+ "Branch hygiene",
+ "Use git worktrees for parallel branches.",
+ "2026-08-01T00:00:00.000Z",
+ );
+ const question = makeEntry("question", "Question", "Anything else left open?", "2026-08-02T00:00:00.000Z");
+ state.entries.memory.worktree = worktree;
+ state.entries.memory.question = question;
+
+ const ranked = formatHarnessStateForPrompt(state, {
+ maxEntriesPerKind: 1,
+ queryTerms: new Map(harnessQueryTerms("worktree?").map((term) => [term, 1])),
+ });
+ expect(ranked).toContain("[global:worktree]");
+ expect(ranked).not.toContain("[global:question]");
+ });
+});
diff --git a/prime-agent-runtime/src/rlm/harness.py b/prime-agent-runtime/src/rlm/harness.py
index 71452e5908..cba1c7ed0c 100644
--- a/prime-agent-runtime/src/rlm/harness.py
+++ b/prime-agent-runtime/src/rlm/harness.py
@@ -11,7 +11,9 @@ from __future__ import annotations
import json
import os
+import re
import stat
+import unicodedata
from dataclasses import asdict, dataclass, field, fields
from datetime import datetime, timezone
from pathlib import Path
@@ -37,6 +39,76 @@ def _slug(raw: str, fallback: str) -> str:
return (normalized or fallback)[:80]
+_CJK_TERM_CHARS = re.compile(
+ r"[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uac00-\ud7af"
+ r"\U00020000-\U0002a6df\U0002a700-\U0002b73f\U0002b740-\U0002b81f"
+ r"\U0002b820-\U0002ceaf\U0002ceb0-\U0002ebef\U0002ebf0-\U0002ee5f"
+ r"\U0002f800-\U0002fa1f\U00030000-\U0003134f\U00031350-\U000323af"
+ r"\U000323b0-\U0003347f]"
+)
+
+
+def _harness_query_runs(text: str) -> list[str]:
+ """Split lowercase text into word runs.
+
+ Letters, digits, and combining marks of any script share a run;
+ punctuation and symbols end it. Runs break only at CJK boundaries:
+ accented Latin stays whole (naïve) while spacing-free CJK is cut
+ apart from adjacent words it would otherwise swallow (修复login).
+ """
+ runs: list[str] = []
+ run: list[str] = []
+ run_is_cjk = False
+ for ch in text:
+ if unicodedata.category(ch).startswith("M") or ch.isalnum():
+ ch_is_cjk = bool(_CJK_TERM_CHARS.match(ch))
+ if run and ch_is_cjk != run_is_cjk:
+ runs.append("".join(run))
+ run = []
+ run_is_cjk = ch_is_cjk
+ run.append(ch)
+ elif run:
+ runs.append("".join(run))
+ run = []
+ if run:
+ runs.append("".join(run))
+ return runs
+
+
+def _harness_query_terms(query: str) -> list[str]:
+ """Tokenize a search query into lowercase substring terms.
+
+ Letters and digits of every script form terms; punctuation and symbols
+ only separate them, so ``worktree?`` never ranks entries by question
+ marks. CJK runs carry no spaces between words, so each run becomes
+ overlapping bigrams: ``修复登录`` yields ``修复``/``复登``/``登录`` and
+ still matches an entry containing ``登录故障``. Each term counts once.
+ Minimum lengths stay below the digest builder's four-character cut
+ because ``search`` tokenizes explicit queries, not mined conversation:
+ three ASCII characters keep real terms (rlm, api, cli), two characters
+ keep short words of other scripts (мир), and single characters are
+ terms only for CJK, where one character is a word.
+ """
+ terms: list[str] = []
+ seen: set[str] = set()
+ for run in _harness_query_runs(query.lower()):
+ if _CJK_TERM_CHARS.search(run):
+ # Bigrams keep whitespace-free CJK findable without single
+ # characters matching too loosely.
+ candidates = [run[i : i + 2] for i in range(len(run) - 1)] or [run]
+ elif run.isascii():
+ candidates = [run] if len(run) >= 3 else []
+ else:
+ # Other scripts space out words: lone characters match too
+ # broadly, so two characters is the floor.
+ candidates = [run] if len(run) >= 2 else []
+ for term in candidates:
+ if term not in seen:
+ seen.add(term)
+ terms.append(term)
+ return terms
+
+
def _agent_dir() -> Path:
raw = (
os.environ.get("PRIME_AGENT_CODING_AGENT_DIR")
@@ -786,6 +858,53 @@ class HarnessState:
lines.append("refinements: 0")
return "\n".join(lines)
+ def search(
+ self,
+ query: str,
+ kind: HarnessKind | None = None,
+ limit: int = 10,
+ *,
+ global_: bool = False,
+ **kwargs: Any,
+ ) -> list[HarnessEntry]:
+ """Return harness entries ranked by weighted term overlap with *query*.
+
+ Terms are scored against an entry's title, content, path, and id;
+ matches in more distinct fields count more.
+ """
+ if target := self._global_target(global_, kwargs):
+ return target.search(query, kind=kind, limit=limit)
+ self._sync_from_disk()
+ if not isinstance(query, str):
+ raise TypeError(f"query must be str, got {type(query).__name__}")
+ if not isinstance(limit, int) or isinstance(limit, bool) or limit < 1:
+ raise TypeError("limit must be a positive int")
+ terms = _harness_query_terms(query)
+ if not terms:
+ return []
+
+ def score(entry: HarnessEntry) -> float:
+ title = entry.title.lower()
+ content = entry.content.lower()
+ path_and_id = f"{entry.path} {entry.id}".lower()
+ total = 0.0
+ for term in terms:
+ fields = (1 if term in title else 0) + (1 if term in content else 0) + (
+ 1 if term in path_and_id else 0
+ )
+ if fields:
+ total += 1 + (fields - 1) * 0.5
+ return total
+
+ entries = self.list(kind, **kwargs) if kind is not None else self.list(None, **kwargs)
+
+ def recency(entry: HarnessEntry) -> str:
+ return entry.updated_at if isinstance(entry.updated_at, str) else ""
+
+ ranked = sorted(entries, key=lambda e: (score(e), recency(e)), reverse=True)
+ ranked = [e for e in ranked if score(e) > 0]
+ return ranked[:limit]
+
def snapshot(self, *, global_: bool = False, **kwargs: Any) -> dict[str, Any]:
if target := self._global_target(global_, kwargs):
return target.snapshot()
diff --git a/prime-agent-runtime/test/test_harness.py b/prime-agent-runtime/test/test_harness.py
index 49cc7c06f1..6e2d6e70ec 100644
--- a/prime-agent-runtime/test/test_harness.py
+++ b/prime-agent-runtime/test/test_harness.py
@@ -1030,3 +1030,108 @@ class HarnessStateTest(unittest.TestCase):
if __name__ == "__main__":
unittest.main()
+
+
+class HarnessSearchTest(unittest.TestCase):
+ def test_search_ranks_relevant_entries_first(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Tea notes", "All about oolong brewing.", id="tea")
+ state.create_memory("Worktree policy", "Use git worktrees for parallel branches.", id="worktree")
+ state.create_prompt_note("RSI program", "Ship [RSI] PRs from worktrees.", id="rsi")
+
+ results = state.search("worktree branches")
+
+ self.assertTrue(results)
+ self.assertEqual(results[0].id, "worktree")
+ self.assertTrue(all(entry.id != "tea" for entry in results))
+
+ def test_search_filters_by_kind_and_limit(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Worktree memory", "worktree workflow", id="m1")
+ state.create_prompt_note("Worktree prompt", "worktree workflow", id="p1")
+ state.create_prompt_note("Worktree prompt 2", "worktree workflow", id="p2")
+
+ prompts = state.search("worktree", kind="prompt")
+ self.assertTrue(prompts)
+ self.assertEqual({entry.kind for entry in prompts}, {"prompt"})
+
+ limited = state.search("worktree", kind="prompt", limit=1)
+ self.assertEqual(len(limited), 1)
+
+ def test_search_matches_non_ascii_queries(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Tokyo note", "東京ミーティングの議事録。", id="tokyo")
+
+ results = state.search("東京")
+ self.assertEqual([entry.id for entry in results], ["tokyo"])
+
+ def test_search_segments_whitespace_free_cjk(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Login fix", "登录故障排查记录。", id="login")
+ state.create_memory("Tea notes", "All about oolong brewing.", id="tea")
+
+ results = state.search("修复登录")
+
+ self.assertEqual([entry.id for entry in results], ["login"])
+
+ def test_search_matches_supplementary_cjk(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Ext B note", "𠀀𠀁 ideographs recorded.", id="extb")
+
+ self.assertEqual([entry.id for entry in state.search("𠀀")], ["extb"])
+ self.assertEqual([entry.id for entry in state.search("𠀀𠀁")], ["extb"])
+
+ def test_search_keeps_accented_latin_words_whole(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Review notes", "The naïve approach failed.", id="naive")
+
+ results = state.search("naïve")
+
+ self.assertEqual([entry.id for entry in results], ["naive"])
+
+ def test_search_matches_combining_mark_scripts(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Book note", "किताब पढ़ रहा हूँ।", id="book")
+
+ results = state.search("किताब")
+
+ self.assertEqual([entry.id for entry in results], ["book"])
+
+ def test_search_drops_single_character_non_cjk_terms(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Russian note", "мир и согласие в команде.", id="mir")
+
+ self.assertEqual(state.search("и"), [])
+ self.assertEqual([entry.id for entry in state.search("мир")], ["mir"])
+
+ def test_search_treats_punctuation_as_separators(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Branch hygiene", "Use git worktrees for parallel branches.", id="worktree")
+ state.create_memory("Question", "Anything else left open?", id="question")
+
+ results = state.search("worktree?")
+ self.assertEqual([entry.id for entry in results], ["worktree"])
+
+ self.assertEqual(state.search("??? / . ,"), [])
+
+ def test_search_drops_zero_score_entries_and_validates_args(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state = HarnessState(Path(temp_dir) / "harness_state.json")
+ state.create_memory("Tea notes", "All about oolong brewing.", id="tea")
+
+ self.assertEqual(state.search("quantum"), [])
+ self.assertEqual(state.search(" "), [])
+ with self.assertRaises(TypeError):
+ state.search(42)
+ with self.assertRaises(TypeError):
+ state.search("worktree", limit=0)
+