minicode-ai 0.14.4 → 0.14.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli/setup.ts CHANGED
@@ -28,7 +28,7 @@ import {
28
28
  isAceEnabled,
29
29
  } from "../src/policy/consequence-engine.ts"
30
30
  import { minicodeEstimator } from "../src/policy/context.ts"
31
- import { resolveContextWindow } from "../src/policy/context-window.ts"
31
+ import { loadContextWindowOverlay, resolveContextWindow } from "../src/policy/context-window.ts"
32
32
  import type { NoProgressDetector } from "../src/policy/no-progress.ts"
33
33
  import type { PermissionAsk } from "../src/policy/permission.ts"
34
34
  import type { RateLimiter } from "../src/policy/ratelimit.ts"
@@ -1238,6 +1238,9 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
1238
1238
  return resolved.source === "safe-default" ? undefined : resolved.tokens
1239
1239
  }
1240
1240
  router.updateContextWindowResolver(getContextWindowTokens)
1241
+ // Muat overlay jendela konteks models.dev sekali sebelum resolusi dipakai;
1242
+ // bersifat best-effort (cache mungkin belum ada — itu normal).
1243
+ await loadContextWindowOverlay().catch(() => undefined)
1241
1244
  // Emit the fallback warning immediately for the startup-selected model.
1242
1245
  getContextWindowTokens()
1243
1246
  const {
@@ -2779,7 +2782,14 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
2779
2782
  const stopContextGuard = session.events.on("provider:extension", (event) => {
2780
2783
  if (event.kind !== "context-window-limit") return
2781
2784
  const data = event.data as
2782
- | { inputTokens?: unknown; outputTokens?: unknown; windowTokens?: unknown }
2785
+ | {
2786
+ inputTokens?: unknown
2787
+ outputTokens?: unknown
2788
+ windowTokens?: unknown
2789
+ factor?: unknown
2790
+ localInputTokens?: unknown
2791
+ storeMessages?: unknown
2792
+ }
2783
2793
  | undefined
2784
2794
  const input = typeof data?.inputTokens === "number" ? data.inputTokens : 0
2785
2795
  const output = typeof data?.outputTokens === "number" ? data.outputTokens : 0
@@ -2788,14 +2798,25 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
2788
2798
  ? data.windowTokens
2789
2799
  : getContextWindowTokens()
2790
2800
  const usedTokens = input + output
2801
+ // Diagnostik inflasi kalibrasi: factor > 1 & localInput kecil = cutoff
2802
+ // palsu. Bila factor besar dan localInput ≈ usage nyata (footer),
2803
+ // biang-nya kalibrasi, bukan konteks yang benar-benar penuh.
2804
+ const localInput =
2805
+ typeof data?.localInputTokens === "number" ? Math.round(data.localInputTokens) : null
2806
+ const factor = typeof data?.factor === "number" ? Math.round(data.factor * 100) / 100 : null
2807
+ const storeMessages = typeof data?.storeMessages === "number" ? data.storeMessages : null
2808
+ const diag =
2809
+ localInput !== null || factor !== null || storeMessages !== null
2810
+ ? ` (factor=${factor ?? "?"}, localInput=${localInput ?? "?"}, messages=${storeMessages ?? "?"})`
2811
+ : ""
2791
2812
  presentation?.noteContextCutoff({
2792
- reason: `context guard stopped request: input ${input} + output ${output} (window ${windowTokens})`,
2813
+ reason: `context guard stopped request: input ${input} + output ${output} (window ${windowTokens})${diag}`,
2793
2814
  usedTokens,
2794
2815
  windowTokens,
2795
2816
  turnId: session.state.turnCount,
2796
2817
  })
2797
2818
  process.stderr.write(
2798
- `[context] request stopped at context budget: input ${input} + output ${output} (window ${windowTokens}).\n`,
2819
+ `[context] request stopped at context budget: input ${input} + output ${output} (window ${windowTokens})${diag}.\n`,
2799
2820
  )
2800
2821
  })
2801
2822
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "minicode-ai",
3
- "version": "0.14.4",
3
+ "version": "0.14.5",
4
4
  "description": "Coding agent CLI built on the frozen MiniCore runtime — tools, MCP, LSP, sandbox, shadow-git checkpoints",
5
5
  "type": "module",
6
6
  "private": false,
@@ -538,13 +538,28 @@ export async function createMinicodeSession(
538
538
  const contextLimitEvent = (input: number, output: number): ProviderEvent => ({
539
539
  type: "extension",
540
540
  kind: "context-window-limit",
541
- data: { inputTokens: input, outputTokens: output, windowTokens: limit },
541
+ data: {
542
+ inputTokens: input,
543
+ outputTokens: output,
544
+ windowTokens: limit,
545
+ // Diagnostik: factor kalibrasi (>=1) dan estimasi lokal tanpa
546
+ // kalibrasi (chars/4). Bila factor besar sementara localInput kecil,
547
+ // cutoff ini palsu (inflasi kalibrasi) — bukan konteks benar-benar penuh.
548
+ factor: calibration.snapshot().factor,
549
+ localInputTokens: localInputTokens,
550
+ storeMessages: req.messages.length,
551
+ },
542
552
  })
543
- const overflow = (actualInput = inputTokens, actualOutput = outputTokens) =>
544
- new AgentError(
545
- "budget_exceeded",
553
+ const overflow = (actualInput = inputTokens, actualOutput = outputTokens) => {
554
+ // context_length_exceeded (bukan budget_exceeded) supaya kernel menempuh
555
+ // jalur recovery `force_compact_and_retry`: meringkas lalu coba lagi,
556
+ // alih-alih mati `✗ Context window exceeded`. Bounded — loop recovery
557
+ // menolak bila kompaksi no-op (compactedForRecovery sudah set).
558
+ return new ProviderError(
559
+ "context_length_exceeded",
546
560
  `context window budget reached for the selected model: input ${actualInput} + output ${actualOutput} exceeds ${limit} tokens; reduce the prompt/tools or select a larger context window`,
547
561
  )
562
+ }
548
563
  const requestController = new AbortController()
549
564
  const forwardAbort = () => requestController.abort(signal.reason)
550
565
  if (signal.aborted) forwardAbort()
@@ -0,0 +1,64 @@
1
+ // Katalog jendela konteks built-in (offline, tanpa fetch) — jaring pengaman
2
+ // saat `provider.contextWindows` tidak dikonfigurasi. Menjawab keluhan "model
3
+ // sudah dipilih tapi jendela konteks jatuh ke fallback 32k".
4
+ //
5
+ // Desain: pencocokan per-KELUARGA pada id model (bukan persis), karena context
6
+ // window hampir selalu sama dalam satu keluarga (semua GPT-5, semua Claude-4,
7
+ // dst). Longest-family-menang supaya `deepseek-v4` mengalahkan `deepseek`.
8
+ //
9
+ // Nilai SENGAJA generous ke atas: over-estimasi menurun WP dengan anggun lewat
10
+ // jalur recovery provider `context_length_exceeded → compact-and-retry`,
11
+ // sedangkan under-estimasi justru memicu pemotongan dini ("context window
12
+ // exceeded") yang sedang kita hilangkan. Bila salah, user bisa menimpa lewat
13
+ // `provider.contextWindows` atau `--context-window` (selalu lebih diutamakan).
14
+
15
+ export interface ContextWindowCatalogEntry {
16
+ /** Substring pencocokan pada id model yang sudah dinormalisasi (lowercase). */
17
+ family: string
18
+ /** Jendela konteks dalam token. */
19
+ tokens: number
20
+ }
21
+
22
+ export const CONTEXT_WINDOW_CATALOG: ContextWindowCatalogEntry[] = [
23
+ { family: "deepseek-v4", tokens: 1_000_000 },
24
+ { family: "deepseek-v3.2", tokens: 1_000_000 },
25
+ { family: "deepseek", tokens: 131_072 },
26
+ { family: "gpt-4.1", tokens: 1_000_000 },
27
+ { family: "gpt-5", tokens: 400_000 },
28
+ { family: "gpt-4o", tokens: 128_000 },
29
+ { family: "gpt-4", tokens: 128_000 },
30
+ { family: "o3", tokens: 200_000 },
31
+ { family: "o1", tokens: 200_000 },
32
+ { family: "claude", tokens: 200_000 },
33
+ { family: "gemini", tokens: 2_000_000 },
34
+ { family: "llama-4", tokens: 1_000_000 },
35
+ { family: "llama", tokens: 131_072 },
36
+ { family: "qwen3", tokens: 262_144 },
37
+ { family: "qwen", tokens: 131_072 },
38
+ { family: "mistral", tokens: 131_072 },
39
+ { family: "grok", tokens: 131_072 },
40
+ { family: "glm", tokens: 131_072 },
41
+ { family: "kimi", tokens: 131_072 },
42
+ { family: "moonshot", tokens: 131_072 },
43
+ ]
44
+
45
+ /**
46
+ * Normalisasi id model supaya tanda baca versi tak menggagalkan pencocokan:
47
+ * `deepseek-v-4.1-flash-1017` → `deepseek-v4.1-flash-1017` (dash setelah
48
+ * `v` dihapus), sehingga cocok dengan keluarga `deepseek-v4`.
49
+ */
50
+ export function normalizeModelId(model: string): string {
51
+ return model.toLowerCase().replace(/v-/g, "v")
52
+ }
53
+
54
+ /** Jendela konteks dari katalog, atau undefined bila keluarga tak dikenal. */
55
+ export function findContextWindowFromCatalog(model: string): number | undefined {
56
+ const m = normalizeModelId(model)
57
+ let best: ContextWindowCatalogEntry | undefined
58
+ for (const entry of CONTEXT_WINDOW_CATALOG) {
59
+ if (m.includes(entry.family) && (!best || entry.family.length > best.family.length)) {
60
+ best = entry
61
+ }
62
+ }
63
+ return best?.tokens
64
+ }
@@ -1,4 +1,7 @@
1
+ import { readFile } from "node:fs/promises"
1
2
  import type { MinicodeConfig } from "../config.ts"
3
+ import { findContextWindowFromCatalog } from "./context-window-catalog.ts"
4
+ import { contextWindowsCachePath } from "./pricing.ts"
2
5
 
3
6
  /** App fallback for providers that do not publish model metadata.
4
7
  * Operators can set an exact `ProviderEntry.contextWindows` value or use the
@@ -7,7 +10,13 @@ export const DEFAULT_FALLBACK_CONTEXT_WINDOW_TOKENS = 32_000
7
10
 
8
11
  export interface ContextWindowResolution {
9
12
  readonly tokens: number
10
- readonly source: "override" | "model" | "provider-default" | "safe-default"
13
+ readonly source:
14
+ | "override"
15
+ | "model"
16
+ | "provider-default"
17
+ | "catalog"
18
+ | "models-dev"
19
+ | "safe-default"
11
20
  readonly model?: string
12
21
  readonly providerId?: string
13
22
  }
@@ -18,6 +27,70 @@ function validWindow(value: unknown): value is number {
18
27
  )
19
28
  }
20
29
 
30
+ // ── Overlay models.dev (jendela konteks) ────────────────────────────────────
31
+ // Diisi dari cache `context-windows.json` saat startup (sync sekali lewat
32
+ // `minicode pricing sync`). Bukan sumber wajib — hanya memperluas cakupan di
33
+ // luar katalog built-in. State dimuat async lalu dibaca sinkron di bawah.
34
+
35
+ let contextWindowOverlay: Record<string, number> | null = null
36
+
37
+ /** Reset state modul — untuk test hermetic. */
38
+ export function __resetContextWindowOverlay(): void {
39
+ contextWindowOverlay = null
40
+ }
41
+
42
+ /** Suntik overlay — untuk test (jalur produksi lewat loadContextWindowOverlay). */
43
+ export function __setContextWindowOverlay(map: Record<string, number> | null): void {
44
+ contextWindowOverlay = map
45
+ }
46
+
47
+ /** Muat cache jendela konteks dari disk (sekali per proses). */
48
+ export async function loadContextWindowOverlay(): Promise<Record<string, number>> {
49
+ if (contextWindowOverlay) return contextWindowOverlay
50
+ try {
51
+ const raw = await readFile(contextWindowsCachePath(), "utf8")
52
+ const parsed = JSON.parse(raw) as { models?: Record<string, unknown> }
53
+ if (parsed && typeof parsed.models === "object" && parsed.models) {
54
+ const out: Record<string, number> = {}
55
+ for (const [k, v] of Object.entries(parsed.models)) {
56
+ if (typeof v === "number" && Number.isFinite(v) && v > 0)
57
+ out[k.toLowerCase()] = Math.floor(v)
58
+ }
59
+ contextWindowOverlay = out
60
+ return out
61
+ }
62
+ } catch {
63
+ // tak ada cache = normal
64
+ }
65
+ contextWindowOverlay = {}
66
+ return contextWindowOverlay
67
+ }
68
+
69
+ /** Cari jendela konteks dari overlay models.dev (sinkron, setelah dimuat). */
70
+ export function findContextWindowFromOverlay(model: string): number | undefined {
71
+ const map = contextWindowOverlay
72
+ if (!map || Object.keys(map).length === 0) return undefined
73
+ const m = model.toLowerCase()
74
+ if (map[m] !== undefined) return map[m]
75
+ // Segmen (pemisah `/`/`:`) dicocokkan dengan kunci overlay sebagai prefiks —
76
+ // pola sama dengan findPrice sehingga `deepseek-v4-flash` menang atas `deepseek`.
77
+ let best: { k: string; v: number } | undefined
78
+ for (const seg of m.split(/[/:]/)) {
79
+ if (!seg) continue
80
+ for (const [k, v] of Object.entries(map)) {
81
+ if (
82
+ seg === k ||
83
+ seg.startsWith(`${k}.`) ||
84
+ seg.startsWith(`${k}-`) ||
85
+ seg.startsWith(`${k}_`)
86
+ ) {
87
+ if (!best || k.length > best.k.length) best = { k, v }
88
+ }
89
+ }
90
+ }
91
+ return best?.v
92
+ }
93
+
21
94
  /** Resolve a budget in the same order as the router: explicit provider pin,
22
95
  * then first provider advertising a bare model, then the provider fallback.
23
96
  * Unknown aliases use a conservative budget and remain visible to callers. */
@@ -64,6 +137,33 @@ export function resolveContextWindow(
64
137
  ...(providerId ? { providerId } : {}),
65
138
  }
66
139
  }
140
+ // Jaring pengaman built-in: kenali keluarga model umum (deepseek, gpt, claude,
141
+ // gemini, …) tanpa config, supaya model baru tak langsung jatuh ke 32k.
142
+ if (modelId) {
143
+ const catalogTokens = findContextWindowFromCatalog(modelId)
144
+ if (validWindow(catalogTokens)) {
145
+ return {
146
+ tokens: catalogTokens,
147
+ source: "catalog",
148
+ ...(modelId ? { model: modelId } : {}),
149
+ ...(providerId ? { providerId } : {}),
150
+ }
151
+ }
152
+ }
153
+ // Overlay models.dev (bila user pernah `minicode pricing sync`): mencakup
154
+ // model long-tail di luar katalog built-in. Dibawah katalog karena katalog
155
+ // dikurasi manual dan tak bergantung pada cache yang bisa kedaluwarsa.
156
+ if (modelId) {
157
+ const overlayTokens = findContextWindowFromOverlay(modelId)
158
+ if (validWindow(overlayTokens)) {
159
+ return {
160
+ tokens: overlayTokens,
161
+ source: "models-dev",
162
+ ...(modelId ? { model: modelId } : {}),
163
+ ...(providerId ? { providerId } : {}),
164
+ }
165
+ }
166
+ }
67
167
  return {
68
168
  tokens: DEFAULT_FALLBACK_CONTEXT_WINDOW_TOKENS,
69
169
  source: "safe-default",
@@ -63,8 +63,20 @@ export function pricingCachePath(): string {
63
63
  return join(homeDir(), ".minicode", "pricing.json")
64
64
  }
65
65
 
66
+ /** Cache terpisah untuk jendela konteks (diisi dari `models.dev` saat sync). */
67
+ export function contextWindowsCachePath(): string {
68
+ return join(homeDir(), ".minicode", "context-windows.json")
69
+ }
70
+
66
71
  const MODELS_DEV_URL = "https://models.dev/api.json"
67
72
 
73
+ interface ContextWindowsCache {
74
+ fetchedAt: number
75
+ source: string
76
+ /** modelId → jendela konteks (token). Kunci di-lowercase. */
77
+ models: Record<string, number>
78
+ }
79
+
68
80
  interface PricingCache {
69
81
  fetchedAt: number
70
82
  source: string
@@ -157,6 +169,7 @@ export interface ModelsDevPayload {
157
169
  {
158
170
  id?: string
159
171
  cost?: { input?: number; output?: number; cache_read?: number; cache_write?: number }
172
+ context_length?: unknown
160
173
  }
161
174
  >
162
175
  }
@@ -201,6 +214,36 @@ export function extractPricing(payload: ModelsDevPayload): Record<string, ModelP
201
214
  return out
202
215
  }
203
216
 
217
+ /** Normalisasi field `context_length` models.dev (bisa number atau objek). */
218
+ function contextLengthNumber(value: unknown): number | undefined {
219
+ if (typeof value === "number" && Number.isFinite(value) && value > 0) return Math.floor(value)
220
+ if (value && typeof value === "object") {
221
+ const v = value as { total?: unknown; input?: unknown; output?: unknown }
222
+ for (const c of [v.total, v.input, v.output]) {
223
+ if (typeof c === "number" && Number.isFinite(c) && c > 0) return Math.floor(c)
224
+ }
225
+ }
226
+ return undefined
227
+ }
228
+
229
+ /** Ekstrak jendela konteks (token total) dari payload models.dev. Diekspor untuk test. */
230
+ export function extractContextWindows(payload: ModelsDevPayload): Record<string, number> {
231
+ const out: Record<string, number> = {}
232
+ for (const provider of Object.values(payload ?? {})) {
233
+ for (const [key, model] of Object.entries(provider?.models ?? {})) {
234
+ const cl = contextLengthNumber(model?.context_length)
235
+ if (cl === undefined) continue
236
+ const id = (model?.id ?? key).toLowerCase()
237
+ if (!id) continue
238
+ // Context length adalah properti model (bukan provider); bila satu id
239
+ // muncul di beberapa provider dengan nilai beda, ambil max (kapasitas
240
+ // asli model). Over-estimasi menurun anggun via recovery compact-and-retry.
241
+ out[id] = out[id] === undefined ? cl : Math.max(out[id]!, cl)
242
+ }
243
+ }
244
+ return out
245
+ }
246
+
204
247
  /** Median harga, mengabaikan entri gratis bila ada yang berbayar. Diekspor untuk test. */
205
248
  export function pickRepresentativePrice(list: ModelPrice[]): ModelPrice | undefined {
206
249
  if (list.length === 0) return undefined
@@ -283,6 +326,15 @@ export async function syncPricing(url: string = MODELS_DEV_URL): Promise<SyncRes
283
326
  await atomicWriteText(pricingCachePath(), serialized)
284
327
  overlay = models
285
328
  overlayMeta = { fetchedAt: cache.fetchedAt, count }
329
+
330
+ // Sekalian simpan jendela konteks (field `context_length`) ke cache terpisah;
331
+ // satu fetch, dua artefak. Gagal menulis cache jendela jangan menggagalkan
332
+ // sync harga — harga tetap lebih penting dan path-nya sudah teruji.
333
+ const windows = extractContextWindows(payload)
334
+ if (Object.keys(windows).length > 0) {
335
+ const wCache: ContextWindowsCache = { fetchedAt: Date.now(), source: url, models: windows }
336
+ await atomicWriteText(contextWindowsCachePath(), JSON.stringify(wCache))
337
+ }
286
338
  return { count, bytes: serialized.length, path: pricingCachePath() }
287
339
  }
288
340