Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): resolve learned thinking-level tiers in the model selector
[._.]/ Adam Eivy5e2dc0f3946f
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 6487dce46f..a5305bd023 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -2,7 +2,7 @@## CoS / task learning-- **Learning-based model routing now prefers the lightest tier that succeeds, instead of the marginally-highest-success tier.** `suggestModelTier` sorted candidate tiers by success rate and returned the top one, so a `heavy` tier at 85% would beat a `light` tier at 82% — silently over-allocating compute for the same outcome, despite an inline comment that said "if a lighter tier has high success, no need to upgrade." It now picks the **lightest** tier among all that clear the high-success threshold (≥80%), realizing that intent and saving the heavier model for task types that actually need it. The cost-ranking covers the full tier namespace that actually lands in `routingAccuracy` — not just `light`/`default`/`medium`/`heavy` but the thinking-level names (`minimal`/`low`/`high`/`xhigh`) recorded by `selectModelForTask`, with `minimal`/`low` (local models) cheapest and `xhigh` (opus) heaviest; an unknown/future tier name (e.g. `user-specified`) ranks as heaviest so it can never masquerade as the cheapest option. Failing-tier avoidance and the insufficient-data short-circuit are unchanged. (`server/services/taskLearning/routing.js`)+- **Learning-based model routing now prefers the lightest tier that succeeds, instead of the marginally-highest-success tier.** `suggestModelTier` sorted candidate tiers by success rate and returned the top one, so a `heavy` tier at 85% would beat a `light` tier at 82% — silently over-allocating compute for the same outcome, despite an inline comment that said "if a lighter tier has high success, no need to upgrade." It now picks the **lightest** tier among all that clear the high-success threshold (≥80%), realizing that intent and saving the heavier model for task types that actually need it. The cost-ranking covers the full tier namespace that actually lands in `routingAccuracy` — not just `light`/`default`/`medium`/`heavy` but the thinking-level names (`minimal`/`low`/`high`/`xhigh`) recorded by `selectModelForTask`, with `minimal`/`low` (local models) cheapest and `xhigh` (opus) heaviest; an unknown/future tier name (e.g. `user-specified`) ranks as heaviest so it can never masquerade as the cheapest option. Failing-tier avoidance and the insufficient-data short-circuit are unchanged. The model selector now resolves a learned thinking-level suggestion (`minimal`/`low`/`high`/`xhigh`) through `getModelForLevel` instead of only its four-entry static map, so a proven thinking-level tier is actually used rather than silently falling through to the provider default. (`server/services/taskLearning/routing.js`, `server/services/agentModelSelection.js`)## Local model stackdiff --git a/server/services/agentModelSelection.js b/server/services/agentModelSelection.jsindex d3f0550b16..c9b93a2f4c 100644--- a/server/services/agentModelSelection.js+++ b/server/services/agentModelSelection.js@@ -122,14 +122,22 @@ export async function selectModelForTask(task, provider, agent = {}) {light: provider.lightModel};+ // The learning store also records thinking-level tier names (minimal/low/+ // high/xhigh) that aren't in tierToModel — resolve those through+ // getModelForLevel so a proven thinking-level suggestion is honored+ // instead of silently falling through to the provider default.+ const resolveTierModel = (tier) => tierToModel[tier] ?? getModelForLevel(tier, provider);+// If we have a specific tier suggestion, use it- if (suggested && tierToModel[suggested]) {+ const suggestedModel = suggested ? resolveTierModel(suggested) : null;+ if (suggestedModel) {console.log(`📊 Learning-based selection: ${taskTypeKey} → ${suggested} (${learningReason})`);return {- model: tierToModel[suggested],+ model: suggestedModel,tier: suggested,reason: 'learning-suggested',learningReason,+ localPreferred: isLocalPreferred(suggested),avoidedTiers: avoidTiers.length > 0 ? avoidTiers : undefined};}diff --git a/server/services/agentModelSelection.test.js b/server/services/agentModelSelection.test.jsnew file mode 100644index 0000000000..a907855663--- /dev/null+++ b/server/services/agentModelSelection.test.js@@ -0,0 +1,61 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';++// Mock the learning store so we control what suggestModelTier returns; keep+// thinkingLevels real so getModelForLevel/isLocalPreferred resolution is exercised.+vi.mock('./taskLearning.js', () => ({+ suggestModelTier: vi.fn()+}));++import { selectModelForTask } from './agentModelSelection.js';+import { suggestModelTier } from './taskLearning.js';++const PROVIDER = {+ defaultModel: 'default-model',+ mediumModel: 'medium-model',+ heavyModel: 'heavy-model',+ lightModel: 'light-model'+};++// A description that matches none of the heuristic branches (image/critical/+// complex/long-context/documentation), with no priority or thinking metadata+// so resolveThinkingLevel resolves "from default" and selection falls through+// to the learning path rather than the thinking-level early return.+const benignTask = { description: 'organize the weekly digest', taskType: 'user' };++describe('selectModelForTask — learning-suggested tier resolution', () => {+ beforeEach(() => vi.clearAllMocks());++ it('honors a literal-tier suggestion via the static map', async () => {+ suggestModelTier.mockResolvedValue({ suggested: 'light', reason: 'r' });+ const result = await selectModelForTask(benignTask, PROVIDER);+ expect(result.model).toBe('light-model');+ expect(result.tier).toBe('light');+ expect(result.reason).toBe('learning-suggested');+ });++ it('resolves a thinking-level suggestion (high) through getModelForLevel instead of dropping to default', async () => {+ suggestModelTier.mockResolvedValue({ suggested: 'high', reason: 'r' });+ const result = await selectModelForTask(benignTask, PROVIDER);+ // high → provider-heavy+ expect(result.model).toBe('heavy-model');+ expect(result.tier).toBe('high');+ expect(result.reason).toBe('learning-suggested');+ });++ it('resolves a local-preferred thinking-level suggestion (minimal) and flags localPreferred', async () => {+ suggestModelTier.mockResolvedValue({ suggested: 'minimal', reason: 'r' });+ const result = await selectModelForTask(benignTask, PROVIDER);+ expect(result.model).toBe('lmstudio');+ expect(result.tier).toBe('minimal');+ expect(result.localPreferred).toBe(true);+ expect(result.reason).toBe('learning-suggested');+ });++ it('falls through to default when the suggested tier resolves to no model', async () => {+ // user-specified is not a thinking level → getModelForLevel returns null, no static map entry.+ suggestModelTier.mockResolvedValue({ suggested: 'user-specified', reason: 'r' });+ const result = await selectModelForTask(benignTask, PROVIDER);+ expect(result.tier).toBe('default');+ expect(result.reason).toBe('standard-task');+ });+});