A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
refactor(coding-agent): remove test-only parser and feature-hint seams (#1860)
Sebastian Müller056014620853
diff --git a/packages/coding-agent/.changes/test-only-parser-hints.md b/packages/coding-agent/.changes/test-only-parser-hints.mdnew file mode 100644index 0000000000..65a6e65a82--- /dev/null+++ b/packages/coding-agent/.changes/test-only-parser-hints.md@@ -0,0 +1 @@+- Simplified model resolution and feature hint shuffling internals.diff --git a/packages/coding-agent/src/core/model-resolver.ts b/packages/coding-agent/src/core/model-resolver.tsindex 00187f0708..950020e703 100644--- a/packages/coding-agent/src/core/model-resolver.ts+++ b/packages/coding-agent/src/core/model-resolver.ts@@ -147,7 +147,7 @@ function tryMatchModel(modelPattern: string, availableModels: Model<Api>[]): Mod}}-export interface ParsedModelResult {+interface ParsedModelResult {model: Model<Api> | undefined;/** Thinking level if explicitly specified in pattern, undefined otherwise */thinkingLevel?: ThinkingLevel;@@ -199,10 +199,8 @@ function findPreferredDefaultModel(availableModels: Model<Api>[]): Model<Api> |* 3. If not found and has colons, split on last colon:* - If suffix is valid thinking level, use it and recurse on prefix* - If suffix is invalid, warn and recurse on prefix with "off"- *- * @internal Exported for testing*/-export function parseModelPattern(+function parseModelPattern(pattern: string,availableModels: Model<Api>[],options?: { allowInvalidThinkingLevelFallback?: boolean },diff --git a/packages/coding-agent/src/modes/interactive/feature-hints.ts b/packages/coding-agent/src/modes/interactive/feature-hints.tsindex c4ffbad4b3..8b3d8134ef 100644--- a/packages/coding-agent/src/modes/interactive/feature-hints.ts+++ b/packages/coding-agent/src/modes/interactive/feature-hints.ts@@ -109,8 +109,6 @@ export class FeatureHintDeck {private remaining: FeatureHint[] = [];private previousId: string | undefined;- constructor(private readonly random: () => number = Math.random) {}-next(context: FeatureHintContext): FeatureHint | undefined {if (this.remaining.length === 0) {this.refill(context);@@ -128,8 +126,7 @@ export class FeatureHintDeck {return text ? [{ id: hint.id, text }] : [];});for (let index = hints.length - 1; index > 0; index--) {- const random = Math.min(0.999999999, Math.max(0, this.random()));- const target = Math.floor(random * (index + 1));+ const target = Math.floor(Math.random() * (index + 1));[hints[index], hints[target]] = [hints[target]!, hints[index]!];}if (hints.length > 1 && hints[hints.length - 1]?.id === this.previousId) {diff --git a/packages/coding-agent/test/interactive-mode-feature-hints.test.ts b/packages/coding-agent/test/interactive-mode-feature-hints.test.tsindex 3fa36d9fc3..8dd248641f 100644--- a/packages/coding-agent/test/interactive-mode-feature-hints.test.ts+++ b/packages/coding-agent/test/interactive-mode-feature-hints.test.ts@@ -54,7 +54,7 @@ function createMode() {describe("feature hint deck", () => {it("shows every available hint before repeating and avoids a boundary repeat", () => {- const deck = new FeatureHintDeck(() => 0);+ const deck = new FeatureHintDeck();const context = { getKeybinding: (action: string) => `Custom ${action}`, isResidentSession: true };const firstCycle = FEATURE_HINTS.map(() => deck.next(context));@@ -64,7 +64,6 @@ describe("feature hint deck", () => {});it("uses configured shortcuts in keybinding-based hints", () => {- const deck = new FeatureHintDeck(() => 0);const context = {getKeybinding: (action: string) => {if (action === "app.prompt.stash") return "Meta+S";@@ -73,39 +72,11 @@ describe("feature hint deck", () => {},isResidentSession: true,};- const hints = FEATURE_HINTS.map(() => deck.next(context));-- expect(hints.find((hint) => hint?.id === "prompt-stash")?.text).toContain("Meta+S");- expect(hints.find((hint) => hint?.id === "follow-up")?.text).toContain("Meta+Enter");- expect(hints.find((hint) => hint?.id === "agents-view")?.text).toContain("Meta+Left");- });-- it("covers Prime Agent workflows with capability-focused copy", () => {- const deck = new FeatureHintDeck(() => 0);- const hints = FEATURE_HINTS.map(() => deck.next({ getKeybinding: () => "Meta+A", isResidentSession: true }));- const textById = new Map(hints.map((hint) => [hint?.id, hint?.text]));-- expect(textById.get("subagents")).toBe("Prime Agent can delegate tasks to subagents and run them in parallel.");- expect(textById.get("agents-view")).toContain("Session View");- expect(textById.get("session-rewind")).toContain("/tree");- expect(textById.get("steering")).toContain("steer");- expect(textById.get("agent-messaging")).toContain("message each other");- expect(textById.get("goal")).toContain("/goal");- expect(textById.get("refine")).toContain("/refine");- expect(textById.get("persistent-ipython")).toContain("kernel variables");- expect(textById.get("context-usage")).toContain("/context");- expect(textById.get("session-fork")).toContain("/fork");- expect(textById.get("compaction")).toContain("/compact");- expect(textById.get("auto-compaction")).toContain("automatically compacts");- expect(textById.get("auto-refine")).toContain("self-improves");- expect(textById.get("background-running")).toContain("close the terminal");- });-- it("keeps every hint concise", () => {- const deck = new FeatureHintDeck(() => 0);- const hints = FEATURE_HINTS.map(() => deck.next({ getKeybinding: () => "Ctrl+Key", isResidentSession: true }));+ const textById = new Map(FEATURE_HINTS.map((hint) => [hint.id, hint.getText(context)]));- expect(hints.every((hint) => hint !== undefined && hint.text.length <= 80)).toBe(true);+ expect(textById.get("prompt-stash")).toContain("Meta+S");+ expect(textById.get("follow-up")).toContain("Meta+Enter");+ expect(textById.get("agents-view")).toContain("Meta+Left");});it("hides resident-only hints in ephemeral sessions", () => {diff --git a/packages/coding-agent/test/model-resolver.test.ts b/packages/coding-agent/test/model-resolver.test.tsindex bb56a730bd..4988f7ad48 100644--- a/packages/coding-agent/test/model-resolver.test.ts+++ b/packages/coding-agent/test/model-resolver.test.ts@@ -1,9 +1,8 @@import type { Model } from "@earendil-works/pi-ai";-import { describe, expect, test } from "vitest";+import { describe, expect, test, vi } from "vitest";import {defaultModelPerProvider,findInitialModel,- parseModelPattern,resolveCliModel,resolveModelScopeFromModels,} from "../src/core/model-resolver.js";@@ -90,163 +89,46 @@ describe("resolveModelScopeFromModels", () => {expect(result[1]?.model.provider).toBe("openai");expect(result[1]?.model.id).toBe("gpt-4o");});-});--describe("parseModelPattern", () => {- describe("simple patterns without colons", () => {- test("exact match returns model with undefined thinking level", () => {- const result = parseModelPattern("claude-sonnet-4-5", allModels);- expect(result.model?.id).toBe("claude-sonnet-4-5");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toBeUndefined();- });- test("partial match returns best model with undefined thinking level", () => {- const result = parseModelPattern("sonnet", allModels);- expect(result.model?.id).toBe("claude-sonnet-4-5");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toBeUndefined();- });-- test("preserves provider-qualified selections when model names overlap", () => {- const primeInferenceModel: Model<"anthropic-messages"> = {- ...mockModels[0],- id: "z-ai/glm-5.2",- name: "GLM 5.2",- provider: "prime-inference",- baseUrl: "https://api.pinference.ai/api/v1",- };- const huggingFaceModel: Model<"anthropic-messages"> = {- ...primeInferenceModel,- id: "zai-org/GLM-5.2",- provider: "huggingface",- baseUrl: "https://router.huggingface.co/v1",- };-- const result = parseModelPattern("huggingface/zai-org/GLM-5.2", [primeInferenceModel, huggingFaceModel]);-- expect(result.model).toBe(huggingFaceModel);- });+ test("resolves a thinking level after a colon-bearing model id", () => {+ const result = resolveModelScopeFromModels(["openrouter/qwen/qwen3-coder:exacto:high"], allModels);- test("no match returns undefined model and thinking level", () => {- const result = parseModelPattern("nonexistent", allModels);- expect(result.model).toBeUndefined();- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toBeUndefined();- });+ expect(result).toEqual([{ model: mockOpenRouterModels[0], thinkingLevel: "high" }]);});- describe("patterns with valid thinking levels", () => {- test("sonnet:high returns sonnet with high thinking level", () => {- const result = parseModelPattern("sonnet:high", allModels);- expect(result.model?.id).toBe("claude-sonnet-4-5");- expect(result.thinkingLevel).toBe("high");- expect(result.warning).toBeUndefined();- });+ test("keeps the model, warns, and drops an invalid thinking suffix", () => {+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ try {+ const result = resolveModelScopeFromModels(["sonnet:random"], allModels);- test("gpt-4o:medium returns gpt-4o with medium thinking level", () => {- const result = parseModelPattern("gpt-4o:medium", allModels);- expect(result.model?.id).toBe("gpt-4o");- expect(result.thinkingLevel).toBe("medium");- expect(result.warning).toBeUndefined();- });-- test("all valid thinking levels work", () => {- for (const level of ["off", "minimal", "low", "medium", "high", "xhigh"]) {- const result = parseModelPattern(`sonnet:${level}`, allModels);- expect(result.model?.id).toBe("claude-sonnet-4-5");- expect(result.thinkingLevel).toBe(level);- expect(result.warning).toBeUndefined();- }- });+ expect(result).toEqual([{ model: mockModels[0], thinkingLevel: undefined }]);+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('Invalid thinking level "random"'));+ } finally {+ warn.mockRestore();+ }});- describe("patterns with invalid thinking levels", () => {- test("sonnet:random returns sonnet with undefined thinking level and warning", () => {- const result = parseModelPattern("sonnet:random", allModels);- expect(result.model?.id).toBe("claude-sonnet-4-5");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toContain("Invalid thinking level");- expect(result.warning).toContain("random");- });-- test("gpt-4o:invalid returns gpt-4o with undefined thinking level and warning", () => {- const result = parseModelPattern("gpt-4o:invalid", allModels);- expect(result.model?.id).toBe("gpt-4o");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toContain("Invalid thinking level");- });- });-- describe("OpenRouter models with colons in IDs", () => {- test("qwen3-coder:exacto matches the model with undefined thinking level", () => {- const result = parseModelPattern("qwen/qwen3-coder:exacto", allModels);- expect(result.model?.id).toBe("qwen/qwen3-coder:exacto");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toBeUndefined();- });-- test("openrouter/qwen/qwen3-coder:exacto matches with provider prefix", () => {- const result = parseModelPattern("openrouter/qwen/qwen3-coder:exacto", allModels);- expect(result.model?.id).toBe("qwen/qwen3-coder:exacto");- expect(result.model?.provider).toBe("openrouter");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toBeUndefined();- });-- test("qwen3-coder:exacto:high matches model with high thinking level", () => {- const result = parseModelPattern("qwen/qwen3-coder:exacto:high", allModels);- expect(result.model?.id).toBe("qwen/qwen3-coder:exacto");- expect(result.thinkingLevel).toBe("high");- expect(result.warning).toBeUndefined();- });-- test("openrouter/qwen/qwen3-coder:exacto:high matches with provider and thinking level", () => {- const result = parseModelPattern("openrouter/qwen/qwen3-coder:exacto:high", allModels);- expect(result.model?.id).toBe("qwen/qwen3-coder:exacto");- expect(result.model?.provider).toBe("openrouter");- expect(result.thinkingLevel).toBe("high");- expect(result.warning).toBeUndefined();- });-- test("gpt-4o:extended matches the extended model with undefined thinking level", () => {- const result = parseModelPattern("openai/gpt-4o:extended", allModels);- expect(result.model?.id).toBe("openai/gpt-4o:extended");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toBeUndefined();- });- });-- describe("invalid thinking levels with OpenRouter models", () => {- test("qwen3-coder:exacto:random returns model with undefined thinking level and warning", () => {- const result = parseModelPattern("qwen/qwen3-coder:exacto:random", allModels);- expect(result.model?.id).toBe("qwen/qwen3-coder:exacto");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toContain("Invalid thinking level");- expect(result.warning).toContain("random");- });-- test("qwen3-coder:exacto:high:random returns model with undefined thinking level and warning", () => {- const result = parseModelPattern("qwen/qwen3-coder:exacto:high:random", allModels);- expect(result.model?.id).toBe("qwen/qwen3-coder:exacto");- expect(result.thinkingLevel).toBeUndefined();- expect(result.warning).toContain("Invalid thinking level");- expect(result.warning).toContain("random");- });- });+ test("preserves provider-qualified selections when model names overlap", () => {+ const primeInferenceModel: Model<"anthropic-messages"> = {+ ...mockModels[0]!,+ id: "z-ai/glm-5.2",+ name: "GLM 5.2",+ provider: "prime-inference",+ baseUrl: "https://api.pinference.ai/api/v1",+ };+ const huggingFaceModel: Model<"anthropic-messages"> = {+ ...primeInferenceModel,+ id: "zai-org/GLM-5.2",+ provider: "huggingface",+ baseUrl: "https://router.huggingface.co/v1",+ };- describe("edge cases", () => {- test("empty pattern matches via partial matching", () => {- const result = parseModelPattern("", allModels);- expect(result.model).not.toBeNull();- expect(result.thinkingLevel).toBeUndefined();- });+ const result = resolveModelScopeFromModels(+ ["huggingface/zai-org/GLM-5.2"],+ [primeInferenceModel, huggingFaceModel],+ );- test("pattern ending with colon treats empty suffix as invalid", () => {- const result = parseModelPattern("sonnet:", allModels);- expect(result.model?.id).toBe("claude-sonnet-4-5");- expect(result.warning).toContain("Invalid thinking level");- });+ expect(result).toEqual([{ model: huggingFaceModel, thinkingLevel: undefined }]);});});