diff --git a/CHANGELOG.md b/CHANGELOG.md index d41ca98..8ef5f01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ This project follows [Semantic Versioning](https://semver.org/). Release notes describe user-visible behavior; implementation details remain in the technical specification and commit history. +## [1.0.3] - 2026-09-10 + +### Added + +- **`/modelRank` interleaved ranking — manual entries merge into the base-score ordering** — the manual ranking no longer floats as a block on top of everything. The dialog (and `rank list`) now show one merged ordering sorted by effective capability, and a move (ctrl/alt+up/down or the per-model actions) acts on that merged list: the moved model gets an anchored manual score strictly between its new neighbors (tier from the neighbor below, raw at the midpoint of the same-tier gap or a ±0.001 decimal step at tier boundaries and tie groups), so nudging a model one spot no longer requires manually ranking every model above it — unranked models materialize an anchored entry on their first move. Anchored scores persist in `capability-rank.json` as an optional `scores` side-map (`{ key: { tier, raw } }`); entries without a stored score keep the legacy ladder semantics (linear percentile by array position), so hand-written and pre-existing files stay valid, and CLI `rank set/add/remove` preserve surviving anchored scores. Dispatch surfaces are unchanged in shape — a manual hit still overrides the base capability score, now via the anchored (tier, raw) pair. +- **Full-superset candidate lists for `/modelRank` & `/poolConfig`** — both config surfaces now list every conversable model of credentialed providers (the full superset) instead of the pruned injection face, so task pools can be configured with models that are not favorites/visible yet, and models already configured in a pool stay listed even when they drop out of the pruned face (force-keep). Actual chain candidacy is still decided at runtime by pool selection ∩ activation. + +### Fixed + +- **Runaway list scrolling in the `/modelRank` dialog** — moving the mouse up/down across the list could set off a wild, never-ending scroll: the plugin mirrored every hover-driven selection change back into the dialog's `current` prop, and the host dialog re-centered the scroll on `current` each time — the centering scroll slid fresh rows under the stationary cursor, each firing another hover → selection change → re-scroll: an unbounded self-sustaining loop. `current` is now driven only by explicit ctrl/alt+up/down hotkey moves, never mirrored from hover. + ## [1.0.2] - 2026-09-07 ### Added diff --git a/README.md b/README.md index 79ce5cc..f974d9d 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ Two manual commands let your configuration beat system defaults. All state is pe ### /modelRank — model capability ranking (manual dialog; conversational: /modelRank-chat) -- **TUI (/modelRank)**: a dialog listing models by effective capability; select a model to pin it to top, move it up/down, or remove it from the ranking. +- **TUI (/modelRank)**: a dialog listing models by effective capability — manual entries interleave with base-score models in one merged ordering; moving a model up/down anchors it a manual score between its new neighbors (one-spot nudges, no need to rank everything above), plus pin-to-top / remove; effective immediately. - **Non-TUI / in-session (/modelRank-chat)**: a conversational flow — it injects the current ranking and a reference ordering; reply "pin glm-5.3 to top" or "clear the ranking" and the agent translates that into `rank` CLI calls to persist. - **Semantics**: manual ranking **takes priority over the base capability score** (realtime index → bundled snapshot → curated table all yield) — matched models (including their prefix variants) get a rank-position score and S/A/B/C tier: rankings with ≤4 entries map positions to S/A/B/C in order; ≥5 entries use quantile buckets (top 20% S / next 20% A / next 20% B / rest C, same semantics as the OpenRouter rank source); within a tier, the linear rank position breaks ties. Unranked models are unaffected. The ranking feeds every decision surface: lane chains, effort affinity, capability-level gates, and deny hints. - **Config file**: `~/.config/opencode/opencode-switchman/capability-rank.json` (`models` array order = strongest first). diff --git a/README.zh.md b/README.zh.md index 0c796c1..8b532ef 100644 --- a/README.zh.md +++ b/README.zh.md @@ -177,7 +177,7 @@ TUI 插件没有目录自动发现机制,需要在 **`tui.jsonc`/`tui.json`** ### /modelRank —— 模型能力排名(手动弹窗;会话式用 /modelRank-chat) -- **TUI(/modelRank)**:弹出按有效能力排序的模型列表,选中模型后可「置顶 / 上移 / 下移 / 移出排名」,即时生效。 +- **TUI(/modelRank)**:弹窗按有效能力给出单一合并排序——手动条目与基础分模型交错排列;上移/下移会给模型锚定一个介于新上下邻居之间的手动分(一次挪一格,无需把上面的模型全排一遍),另有置顶/移出排名,即时生效。 - **非 TUI / 会话内(/modelRank-chat)**:会话式流程——注入当前排名与参考排序,回复「把 glm-5.3 排到最前」「清空排名」由 agent 换算 `rank` 命令落盘。 - **语义**:手动排名**优先于基础能力分**(实时第三方指数 → 内置快照 → 策展表全部让位)——命中模型(含其前缀变体)按排名序位取能力分与 S/A/B/C 档:排名 ≤4 项时依次 S/A/B/C;≥5 项按分位口径(top20% S / 次20% A / 次20% B / 其余 C,与 OpenRouter 序位派生同口径);同档内按序位线性分细排。未排名模型不受影响。排名参与所有决策面:六档链排序、档位亲和、能力等级闸与 deny 改派建议。 - **配置文件**:`~/.config/opencode/opencode-switchman/capability-rank.json`(`models` 数组顺序=能力降序,#1 最强)。 diff --git a/docs/quick-start.md b/docs/quick-start.md index e39b29c..974180c 100644 --- a/docs/quick-start.md +++ b/docs/quick-start.md @@ -76,7 +76,7 @@ Not every model a provider offers needs to join the matrix. You decide the **act By default, model capability comes from an automatic cascade (live third-party index → bundled snapshot → curated table). If you disagree — your workload, your ranking. -**TUI**: run `/modelRank` — a dialog lists every model by effective capability; select a model to pin it to top, move it up/down, or remove it from the ranking. +**TUI**: run `/modelRank` — a dialog listing every model by effective capability, manual entries interleaved with base-score models; move a model up/down to anchor it a manual score between its new neighbors (one-spot nudges), or pin it to top / remove it from the ranking. ![/modelRank — capability ranking with manual-rank vs base-score provenance](assets/tui-model-rank.png) diff --git a/docs/quick-start.zh.md b/docs/quick-start.zh.md index 6da3364..5af0d99 100644 --- a/docs/quick-start.zh.md +++ b/docs/quick-start.zh.md @@ -76,7 +76,7 @@ 默认模型能力来自自动级联(实时第三方指数 → 内置快照 → 策展表)。不同意?你的工作负载你说了算。 -**TUI**:运行 `/modelRank`——弹窗按有效能力列出所有模型;选中模型可「置顶 / 上移 / 下移 / 移出排名」。 +**TUI**:运行 `/modelRank`——弹窗按有效能力列出所有模型,手动条目与基础分模型交错;上移/下移会给模型锚定一个介于新上下邻居之间的手动分(一次挪一格),另有置顶/移出排名。 ![/modelRank——模型能力排名:手动命中优先于基础能力分](assets/tui-model-rank.png) diff --git a/package.json b/package.json index 654b1df..26e8c51 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencode-switchman", - "version": "1.0.2", + "version": "1.0.3", "description": "OpenCode orchestration plugin: six-lane shell matrix dispatch (hard ROUTE_META validation / breaker self-healing / probes / three-pool quota awareness / cost-aware tiebreaker)", "license": "MIT", "repository": { diff --git a/src/capability.ts b/src/capability.ts index 2f4d7b3..e2d95d1 100644 --- a/src/capability.ts +++ b/src/capability.ts @@ -450,19 +450,35 @@ function manualRankMatch(normKey: string): { rank: NonNullable= 0 ? { rank, index: best } : null } -/** Manual rank hit -> DynamicBaseResult (source="manual"; miss=null, falls through the original fallback chain) */ +/** Manual rank hit -> DynamicBaseResult (source="manual"; miss=null, falls through the original fallback chain). + * [2026-09-10]-[anchored-score entries (capability-rank.json `scores` side-map) take precedence: tier + rawScore come + * from the stored (tier, raw) pair so manual entries interleave with base-score models; entries without an anchored + * score keep the legacy ladder (linear percentile by array position + its tier table)] */ export function manualRankResult(modelId: string): DynamicBaseResult | null { const norm = normalizeModelKey(modelId) if (!norm) return null const hit = manualRankMatch(norm) if (!hit) return null + const entryKey = hit.rank.models[hit.index]! + const version = `manual-${hit.rank.updated_at || "unset"}` + const anchored = hit.rank.scores?.[entryKey] + if (anchored) { + return { + score: TIER_SCORE[anchored.tier], + rawScore: anchored.raw, + tier: anchored.tier, + source: "manual", + version, + matchedAs: entryKey, + } + } const tier = manualTierAt(hit.index, hit.rank.models.length) return { score: TIER_SCORE[tier], rawScore: manualLinearScore(hit.index, hit.rank.models.length), tier, source: "manual", - version: `manual-${hit.rank.updated_at || "unset"}`, - matchedAs: hit.rank.models[hit.index]!, + version, + matchedAs: entryKey, } } diff --git a/src/config-cli.ts b/src/config-cli.ts index 182eaab..dbe15b0 100644 --- a/src/config-cli.ts +++ b/src/config-cli.ts @@ -8,32 +8,47 @@ import { loadSupersetShells, loadManifest, paths } from "./state" import { loadCapabilityRank, loadPoolConfig, poolAllowlist, writeCapabilityRank, clearCapabilityRank, writePoolConfig, resetPoolConfig, + type RankScoreOverride, } from "./user-overrides" import { LANE_ORDER, type Lane } from "./types" import { TIER_RANK } from "./model-ranks" -interface ModelRow { key: string; modelId: string; tier: string; source: string; raw: number | null } +interface ModelRow { key: string; modelId: string; tier: string; source: string; raw: number | null; manualIdx?: number } +// [2026-09-10]-[manual-first tie-break: on exact (tier, raw) ties manual entries sort above base entries, ordered by +// their models-array slot — lets anchored moves win ties without touching base-vs-base ordering] function capabilityCompare(a: ModelRow, b: ModelRow): number { return TIER_RANK[a.tier as "S"] - TIER_RANK[b.tier as "S"] || (b.raw ?? -Infinity) - (a.raw ?? -Infinity) || + (a.manualIdx ?? Infinity) - (b.manualIdx ?? Infinity) || a.key.localeCompare(b.key) } function toRow(modelId: string): ModelRow { const base = baseScoreDynamic(modelId) - return { - key: normalizeModelKey(modelId), + const key = normalizeModelKey(modelId) + const row: ModelRow = { + key, modelId, tier: base.tier, source: base.source, raw: base.rawScore ?? null, } + // manual array slot (undefined for non-manual rows): the merged view uses it as the exact-tie tie-break + if (base.source === "manual" && base.matchedAs) { + const idx = loadCapabilityRank()?.models.indexOf(base.matchedAs) ?? -1 + if (idx >= 0) row.manualIdx = idx + } + return row } -/** All available models (superset manifest first, falling back to the bundled manifest; deduped by modelId across provider pools); sorted by effective capability descending */ +/** All available models (full-superset candidates first — every conversable model of credentialed providers — falling back to + * the injection-face shells, then the bundled manifest; deduped by modelId across provider pools); sorted by effective capability descending + * [2026-09-10]-[candidate surface widened from the pruned injection face to the full superset: pools can now be configured + * with models that are not (yet) favorites/visible — actual chain candidacy is still decided at runtime by pool selection ∩ activation] */ export function allModelRows(): ModelRow[] { - const shells = loadSupersetShells()?.shells ?? loadManifest().shells + const sup = loadSupersetShells() + const shells = (sup?.candidates && sup.candidates.length > 0 ? sup.candidates : sup?.shells) ?? loadManifest().shells const seen = new Set() const rows: ModelRow[] = [] for (const s of shells) { @@ -133,21 +148,20 @@ function cmdPool(args: string[]): number { throw new Error(`unknown subcommand pool ${sub} (list/add/remove/set/clear)`) } -/** Merged view of the manual ranking + reference ordering of available models (indices are globally contiguous, referenced by rank set/add/remove) */ +/** [2026-09-10]-[merged interleaved view: manual entries no longer float as a block on top — every model (manual + base) + * is sorted by effective capability (tier, raw; manual wins exact ties), so /modelRank moves and rank CLI indices act + * on one global ordering. Manual keys missing from the model universe keep a row (dead-key tolerance)] */ export function rankViewRows(): ModelRow[] { const rank = loadCapabilityRank() - const rankedKeys = new Set(rank?.models ?? []) - const ranked: ModelRow[] = (rank?.models ?? []).map((k) => ({ ...toRow(k), source: "manual" })) - const flat = allModelRows() - const seen = new Set(rankedKeys) - const rest: ModelRow[] = [] - for (const r of flat) { - if (seen.has(r.key)) continue - seen.add(r.key) - rest.push(r) + const rows = allModelRows() + const seen = new Set(rows.map((r) => r.key)) + for (const k of rank?.models ?? []) { + if (seen.has(k)) continue + seen.add(k) + rows.push(toRow(k)) } - rest.sort(capabilityCompare) - return [...ranked, ...rest] + rows.sort(capabilityCompare) + return rows } function cmdRank(args: string[]): number { @@ -156,14 +170,13 @@ function cmdRank(args: string[]): number { if (!sub || sub === "list") { const view = rankViewRows() const manualCount = rank?.models.length ?? 0 - console.log(`Model capability ranking (config file ${paths().capabilityRank}; the manual ranking takes priority over the base capability score; higher up = stronger)`) - console.log(`== Manual ranking (${manualCount}${manualCount > 0 ? "" : "; unconfigured = all use the base capability score"}) ==`) + console.log(`Model capability ranking (config file ${paths().capabilityRank}; one merged ordering — manual entries interleave with base-score models by effective score; higher up = stronger)`) + console.log(`== Merged ordering (${manualCount} manual${manualCount > 0 ? "" : "; none = all use the base capability score"}; CLI set/add order entries by the legacy ladder, TUI moves anchor scores between neighbors) ==`) view.forEach((row, i) => { - if (i < manualCount) console.log(` #${String(i + 1).padStart(2, "0")} ${row.modelId} (${row.tier}-tier·manual)`) - }) - console.log("== Reference ordering of available models (base capability score) ==") - view.forEach((row, i) => { - if (i >= manualCount) console.log(` #${String(i + 1).padStart(2, "0")} ${row.modelId} (${row.tier}-tier)`) + const manualTag = row.source === "manual" + ? `·manual${row.raw !== null ? ` ${row.raw}` : ""}` + : "" + console.log(` #${String(i + 1).padStart(2, "0")} ${row.modelId} (${row.tier}-tier${manualTag})`) }) return 0 } @@ -171,11 +184,28 @@ function cmdRank(args: string[]): number { if (rest.length === 0) throw new Error(`rank ${sub} requires an index or model name`) const refs = resolveRefs(rest, rankViewRows()) const current = [...(rank?.models ?? [])] + // [2026-09-10]-[CLI writes must preserve anchored scores: add keeps every existing entry's score, remove/set drop + // only the keys leaving the list (ladder fallback covers the rest)] + const keepScores = (keep: string[]): Record | undefined => { + const next: Record = {} + for (const [k, v] of Object.entries(rank?.scores ?? {})) if (keep.includes(k)) next[k] = v + return Object.keys(next).length > 0 ? next : undefined + } let next: string[] - if (sub === "add") next = [...current, ...refs.filter((k) => !current.includes(k))] - else if (sub === "remove") next = current.filter((k) => !refs.includes(k)) - else next = refs - const file = writeCapabilityRank(next) + let nextScores: Record | undefined + if (sub === "add") { + next = [...current, ...refs.filter((k) => !current.includes(k))] + nextScores = keepScores([...current, ...refs]) + } + else if (sub === "remove") { + next = current.filter((k) => !refs.includes(k)) + nextScores = keepScores(next) + } + else { + next = refs + nextScores = keepScores(refs) + } + const file = writeCapabilityRank(next, nextScores) console.log(`Updated the manual capability ranking (${file.models.length} models, effective immediately, sidebar refreshes in sync)`) return cmdRank(["list"]) } diff --git a/src/index.ts b/src/index.ts index bac4e64..b9c65c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -608,9 +608,15 @@ export const SwitchmanPlugin: Plugin = async (input, rawOptions) => { roAliases: true, degradedFamilyByProvider: true, markDegraded: true, }) const fullShells = defs.length + const candidateEntries = defs.map(toManifestEntry) + // [2026-09-10]-[pool-config models join the force-keep set: task-pool selections must be injectable even when the + // model missed favorites/visible set (runtime chains still narrow to pool selection ∩ activation, gate 5.5 unchanged)]- + const poolConfiguredIds = new Set() + for (const ids of Object.values(loadPoolConfig())) for (const id of ids) poolConfiguredIds.add(id) defs = selectInjectableDefs(defs, { customLanes: (options.lanes as Record | null) ?? null, keepModels: options.injection!.mode === "all" ? new Set(supersetModels) : new Set(validConfiguredModels), + keepModelIds: poolConfiguredIds, preferredModels: new Set(validConfiguredModels.map((m) => m.slice(m.indexOf("/") + 1))), capabilityOf: (modelId) => baseScoreDynamic(modelId), billingBoostOf, unknownOf: unknownOfModel, @@ -622,8 +628,12 @@ export const SwitchmanPlugin: Plugin = async (input, rawOptions) => { try { writeJsonAtomic(paths().shellSuperset, { generated_at: new Date().toISOString(), - counts: { superset_models: supersetModels.length, shells: defs.length, full_shells: fullShells, degraded: degradedModelCount }, + counts: { superset_models: supersetModels.length, shells: defs.length, full_shells: fullShells, candidate_shells: candidateEntries.length, degraded: degradedModelCount }, mode: runMode, + // [2026-09-10]-[full-superset candidate surface for the /modelRank //poolConfig config lists: every conversable model + // of credentialed providers (readers dedupe by modelId, same rule as before) so pools can be configured beyond the + // pruned injection face; `shells` stays the injection face (dynamicManifest/banner/deny-candidate semantics unchanged)]- + candidates: candidateEntries, shells: defs.map(toManifestEntry), }) } catch { /* fail-open */ } diff --git a/src/shells.json b/src/shells.json index 8f16bc9..e1ccff3 100644 --- a/src/shells.json +++ b/src/shells.json @@ -1,6 +1,6 @@ { "_note": "opencode-switchman full matrix: authoritative enabled surface (models enabled in model management, scripts/visible-models.txt) × all declared thinking efforts (models.dev reasoning_options, toggle→off). Generated by scripts/gen-shells.ts — do not edit by hand; after model on/off changes, sync the pin file and re-run bun run gen:shells. The six lane chains are computed from capability score × lane affinity × structural gate × billing factor (api billing 0.85, unknown group 0.75, sunk by coefficient; no vendor-reserved seats).", - "generated_at": "2026-09-07T08:09:07.964Z", + "generated_at": "2026-09-10T03:30:20.019Z", "counts": { "shells": 104, "visible_models": 13 diff --git a/src/shells.ts b/src/shells.ts index 2a31140..9e0a544 100644 --- a/src/shells.ts +++ b/src/shells.ts @@ -7,6 +7,7 @@ import { LANE_ORDER } from "./types" import type { ShellDefinition } from "./catalog" import type { CapabilityScore, LaneShellAttr } from "./lane-policy" import { laneBaseChain } from "./lane-policy" +import { normalizeModelKey } from "./capability" import { loadCachedThinkingShapes, deriveThinkingParam } from "./copilot-thinking" // [2026-09-01]-[aligned with core packages/opencode/src/plugin/github-copilot/models.ts: for the github-copilot pool, @@ -115,6 +116,11 @@ export interface InjectableSelectOpts { * competition; the sole meaning of slimming becomes "uninjected = really unavailable", eliminating favorites false * positives and missing vision shells] */ keepModels?: ReadonlySet + /** [2026-09-10]-[pool-configured models force-kept (normalized modelId keys, same key space as pool-config.json values): + * task-pool selections may reference models absent from favorites/visible set; without force-keep their shells never + * inject and a configured pool member is denied at gate 1 before the activation intersection can speak. Runtime chain + * candidacy is still gated by activation (favorites/visible set) ∩ pool selection — force-keep only makes dispatch possible] */ + keepModelIds?: ReadonlySet /** [2026-09-02]-[favorites first (by modelId): favorite models sort first within the same tier in the chain algorithm, passed through to computeLaneChain] */ preferredModels?: ReadonlySet capabilityOf: (modelId: string) => number | CapabilityScore @@ -171,6 +177,12 @@ export function selectInjectableDefs( if (opts.keepModels.has(`${d.provider}/${d.modelId}`)) keep.add(d.name) } } + // [2026-09-10]-[pool-config force-keep: normalized modelId match (pool-config.json stores bare normalized modelIds)] + if (opts.keepModelIds) { + for (const d of defs) { + if (opts.keepModelIds.has(normalizeModelKey(d.modelId))) keep.add(d.name) + } + } if (keep.size === 0) return [...defs] return defs.filter((d) => keep.has(d.name)) } diff --git a/src/state.ts b/src/state.ts index 734bd1d..4cea3b3 100644 --- a/src/state.ts +++ b/src/state.ts @@ -256,11 +256,17 @@ export function laneShells(ctx: RuntimeContext, lane: string): string[] { return Array.isArray(l) ? l : [] } -/** Dynamic superset manifest (config hook persists shell-superset.json; missing/broken = null) */ -export function loadSupersetShells(): { shells: ShellManifestEntry[]; generated_at?: string } | null { - const data = readJson<{ shells?: unknown; generated_at?: string }>(paths().shellSuperset) +/** Dynamic superset manifest (config hook persists shell-superset.json; missing/broken = null) + * [2026-09-10]-[candidates = full-superset entries feeding the /modelRank //poolConfig config surfaces (pre-pruning, + * deduped by the readers); shells = injection face. Older files without candidates = undefined (readers fall back to shells)] */ +export function loadSupersetShells(): { shells: ShellManifestEntry[]; candidates?: ShellManifestEntry[]; generated_at?: string } | null { + const data = readJson<{ shells?: unknown; candidates?: unknown; generated_at?: string }>(paths().shellSuperset) if (!data || !Array.isArray(data.shells) || data.shells.length === 0) return null - return { shells: data.shells as ShellManifestEntry[], generated_at: data.generated_at } + return { + shells: data.shells as ShellManifestEntry[], + candidates: Array.isArray(data.candidates) ? (data.candidates as ShellManifestEntry[]) : undefined, + generated_at: data.generated_at, + } } // [2026-09-01]-[provider.list cache across restarts: models/providers are plain string arrays; broken/missing = null (caller falls back to blocking probe)] diff --git a/src/tui.tsx b/src/tui.tsx index 1b0e076..6322f02 100644 --- a/src/tui.tsx +++ b/src/tui.tsx @@ -524,6 +524,34 @@ function openModelRankDialog(api: TuiPluginApi): void { api.ui.dialog.replace(() => ) } +// ---- [2026-09-10]-[interleaved move plumbing: one shared path for the list hotkeys and the per-model actions dialog — +// loads the rank file, applies applyRankMove against the merged view, persists models+scores, toasts the anchored result]---- + +function rankMoveApply(api: TuiPluginApi, key: string, delta: -1 | 0 | 1): boolean { + const rank = loadCapabilityRank() + const view = rankViewRows() + const name = view.find((r) => r.key === key)?.modelId ?? key + const res = applyRankMove(rank?.models ?? [], rank?.scores, view, key, delta) + if (!res) { + api.ui.toast({ + variant: "info", + message: `${name} is already at the ${delta === 1 ? "bottom" : "top"} of the merged capability list`, + }) + return false + } + try { + writeCapabilityRank(res.models, res.scores) + } catch (exc) { + api.ui.toast({ variant: "error", message: `write failed: ${exc instanceof Error ? exc.message : exc}` }) + return false + } + api.ui.toast({ + variant: "success", + message: `${delta === 0 ? "Pinned" : "Moved"} ${name} to #${res.position + 1} (manual score ${res.score.tier}/${res.score.raw}, anchored between its neighbors; effective immediately, sidebar refreshes)`, + }) + return true +} + // ---- [2026-09-06]-[/modelRank list hotkeys: ctrl+up / ctrl+down (alt+up / alt+down mirrored for terminals that swallow // ctrl+arrows) move the highlighted model within the manual ranking without opening the per-model action dialog. // The host DialogSelect has no per-dialog key API exposed to plugins, so the binding lives in the global keymap layer @@ -543,38 +571,28 @@ function RankPickerDialog(props: { api: TuiPluginApi }) { // Highlight tracking: onMove covers filter/navigate; before the first onMove the host selects row 0 const [highlight, setHighlight] = createSignal(null) const highlighted = () => highlight() ?? rows()[0]?.key ?? null + // [2026-09-10]-[hover feedback loop fix: mirroring onMove back into `current` re-fired the host's + // current-effects on every mouse-over — the setTimeout moveTo(index, center) call re-scrolled the list + // to center the hovered row, sliding fresh rows under the stationary cursor, each firing another + // onMouseOver → onMove → current change → re-scroll: an unbounded self-sustaining scroll loop. `current` + // is now driven only by explicit programmatic jumps (the ctrl/alt+up/down hotkey moves below), never + // mirrored from onMove]-[fixes the runaway /modelRank list scrolling after mouse movement; the ● marker + // now marks the last hotkey-moved model only] + const [cursor, setCursor] = createSignal(null) const claim: RankHotkeyClaim = { move: (delta) => { const key = highlighted() if (!key) return - const row = rows().find((r) => r.key === key) - const name = row?.modelId ?? key - const cur = [...(loadCapabilityRank()?.models ?? [])] - const i = cur.indexOf(key) - const res = applyRankMove(cur, key, delta) - const skip = (message: string) => props.api.ui.toast({ variant: "info", message }) - if (!res) { - // Boundary no-ops must stay audible, otherwise mashing the key feels broken - if (i < 0) skip(`${name} is not in the manual ranking (ctrl+up adds it at the end; enter = per-model actions)`) - else skip(`${name} is already at the ${delta === -1 ? "top" : "bottom"} of the manual ranking`) - return - } - try { - writeCapabilityRank(res.models) - } catch (exc) { - props.api.ui.toast({ variant: "error", message: `write failed: ${exc instanceof Error ? exc.message : exc}` }) - return + // [2026-09-10]-[interleaved semantics: the move acts on the merged view (manual + base models); any model can + // move up/down — unranked ones materialize an anchored manual score between their neighbors] + if (rankMoveApply(props.api, key, delta)) { + // Cursor follows the moved model: `current` is the only cursor control the plugin DialogSelect exposes + // (side effect: the ● marker stays on the last-moved model until the dialog closes); set here ONLY — + // see the hover feedback loop note above for why onMove must not feed into `current` + setHighlight(key) + setCursor(key) + setRev((v) => v + 1) } - props.api.ui.toast({ - variant: "success", - message: i < 0 - ? `Added ${name} to the ranking (manual rank #${res.index + 1}, effective immediately, sidebar refreshes)` - : `Moved ${name} to rank #${res.index + 1} (effective immediately, sidebar refreshes)`, - }) - // Cursor follows the moved model: `current` is the only cursor control the plugin DialogSelect exposes - // (side effect: the ● marker stays on the last-moved model until the dialog closes) - setHighlight(key) - setRev((v) => v + 1) }, } rankHotkeyClaim = claim @@ -583,15 +601,15 @@ function RankPickerDialog(props: { api: TuiPluginApi }) { }) return ( ({ title: `#${String(i + 1).padStart(2, "0")} ${r.modelId}`, value: r.key, - description: `${r.tier}-tier · ${r.source === "manual" ? "manual rank" : "base capability score"}`, + description: `${r.tier}-tier · ${r.source === "manual" ? `manual${r.raw !== null ? ` ${r.raw}` : ""}` : "base capability score"}`, onSelect: () => props.api.ui.dialog.replace(() => ), }))} - current={highlighted() ?? undefined} + current={cursor() ?? undefined} onMove={(opt) => setHighlight(String(opt.value))} flat /> @@ -599,43 +617,44 @@ function RankPickerDialog(props: { api: TuiPluginApi }) { } function RankActionsDialog(props: { api: TuiPluginApi; model: string; modelKey: string }) { - const rank = () => [...(loadCapabilityRank()?.models ?? [])] - const at = () => rank().indexOf(props.modelKey) - // [2026-09-06]-[reorder rules now come from the shared applyRankMove helper (same semantics as the list hotkeys); - // only the boundary feedback is dialog-specific]- - const apply = (next: string[], message: string) => { - writeCapabilityRank(next) - props.api.ui.toast({ variant: "success", message: `${message} (effective immediately, sidebar refreshes)` }) - props.api.ui.dialog.replace(() => ) - } + // [2026-09-10]-[interleaved semantics: up/down/pin act on the merged view via the shared rankMoveApply path (any + // model can move — unranked ones materialize an anchored manual score between their neighbors); the obsolete + // "add at the end" action is gone; remove strips both the membership and the anchored score] + const rank = () => loadCapabilityRank() + const ranked = () => (rank()?.models ?? []).includes(props.modelKey) + const anchored = () => rank()?.scores?.[props.modelKey] const move = (delta: -1 | 0 | 1) => { - const res = applyRankMove(rank(), props.modelKey, delta) - if (!res) { - props.api.ui.toast({ variant: "info", message: `${props.model} is already at the ${delta === 1 ? "bottom" : "top"} of the manual ranking` }) + if (rankMoveApply(props.api, props.modelKey, delta)) { + props.api.ui.dialog.replace(() => ) + } + } + const remove = () => { + const cur = rank() + const nextModels = (cur?.models ?? []).filter((k) => k !== props.modelKey) + const nextScores = { ...(cur?.scores ?? {}) } + delete nextScores[props.modelKey] + try { + writeCapabilityRank(nextModels, Object.keys(nextScores).length > 0 ? nextScores : undefined) + } catch (exc) { + props.api.ui.toast({ variant: "error", message: `write failed: ${exc instanceof Error ? exc.message : exc}` }) return } - apply(res.models, delta === 0 ? `Pinned ${props.model} to top` : `Moved ${props.model} to rank #${res.index + 1}`) + props.api.ui.toast({ variant: "success", message: `Removed ${props.model} from the manual ranking (falls back to the base capability score, effective immediately, sidebar refreshes)` }) + props.api.ui.dialog.replace(() => ) } - const ranked = () => at() >= 0 - const options = createMemo(() => { - const list = [ - { title: "▲ Pin to top (set as strongest)", value: "top", onSelect: () => move(0) }, - ...(ranked() && at() > 0 - ? [{ title: "↑ Move up one", value: "up", onSelect: () => move(-1) }] - : []), - ...(ranked() && at() < rank().length - 1 - ? [{ title: "↓ Move down one", value: "down", onSelect: () => move(1) }] - : []), - ...(ranked() - ? [{ title: "✕ Remove from ranking (fall back to base score)", value: "out", onSelect: () => apply(rank().filter((k) => k !== props.modelKey), `Removed ${props.model}`) }] - : [{ title: "+ Add to ranking (at the end)", value: "in", onSelect: () => move(1) }]), - { title: "← Back to ranking list", value: "__back", onSelect: () => props.api.ui.dialog.replace(() => ) }, - ] - return list - }) + const options = createMemo(() => [ + { title: "▲ Pin to top (above every model)", value: "top", onSelect: () => move(0) }, + { title: "↑ Move up one (swaps with the model above; anchors a manual score between neighbors)", value: "up", onSelect: () => move(-1) }, + { title: "↓ Move down one (swaps with the model below)", value: "down", onSelect: () => move(1) }, + ...(ranked() + ? [{ title: "✕ Remove from ranking (fall back to the base score)", value: "out", onSelect: remove }] + : []), + { title: "← Back to ranking list", value: "__back", onSelect: () => props.api.ui.dialog.replace(() => ) }, + ]) + const a = anchored() return ( diff --git a/src/user-overrides.ts b/src/user-overrides.ts index c15e38f..6ee442a 100644 --- a/src/user-overrides.ts +++ b/src/user-overrides.ts @@ -15,8 +15,20 @@ import { paths, readJson, writeJsonAtomic, nowIso } from "./state" export interface CapabilityRankFile { version: 1 updated_at: string - /** Normalized modelId, order = capability descending (#1 strongest) */ + /** Normalized modelId, order = capability descending (#1 strongest); among manual entries it only breaks exact (tier, raw) ties */ models: string[] + /** [2026-09-10]-[interleaved ranking: per-entry anchored manual scores (tier + raw). Entries WITHOUT a score keep the + * legacy ladder semantics (linear percentile by array position), so hand-written/legacy files stay valid; a TUI move + * materializes the moved model's score between its merged-view neighbors, letting manual entries interleave with + * base-score models instead of floating as a block on top] */ + scores?: Record +} + +/** Manual anchored score: tier drives the dispatch score (TIER_SCORE) and cross-tier order; raw (0-100-ish, 3 decimals) + * orders within the tier. null base raws are treated as 0 when anchoring. */ +export interface RankScoreOverride { + tier: "S" | "A" | "B" | "C" + raw: number } export interface PoolConfigFile { @@ -75,11 +87,28 @@ export function validateCapabilityRank(v: unknown): CapabilityRankFile | null { if (!v || typeof v !== "object" || Array.isArray(v)) return null const raw = (v as { models?: unknown }).models if (!Array.isArray(raw)) return null - return { + const models = normalizeModelList(raw) + const file: CapabilityRankFile = { version: 1, updated_at: typeof (v as { updated_at?: unknown }).updated_at === "string" ? String((v as { updated_at?: unknown }).updated_at) : "", - models: normalizeModelList(raw), + models, } + // [2026-09-10]-[scores side-map: keep only entries whose key survived list normalization; drop malformed ones + // (fail-open = that entry falls back to the ladder); raw rounded to 3 decimals (tie-breaking granularity)] + const rawScores = (v as { scores?: unknown }).scores + if (rawScores && typeof rawScores === "object" && !Array.isArray(rawScores)) { + const scores: Record = {} + for (const [k, val] of Object.entries(rawScores as Record)) { + if (!models.includes(k) || !val || typeof val !== "object") continue + const tier = (val as { tier?: unknown }).tier + const num = (val as { raw?: unknown }).raw + if (tier !== "S" && tier !== "A" && tier !== "B" && tier !== "C") continue + if (typeof num !== "number" || !Number.isFinite(num)) continue + scores[k] = { tier, raw: Math.round(num * 1000) / 1000 } + } + if (Object.keys(scores).length > 0) file.scores = scores + } + return file } export function validatePoolConfig(v: unknown): PoolConfigFile | null { @@ -134,31 +163,108 @@ export function overrideSummary(): { rankModels: number; poolLanes: number } { // ---- Pure manual-ranking move (shared semantics for the TUI /modelRank dialog: enter actions and ctrl+up/ctrl+down hotkeys) ---- -/** [2026-09-06]-[/modelRank hotkeys: extract the reorder rules shared by the RankActions dialog and the new list-level - * ctrl+up/ctrl+down hotkeys into one pure helper so both surfaces stay in lockstep]- - * [delta 0 = pin to top; ±1 = move one spot toward the front/back (unranked + down is a no-op, unranked + up/pin - * materializes an entry: pin → front, up → appended at the end, mirroring the dialog's "Add to ranking (at the end)"). - * Boundary-hit ranked moves return null (no write). Pure: returns the next list + the model's new index, no I/O] */ -export function applyRankMove(models: readonly string[], key: string, delta: -1 | 0 | 1): { models: string[]; index: number } | null { - const cur = [...models] - const i = cur.indexOf(key) +/** [2026-09-10]-[interleaved move semantics: a move acts on the MERGED effective view (manual + base-score models sorted + * by capability), not just the manual sub-list. Moving model X one spot gives X an anchored manual score strictly + * between its new neighbors: tier from the neighbor it must stay below/above, raw at the midpoint (same tier) or a + * ±0.001 step (tier boundary / tie group) — decimals break ties, per the "nudge above the model below, below the model + * above" contract. delta 0 = pin to top; ±1 = one spot up/down in the merged view (unranked models materialize an + * anchored entry too — no more block-on-top ranking). Boundary hits (already #1 / already last) return null (no write). + * Pure: no I/O; returns the next models array (merged-view order, tie-break) + next scores map + new manual index, + * merged position and the anchored score] */ +export interface RankMoveRow { + key: string + tier: string + raw: number | null +} + +export interface RankMoveResult { + models: string[] + scores: Record + /** new index of the key within models */ + index: number + /** new position of the key within the merged view */ + position: number + /** the anchored manual score written for the key */ + score: RankScoreOverride +} + +const SCORE_STEP = 0.001 + +function tierRankOf(tier: string): number { + return tier === "S" ? 3 : tier === "A" ? 2 : tier === "B" ? 1 : 0 +} + +function rawOf(row: RankMoveRow): number { + // comparator treats null raw as -Infinity within the tier; 0 is a safe anchor base for arithmetic + return row.raw ?? 0 +} + +/** Anchored score strictly above `below` and strictly below `above` (null = unbounded top) in (tier, raw) key space */ +function scoreAbove(below: RankMoveRow, above: RankMoveRow | null): RankScoreOverride { + // `above` sorts weakly above `below` in the comparator, so its tier rank is >= — X always takes below's tier + const tier = below.tier as "S" | "A" | "B" | "C" + if (!above || tierRankOf(above.tier) > tierRankOf(below.tier)) { + // different tiers (or no upper bound): stay in the lower tier, step above the neighbor (tier dominance keeps us below `above`) + return { tier, raw: Math.round((rawOf(below) + SCORE_STEP) * 1000) / 1000 } + } + const lo = rawOf(below) + const hi = rawOf(above) + const mid = Math.round(((lo + hi) / 2) * 1000) / 1000 + if (mid > lo && mid < hi) return { tier, raw: mid } + // gap too narrow (or degenerate): step past the lower neighbor; may overshoot exact-tie groups — inherent to score granularity + return { tier, raw: Math.round((lo + SCORE_STEP) * 1000) / 1000 } +} + +/** Anchored score strictly below `above` and strictly above `below` (null = unbounded bottom) */ +function scoreBelow(above: RankMoveRow, below: RankMoveRow | null): RankScoreOverride { + if (!below) return { tier: above.tier as "S" | "A" | "B" | "C", raw: Math.round((rawOf(above) - SCORE_STEP) * 1000) / 1000 } + return scoreAbove(below, above) +} + +export function applyRankMove( + models: readonly string[], + scores: Readonly> | undefined, + rows: readonly RankMoveRow[], + key: string, + delta: -1 | 0 | 1, +): RankMoveResult | null { + const i = rows.findIndex((r) => r.key === key) + if (i < 0) return null + let score: RankScoreOverride + let position: number if (delta === 0) { - if (i >= 0) cur.splice(i, 1) - cur.unshift(key) - return { models: cur, index: 0 } + if (i === 0 && models[0] === key && scores?.[key]) { + // already pinned at #1 with an anchored score: keep the entry but still return it (idempotent write) + score = scores[key]! + } else { + score = scoreAbove(rows[0]!, null) + } + position = 0 + } else if (delta === -1) { + if (i === 0) return null + score = scoreAbove(rows[i - 1]!, rows[i - 2] ?? null) + position = i - 1 + } else { + const next = rows[i + 1] + if (!next) return null + score = scoreBelow(next, rows[i + 2] ?? null) + position = i + 1 } - if (i < 0) return delta === 1 ? { models: [...cur, key], index: cur.length } : null - const target = i + delta - if (target < 0 || target >= cur.length) return null - cur.splice(i, 1) - cur.splice(target, 0, key) - return { models: cur, index: target } + // models array = merged-view order of manual keys (stable tie-break + display); keys absent from the view keep their tail slots + const manualKeys = new Set([...models, key]) + const without = rows.filter((_, idx) => idx !== i).map((r) => r.key) + const reordered = [...without.slice(0, position), key, ...without.slice(position)].filter((k) => manualKeys.has(k)) + const seen = new Set(reordered) + const nextModels = [...reordered, ...models.filter((k) => !seen.has(k))] + const nextScores: Record = { ...(scores ?? {}) } + nextScores[key] = score + return { models: nextModels, scores: nextScores, index: nextModels.indexOf(key), position, score } } // ---- Writes (shared by CLI/TUI; atomic replacement + cache invalidation; empty list = delete key/file back to default) ---- -export function writeCapabilityRank(models: string[]): CapabilityRankFile { - const file = validateCapabilityRank({ models })! +export function writeCapabilityRank(models: string[], scores?: Record): CapabilityRankFile { + const file = validateCapabilityRank({ models, scores })! writeJsonAtomic(paths().capabilityRank, { ...file, updated_at: nowIso() }) mtimeCache.delete(paths().capabilityRank) return file diff --git a/src/version.ts b/src/version.ts index 6b191a4..dbfea85 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,2 +1,2 @@ // Generated at build time: run bun scripts/gen-version.ts -export const PLUGIN_VERSION = "1.0.2" +export const PLUGIN_VERSION = "1.0.3" diff --git a/test/candidates-cli.test.ts b/test/candidates-cli.test.ts new file mode 100644 index 0000000..f67cb41 --- /dev/null +++ b/test/candidates-cli.test.ts @@ -0,0 +1,68 @@ +// [2026-09-10]-[candidate surface widening: /modelRank //poolConfig lists read candidates (full superset) first, falling +// back to the injection-face shells, then the bundled manifest; anchors the read path of shell-superset.json] +import { describe, expect, test, beforeAll, afterAll } from "bun:test" +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs" +import { join } from "node:path" +import { tmpdir } from "node:os" +import { paths } from "../src/state" +import { allModelRows } from "../src/config-cli" + +process.env.SWITCHMAN_STATE = mkdtempSync(join(tmpdir(), "switchman-candcli-")) +const stateDir: string = process.env.SWITCHMAN_STATE +mkdirSync(stateDir, { recursive: true }) + +const entry = (modelId: string, name: string) => ({ + name, pool: "copilot", provider: "github-copilot", modelId, effort: "high", + family: "openai", capability: "rw", vision: false, matrixKey: `github-copilot|${modelId}|high`, +}) + +beforeAll(() => { + // Pin the base-score source so tier/sort assertions do not drift with realtime capability data + writeFileSync(join(stateDir, "capability.json"), JSON.stringify({ version: 1 })) +}) + +describe("allModelRows (candidate source precedence)", () => { + test("candidates (full superset) take precedence over the pruned shells face", () => { + writeFileSync(paths().shellSuperset, JSON.stringify({ + generated_at: new Date().toISOString(), + counts: { superset_models: 3, shells: 1, full_shells: 3 }, + candidates: [entry("m1", "cp-mx-m1-high"), entry("m2", "cp-mx-m2-high"), entry("m3", "cp-mx-m3-high")], + shells: [entry("m1", "cp-mx-m1-high")], + })) + const rows = allModelRows().map((r) => r.modelId).sort() + expect(rows).toEqual(["m1", "m2", "m3"]) + }) + test("no candidates field (older file): falls back to the injection-face shells", () => { + writeFileSync(paths().shellSuperset, JSON.stringify({ + generated_at: new Date().toISOString(), + counts: { superset_models: 2, shells: 1 }, + shells: [entry("m1", "cp-mx-m1-high")], + })) + expect(allModelRows().map((r) => r.modelId)).toEqual(["m1"]) + }) + test("empty candidates array: treated as absent (falls back to shells, not to an empty list)", () => { + writeFileSync(paths().shellSuperset, JSON.stringify({ + generated_at: new Date().toISOString(), + candidates: [], + shells: [entry("m2", "cp-mx-m2-high")], + })) + expect(allModelRows().map((r) => r.modelId)).toEqual(["m2"]) + }) + test("same modelId across provider pools dedupes to one row", () => { + writeFileSync(paths().shellSuperset, JSON.stringify({ + generated_at: new Date().toISOString(), + candidates: [ + entry("m1", "cp-mx-m1-high"), + { ...entry("m1", "glm-mx-m1-high"), pool: "glm", provider: "zhipuai-coding-plan", matrixKey: `zhipuai-coding-plan|m1|high` }, + ], + shells: [entry("m1", "cp-mx-m1-high")], + })) + expect(allModelRows().length).toBe(1) + }) + test("broken superset file: falls back to the bundled manifest (non-empty, no throw)", () => { + writeFileSync(paths().shellSuperset, "{ not json") + expect(allModelRows().length).toBeGreaterThan(0) + }) +}) + +afterAll(() => rmSync(stateDir, { recursive: true, force: true })) diff --git a/test/rank-move.test.ts b/test/rank-move.test.ts index 891ca53..93cb565 100644 --- a/test/rank-move.test.ts +++ b/test/rank-move.test.ts @@ -1,6 +1,8 @@ -// [2026-09-06]-[applyRankMove unit contract: shared reorder semantics for the /modelRank dialog (enter actions + -// ctrl+up/ctrl+down hotkeys). Pure function — no filesystem, but the module chain expects a state dir to exist] -// Sandbox: SWITCHMAN_STATE points to a temp dir (same fixture pattern as user-overrides.test.ts) +// [2026-09-10]-[applyRankMove unit contract: interleaved move semantics for the /modelRank dialog (enter actions + +// ctrl+up/ctrl+down hotkeys) — moves act on the merged view and anchor a manual (tier, raw) score between the +// moved model's new neighbors, with decimal steps breaking ties. Pure function — no filesystem, but the module +// chain expects a state dir to exist. Sandbox: SWITCHMAN_STATE points to a temp dir (same fixture pattern as +// user-overrides.test.ts)] import { describe, test, expect, beforeAll } from "bun:test" import { mkdtempSync, mkdirSync } from "node:fs" import { tmpdir } from "node:os" @@ -9,32 +11,86 @@ import { join } from "node:path" process.env.SWITCHMAN_STATE = mkdtempSync(join(tmpdir(), "switchman-rm-")) mkdirSync(process.env.SWITCHMAN_STATE, { recursive: true }) -import { applyRankMove } from "../src/user-overrides" +import { applyRankMove, type RankMoveRow } from "../src/user-overrides" -describe("applyRankMove (manual ranking reorder shared by dialog actions and hotkeys)", () => { - const list = ["a", "b", "c"] +// merged-view fixture (already sorted strongest-first); m3 is manual with an anchored score +const rows: RankMoveRow[] = [ + { key: "m1", tier: "S", raw: 90 }, + { key: "m2", tier: "S", raw: 80 }, + { key: "m3", tier: "A", raw: 95 }, + { key: "m4", tier: "A", raw: 60 }, + { key: "m5", tier: "B", raw: 40 }, +] +const models = ["m3"] +const scores = { m3: { tier: "A" as const, raw: 95 } } - test("±1 swaps with the neighbor and reports the new index", () => { - expect(applyRankMove(list, "b", -1)).toEqual({ models: ["b", "a", "c"], index: 0 }) - expect(applyRankMove(list, "b", 1)).toEqual({ models: ["a", "c", "b"], index: 2 }) +describe("applyRankMove (interleaved move on the merged view)", () => { + test("move up across a tier boundary: stay in the lower tier, step above the neighbor", () => { + // m4 (A,60) up between m3 (A,95) and m2 (S,80): different tiers → A/95.001 (tier dominance keeps it below m2) + const res = applyRankMove(models, scores, rows, "m4", -1)! + expect(res.score).toEqual({ tier: "A", raw: 95.001 }) + expect(res.position).toBe(2) }) - test("boundary hits are null no-ops (no write)", () => { - expect(applyRankMove(list, "a", -1)).toBeNull() - expect(applyRankMove(list, "c", 1)).toBeNull() + test("move up within one tier: midpoint between the neighbors", () => { + // m5 (B,40) up between m4 (A,60) and m3 (A,95): same tier → midpoint A/77.5 + const res = applyRankMove(models, scores, rows, "m5", -1)! + expect(res.score).toEqual({ tier: "A", raw: 77.5 }) + }) + + test("move down across a tier boundary: take the lower neighbor's tier, step above it", () => { + // m3 (A,95) down between m4 (A,60) and m5 (B,40) → B/40.001 (below every A-tier, above m5) + const res = applyRankMove(models, scores, rows, "m3", 1)! + expect(res.score).toEqual({ tier: "B", raw: 40.001 }) + expect(res.position).toBe(3) + }) + + test("unranked models move both ways now (up materializes an anchored entry; down works too)", () => { + const up = applyRankMove(models, scores, rows, "m2", -1)! + // m2 (S,80) above m1 (S,90), no upper bound → S/90.001 + expect(up.score).toEqual({ tier: "S", raw: 90.001 }) + expect(up.models).toContain("m2") + const down = applyRankMove(models, scores, rows, "m2", 1)! + // between m3 (A,95) and m4 (A,60): midpoint A/77.5 + expect(down.score).toEqual({ tier: "A", raw: 77.5 }) }) - test("pin to top: ranked and unranked both land at index 0", () => { - expect(applyRankMove(list, "c", 0)).toEqual({ models: ["c", "a", "b"], index: 0 }) - expect(applyRankMove(list, "z", 0)).toEqual({ models: ["z", "a", "b", "c"], index: 0 }) + test("tie groups: same (tier, raw) neighbors cannot be split — step past the group", () => { + const tied: RankMoveRow[] = [ + { key: "t1", tier: "A", raw: 60 }, + { key: "t2", tier: "A", raw: 60 }, + { key: "x", tier: "B", raw: 10 }, + ] + const res = applyRankMove([], undefined, tied, "x", -1)! + expect(res.score).toEqual({ tier: "A", raw: 60.001 }) + expect(res.position).toBe(1) // insertion slot; the anchored raw (60.001 > t1) re-sorts it above the tied pair + }) + + test("pin to top: steps above row #1 (ranked or unranked)", () => { + const res = applyRankMove(models, scores, rows, "m5", 0)! + expect(res.score).toEqual({ tier: "S", raw: 90.001 }) + expect(res.position).toBe(0) + expect(res.models[0]).toBe("m5") + expect(res.scores.m5).toEqual({ tier: "S", raw: 90.001 }) + expect(res.scores.m3).toEqual({ tier: "A", raw: 95 }) // other anchored scores survive + }) + + test("boundary hits are null no-ops (no write)", () => { + expect(applyRankMove(models, scores, rows, "m1", -1)).toBeNull() + expect(applyRankMove(models, scores, rows, "m5", 1)).toBeNull() + expect(applyRankMove(models, scores, rows, "missing", -1)).toBeNull() }) - test("unranked: up appends at the end, down is a no-op (hotkey hint path)", () => { - expect(applyRankMove(list, "z", 1)).toEqual({ models: ["a", "b", "c", "z"], index: 3 }) - expect(applyRankMove(list, "z", -1)).toBeNull() + test("models array is rebuilt in merged-view order; keys absent from the view keep their tail slots", () => { + const res = applyRankMove(["dead", ...models], scores, rows, "m2", -1)! + // m2 moves to #0: manual keys in merged order = m2, m3; dead key not in the view → appended + expect(res.models).toEqual(["m2", "m3", "dead"]) + expect(res.index).toBe(0) }) - test("input list is not mutated", () => { - expect(list).toEqual(["a", "b", "c"]) + test("input arrays are not mutated", () => { + expect(models).toEqual(["m3"]) + expect(scores.m3).toEqual({ tier: "A", raw: 95 }) + expect(rows.map((r) => r.key)).toEqual(["m1", "m2", "m3", "m4", "m5"]) }) }) diff --git a/test/shells-select.test.ts b/test/shells-select.test.ts index 5b69d55..ea6802b 100644 --- a/test/shells-select.test.ts +++ b/test/shells-select.test.ts @@ -62,3 +62,28 @@ describe("selectInjectableDefs (available-model force-keep)", () => { expect(kept).not.toContain("cp-mx-m3-high-ro") }) }) + +// [2026-09-10]-[pool-config force-keep: /modelRank //poolConfig candidates widen to the full superset, so selections may +// name models outside favorites; their shells must stay injectable (runtime chains still apply pool ∩ activation)] +describe("selectInjectableDefs (pool-config keepModelIds)", () => { + test("keepModelIds force-keeps every face of a pool-configured model that lost chain competition and missed keepModels", () => { + // m3-high survives via economy; m3-high-ro is dropped by chain competition — bare normalized id must resurrect it + const kept = selectInjectableDefs(defs, { ...opts, keepModelIds: new Set(["m3"]) }).map((d) => d.name).sort() + expect(kept).toContain("cp-mx-m3-high") + expect(kept).toContain("cp-mx-m3-high-ro") + expect(kept).not.toContain("cp-mx-m2-high-ro") + }) + test("keepModelIds keys are bare normalized modelIds (no provider prefix), unlike keepModels full keys", () => { + // provider-qualified key in keepModelIds matches nothing — proves the two key spaces stay distinct + const kept = selectInjectableDefs(defs, { ...opts, keepModelIds: new Set(["github-copilot/m3"]) }).map((d) => d.name).sort() + expect(kept).toEqual(selectInjectableDefs(defs, opts).map((d) => d.name).sort()) + }) + test("keepModelIds ∪ keepModels union: both force-keep paths compose", () => { + const kept = selectInjectableDefs(defs, { + ...opts, + keepModels: new Set(["github-copilot/m2"]), + keepModelIds: new Set(["m3"]), + }).map((d) => d.name).sort() + for (const name of ["cp-mx-m2-high", "cp-mx-m2-high-ro", "cp-mx-m3-high", "cp-mx-m3-high-ro"]) expect(kept).toContain(name) + }) +}) diff --git a/test/user-overrides.test.ts b/test/user-overrides.test.ts index 80bd443..71e18aa 100644 --- a/test/user-overrides.test.ts +++ b/test/user-overrides.test.ts @@ -112,6 +112,54 @@ describe("capability-rank.json (manual capability ranking)", () => { }) }) +// [2026-09-10]-[anchored scores side-map: interleaved manual ranking — per-entry (tier, raw) overrides beat the ladder, +// survive the write/read round-trip, cover prefix variants, and drop malformed entries fail-open] +describe("capability-rank.json anchored scores (interleaved ranking)", () => { + test("write/read round-trip preserves the scores side-map", () => { + writeCapabilityRank(["glm-5.3", "gpt-5.6"], { "glm-5.3": { tier: "A", raw: 57.2549 } }) + const loaded = loadCapabilityRank() + expect(loaded?.models).toEqual(["glm-5.3", "gpt-5.6"]) + expect(loaded?.scores?.["glm-5.3"]).toEqual({ tier: "A", raw: 57.255 }) // 3-decimal rounding on validate + }) + + test("manualRankResult: an anchored entry overrides the ladder (tier + rawScore from the stored pair)", () => { + writeCapabilityRank(["glm-5.3", "gpt-5.6"], { "glm-5.3": { tier: "B", raw: 42 } }) + const hit = baseScoreDynamic("glm-5.3") + expect(hit.source).toBe("manual") + expect(hit.tier).toBe("B") + expect(hit.rawScore).toBe(42) + expect(hit.score).toBe(0.7) + // ladder fallback for the entry without an anchored score (2-item list runner-up = A/0) + const ladder = baseScoreDynamic("gpt-5.6") + expect(ladder.tier).toBe("A") + expect(ladder.rawScore).toBe(0) + }) + + test("anchored entries cover prefix variants (exact entry key wins)", () => { + writeCapabilityRank(["gpt-5.6"], { "gpt-5.6": { tier: "B", raw: 44.5 } }) + const hit = manualRankResult("gpt-5.6-luna") + expect(hit?.source).toBe("manual") + expect(hit?.matchedAs).toBe("gpt-5.6") + expect(hit?.tier).toBe("B") + expect(hit?.rawScore).toBe(44.5) + }) + + test("validation drops malformed score entries (bad tier, non-finite raw, unknown key) fail-open", () => { + const v = validateCapabilityRank({ + models: ["a", "b"], + scores: { a: { tier: "X", raw: 50 }, b: { tier: "A", raw: Number.NaN }, ghost: { tier: "S", raw: 90 } }, + }) + expect(v?.scores).toBeUndefined() + }) + + test("after clear everything falls back again", () => { + writeCapabilityRank(["glm-5.3"], { "glm-5.3": { tier: "S", raw: 99 } }) + clearCapabilityRank() + expect(loadCapabilityRank()).toBeNull() + expect(baseScoreDynamic("glm-5.3").source).toBe("exact") + }) +}) + // ---- Task-pool selection (lane keys: economy/mechanical/main/hard/vision/review; the same model may repeat across pools) ---- describe("pool-config.json (task-pool selection)", () => {