Skip to content

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

address review (claude): rank full tier namespace by real cost in suggestModelTier

[._.]/ Adam Eivy163250374d6a
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 86cef5b3cb..6487dce46f 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. 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. (`server/services/taskLearning/routing.js`)
## Local model stack
diff --git a/server/services/taskLearning.test.js b/server/services/taskLearning.test.js
index 632fd191a8..5bbae9fba1 100644
--- a/server/services/taskLearning.test.js
+++ b/server/services/taskLearning.test.js
@@ -612,6 +612,61 @@ describe('TaskLearning - suggestModelTier with routing signals', () => {
expect(result.suggested).toBe('light');
expect(result.reason).toMatch(/lightest of 2 proven tiers/);
});
+
+ it('should rank thinking-level tier names by real cost, not as default-weight', async () => {
+ // routingAccuracy keys can be thinking-level names (minimal/low/high/xhigh),
+ // not just light/default/medium/heavy. xhigh (opus) is the heaviest; minimal
+ // (local-small) is the cheapest. Both clear 80%, so we must pick minimal —
+ // an earlier bug ranked every unmapped name at default-weight, which would
+ // have let xhigh win the tie.
+ const data = makeLearningData({
+ byTaskType: {
+ 'reason-task': {
+ completed: 20, succeeded: 18, failed: 2,
+ totalDurationMs: 2000000, avgDurationMs: 100000,
+ successRate: 90
+ }
+ },
+ routingAccuracy: {
+ 'reason-task': {
+ minimal: { succeeded: 9, failed: 2, lastAttempt: '2026-01-26T00:00:00.000Z' }, // 82%
+ xhigh: { succeeded: 10, failed: 1, lastAttempt: '2026-01-25T00:00:00.000Z' } // 91%
+ }
+ }
+ });
+ readFile.mockResolvedValue(JSON.stringify(data));
+
+ const result = await suggestModelTier('reason-task');
+
+ expect(result).not.toBeNull();
+ expect(result.suggested).toBe('minimal');
+ });
+
+ it('should not prefer an unknown tier name as the cheapest', async () => {
+ // user-specified (and any future/unknown tier name) must rank as heaviest,
+ // so it never wins the "lightest proven tier" tie over a known-light tier.
+ const data = makeLearningData({
+ byTaskType: {
+ 'pref-task': {
+ completed: 20, succeeded: 18, failed: 2,
+ totalDurationMs: 2000000, avgDurationMs: 100000,
+ successRate: 90
+ }
+ },
+ routingAccuracy: {
+ 'pref-task': {
+ 'user-specified': { succeeded: 10, failed: 1, lastAttempt: '2026-01-26T00:00:00.000Z' }, // 91%
+ light: { succeeded: 9, failed: 2, lastAttempt: '2026-01-25T00:00:00.000Z' } // 82%
+ }
+ }
+ });
+ readFile.mockResolvedValue(JSON.stringify(data));
+
+ const result = await suggestModelTier('pref-task');
+
+ expect(result).not.toBeNull();
+ expect(result.suggested).toBe('light');
+ });
});
describe('TaskLearning - recalculateModelTierMetrics', () => {
diff --git a/server/services/taskLearning/routing.js b/server/services/taskLearning/routing.js
index 4c63a6ad40..034d0072ca 100644
--- a/server/services/taskLearning/routing.js
+++ b/server/services/taskLearning/routing.js
@@ -13,13 +13,34 @@ import { loadLearningData, emitLog } from './store.js';
import { resetTaskTypeLearning } from './metrics.js';
/**
- * Relative resource cost of each model tier, lightest → heaviest.
- * Used to break ties between tiers that both clear the high-success
- * threshold: prefer the cheapest tier that still works well.
- * (Mirrors the tier ordering in agentModelSelection.js.)
+ * Relative resource cost of each tier name that can land in
+ * `routingAccuracy`, lightest → heaviest. Used to break ties between tiers
+ * that both clear the high-success threshold: prefer the cheapest tier that
+ * still works well.
+ *
+ * The keys here are whatever `selectModelForTask` records as `tier`
+ * (agentModelSelection.js → agentLifecycle.js `modelTier`), which is a mixed
+ * namespace: the literal tiers (`light`/`default`/`medium`/`heavy`) AND the
+ * thinking-level names from thinkingLevels.js (`off`/`minimal`/`low`/`medium`/
+ * `high`/`xhigh`, where `minimal`/`low` are local-preferred and therefore the
+ * cheapest, and `high`/`xhigh` map to provider-heavy/opus). An unknown name
+ * (e.g. `user-specified`, or a future tier) must NOT be treated as cheap, or
+ * the "prefer lightest" logic would pick it over a known-light tier — so the
+ * fallback ranks unknowns as heaviest.
*/
-const TIER_WEIGHT = { light: 0, default: 1, medium: 2, heavy: 3 };
-const tierWeight = (tier) => TIER_WEIGHT[tier] ?? TIER_WEIGHT.default;
+const TIER_WEIGHT = {
+ minimal: 0, // local-small (thinking level)
+ low: 1, // local-medium (thinking level)
+ light: 2, // provider light model (e.g. haiku)
+ off: 3, // no extended thinking → provider default
+ default: 3, // provider default model
+ medium: 4, // provider default + standard cloud thinking
+ high: 5, // provider-heavy (thinking level)
+ heavy: 6, // provider heavy model
+ xhigh: 7 // opus / heaviest (thinking level)
+};
+const HEAVIEST_WEIGHT = Math.max(...Object.values(TIER_WEIGHT)) + 1;
+const tierWeight = (tier) => TIER_WEIGHT[tier] ?? HEAVIEST_WEIGHT;
/** Minimum success rate (%) for a tier to count as "proven" for a task type. */
const HIGH_SUCCESS_THRESHOLD = 80;