-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Feat/model scheduler probe routing #2434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
calvinxyan
wants to merge
7
commits into
code-yeongyu:dev
Choose a base branch
from
calvinxyan:feat/model-scheduler-probe-routing
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,925
−37
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
88e64f7
feat(shared): read user-configured provider models
397adca
feat(shared): filter provider cache by configured models
4b3e2cf
feat(config): add model scheduler configuration schema
4b3e33d
feat(config): export model scheduler config types
1716ba5
feat(model-scheduler): add probe-based health routing
12c00aa
feat(hooks): run model scheduler during session startup
9254706
chore(lockfile): sync optional package versions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { ZodError } from "zod/v4" | ||
| import { ModelSchedulerConfigSchema } from "./model-scheduler" | ||
|
|
||
| describe("ModelSchedulerConfigSchema", () => { | ||
| test("parses valid scheduler config", () => { | ||
| const result = ModelSchedulerConfigSchema.parse({ | ||
| enabled: true, | ||
| interval_minutes: 60, | ||
| mode: "active", | ||
| preflight_on_session_created: true, | ||
| failure_threshold: 2, | ||
| recovery_threshold: 2, | ||
| agent_cooldown_minutes: 180, | ||
| protect_manual_routing: true, | ||
| probe_enabled: true, | ||
| probe_timeout_ms: 15000, | ||
| probe_max_latency_ms: 8000, | ||
| }) | ||
|
|
||
| expect(result.mode).toBe("active") | ||
| expect(result.interval_minutes).toBe(60) | ||
| expect(result.probe_enabled).toBe(true) | ||
| }) | ||
|
|
||
| test("rejects invalid interval", () => { | ||
| let thrownError: unknown | ||
|
|
||
| try { | ||
| ModelSchedulerConfigSchema.parse({ interval_minutes: 0 }) | ||
| } catch (error) { | ||
| thrownError = error | ||
| } | ||
|
|
||
| expect(thrownError).toBeInstanceOf(ZodError) | ||
| }) | ||
|
|
||
| test("rejects invalid probe timeout", () => { | ||
| let thrownError: unknown | ||
|
|
||
| try { | ||
| ModelSchedulerConfigSchema.parse({ probe_timeout_ms: 999 }) | ||
| } catch (error) { | ||
| thrownError = error | ||
| } | ||
|
|
||
| expect(thrownError).toBeInstanceOf(ZodError) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { z } from "zod" | ||
|
|
||
| export const ModelSchedulerModeSchema = z.enum(["observe", "dry-run", "active"]) | ||
|
|
||
| export const ModelSchedulerConfigSchema = z.object({ | ||
| enabled: z.boolean().optional(), | ||
| interval_minutes: z.number().int().min(1).max(24 * 60).optional(), | ||
| mode: ModelSchedulerModeSchema.optional(), | ||
| preflight_on_session_created: z.boolean().optional(), | ||
| failure_threshold: z.number().int().min(1).max(10).optional(), | ||
| recovery_threshold: z.number().int().min(1).max(10).optional(), | ||
| agent_cooldown_minutes: z.number().int().min(0).max(24 * 60).optional(), | ||
| protect_manual_routing: z.boolean().optional(), | ||
| probe_enabled: z.boolean().optional(), | ||
| probe_timeout_ms: z.number().int().min(1000).max(300000).optional(), | ||
| probe_max_latency_ms: z.number().int().min(100).max(300000).optional(), | ||
| }) | ||
|
|
||
| export type ModelSchedulerMode = z.infer<typeof ModelSchedulerModeSchema> | ||
| export type ModelSchedulerConfig = z.infer<typeof ModelSchedulerConfigSchema> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { | ||
| AGENT_MODEL_REQUIREMENTS, | ||
| CATEGORY_MODEL_REQUIREMENTS, | ||
| fuzzyMatchModel, | ||
| } from "../../shared" | ||
| import type { RoutingEntry, RoutingTargetKind } from "./types" | ||
|
|
||
| function normalizeKey(value: string): string { | ||
| return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") | ||
| } | ||
|
|
||
| function resolveCandidate(model: string | null | undefined, availableModels: Set<string>): string[] { | ||
| if (!model) return [] | ||
|
|
||
| for (const availableModel of availableModels) { | ||
| if (availableModel.toLowerCase() === model.trim().toLowerCase()) { | ||
| return [availableModel] | ||
| } | ||
| } | ||
|
|
||
| return [] | ||
| } | ||
|
|
||
| export function collectCandidateModels(args: { | ||
| kind: RoutingTargetKind | ||
| key: string | ||
| routingEntry?: RoutingEntry | null | ||
| currentModel: string | null | ||
| availableModels: Set<string> | ||
| }): string[] { | ||
| const resolvedCandidates = new Set<string>() | ||
| const pushResolved = (model: string | null | undefined) => { | ||
| for (const resolved of resolveCandidate(model, args.availableModels)) { | ||
| resolvedCandidates.add(resolved) | ||
| } | ||
| } | ||
|
|
||
| pushResolved(args.currentModel) | ||
| for (const fallback of args.routingEntry?.fallback ?? []) { | ||
| pushResolved(fallback) | ||
| } | ||
|
|
||
| const requirements = args.kind === "agent" | ||
| ? AGENT_MODEL_REQUIREMENTS[normalizeKey(args.key)] | ||
| : CATEGORY_MODEL_REQUIREMENTS[normalizeKey(args.key)] | ||
|
|
||
| for (const fallbackEntry of requirements?.fallbackChain ?? []) { | ||
| const matchedModel = fuzzyMatchModel( | ||
| fallbackEntry.model, | ||
| args.availableModels, | ||
| fallbackEntry.providers, | ||
| ) | ||
| if (matchedModel) { | ||
| resolvedCandidates.add(matchedModel) | ||
| } | ||
| } | ||
|
|
||
| return Array.from(resolvedCandidates) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export const MODEL_HEALTH_FILE = "model-health.json" | ||
| export const MODEL_SCHEDULER_AUDIT_FILE = "scheduler-audit.jsonl" | ||
| export const MODEL_ROUTING_FILE = "model-routing.json" | ||
|
|
||
| export const DEFAULT_MODEL_SCHEDULER_CONFIG = { | ||
| enabled: true, | ||
| interval_minutes: 60, | ||
| mode: "active", | ||
| preflight_on_session_created: true, | ||
| failure_threshold: 1, | ||
| recovery_threshold: 1, | ||
| agent_cooldown_minutes: 180, | ||
| protect_manual_routing: true, | ||
| probe_enabled: true, | ||
| probe_timeout_ms: 15000, | ||
| probe_max_latency_ms: 8000, | ||
| } as const |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Duplicate Prompt for AI agents |
||
| import { dirname, join } from "node:path" | ||
| import { getOmoOpenCodeCacheDir } from "../../shared" | ||
| import { MODEL_HEALTH_FILE, MODEL_SCHEDULER_AUDIT_FILE } from "./constants" | ||
| import type { ModelHealthSnapshot, ModelSchedulerAuditEntry } from "./types" | ||
|
|
||
| function ensureParentDir(filePath: string): void { | ||
| const parentDir = dirname(filePath) | ||
| if (!existsSync(parentDir)) { | ||
| mkdirSync(parentDir, { recursive: true }) | ||
| } | ||
| } | ||
|
|
||
| function writeJsonAtomic(filePath: string, data: unknown): void { | ||
| ensureParentDir(filePath) | ||
| const tempPath = `${filePath}.tmp.${Date.now()}` | ||
|
|
||
| try { | ||
| writeFileSync(tempPath, JSON.stringify(data, null, 2), "utf-8") | ||
| renameSync(tempPath, filePath) | ||
| } catch (error) { | ||
| if (existsSync(tempPath)) { | ||
| unlinkSync(tempPath) | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export function getModelHealthFilePath(): string { | ||
| return join(getOmoOpenCodeCacheDir(), MODEL_HEALTH_FILE) | ||
| } | ||
|
|
||
| export function getModelSchedulerAuditFilePath(): string { | ||
| return join(getOmoOpenCodeCacheDir(), MODEL_SCHEDULER_AUDIT_FILE) | ||
| } | ||
|
|
||
| export function readModelHealthSnapshot(): ModelHealthSnapshot | null { | ||
| const filePath = getModelHealthFilePath() | ||
| if (!existsSync(filePath)) return null | ||
|
|
||
| try { | ||
| const raw = readFileSync(filePath, "utf-8") | ||
| return JSON.parse(raw) as ModelHealthSnapshot | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| export function writeModelHealthSnapshot(snapshot: ModelHealthSnapshot): void { | ||
| writeJsonAtomic(getModelHealthFilePath(), snapshot) | ||
| } | ||
|
|
||
| export function appendModelSchedulerAuditEntry(entry: ModelSchedulerAuditEntry): void { | ||
| const filePath = getModelSchedulerAuditFilePath() | ||
| ensureParentDir(filePath) | ||
| writeFileSync(filePath, `${JSON.stringify(entry)}\n`, { | ||
| encoding: "utf-8", | ||
| flag: "a", | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export * from "./constants" | ||
| export * from "./candidate-models" | ||
| export * from "./health-store" | ||
| export * from "./model-probe" | ||
| export * from "./routing-store" | ||
| export * from "./scheduler" | ||
| export * from "./selector" | ||
| export * from "./types" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Duplicate
normalizeKeyfunction introduced in two filesPrompt for AI agents