Skip to content

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

Rust

fix(coding-agent): rank model search by intent (#539)

elieb09fbdb46c2c
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index 032ea08b84..9859b8d865 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -2,6 +2,7 @@
## [Unreleased]
+- Fixed model searches ranking stronger matches ahead of weaker signed-in matches while preferring signed-in providers for equivalent results ([#539](https://github.com/PrimeIntellect-ai/prime-agent/pull/539) by [@eliebak](https://github.com/eliebak)).
- Fixed large IPython variables repeatedly slowing later turns by excluding them from persistent snapshots and removing them when context is compacted.
- Fixed daemon socket paths being used verbatim in identity derivations: on supported platforms, `--daemon-socket` spellings differing only by duplicate or trailing slashes now normalize to one canonical path, so worker-descriptor namespaces, daemon log files, and persisted descriptors agree.
- Added a `thinking` option to `rlm.run` for spawning subagents with an explicit reasoning level; invalid levels for the resolved child model fail spawn.
diff --git a/packages/coding-agent/src/modes/interactive/components/model-selector.ts b/packages/coding-agent/src/modes/interactive/components/model-selector.ts
index a3ccd7598c..58a08eb319 100644
--- a/packages/coding-agent/src/modes/interactive/components/model-selector.ts
+++ b/packages/coding-agent/src/modes/interactive/components/model-selector.ts
@@ -3,7 +3,7 @@ import {
type Component,
Container,
type Focusable,
- fuzzyFilterScored,
+ fuzzyMatch,
getKeybindings,
Spacer,
Text,
@@ -33,6 +33,77 @@ interface ScopedModelItem {
thinkingLevel?: string;
}
+enum ModelSearchMatchQuality {
+ ExactShortId,
+ ExactFullId,
+ PrefixOrToken,
+ Fuzzy,
+}
+
+interface ModelSearchMatch {
+ quality: ModelSearchMatchQuality;
+ score: number;
+}
+
+function normalizeModelSearchText(value: string): string {
+ return value.toLowerCase().replace(/[\s\-_.:/]+/g, "");
+}
+
+function getModelSearchFields(item: ModelItem): { shortId: string; fullIds: string[]; all: string[] } {
+ const shortId = item.id.slice(item.id.lastIndexOf("/") + 1);
+ const fullIds = [item.id, `${item.provider}/${item.id}`];
+ return {
+ shortId,
+ fullIds,
+ all: [shortId, ...fullIds, item.model.name, item.provider],
+ };
+}
+
+function getBestFuzzyScore(queryTokens: string[], fields: string[]): number | null {
+ let total = 0;
+ for (const token of queryTokens) {
+ let best = Number.POSITIVE_INFINITY;
+ for (const field of fields) {
+ const match = fuzzyMatch(token, field);
+ if (match.matches) best = Math.min(best, match.score);
+ }
+ if (!Number.isFinite(best)) return null;
+ total += best;
+ }
+ return total;
+}
+
+function scoreModelSearch(item: ModelItem, query: string): ModelSearchMatch | null {
+ const queryTokens = query.trim().split(/\s+/);
+ const normalizedQuery = normalizeModelSearchText(query);
+ const normalizedTokens = queryTokens.map(normalizeModelSearchText).filter(Boolean);
+ if (!normalizedQuery || normalizedTokens.length === 0) return null;
+
+ const fields = getModelSearchFields(item);
+ if (normalizeModelSearchText(fields.shortId) === normalizedQuery) {
+ return { quality: ModelSearchMatchQuality.ExactShortId, score: 0 };
+ }
+ if (fields.fullIds.some((field) => normalizeModelSearchText(field) === normalizedQuery)) {
+ return { quality: ModelSearchMatchQuality.ExactFullId, score: 0 };
+ }
+
+ const normalizedFields = fields.all.map(normalizeModelSearchText);
+ const fieldTokens = fields.all
+ .flatMap((field) => field.split(/[\s/_-]+/))
+ .map(normalizeModelSearchText)
+ .filter(Boolean);
+ const fuzzyScore = getBestFuzzyScore(normalizedTokens, normalizedFields);
+ const isPrefixOrToken = normalizedTokens.every(
+ (token) =>
+ normalizedFields.some((field) => field.startsWith(token)) ||
+ fieldTokens.some((field) => field.startsWith(token)),
+ );
+ if (isPrefixOrToken && fuzzyScore !== null) {
+ return { quality: ModelSearchMatchQuality.PrefixOrToken, score: fuzzyScore };
+ }
+ return fuzzyScore === null ? null : { quality: ModelSearchMatchQuality.Fuzzy, score: fuzzyScore };
+}
+
export interface ModelSelectorOptions {
availableModels?: ReadonlyArray<Model<any>>;
configuredProviders?: ReadonlySet<string>;
@@ -321,18 +392,21 @@ export class ModelSelectorComponent extends Container implements Focusable {
const queryChanged = query !== this.searchQuery;
this.searchQuery = query;
if (query.trim()) {
- const scored = fuzzyFilterScored(
- this.activeModels,
- query,
- ({ id, provider }) => `${id} ${provider} ${provider}/${id} ${provider} ${id}`,
- );
- scored.sort(
+ const matches = this.activeModels.flatMap((item) => {
+ const match = scoreModelSearch(item, query);
+ return match ? [{ item, ...match }] : [];
+ });
+ matches.sort(
(a, b) =>
+ a.quality - b.quality ||
a.score - b.score ||
Number(this.isProviderConfigured(b.item)) - Number(this.isProviderConfigured(a.item)) ||
- this.recentRankOf(a.item) - this.recentRankOf(b.item),
+ Number(modelsAreEqual(this.currentModel, b.item.model)) -
+ Number(modelsAreEqual(this.currentModel, a.item.model)) ||
+ this.recentRankOf(a.item) - this.recentRankOf(b.item) ||
+ this.getModelKey(a.item).localeCompare(this.getModelKey(b.item), undefined, { numeric: true }),
);
- this.filteredModels = scored.map((r) => r.item);
+ this.filteredModels = matches.map(({ item }) => item);
} else {
this.filteredModels = this.activeModels;
}
diff --git a/packages/coding-agent/test/model-selector-actions.test.ts b/packages/coding-agent/test/model-selector-actions.test.ts
index 9b7694f968..a8320a4e71 100644
--- a/packages/coding-agent/test/model-selector-actions.test.ts
+++ b/packages/coding-agent/test/model-selector-actions.test.ts
@@ -200,12 +200,90 @@ describe("ModelSelectorComponent", () => {
expect(output).toContain("(2/12)");
});
- it("orders search matches by recency among equally-good fuzzy matches", async () => {
+ it("keeps exact matches ahead of weaker signed-in matches and prefers sign-in for equivalent matches", async () => {
+ const harness = await createHarness({
+ models: [{ id: "base", name: "Base", reasoning: true }],
+ });
+ harnesses.push(harness);
+
+ const base = harness.getModel("base")!;
+ const signedInExact = { ...base, provider: "prime-inference", id: "z-ai/glm-5.2", name: "GLM 5.2" };
+ const signedOutExact = { ...base, provider: "opencode", id: "glm-5.2", name: "GLM 5.2" };
+ const signedInFuzzy = {
+ ...base,
+ provider: "prime-inference",
+ id: "glorious-language-model-5.2",
+ name: "Glorious Language Model 5.2",
+ };
+ const selector = new ModelSelectorComponent(
+ createFakeTui(),
+ undefined,
+ harness.session.modelRegistry,
+ [],
+ () => {},
+ () => {},
+ "glm5.2",
+ {
+ availableModels: [signedInFuzzy, signedOutExact, signedInExact],
+ configuredProviders: new Set(["prime-inference"]),
+ },
+ );
+
+ await waitForAsyncRender();
+
+ const lines = stripAnsi(selector.render(120).join("\n")).split("\n");
+ const signedInExactRow = lines.findIndex((line) => line.includes("z-ai/glm-5.2"));
+ const signedOutExactRow = lines.findIndex((line) => line.includes("opencode"));
+ const signedInFuzzyRow = lines.findIndex((line) => line.includes("glorious-language-model-5.2"));
+ expect(signedInExactRow).toBeGreaterThanOrEqual(0);
+ expect(signedInExactRow).toBeLessThan(signedOutExactRow);
+ expect(signedOutExactRow).toBeLessThan(signedInFuzzyRow);
+ });
+
+ it("orders provider-qualified exact, prefix, and fuzzy matches by quality", async () => {
+ const harness = await createHarness({
+ models: [{ id: "base", name: "Base", reasoning: true }],
+ });
+ harnesses.push(harness);
+
+ const base = harness.getModel("base")!;
+ const exact = { ...base, provider: "openai", id: "gpt-5", name: "GPT-5" };
+ const prefix = { ...base, provider: "prime-inference", id: "openai-gpt-5-preview", name: "GPT-5 Preview" };
+ const fuzzy = { ...base, provider: "prime-inference", id: "other-openai-gpt-5", name: "Other GPT-5" };
+ const selector = new ModelSelectorComponent(
+ createFakeTui(),
+ undefined,
+ harness.session.modelRegistry,
+ [],
+ () => {},
+ () => {},
+ "openai/gpt5",
+ {
+ availableModels: [fuzzy, prefix, exact],
+ configuredProviders: new Set(["prime-inference"]),
+ },
+ );
+
+ await waitForAsyncRender();
+
+ const lines = stripAnsi(selector.render(120).join("\n")).split("\n");
+ const exactRow = lines.findIndex(
+ (line) => line.includes("gpt-5") && !line.includes("preview") && !line.includes("other-"),
+ );
+ const prefixRow = lines.findIndex((line) => line.includes("openai-gpt-5-preview"));
+ const fuzzyRow = lines.findIndex((line) => line.includes("other-openai-gpt-5"));
+ expect(exactRow).toBeGreaterThanOrEqual(0);
+ expect(exactRow).toBeLessThan(prefixRow);
+ expect(prefixRow).toBeLessThan(fuzzyRow);
+ });
+
+ it("uses current model, recency, and alphabetical order for equivalent matches", async () => {
const harness = await createHarness({
models: [
{ id: "glm-5", name: "GLM 5", reasoning: true },
{ id: "glm-5.1", name: "GLM 5.1", reasoning: true },
{ id: "glm-5.2", name: "GLM 5.2", reasoning: true },
+ { id: "glm-6", name: "GLM 6", reasoning: true },
],
});
harnesses.push(harness);
@@ -213,24 +291,26 @@ describe("ModelSelectorComponent", () => {
const provider = harness.getModel("glm-5")!.provider;
const selector = new ModelSelectorComponent(
createFakeTui(),
- undefined,
+ harness.getModel("glm-5.1"),
harness.session.modelRegistry,
[],
() => {},
() => {},
"glm",
- { recentModels: [`${provider}/glm-5.2`, `${provider}/glm-5.1`] },
+ { recentModels: [`${provider}/glm-5.2`] },
);
await waitForAsyncRender();
const lines = stripAnsi(selector.render(120).join("\n")).split("\n");
- const row52 = lines.findIndex((line) => /glm-5\.2/.test(line));
- const row51 = lines.findIndex((line) => /glm-5\.1/.test(line));
- const row5 = lines.findIndex((line) => /glm-5(?![.\d])/.test(line));
- expect(row52).toBeGreaterThanOrEqual(0);
- expect(row52).toBeLessThan(row51);
- expect(row51).toBeLessThan(row5);
+ const currentRow = lines.findIndex((line) => /glm-5\.1/.test(line));
+ const recentRow = lines.findIndex((line) => /glm-5\.2/.test(line));
+ const alphabeticalRow = lines.findIndex((line) => /glm-5(?![.\d])/.test(line));
+ const lastRow = lines.findIndex((line) => /glm-6/.test(line));
+ expect(currentRow).toBeGreaterThanOrEqual(0);
+ expect(currentRow).toBeLessThan(recentRow);
+ expect(recentRow).toBeLessThan(alphabeticalRow);
+ expect(alphabeticalRow).toBeLessThan(lastRow);
});
it("treats a whitespace-only query as no search and keeps the current model first", async () => {