|
| 1 | +/** |
| 2 | + * Repair context windows that a saved config inherited from a wrong registry seed. |
| 3 | + * |
| 4 | + * `enrichProviderFromRegistry` is fill-only: it seeds `modelContextWindows` when |
| 5 | + * the row has none and never rewrites it afterwards. That posture is right — a |
| 6 | + * hand-tuned window must survive an upgrade — but it means a registry table that |
| 7 | + * shipped WRONG numbers is frozen into every config that was saved while those |
| 8 | + * numbers were current. Correcting the registry alone fixes new installs and |
| 9 | + * leaves existing ones reporting the old figure forever. |
| 10 | + * |
| 11 | + * This rewrites one thing: a window whose saved value is still byte-for-byte the |
| 12 | + * wrong number this file names, on a provider that still carries the registry's |
| 13 | + * adapter. A value the user changed does not match `from` and is left alone, and |
| 14 | + * nothing else in the row is touched. Same shape and the same restraint as |
| 15 | + * `model-rename-migration`, for the case where the id was right and the number |
| 16 | + * was not. |
| 17 | + */ |
| 18 | +import { PROVIDER_REGISTRY } from "./registry"; |
| 19 | +import type { OcxConfig } from "../types"; |
| 20 | + |
| 21 | +export interface StaleContextWindow { |
| 22 | + /** Registry provider id whose saved rows may carry the wrong window. */ |
| 23 | + provider: string; |
| 24 | + model: string; |
| 25 | + /** The wrong value this migration is allowed to replace, and nothing else. */ |
| 26 | + from: number; |
| 27 | + to: number; |
| 28 | +} |
| 29 | + |
| 30 | +export interface StaleContextWindowProjection { |
| 31 | + config: OcxConfig; |
| 32 | + changed: boolean; |
| 33 | + warnings: string[]; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Cognition windows corrected against a live `GetCascadeModelConfigs` response. |
| 38 | + * |
| 39 | + * The shipped table had been assembled from each model's ORIGINAL vendor window |
| 40 | + * rather than from what Cognition serves, so the Claude rows claimed 200k against |
| 41 | + * an actual 1M and Grok claimed 256k against 500k. Cognition documents no window |
| 42 | + * anywhere, so the per-account catalog is the only first-party source; these are |
| 43 | + * the degraded-mode figures, and live discovery supersedes them when it runs. |
| 44 | + */ |
| 45 | +export const STALE_CONTEXT_WINDOWS: readonly StaleContextWindow[] = [ |
| 46 | + { provider: "devin", model: "swe-1-7", from: 256_000, to: 262_000 }, |
| 47 | + { provider: "devin", model: "swe-1-7-lightning", from: 256_000, to: 202_752 }, |
| 48 | + { provider: "devin", model: "gpt-5-6-sol", from: 1_050_000, to: 1_000_000 }, |
| 49 | + { provider: "devin", model: "gpt-5-6-luna", from: 1_050_000, to: 1_000_000 }, |
| 50 | + { provider: "devin", model: "gpt-5-6-terra", from: 1_050_000, to: 1_000_000 }, |
| 51 | + { provider: "devin", model: "claude-opus-4-8", from: 200_000, to: 1_000_000 }, |
| 52 | + { provider: "devin", model: "claude-fable-5-1", from: 200_000, to: 1_000_000 }, |
| 53 | + { provider: "devin", model: "claude-sonnet-5", from: 200_000, to: 1_000_000 }, |
| 54 | + { provider: "devin", model: "kimi-k2-7", from: 256_000, to: 262_144 }, |
| 55 | + { provider: "devin", model: "grok-4-5", from: 256_000, to: 500_000 }, |
| 56 | +]; |
| 57 | + |
| 58 | +function providerStillMatchesRegistry(id: string, adapter: unknown): boolean { |
| 59 | + const entry = PROVIDER_REGISTRY.find(row => row.id === id); |
| 60 | + return entry !== undefined && entry.adapter === adapter; |
| 61 | +} |
| 62 | + |
| 63 | +/** Pure projection. The caller decides whether to persist. */ |
| 64 | +export function projectStaleContextWindows( |
| 65 | + config: OcxConfig, |
| 66 | + entries: readonly StaleContextWindow[] = STALE_CONTEXT_WINDOWS, |
| 67 | +): StaleContextWindowProjection { |
| 68 | + const warnings: string[] = []; |
| 69 | + const repaired = new Map<string, string[]>(); |
| 70 | + |
| 71 | + for (const entry of entries) { |
| 72 | + const prov = config.providers?.[entry.provider]; |
| 73 | + if (!prov) continue; |
| 74 | + if (!providerStillMatchesRegistry(entry.provider, prov.adapter)) continue; |
| 75 | + const windows = prov.modelContextWindows; |
| 76 | + if (!windows || windows[entry.model] !== entry.from) continue; |
| 77 | + windows[entry.model] = entry.to; |
| 78 | + const list = repaired.get(entry.provider) ?? []; |
| 79 | + list.push(`${entry.model} ${entry.from} -> ${entry.to}`); |
| 80 | + repaired.set(entry.provider, list); |
| 81 | + } |
| 82 | + |
| 83 | + for (const [provider, list] of repaired) { |
| 84 | + warnings.push( |
| 85 | + `corrected ${list.length} context window(s) on "${provider}" that the saved config ` |
| 86 | + + `inherited from a wrong registry seed: ${list.join(", ")}.`, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + return { config, changed: repaired.size > 0, warnings }; |
| 91 | +} |
| 92 | + |
0 commit comments