Skip to content
This repository was archived by the owner on Oct 9, 2026. It is now read-only.

Commit b30188a

Browse files
committed
feat(session): confidence-gated model tier routing behind optional jev config
Add an opt-in decision router that can serve a turn on a cheaper model tier when a classifier is confident the request is easy, and falls back to the resolved model on every failure mode. - session/jev.ts: Layer-0 deterministic availability filter (time window, context ceiling, quota, rate cap) runs first and resolves most turns for free; the remaining ambiguous residue goes to a TypeSafe System One call (two questions, one call: tier choice + complexity score). Three independent gates (tier confidence, complexity score, complexity confidence) must pass to route down; unknown complexity defaults to hard. A verify-and-escalate pass can re-run a finished turn from its original state on the next stronger tier when the cheap output is confidently inadequate, bounded by maxEscalationsPerTurn. Engines never throw: the chain ends in a terminal off-engine, and any failure keeps the resolved model. - config: new optional jev block (enabled: false by default, tiers with free-text capability descriptions, pool-specific thresholds) declared on both the V1 and V2 config schemas with a V1->V2 migrate passthrough. Credentials resolve from TYPESAFE_API_KEY, the auth.json typesafe entry, or a custom provider options.apiKey. - session/prompt: route the user-message model at creation when not explicitly chosen, re-resolve per step after the first, and hold per-turn escalation state in the run loop. Escalation discards the inadequate attempt and re-runs from the original state; tool side effects from earlier steps are retained. - mock-first tests cover the availability filter, gate logic, fail-open behavior, escalation cap and the never-throw engine contract without a key or network. Refers to anomalyco#34370
1 parent b471c2b commit b30188a

7 files changed

Lines changed: 1345 additions & 2 deletions

File tree

‎packages/core/src/config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { ConfigToolOutput } from "./config/tool-output"
2525
import { ConfigWatcher } from "./config/watcher"
2626
import { ConfigV1 } from "./v1/config/config"
2727
import { ConfigMigrateV1 } from "./v1/config/migrate"
28+
import { ConfigJevV1 } from "./v1/config/jev"
2829

2930
export class Info extends Schema.Class<Info>("Config.Info")({
3031
$schema: Schema.optional(Schema.String).annotate({
@@ -39,6 +40,9 @@ export class Info extends Schema.Class<Info>("Config.Info")({
3940
default_agent: Schema.String.pipe(Schema.optional).annotate({
4041
description: "Default primary agent to use when no session agent is selected",
4142
}),
43+
jev: ConfigJevV1.Info.pipe(Schema.optional).annotate({
44+
description: "Jev decision routing — confidence-gated model tier routing (default: disabled)",
45+
}),
4246
autoupdate: Schema.Union([Schema.Boolean, Schema.Literal("notify")])
4347
.pipe(Schema.optional)
4448
.annotate({

‎packages/core/src/v1/config/config.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ConfigAgentV1 } from "./agent"
88
import { ConfigAttachmentV1 } from "./attachment"
99
import { ConfigCommandV1 } from "./command"
1010
import { ConfigFormatterV1 } from "./formatter"
11+
import { ConfigJevV1 } from "./jev"
1112
import { ConfigLayoutV1 } from "./layout"
1213
import { ConfigLSPV1 } from "./lsp"
1314
import { ConfigMCPV1 } from "./mcp"
@@ -77,6 +78,10 @@ export const Info = Schema.Struct({
7778
small_model: Schema.optional(Schema.String).annotate({
7879
description: "Small model to use for tasks like title generation in the format of provider/model",
7980
}),
81+
jev: Schema.optional(ConfigJevV1.Info).annotate({
82+
description:
83+
"Jev decision routing — confidence-gated model tier routing (default: disabled). Credentials resolve from TYPESAFE_API_KEY, the auth.json `typesafe` entry, or a custom `provider.typesafe` options.apiKey.",
84+
}),
8085
default_agent: Schema.optional(Schema.String).annotate({
8186
description:
8287
"Default agent to use when none is specified. Must be a primary agent. Falls back to 'build' if not set or if the specified agent is invalid.",

‎packages/core/src/v1/config/jev.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
export * as ConfigJevV1 from "./jev"
2+
3+
import { Schema } from "effect"
4+
5+
const Hour = Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 23 }))
6+
const UnitInterval = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }))
7+
8+
export const Tier = Schema.Struct({
9+
id: Schema.NonEmptyString.annotate({ description: "Stable tier identifier" }),
10+
model: Schema.String.annotate({ description: "Model this tier maps to, in the format provider/model" }),
11+
capability: Schema.NonEmptyString.annotate({
12+
description:
13+
"Free-text eligibility test describing what this tier handles well (never a model name). This is how the router learns your pool.",
14+
}),
15+
costHintUsdPerMTokOut: Schema.optional(Schema.Number).annotate({
16+
description: "Reporting only; the router never optimizes cost",
17+
}),
18+
window: Schema.optional(
19+
Schema.Struct({
20+
startHour: Hour,
21+
endHour: Hour,
22+
utcOffsetMinutes: Schema.Int,
23+
}),
24+
).annotate({
25+
description: "Tier is only eligible inside this local-time window; startHour may be > endHour to wrap midnight",
26+
}),
27+
maxContextTokens: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))).annotate({
28+
description: "Requests above this context size are ineligible",
29+
}),
30+
quota: Schema.optional(Schema.Struct({ used: Schema.Number, limit: Schema.Number })).annotate({
31+
description: "Budget for the current period; omit for unmetered tiers",
32+
}),
33+
rate: Schema.optional(Schema.Struct({ used: Schema.Number, limit: Schema.Number })).annotate({
34+
description: "Rolling request cap",
35+
}),
36+
})
37+
export type Tier = Schema.Schema.Type<typeof Tier>
38+
39+
export const Info = Schema.Struct({
40+
enabled: Schema.optional(Schema.Boolean).annotate({ description: "Enable Jev decision routing (default: false)" }),
41+
tiers: Schema.optional(Schema.mutable(Schema.Array(Tier))).annotate({
42+
description:
43+
"Model tiers ordered cheapest -> most capable. Order is load-bearing: the last tier is the fail-open default. Define the pool so its most capable tier matches your default model.",
44+
}),
45+
timeoutMs: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 100, maximum: 10_000 }))).annotate({
46+
description: "Hard timeout for a Jev call (default: 1500ms); on timeout the router fails open",
47+
}),
48+
thresholds: Schema.optional(
49+
Schema.Struct({
50+
minConfidenceToDegrade: Schema.optional(UnitInterval).annotate({
51+
description:
52+
"Confidence required to route DOWN to a cheaper tier (default: 0.85; flat-capability pools may use 0.55-0.65)",
53+
}),
54+
maxComplexityForDegrade: Schema.optional(UnitInterval).annotate({
55+
description: "Complexity score at or below which degrading is allowed (default: 0.5)",
56+
}),
57+
minComplexityConfidence: Schema.optional(UnitInterval).annotate({
58+
description: "Minimum confidence in the complexity score to degrade (default: 0.5)",
59+
}),
60+
allowVerify: Schema.optional(Schema.Boolean).annotate({
61+
description: "Enable post-hoc adequacy verification and escalation (default: true)",
62+
}),
63+
minConfidenceToEscalate: Schema.optional(UnitInterval).annotate({
64+
description: "Confidence required to act on an 'inadequate' verdict (default: 0.7)",
65+
}),
66+
maxEscalationsPerTurn: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 10 }))).annotate({
67+
description: "Re-runs allowed per turn after a failed verification (default: 1)",
68+
}),
69+
}),
70+
).annotate({ description: "Routing thresholds — pool-specific, tune per pool shape" }),
71+
})
72+
export type Info = Schema.Schema.Type<typeof Info>

‎packages/core/src/v1/config/migrate.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function migrate(info: typeof ConfigV1.Info.Type) {
3838
shell: info.shell,
3939
model: info.model,
4040
default_agent: info.default_agent,
41+
jev: info.jev,
4142
autoupdate: info.autoupdate,
4243
share: info.share ?? (info.autoshare ? "auto" : undefined),
4344
enterprise: info.enterprise,

0 commit comments

Comments
 (0)