v4.3.19-5: 컨텍스트 게이지 provider-aware하게 수정 + 캐시 버그 제거

- resolveNumCtx()가 Ollama 전용이라 primary가 Google로 바뀐 뒤 세션
  컨텍스트 게이지가 419%까지 잘못 표시되던 버그 수정 — google provider면
  Gemini API에서 inputTokenLimit을 실시간 조회(하드코딩 아님, API 실패시만
  1048576 폴백)
- 최종 폴백(8192)을 캐시하던 버그 제거 — 재시작 직후 첫 조회가 일시적으로
  실패하면 프로세스 수명 내내 8192에 고정되던 문제, 이제 재시도 가능
- /api/model-context: model 파라미터 없을 때(메인 채팅 사이드바 게이지)
  무조건 200000(클로드 가정) 반환하던 걸 resolveNumCtx() 재사용으로 교체
  — 이제 실제 활성 provider와 항상 동기화됨

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5 committed 2026-07-26 17:17:14 +09:00
1 parent 5f83803ed4
commit 814f32a63e
2 files changed
+52 -6

No files matched your search

+10 -2
View File
@@ -3494,8 +3494,16 @@ function _readOllamaManifestNumCtx(modelName: string): number | null {
app.get('/api/model-context', async (req, res) => {
const model = String(req.query.model || '').trim().toLowerCase();
// Claude models: 200K (no local metadata)
if (!model || model.includes('claude')) {
// No model specified: caller wants the *current active* model's window (main
// chat sidebar gauge) — reuse resolveNumCtx() so this stays in sync with
// whatever provider is actually primary (ollama/google/etc) instead of the
// old hardcoded 200000 that silently applied no matter what was configured.
if (!model) {
res.json({ contextWindow: resolveNumCtx() });
return;
}
// Explicit Claude model name (e.g. from code.js's per-model lookup): 200K, no local metadata.
if (model.includes('claude')) {
res.json({ contextWindow: 200000 });
return;
}
+42 -4
View File
@@ -121,7 +121,40 @@ export function resolveNumCtx(): number {
}
} catch { /* fall through */ }
// 3. Query Ollama for the active model's actual context_length
// 3. Google (Gemini): not something Ollama's /api/show knows about, and unlike
// the Claude hardcode below, Gemini's Developer API actually exposes a per-model
// inputTokenLimit — query it live rather than hardcoding one model generation's
// value (3.6-flash/3.5-flash-lite/3.1-flash-lite all happen to share 1,048,576
// today, but a future model swap could silently make a hardcode wrong again,
// same failure mode as the 8192 bug this function used to have).
try {
const cfg: any = getConfig().getConfig();
const provider = String(cfg?.llm?.provider || 'ollama');
if (provider === 'google') {
const googleCfg = cfg?.llm?.providers?.google || {};
const model = String(googleCfg.model || 'gemini-3.6-flash');
const apiKey = getConfig().resolveSecret(googleCfg.api_key);
if (apiKey) {
const result = spawnSync('curl', [
'-s', `https://generativelanguage.googleapis.com/v1beta/models/${model}?key=${apiKey}`,
], { timeout: 3000, encoding: 'utf-8' });
if (result.status === 0 && result.stdout) {
const data = JSON.parse(result.stdout as string);
const limit = Number(data?.inputTokenLimit);
if (Number.isFinite(limit) && limit > 512) {
_cachedNumCtx = limit;
return _cachedNumCtx;
}
}
}
// Query failed (key/network/model issue) but we know it's Google — better to
// use the known-common Flash-family value than fall all the way to 8192.
_cachedNumCtx = 1048576;
return _cachedNumCtx;
}
} catch { /* fall through */ }
// 4. Query Ollama for the active model's actual context_length
try {
const cfg: any = getConfig().getConfig();
const model = String(cfg?.models?.primary || cfg?.llm?.providers?.ollama?.model || '');
@@ -145,7 +178,7 @@ export function resolveNumCtx(): number {
}
} catch { /* fall through */ }
// 4. Heuristic: cloud-routed models typically have large context windows
// 5. Heuristic: cloud-routed models typically have large context windows
try {
const cfg: any = getConfig().getConfig();
const model = String(cfg?.models?.primary || '');
@@ -155,8 +188,13 @@ export function resolveNumCtx(): number {
}
} catch { /* fall through */ }
_cachedNumCtx = 8192;
return _cachedNumCtx;
// Do NOT cache this last-resort fallback — unlike the paths above, it means every
// real resolution attempt failed (steps 1-3) and we don't even know if the model is
// cloud-routed (step 4's own heuristic also fell through). Caching it here previously
// meant one transient failure (e.g. Ollama not yet reachable moments after a restart)
// permanently stuck every session's context gauge at 8192 for the rest of the process's
// life — observed live showing 419% usage on a cloud model with a 1M-token window.
return 8192;
}
function estimateMessageTokens(msg: ChatMessage): number {