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 (codex): don't suggest non-routable local-preferred tiers

[._.]/ Adam Eivya82b5e3953f6
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index a5305bd023..873f2b73d5 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. 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`)
+- **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 (`high`/`xhigh`/`off`) 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. Local-preferred thinking levels (`minimal`/`low`) are excluded from suggestion candidates because the learning→selection path can't switch the active provider to a local one — so the recommender never suggests a tier nothing can run, and the lightest tier the runtime *can* run (e.g. `light`) wins instead of falling through to default. (`server/services/taskLearning/routing.js`, `server/services/agentModelSelection.js`)
## Local model stack
diff --git a/server/services/taskLearning.test.js b/server/services/taskLearning.test.js
index 5bbae9fba1..3aa6cca861 100644
--- a/server/services/taskLearning.test.js
+++ b/server/services/taskLearning.test.js
@@ -613,12 +613,11 @@ describe('TaskLearning - suggestModelTier with routing signals', () => {
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.
+ it('should rank routable thinking-level tier names by real cost, not as default-weight', async () => {
+ // routingAccuracy keys can be thinking-level names (high/xhigh), not just
+ // light/default/medium/heavy. xhigh (opus) is heavier than high (provider-
+ // heavy). Both clear 80%, so we must pick high — an earlier bug ranked every
+ // unmapped name at default-weight, which would have let xhigh win the tie.
const data = makeLearningData({
byTaskType: {
'reason-task': {
@@ -629,8 +628,8 @@ describe('TaskLearning - suggestModelTier with routing signals', () => {
},
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%
+ high: { 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%
}
}
});
@@ -639,7 +638,35 @@ describe('TaskLearning - suggestModelTier with routing signals', () => {
const result = await suggestModelTier('reason-task');
expect(result).not.toBeNull();
- expect(result.suggested).toBe('minimal');
+ expect(result.suggested).toBe('high');
+ });
+
+ it('should not suggest a non-routable local-preferred tier (minimal/low) — picks the lightest tier the runtime can run', async () => {
+ // minimal/low resolve to a local model, but the learning→selection path
+ // doesn't switch providers for them, so suggesting minimal would make the
+ // selector fall through to the provider default. Even though minimal is the
+ // cheapest by cost, it must be excluded so the proven `light` tier wins.
+ const data = makeLearningData({
+ byTaskType: {
+ 'local-task': {
+ completed: 20, succeeded: 18, failed: 2,
+ totalDurationMs: 2000000, avgDurationMs: 100000,
+ successRate: 90
+ }
+ },
+ routingAccuracy: {
+ 'local-task': {
+ minimal: { 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('local-task');
+
+ expect(result).not.toBeNull();
+ expect(result.suggested).toBe('light');
});
it('should not prefer an unknown tier name as the cheapest', async () => {
diff --git a/server/services/taskLearning/routing.js b/server/services/taskLearning/routing.js
index 034d0072ca..8538b56fbe 100644
--- a/server/services/taskLearning/routing.js
+++ b/server/services/taskLearning/routing.js
@@ -42,6 +42,19 @@ const TIER_WEIGHT = {
const HEAVIEST_WEIGHT = Math.max(...Object.values(TIER_WEIGHT)) + 1;
const tierWeight = (tier) => TIER_WEIGHT[tier] ?? HEAVIEST_WEIGHT;
+/**
+ * Tier names the learning→selection path can't actually route to. The
+ * local-preferred thinking levels (`minimal`/`low`) resolve to a local model
+ * (LM Studio/Ollama), but `selectModelForTask` doesn't switch the active
+ * provider for them (the `localPreferred` flag is unwired) — so a task can't
+ * be routed there from a learned suggestion. Excluding them from suggestion
+ * candidates is what keeps "prefer the lightest tier" honest: recommending a
+ * tier nothing can run would make the selector fall through to the provider
+ * default instead of the lightest tier it CAN run (e.g. `light`). They stay in
+ * TIER_WEIGHT (the cost ordering is still accurate) — they're just not offered.
+ */
+const NON_ROUTABLE_LEARNED_TIERS = new Set(['minimal', 'low']);
+
/** Minimum success rate (%) for a tier to count as "proven" for a task type. */
const HIGH_SUCCESS_THRESHOLD = 80;
@@ -85,7 +98,7 @@ export async function suggestModelTier(taskType) {
if (routingData) {
// Find tiers with enough data and their success rates
const tierResults = Object.entries(routingData)
- .filter(([, r]) => (r.succeeded + r.failed) >= 3)
+ .filter(([tier, r]) => (r.succeeded + r.failed) >= 3 && !NON_ROUTABLE_LEARNED_TIERS.has(tier))
.map(([tier, r]) => {
const total = r.succeeded + r.failed;
return { tier, successRate: Math.round((r.succeeded / total) * 100), total };