feat(app): search history by workspace, agent, and branch (#2995)
History was a date-grouped list you scrolled. Finding a session from last week meant eyeballing every row. This adds a search field to the existing filter rail, next to the host picker. Search runs on the daemon, not the client. The daemon already holds every persisted agent in memory, so it ranks the whole history and the client never has to warn that it only looked at the pages it had loaded. A searched response is one ranked page with no cursor. The global top K is always contained in the union of each host's own top K, so merging per-host pages and slicing back to the limit is the exact global answer — no k-way merge, and no offset into a ranking that is recomputed per request, which would duplicate and skip rows as history changes underneath it. Past result 200 of a relevance order the answer is a narrower query, so the footer says that instead of offering a "Load more" that cannot deliver. Matching is the app's existing picker matcher, moved to the protocol package so app and daemon share one implementation instead of drifting into two "why didn't this show up?" rule sets. Typo tolerance is opt-in, so the comboboxes keep exact narrowing; the budget is per token, and four-character tokens get transpositions only, because at that length a free substitution turns "main" into "mail", "maid" and "rain". Ranked rows carry the spans they matched on, so results can mark them. The subsequence and typo tiers match characters the eye would never find unaided, and an unexplained row reads as a bug. The ranker computes the spans anyway; sending them keeps the client from deriving a second opinion that could disagree with the ranking it is explaining. Hosts that fail or are offline are now named above the list. Without that, "No sessions match" is a claim the app has no basis for. Gated on server_info.features.agentHistorySearch, checked once against the connected hosts. Reachability is reported by the banner instead, so one offline host in the registry no longer hides the field.
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import { compareMatchScores, type MatchScore, scoreTextFields } from "@/utils/score-match";
|
||||
import {
|
||||
compareMatchScores,
|
||||
type MatchScore,
|
||||
scoreTextFields,
|
||||
} from "@getpaseo/protocol/search/text-match";
|
||||
|
||||
interface CommandAutocompleteEntry {
|
||||
command: {
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { compareMatchScores, scoreMatch } from "./score-match";
|
||||
|
||||
describe("scoreMatch", () => {
|
||||
it("returns tier 0 for empty query", () => {
|
||||
expect(scoreMatch("", "anything")).toEqual({ tier: 0, offset: 0 });
|
||||
});
|
||||
|
||||
it("returns tier 0 for exact match ignoring case", () => {
|
||||
expect(scoreMatch("pi", "pi")).toEqual({ tier: 0, offset: 0 });
|
||||
expect(scoreMatch("PI", "pi")).toEqual({ tier: 0, offset: 0 });
|
||||
expect(scoreMatch("Pi", "pI")).toEqual({ tier: 0, offset: 0 });
|
||||
});
|
||||
|
||||
it("returns tier 1 for whole-word match at start", () => {
|
||||
expect(scoreMatch("feat", "feat/pi-direct-sdk")).toEqual({ tier: 1, offset: 0 });
|
||||
});
|
||||
|
||||
it("returns tier 1 for whole-word match in middle (word boundaries on both sides)", () => {
|
||||
expect(scoreMatch("pi", "feat/pi-direct-sdk")).toEqual({ tier: 1, offset: 5 });
|
||||
expect(scoreMatch("pi", "a b pi c d")).toEqual({ tier: 1, offset: 4 });
|
||||
});
|
||||
|
||||
it("returns tier 2 for string prefix that does not complete a word", () => {
|
||||
expect(scoreMatch("par", "party")).toEqual({ tier: 2, offset: 0 });
|
||||
});
|
||||
|
||||
it("returns tier 3 for word-boundary start that does not complete a word", () => {
|
||||
expect(scoreMatch("par", "a/party")).toEqual({ tier: 3, offset: 2 });
|
||||
});
|
||||
|
||||
it("returns tier 4 for substring inside a word", () => {
|
||||
expect(scoreMatch("art", "party")).toEqual({ tier: 4, offset: 1 });
|
||||
});
|
||||
|
||||
it("returns null when query is not found", () => {
|
||||
expect(scoreMatch("xyz", "feat/pi-direct-sdk")).toBeNull();
|
||||
});
|
||||
|
||||
it("picks the best tier across multiple occurrences", () => {
|
||||
expect(scoreMatch("ab", "xabxab-yy")).toEqual({ tier: 4, offset: 1 });
|
||||
expect(scoreMatch("ab", "xxab x-ab-y")).toEqual({ tier: 1, offset: 7 });
|
||||
});
|
||||
|
||||
it("prefers earlier offset when tiers are equal", () => {
|
||||
expect(scoreMatch("pi", "pi-a pi-b")).toEqual({ tier: 1, offset: 0 });
|
||||
});
|
||||
|
||||
it("treats common separators as word boundaries", () => {
|
||||
expect(scoreMatch("pi", "x/pi")).toEqual({ tier: 1, offset: 2 });
|
||||
expect(scoreMatch("pi", "x-pi")).toEqual({ tier: 1, offset: 2 });
|
||||
expect(scoreMatch("pi", "x_pi")).toEqual({ tier: 1, offset: 2 });
|
||||
expect(scoreMatch("pi", "x pi")).toEqual({ tier: 1, offset: 2 });
|
||||
expect(scoreMatch("pi", "x.pi")).toEqual({ tier: 1, offset: 2 });
|
||||
expect(scoreMatch("pi", "x:pi")).toEqual({ tier: 1, offset: 2 });
|
||||
expect(scoreMatch("202", "#202 feat")).toEqual({ tier: 1, offset: 1 });
|
||||
});
|
||||
|
||||
it("scores PR-title-shaped text realistically", () => {
|
||||
const pr202 = "#202 feat(server): replace Pi ACP with direct SDK provider";
|
||||
expect(scoreMatch("pi", pr202)?.tier).toBe(1);
|
||||
expect(scoreMatch("202", pr202)).toEqual({ tier: 1, offset: 1 });
|
||||
expect(scoreMatch("replace", pr202)?.tier).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compareMatchScores", () => {
|
||||
it("sorts by tier ascending", () => {
|
||||
expect(compareMatchScores({ tier: 1, offset: 10 }, { tier: 2, offset: 0 })).toBeLessThan(0);
|
||||
expect(compareMatchScores({ tier: 3, offset: 0 }, { tier: 1, offset: 99 })).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("tie-breaks by offset ascending at the same tier", () => {
|
||||
expect(compareMatchScores({ tier: 1, offset: 0 }, { tier: 1, offset: 5 })).toBeLessThan(0);
|
||||
expect(compareMatchScores({ tier: 1, offset: 5 }, { tier: 1, offset: 5 })).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -1,93 +0,0 @@
|
||||
export interface MatchScore {
|
||||
tier: number;
|
||||
offset: number;
|
||||
spread?: number;
|
||||
}
|
||||
|
||||
function isWordBoundaryChar(ch: string | undefined): boolean {
|
||||
if (ch === undefined) return true;
|
||||
return !/[a-z0-9]/.test(ch);
|
||||
}
|
||||
|
||||
function scoreSubstringMatch(query: string, text: string): MatchScore | null {
|
||||
let best: MatchScore | null = null;
|
||||
let pos = 0;
|
||||
while (pos <= text.length - query.length) {
|
||||
const found = text.indexOf(query, pos);
|
||||
if (found === -1) break;
|
||||
const before = found > 0 ? text[found - 1] : undefined;
|
||||
const after = text[found + query.length];
|
||||
const startsAtBoundary = found === 0 || isWordBoundaryChar(before);
|
||||
const endsAtBoundary = after === undefined || isWordBoundaryChar(after);
|
||||
let tier: number;
|
||||
if (startsAtBoundary && endsAtBoundary) {
|
||||
tier = 1;
|
||||
} else if (found === 0) {
|
||||
tier = 2;
|
||||
} else if (startsAtBoundary) {
|
||||
tier = 3;
|
||||
} else {
|
||||
tier = 4;
|
||||
}
|
||||
if (!best || tier < best.tier || (tier === best.tier && found < best.offset)) {
|
||||
best = { tier, offset: found };
|
||||
}
|
||||
pos = found + 1;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
function scoreSubsequenceMatch(query: string, text: string): MatchScore | null {
|
||||
let queryIndex = 0;
|
||||
let firstIndex = -1;
|
||||
let lastIndex = -1;
|
||||
for (let textIndex = 0; textIndex < text.length && queryIndex < query.length; textIndex += 1) {
|
||||
if (text[textIndex] !== query[queryIndex]) continue;
|
||||
if (firstIndex === -1) firstIndex = textIndex;
|
||||
lastIndex = textIndex;
|
||||
queryIndex += 1;
|
||||
}
|
||||
|
||||
if (queryIndex !== query.length || firstIndex === -1) return null;
|
||||
return { tier: 5, offset: firstIndex, spread: lastIndex - firstIndex + 1 };
|
||||
}
|
||||
|
||||
export function scoreMatch(query: string, text: string): MatchScore | null {
|
||||
if (!query) return { tier: 0, offset: 0 };
|
||||
const q = query.toLowerCase();
|
||||
const t = text.toLowerCase();
|
||||
if (t === q) return { tier: 0, offset: 0 };
|
||||
|
||||
return scoreSubstringMatch(q, t) ?? scoreSubsequenceMatch(q, t);
|
||||
}
|
||||
|
||||
export function compareMatchScores(a: MatchScore, b: MatchScore): number {
|
||||
if (a.tier !== b.tier) return a.tier - b.tier;
|
||||
if (a.offset !== b.offset) return a.offset - b.offset;
|
||||
return (a.spread ?? 0) - (b.spread ?? 0);
|
||||
}
|
||||
|
||||
export function scoreTextFields(query: string, fields: string[]): MatchScore | null {
|
||||
const tokens = query
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(/\s+/)
|
||||
.filter((token) => token.length > 0);
|
||||
if (tokens.length === 0) return { tier: 0, offset: 0, spread: 0 };
|
||||
|
||||
const aggregate: MatchScore = { tier: 0, offset: 0, spread: 0 };
|
||||
for (const token of tokens) {
|
||||
let best: MatchScore | null = null;
|
||||
for (const field of fields) {
|
||||
const score = scoreMatch(token, field);
|
||||
if (score && (!best || compareMatchScores(score, best) < 0)) {
|
||||
best = score;
|
||||
}
|
||||
}
|
||||
if (!best) return null;
|
||||
aggregate.tier += best.tier;
|
||||
aggregate.offset += best.offset;
|
||||
aggregate.spread = (aggregate.spread ?? 0) + (best.spread ?? token.length);
|
||||
}
|
||||
return aggregate;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { scoreMatch } from "./score-match";
|
||||
import { scoreMatch } from "@getpaseo/protocol/search/text-match";
|
||||
|
||||
export interface BuildWorkingDirectorySuggestionsInput {
|
||||
recommendedPaths: string[];
|
||||
|
||||
Reference in New Issue
Block a user