minicode-ai 0.22.0 → 0.25.0

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.
Files changed (100) hide show
  1. package/README.md +2 -2
  2. package/cli/commands.ts +10 -0
  3. package/cli/index.ts +11 -0
  4. package/cli/setup.ts +64 -33
  5. package/cli/tui.ts +29 -3
  6. package/docs/README.md +2 -2
  7. package/docs/SUMMARY.md +10 -19
  8. package/docs/TERMINAL_CONTRACT.md +119 -32
  9. package/docs/architecture.md +1 -1
  10. package/docs/autonomous-scheduler.md +283 -2
  11. package/docs/choosing-mode.md +4 -4
  12. package/docs/concepts.md +39 -3
  13. package/docs/config.md +90 -4
  14. package/docs/environment.md +6 -0
  15. package/docs/getting-started.md +58 -2
  16. package/docs/repl.md +1 -1
  17. package/docs/security.md +49 -2
  18. package/docs/tools.md +2 -1
  19. package/package.json +2 -36
  20. package/src/app/provider-layer.ts +1 -1
  21. package/src/app/session.ts +129 -20
  22. package/src/config.ts +102 -2
  23. package/src/constants.ts +32 -4
  24. package/src/lib/safe-open.ts +21 -12
  25. package/src/memory/vector.ts +0 -31
  26. package/src/policy/compaction.ts +27 -0
  27. package/src/policy/context-budget.ts +132 -0
  28. package/src/policy/context-escalation.ts +140 -0
  29. package/src/policy/context-window.ts +14 -9
  30. package/src/policy/context.ts +10 -1
  31. package/src/policy/microcompact.ts +162 -0
  32. package/src/policy/no-progress.ts +1 -1
  33. package/src/policy/permission.ts +5 -0
  34. package/src/policy/scrub.ts +15 -2
  35. package/src/policy/tool-contract.ts +11 -9
  36. package/src/presentation/reducer.ts +1 -0
  37. package/src/providers/anthropic.ts +17 -3
  38. package/src/providers/detect.ts +155 -13
  39. package/src/providers/provision.ts +92 -1
  40. package/src/providers/responses.ts +13 -1
  41. package/src/session/persistence.ts +56 -0
  42. package/src/task/autonomous-policy.ts +7 -0
  43. package/src/task/graph-validate.ts +1 -1
  44. package/src/task/session-authority.ts +0 -5
  45. package/src/task/store.ts +0 -4
  46. package/src/tools/ask_user.ts +14 -1
  47. package/src/tools/bash.ts +57 -9
  48. package/src/tools/context.ts +10 -0
  49. package/src/tools/delete_file.ts +3 -2
  50. package/src/tools/edit.ts +5 -4
  51. package/src/tools/git.ts +70 -12
  52. package/src/tools/glob.ts +13 -5
  53. package/src/tools/grep.ts +26 -10
  54. package/src/tools/index.ts +3 -2
  55. package/src/tools/lsp.ts +15 -9
  56. package/src/tools/move_file.ts +4 -3
  57. package/src/tools/patch.ts +5 -4
  58. package/src/tools/read_file.ts +3 -2
  59. package/src/tools/read_image.ts +3 -2
  60. package/src/tools/task.ts +1 -0
  61. package/src/tools/todo.ts +4 -8
  62. package/src/tools/write_file.ts +4 -3
  63. package/src/ui/approval/prompt.ts +226 -77
  64. package/src/ui/assistant/simple.ts +1 -1
  65. package/src/ui/footer.ts +41 -29
  66. package/src/ui/i18n/en.ts +23 -4
  67. package/src/ui/i18n/id.ts +25 -4
  68. package/src/ui/input/input.ts +2 -2
  69. package/src/ui/render/activity-group.ts +1 -0
  70. package/src/ui/render/activity-panel.ts +21 -3
  71. package/src/ui/render/hint.ts +28 -0
  72. package/src/ui/render/ledger.ts +1 -9
  73. package/src/ui/render/logo.ts +86 -0
  74. package/src/ui/render/markdown-table.ts +0 -3
  75. package/src/ui/render/session-summary.ts +22 -1
  76. package/src/ui/render/table-grid.ts +0 -3
  77. package/src/ui/render/theme.ts +36 -5
  78. package/src/ui/runtime/screen.ts +11 -3
  79. package/src/ui/screens/dialog.ts +45 -81
  80. package/src/ui/screens/form.ts +29 -33
  81. package/src/ui/screens/lifecycle.ts +80 -0
  82. package/src/ui/screens/model-manager.ts +84 -91
  83. package/src/ui/screens/picker.ts +36 -33
  84. package/src/ui/screens/provider-manager.ts +50 -59
  85. package/src/ui/screens/watchdog.ts +27 -0
  86. package/src/ui/screens/wizard.ts +3 -20
  87. package/src/ui/tui/app.ts +97 -11
  88. package/src/ui/tui/transcript.ts +55 -16
  89. package/vendor/minicore/src/core/compact.ts +10 -0
  90. package/vendor/minicore/src/core/errors.ts +11 -1
  91. package/vendor/minicore/src/core/executor.ts +6 -1
  92. package/vendor/minicore/src/core/loop.ts +92 -2
  93. package/vendor/minicore/src/core/session.ts +8 -0
  94. package/vendor/minicore/src/providers/openai-compat.ts +15 -2
  95. package/docs/P14_OPEN_WORK.md +0 -290
  96. package/docs/config-providers.md +0 -87
  97. package/docs/glossary.md +0 -39
  98. package/docs/quickstart.md +0 -60
  99. package/docs/security-model.md +0 -96
  100. package/docs/terminal.md +0 -91
@@ -26,6 +26,8 @@ import {
26
26
  createIncrementalTokenCounter,
27
27
  minicodeEstimator,
28
28
  } from "../policy/context.ts"
29
+ import { contextBudgetFor, outputReserveFor } from "../policy/context-budget.ts"
30
+ import { nextLadderStep } from "../policy/context-escalation.ts"
29
31
  import { DEFAULT_FALLBACK_CONTEXT_WINDOW_TOKENS } from "../policy/context-window.ts"
30
32
  import { parallelExecutor } from "../policy/executor.ts"
31
33
  import {
@@ -148,6 +150,8 @@ export async function createMinicodeSession(
148
150
  Omit<SessionConfig, "permissions" | "estimator" | "recovery" | "system" | "executor">
149
151
  > & {
150
152
  systemExtra?: string
153
+ /** Bahasa chat model ("id"/"en"); undefined = tanpa direktif (test/bench). */
154
+ lang?: "en" | "id"
151
155
  cwd?: string
152
156
  /** Live model identity used for per-model token calibration. */
153
157
  getContextModel?: () => string | undefined
@@ -190,6 +194,18 @@ export async function createMinicodeSession(
190
194
  noProgress?: NoProgressOptions | false
191
195
  /** Hook opsional agar composition root dapat mereset riwayat osilasi saat verify hijau. */
192
196
  onNoProgress?: (detector: NoProgressDetector) => void
197
+ /**
198
+ * Dipanggil saat jendela konteks dinaikkan otomatis setelah overflow
199
+ * terverifikasi. Composition root memakainya untuk memberi tahu user
200
+ * (footer/log) dan — bila diinginkan — mempersistensikan nilai baru.
201
+ * Tidak pernah dipanggil untuk 429/402/auth/jaringan.
202
+ */
203
+ onContextWindowEscalated?: (info: {
204
+ model: string
205
+ fromTokens: number
206
+ toTokens: number
207
+ observedTokens: number
208
+ }) => void
193
209
  /**
194
210
  * [Area B · PROMPT 06] Penyimpanan binding persetujuan milik SESI.
195
211
  *
@@ -267,6 +283,7 @@ export async function createMinicodeSession(
267
283
  cwd: defaultCwd,
268
284
  extra: opts.systemExtra ?? "",
269
285
  signal: (opts as unknown as { signal?: AbortSignal }).signal,
286
+ lang: opts.lang,
270
287
  })
271
288
  const {
272
289
  concurrency,
@@ -540,15 +557,48 @@ export async function createMinicodeSession(
540
557
  let lastProviderInputTokens: number | undefined
541
558
  let lastRequestMessageCount = 0
542
559
  const contextWindowForModel = (model?: string): number => {
560
+ // Eskalasi adaptif menang atas resolusi statis: ia adalah bukti dari
561
+ // endpoint bahwa jendela lebih besar daripada tebakan. Tetap tunduk pada
562
+ // nilai eksak operator? TIDAK: operator yang memasang flag eksplisit sudah
563
+ // dihormati di `contextWindowBase`, namun bila flag itu TERBUKTI kurang
564
+ // (overflow nyata terjadi), menaikkan satu anak tangga lebih benar daripada
565
+ // mengulang kegagalan yang sama.
566
+ const escalated = escalatedWindows.get(escalationKey(model))
567
+ if (escalated !== undefined) return escalated
568
+ return contextWindowBase(model)
569
+ }
570
+ // Batas output model: dipakai sebagai reserve output (bukan angka tetap 20k).
571
+ // Dicari dari metadata provider (`maxOutputTokens`, diisi dari `/models` bila
572
+ // endpoint melaporkannya); tanpa metadata → `undefined` = reserve default.
573
+ const maxOutputTokensForModel = (model?: string): number | undefined => {
574
+ try {
575
+ const fromCore = rest.maxOutputTokensForModel?.(model)
576
+ if (typeof fromCore === "number" && Number.isSafeInteger(fromCore) && fromCore > 0)
577
+ return fromCore
578
+ const fromProvider = (
579
+ provider as { maxOutputTokensForModel?: (m?: string) => number }
580
+ ).maxOutputTokensForModel?.(model)
581
+ if (
582
+ typeof fromProvider === "number" &&
583
+ Number.isSafeInteger(fromProvider) &&
584
+ fromProvider > 0
585
+ )
586
+ return fromProvider
587
+ } catch {}
588
+ return undefined
589
+ }
590
+ // ── Eskalasi adaptif jendela konteks ───────────────────────────────────
591
+ // Nilai yang dinaikkan SESI INI saja (bukan ditulis ke config: perubahan
592
+ // konfigurasi harus lewat /sync atau niat operator). Kunci = `provider::model`
593
+ // ternormalisasi; nilai = jendela hasil eskalasi.
594
+ const escalatedWindows = new Map<string, number>()
595
+ const escalationKey = (model?: string): string => {
596
+ const raw = model ?? currentContextModel() ?? "default"
597
+ return raw.includes("::") ? raw : `${provider.id}::${raw}`
598
+ }
599
+ /** Jendela "dasar" sebelum eskalasi: hasil resolusi biasa, tanpa eskalasi. */
600
+ const contextWindowBase = (model?: string): number => {
543
601
  const explicit = rest.contextWindowTokens
544
- if (
545
- !rest.contextWindowTokensForModel &&
546
- typeof explicit === "number" &&
547
- Number.isSafeInteger(explicit) &&
548
- explicit > 0
549
- ) {
550
- return explicit
551
- }
552
602
  try {
553
603
  const resolved =
554
604
  rest.contextWindowTokensForModel?.(model) ?? provider.contextWindowTokensForModel?.(model)
@@ -559,6 +609,33 @@ export async function createMinicodeSession(
559
609
  ? explicit
560
610
  : DEFAULT_FALLBACK_CONTEXT_WINDOW_TOKENS
561
611
  }
612
+ /**
613
+ * Naikkan jendela satu anak tangga setelah overflow terverifikasi.
614
+ *
615
+ * KENAPA otomatis: user yang sedang bekerja tidak bisa tahu ada anak tangga
616
+ * berikutnya — dan sesi yang mati di tengah jalan jauh lebih mahal daripada
617
+ * satu eskalasi. Namun ia tetap BOUNDED: satu langkah per kejadian, tak
618
+ * pernah melebihi batas atas, dan hasilnya hanya berlaku untuk sesi ini.
619
+ */
620
+ const escalateContextWindow = (model: string | undefined, observedTokens: number) => {
621
+ const key = escalationKey(model)
622
+ const base = escalatedWindows.get(key) ?? contextWindowBase(model)
623
+ // Bila pengamatan sudah jelas melampaui jendela dasar, naik dari nilai
624
+ // pengamatan; kalau tidak, naik dari jendela yang sekarang dipakai.
625
+ const from = Math.max(base, Number.isFinite(observedTokens) ? observedTokens : 0)
626
+ const next = nextLadderStep(from)
627
+ if (next === undefined || next <= base) return
628
+ escalatedWindows.set(key, next)
629
+ try {
630
+ rest.onContextWindowEscalated?.({
631
+ model: key,
632
+ fromTokens: base,
633
+ toTokens: next,
634
+ observedTokens,
635
+ })
636
+ } catch {}
637
+ }
638
+
562
639
  const dynamicProvider: ModelProvider = {
563
640
  id: provider.id,
564
641
  get models() {
@@ -568,6 +645,7 @@ export async function createMinicodeSession(
568
645
  const system = computeLiveSystem()
569
646
  const model = req.model ?? currentContextModel()
570
647
  let limit = contextWindowForModel(model)
648
+ const maxOutputTokens = maxOutputTokensForModel(model)
571
649
  const estimatorModel = model?.includes("::") ? model : (currentContextModel() ?? model)
572
650
  let calibration = calibrationFor(estimatorModel)
573
651
  let estimator = (text: string) => preciseEstimator(estimatorModel, text)
@@ -598,10 +676,11 @@ export async function createMinicodeSession(
598
676
  }
599
677
  // Sisakan ruang framing dan respons minimum. Tanpa reserve, request yang
600
678
  // tepat menyentuh batas provider tetap ditolak setelah biaya dibakar.
601
- // Reserve ini HANYA headroom framing + respons minimum (skema tool sudah
602
- // termasuk di `inputTokens` via estimateTools), jadi cap 256 cukup —
603
- // tidak perlu diskala dengan ukuran skema tool.
604
- let reserve = Math.min(LIMITS.CONTEXT_WINDOW_RESERVE_CAP_TOKENS, Math.floor(limit / 10))
679
+ // Reserve dihitung dari anggaran konteks terpusat (`context-budget.ts`):
680
+ // `min(max_output_tokens, 20k)` dengan lantai 4k dan lantai 2% jendela.
681
+ // Nilai lama `min(256, limit/10)` praktis nol untuk jendela besar, jadi
682
+ // model berjendela 1M tetap kehabisan ruang jawaban di ambang batas.
683
+ let reserve = outputReserveFor(limit, maxOutputTokens)
605
684
  let outputAllowance = Math.max(0, limit - inputTokens - reserve)
606
685
  let outputTokens = 0
607
686
  const streamedText = createIncrementalTokenCounter()
@@ -626,6 +705,13 @@ export async function createMinicodeSession(
626
705
  // jalur recovery `force_compact_and_retry`: meringkas lalu coba lagi,
627
706
  // alih-alih mati `✗ Context window exceeded`. Bounded — loop recovery
628
707
  // menolak bila kompaksi no-op (compactedForRecovery sudah set).
708
+ //
709
+ // Eskalasi adaptif: overflow INI adalah bukti terverifikasi bahwa jendela
710
+ // yang kita pakai terlalu kecil ATAU penuh. Naikkan satu anak tangga untuk
711
+ // sesi ini supaya percobaan berikutnya (dan turn berikut) punya ruang.
712
+ // Hanya dipanggil pada jalur yang benar-benar kehabisan konteks — bukan
713
+ // 429/402/auth/jaringan.
714
+ escalateContextWindow(model, actualInput + actualOutput)
629
715
  return new ProviderError(
630
716
  "context_length_exceeded",
631
717
  `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`,
@@ -665,7 +751,7 @@ export async function createMinicodeSession(
665
751
  estimateSystem(system, estimator) +
666
752
  estimateTools(tools, estimator),
667
753
  )
668
- reserve = Math.min(LIMITS.CONTEXT_WINDOW_RESERVE_CAP_TOKENS, Math.floor(limit / 10))
754
+ reserve = outputReserveFor(limit, maxOutputTokens)
669
755
  outputAllowance = Math.max(0, limit - inputTokens - reserve)
670
756
  }
671
757
  }
@@ -677,7 +763,7 @@ export async function createMinicodeSession(
677
763
  estimateSystem(system, estimator) +
678
764
  estimateTools(tools, estimator),
679
765
  )
680
- reserve = Math.min(LIMITS.CONTEXT_WINDOW_RESERVE_CAP_TOKENS, Math.floor(limit / 10))
766
+ reserve = outputReserveFor(limit, maxOutputTokens)
681
767
  outputAllowance = Math.max(0, limit - inputTokens - reserve)
682
768
  }
683
769
  admittedFactor = currentFactor
@@ -706,14 +792,16 @@ export async function createMinicodeSession(
706
792
  // sebenarnya konteks kepanjangan meski teksnya tak menyebut "context".
707
793
  // Reklasifikasi sebagai context_length_exceeded supaya kernel menempuh
708
794
  // compact-and-retry (bounded + no-op-safe) alih-alih mati fatal. Ambang
709
- // `localInputTokens >= CONTEXT_RECLASSIFY_400_MIN_LOCAL_TOKENS` = proxy
710
- // monotonik ukuran konteks berbasis chars/token estimator (toleran
711
- // terhadap under-count); sesi kecil dengan 400 format-argumen yang sah
712
- // tidak akan salah diklasifikasi.
795
+ // tanngal
796
+ // `localInputTokens >= contextReclassifyThreshold(limit)` — ambang
797
+ // RELATIF jendela (`min(50_000, window*0.5)`), bukan 50.000 mutlak.
798
+ // Angka mutlak membuat model berjendela < 50k mustahil menempuh
799
+ // compact-and-retry: jendelanya penuh jauh sebelum ambang tercapai,
800
+ // sehingga 400 generik selalu mati fatal.
713
801
  if (
714
802
  error instanceof ProviderError &&
715
803
  (error.category === "invalid_request" || error.category === "unknown") &&
716
- localInputTokens >= LIMITS.CONTEXT_RECLASSIFY_400_MIN_LOCAL_TOKENS
804
+ localInputTokens >= contextBudgetFor(limit, maxOutputTokens).reclassifyThreshold
717
805
  ) {
718
806
  throw overflow(inputTokens, outputTokens)
719
807
  }
@@ -736,6 +824,7 @@ export async function createMinicodeSession(
736
824
  ...rest,
737
825
  contextWindowTokens: contextWindowForModel(currentContextModel()),
738
826
  contextWindowTokensForModel: contextWindowForModel,
827
+ maxOutputTokensForModel: maxOutputTokensForModel,
739
828
  // Kompaksi lebih agresif (host-side): ringkas mulai 65% jendela, bukan 75%.
740
829
  budget: aggressiveBudgetPolicy,
741
830
  provider: dynamicProvider,
@@ -873,7 +962,27 @@ export async function createMinicodeSession(
873
962
  },
874
963
  run(input: string, runOpts?: { signal?: AbortSignal; model?: string }) {
875
964
  activeContextModel = runOpts?.model ?? opts.model
876
- return core.run(input, runOpts)
965
+ const modelForRun = activeContextModel
966
+ return core.run(input, runOpts).catch((error: unknown) => {
967
+ // Kernel melaporkan kehabisan konteks sebagai `budget_exceeded` dengan
968
+ // `details.reason` = `fixpoint` atau `critical`. Keduanya BUKTI
969
+ // terverifikasi bahwa jendela yang dipakai terlalu kecil untuk sesi ini
970
+ // — sama sahnya dengan penolakan provider. Tanpa eskalasi di sini,
971
+ // sesi yang sudah di titik tetap langsung mati padahal endpoint mungkin
972
+ // punya kapasitas jauh lebih besar.
973
+ //
974
+ // Bukan pemicu: `max_steps_exceeded`, `max_turns`, `aborted`, timeout,
975
+ // dan error provider non-konteks — menaikkan jendela tak akan menolong.
976
+ if (error instanceof AgentError && error.kind === "budget_exceeded") {
977
+ const reason = error.details?.reason
978
+ if (reason === "fixpoint" || reason === "critical") {
979
+ const used =
980
+ typeof error.details?.fixedTokens === "number" ? error.details.fixedTokens : 0
981
+ escalateContextWindow(modelForRun, used)
982
+ }
983
+ }
984
+ throw error
985
+ })
877
986
  },
878
987
  abort() {
879
988
  core.abort()
package/src/config.ts CHANGED
@@ -33,6 +33,17 @@ export async function withConfigLock<T>(path: string, fn: () => Promise<T>): Pro
33
33
  }
34
34
  }
35
35
 
36
+ /** Asal nilai jendela konteks. `user` = operator menetapkan sendiri (presedensi
37
+ * tertinggi, tak pernah ditimpa otomatis); `endpoint` = dibaca dari metadata
38
+ * `/models` endpoint yang dipakai; `probe` = hasil eskalasi adaptif terverifikasi;
39
+ * `public-directory` = katalog publik (cache); `static-table` = tabel bawaan. */
40
+ export type ContextWindowSource =
41
+ | "user"
42
+ | "endpoint"
43
+ | "probe"
44
+ | "public-directory"
45
+ | "static-table"
46
+
36
47
  export interface ProviderEntry {
37
48
  id: string
38
49
  baseUrl: string
@@ -40,9 +51,24 @@ export interface ProviderEntry {
40
51
  apiKey: string
41
52
  models: string[]
42
53
  /** Context window tokens by exact model id; `*` is a provider-wide fallback.
43
- * Values are operator-supplied because gateways often expose aliases whose
44
- * actual model capacity cannot be discovered reliably at runtime. */
54
+ * Values may be operator-supplied or discovered from the endpoint's `/models`
55
+ * metadata. Gateways often expose aliases whose real capacity is invisible
56
+ * otherwise, so an exact value from the endpoint in use wins over any catalog. */
45
57
  contextWindows?: Record<string, number>
58
+ /** Asal tiap nilai `contextWindows`, per model id. Dipakai untuk presedensi:
59
+ * nilai `user` tak boleh ditimpa otomatis oleh endpoint/sync, sedangkan nilai
60
+ * `endpoint` boleh diperbarui dan kalah dari nilai eksak yang lebih baru.
61
+ * Entri tanpa catatan diperlakukan sebagai `user` (kompatibel ke belakang:
62
+ * config lama tanpa field ini dianggap niat operator). */
63
+ contextWindowSources?: Record<string, ContextWindowSource>
64
+ /** Batas output per model id (`max_output_tokens`) bila endpoint melaporkan.
65
+ * Dipakai sebagai reserve output proporsional; tanpa ini reserve memakai
66
+ * default 20k. */
67
+ maxOutputTokens?: Record<string, number>
68
+ /** Alias lama/global untuk batas output berlaku ke semua model provider. */
69
+ maxOutputTokensDefault?: number
70
+ /** Cache dari metadata endpoint (Fase 7.1); `undefined` = default aktif. */
71
+ enablePromptCaching?: boolean
46
72
  providerHint?: string
47
73
  /** Knob generik reasoning effort — dipetakan per-wire (openai reasoning_effort, anthropic thinking, gemini). */
48
74
  reasoningEffort?: "low" | "medium" | "high"
@@ -107,6 +133,79 @@ export function localConfigNotice(cwd: string, allowLocal: boolean): string | un
107
133
  return `[config] local config enabled: ${p}`
108
134
  }
109
135
 
136
+ const VALID_CONTEXT_SOURCES: readonly ContextWindowSource[] = [
137
+ "user",
138
+ "endpoint",
139
+ "probe",
140
+ "public-directory",
141
+ "static-table",
142
+ ]
143
+
144
+ /** Peta token-positif per model — dipakai untuk `contextWindows` dan
145
+ * `maxOutputTokens` supaya validasi keduanya tak bisa berbeda diam-diam. */
146
+ function normalizeTokenMap(
147
+ raw: unknown,
148
+ opts: { allowStar: boolean },
149
+ ): Record<string, number> | undefined {
150
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined
151
+ const out: Record<string, number> = {}
152
+ for (const [model, tokens] of Object.entries(raw as Record<string, unknown>)) {
153
+ const keyOk = opts.allowStar ? model === "*" || model.length > 0 : model.length > 0
154
+ if (
155
+ keyOk &&
156
+ typeof tokens === "number" &&
157
+ Number.isSafeInteger(tokens) &&
158
+ tokens > 0 &&
159
+ tokens <= 20_000_000
160
+ ) {
161
+ out[model] = tokens
162
+ }
163
+ }
164
+ return Object.keys(out).length > 0 ? out : undefined
165
+ }
166
+
167
+ /** Field kapasitas tambahan (`contextWindowSources`, `maxOutputTokens`) — dipisah
168
+ * agar `normalizeConfig` tetap terbaca dan asalnya tak bisa hilang diam-diam:
169
+ * asal hanya dipertahankan untuk model yang punya nilai jendela, sisa asing
170
+ * dibuang supaya config tidak menumpuk sampah dari versi lama. */
171
+ function normalizeCapacityExtras(
172
+ p: ProviderEntry,
173
+ contextWindows: Record<string, number> | undefined,
174
+ ): Pick<ProviderEntry, "contextWindowSources" | "maxOutputTokens" | "maxOutputTokensDefault"> {
175
+ const extras: Pick<
176
+ ProviderEntry,
177
+ "contextWindowSources" | "maxOutputTokens" | "maxOutputTokensDefault"
178
+ > = {}
179
+ const rawSources = (p as { contextWindowSources?: unknown }).contextWindowSources
180
+ if (rawSources && typeof rawSources === "object" && !Array.isArray(rawSources)) {
181
+ const sources: Record<string, ContextWindowSource> = {}
182
+ for (const [model, value] of Object.entries(rawSources as Record<string, unknown>)) {
183
+ if (
184
+ typeof value === "string" &&
185
+ (VALID_CONTEXT_SOURCES as readonly string[]).includes(value) &&
186
+ contextWindows?.[model] !== undefined
187
+ ) {
188
+ sources[model] = value as ContextWindowSource
189
+ }
190
+ }
191
+ if (Object.keys(sources).length > 0) extras.contextWindowSources = sources
192
+ }
193
+ const maxOut = normalizeTokenMap((p as { maxOutputTokens?: unknown }).maxOutputTokens, {
194
+ allowStar: false,
195
+ })
196
+ if (maxOut) extras.maxOutputTokens = maxOut
197
+ const maxOutDefault = (p as { maxOutputTokensDefault?: unknown }).maxOutputTokensDefault
198
+ if (
199
+ typeof maxOutDefault === "number" &&
200
+ Number.isSafeInteger(maxOutDefault) &&
201
+ maxOutDefault > 0 &&
202
+ maxOutDefault <= 20_000_000
203
+ ) {
204
+ extras.maxOutputTokensDefault = maxOutDefault
205
+ }
206
+ return extras
207
+ }
208
+
110
209
  export function normalizeConfig(raw: unknown): MinicodeConfig {
111
210
  const cfg = raw as Record<string, unknown>
112
211
  const providers = Array.isArray(cfg?.providers)
@@ -134,6 +233,7 @@ export function normalizeConfig(raw: unknown): MinicodeConfig {
134
233
  ...p,
135
234
  contextWindows:
136
235
  contextWindows && Object.keys(contextWindows).length > 0 ? contextWindows : undefined,
236
+ ...normalizeCapacityExtras(p, contextWindows),
137
237
  }
138
238
  })
139
239
  : []
package/src/constants.ts CHANGED
@@ -12,6 +12,9 @@ export const LIMITS = {
12
12
  /** Tool output truncation */
13
13
  BASH_OUTPUT_MAX_CHARS: 20_000,
14
14
  BASH_DEFAULT_TIMEOUT_MS: 30_000,
15
+ /** Timeout default bash di mode `unrestricted` (30 menit): kompilasi/instal/
16
+ * push jaringan tak boleh mati oleh timeout pendek; `timeoutMs` tetap menang. */
17
+ UNRESTRICTED_BASH_TIMEOUT_MS: 1_800_000,
15
18
  /** bash background: proses hidup melewati batas turn, dipanggil via bash_output */
16
19
  BASH_BACKGROUND_MAX_JOBS: 8,
17
20
  BASH_BACKGROUND_MAX_LIFETIME_MS: 3_600_000,
@@ -54,11 +57,36 @@ export const LIMITS = {
54
57
  * request provider: `Math.min(reserve, limit/10)` — cadangan tak naik tanpa
55
58
  * batas pada jendela raksasa (1M token tetap cukup 256 untuk framing). */
56
59
  CONTEXT_WINDOW_RESERVE_CAP_TOKENS: 256,
57
- /** Ambang ukuran konteks (estimasi chars/token lokal) untuk mengklasifikasi
58
- * 400 `invalid_request` generik sebagai `context_length_exceeded` alih-alih
59
- * mati fatal. Di bawah ambang ini, 400 dianggap error format sah (bukan
60
- * konteks). Proxy monotonik ukuran konteks, bukan hitungan presisi. */
60
+ /** BATAS ATAS ambang ukuran konteks (estimasi chars/token lokal) untuk
61
+ * mengklasifikasi 400 `invalid_request` generik sebagai
62
+ * `context_length_exceeded` alih-alih mati fatal. Di bawah ambang ini, 400
63
+ * dianggap error format sah (bukan konteks). Proxy monotonik ukuran konteks,
64
+ * bukan hitungan presisi.
65
+ *
66
+ * KENAPA batas atas, bukan angka mutlak: nilai ini dulu 50.000 mutlak,
67
+ * sehingga model berjendela < 50.000 tak pernah bisa menempuh jalur
68
+ * recovery `compact-and-retry` — jendelanya penuh jauh sebelum ambangnya
69
+ * tercapai. Ambang efektif kini `min(nilai ini, window * 0.5)` (lihat
70
+ * `contextReclassifyThreshold`), jadi tetap aman untuk jendela besar tanpa
71
+ * mematikan recovery di jendela kecil. */
61
72
  CONTEXT_RECLASSIFY_400_MIN_LOCAL_TOKENS: 50_000,
73
+ /** Fraksi jendela konteks untuk ambang reklasifikasi 400 di atas. 0.5 dipilih
74
+ * karena 400 generik yang benar-benar "konteks kepanjangan" praktis selalu
75
+ * terjadi setelah separuh jendela terpakai (chars/4 under-count paling buruk
76
+ * ~2×, jadi 0.5 masih di bawah garis aman). */
77
+ CONTEXT_RECLASSIFY_400_WINDOW_RATIO: 0.5,
78
+ /** Microcompact — jumlah hasil tool TERBARU yang dipertahankan utuh. 5 adalah
79
+ * titik kumpul lintas implementasi harness produksi (median 5 dari enam
80
+ * implementasi yang disurvei); di bawah 3 model cenderung mengulang
81
+ * pembacaan berkas yang sama. */
82
+ MICROCOMPACT_KEEP_RECENT: 5,
83
+ /** Hemat minimum agar pemangkasan hasil tool sepadan. Di bawah ini, yang
84
+ * dibuang hanya informasi tanpa manfaat token. */
85
+ MICROCOMPACT_MIN_SAVING_TOKENS: 20_000,
86
+ /** Hasil tool harus menempati porsi ini dari budget konteks agar pemangkasan
87
+ * dipertimbangkan — bila konteks dikuasai pesan lain, memangkas tool hanya
88
+ * membuang fakta. */
89
+ MICROCOMPACT_MIN_TOOL_SHARE: 0.4,
62
90
  /** R11 — ambang volume-baca per turn (char hasil tool terakumulasi) untuk
63
91
  * observer opt-in `MINICODE_READ_VOLUME_GUARD=1`. Di atas ini, peringatkan
64
92
  * sekali per turn: "banyak dibaca tanpa kompaksi" = risiko degradasi
@@ -52,13 +52,15 @@ async function resolveRealOrAncestor(abs: string): Promise<string> {
52
52
  export async function resolveSafePath(
53
53
  p: string,
54
54
  root: string,
55
+ relaxed = false,
55
56
  ): Promise<{ abs: string; real: string }> {
56
- if (isPathOutsideRoot(p, root)) throw new Error(`path outside workspace: ${p}`)
57
+ if (!relaxed && isPathOutsideRoot(p, root)) throw new Error(`path outside workspace: ${p}`)
57
58
  if (isSensitive(p)) throw new Error(`blocked sensitive file: ${p}`)
58
59
  const abs = isAbsolute(p) ? resolve(p) : resolve(root, p)
59
60
  const realRoot = await realpath(root).catch(() => root)
60
61
  const real = await resolveRealOrAncestor(abs)
61
- if (isPathOutsideRoot(real, realRoot)) throw new Error(`symlink points outside workspace: ${p}`)
62
+ if (!relaxed && isPathOutsideRoot(real, realRoot))
63
+ throw new Error(`symlink points outside workspace: ${p}`)
62
64
  if (isSensitive(real)) throw new Error(`blocked sensitive file: ${p}`)
63
65
  return { abs, real }
64
66
  }
@@ -81,11 +83,12 @@ export async function resolveSafePath(
81
83
  export async function safeOpenRead(
82
84
  abs: string,
83
85
  root: string,
86
+ relaxed = false,
84
87
  ): Promise<{ handle: FileHandle; realPath: string }> {
85
88
  const realRoot = await realpath(root).catch(() => root)
86
89
  // Pre-check cepat (fail-fast tanpa open)
87
90
  const preReal = await realpath(abs).catch(() => abs)
88
- if (isPathOutsideRoot(preReal, realRoot))
91
+ if (!relaxed && isPathOutsideRoot(preReal, realRoot))
89
92
  throw new Error(`symlink points outside workspace: ${abs}`)
90
93
  // Sensitif pada target nyata — symlink bernama jinak bisa menunjuk .env;
91
94
  // di sini agar semua pembaca via open (read_file/read_image/stat) mewarisinya.
@@ -96,7 +99,7 @@ export async function safeOpenRead(
96
99
  // keluar sesudah realpath pertama tetapi sebelum open.
97
100
  const expected = await statPath(preReal, { bigint: true })
98
101
  const checkedReal = await realpath(preReal)
99
- if (isPathOutsideRoot(checkedReal, realRoot))
102
+ if (!relaxed && isPathOutsideRoot(checkedReal, realRoot))
100
103
  throw new Error(`symlink points outside workspace: ${abs}`)
101
104
  if (isSensitive(checkedReal)) throw new Error(`blocked sensitive file: ${abs}`)
102
105
  if (expected.ino === 0n)
@@ -137,8 +140,8 @@ export async function safeOpenRead(
137
140
  return { handle, realPath: checkedReal }
138
141
  }
139
142
 
140
- export async function safeReadFile(abs: string, root: string): Promise<string> {
141
- const { handle } = await safeOpenRead(abs, root)
143
+ export async function safeReadFile(abs: string, root: string, relaxed = false): Promise<string> {
144
+ const { handle } = await safeOpenRead(abs, root, relaxed)
142
145
  try {
143
146
  return await handle.readFile("utf8")
144
147
  } finally {
@@ -154,10 +157,11 @@ export async function safeReadFileCapped(
154
157
  abs: string,
155
158
  root: string,
156
159
  maxBytes: number,
160
+ relaxed = false,
157
161
  ): Promise<{ text: string; truncated: boolean }> {
158
162
  if (!Number.isSafeInteger(maxBytes) || maxBytes < 1)
159
163
  throw new RangeError("maxBytes must be a positive safe integer")
160
- const { handle } = await safeOpenRead(abs, root)
164
+ const { handle } = await safeOpenRead(abs, root, relaxed)
161
165
  try {
162
166
  const st = await handle.stat()
163
167
  if (!st.isFile()) throw new Error(`refusing to read non-regular file: ${abs}`)
@@ -178,8 +182,8 @@ export async function safeReadFileCapped(
178
182
  }
179
183
  }
180
184
 
181
- export async function safeStat(abs: string, root: string) {
182
- const { handle } = await safeOpenRead(abs, root)
185
+ export async function safeStat(abs: string, root: string, relaxed = false) {
186
+ const { handle } = await safeOpenRead(abs, root, relaxed)
183
187
  try {
184
188
  return await handle.stat()
185
189
  } finally {
@@ -201,17 +205,22 @@ export async function safeStat(abs: string, root: string) {
201
205
  * via isPathOutsideRoot(real) + isSensitive(real), dan cek realpath di
202
206
  * bawah menjaga parent-dir yang di-swap jadi symlink keluar.
203
207
  */
204
- export async function assertSafeWriteTarget(abs: string, root: string): Promise<string> {
208
+ export async function assertSafeWriteTarget(
209
+ abs: string,
210
+ root: string,
211
+ relaxed = false,
212
+ ): Promise<string> {
205
213
  const realRoot = await realpath(root).catch(() => root)
206
214
  const dir = dirname(abs)
207
215
  const realDir = await resolveRealOrAncestor(dir)
208
- if (isPathOutsideRoot(realDir, realRoot)) throw new Error(`parent outside workspace: ${abs}`)
216
+ if (!relaxed && isPathOutsideRoot(realDir, realRoot))
217
+ throw new Error(`parent outside workspace: ${abs}`)
209
218
  const fileReal = await realpath(abs).catch(() => null)
210
219
  // basename (bukan split "/"): path Windows memakai backslash — split "/"
211
220
  // mengembalikan seluruh path absolut sehingga resolve() mengabaikan realDir
212
221
  // dan verifikasi parent symlink gugur diam-diam di Windows.
213
222
  const realAbs = fileReal ?? resolve(realDir, basename(abs))
214
- if (isPathOutsideRoot(realAbs, realRoot))
223
+ if (!relaxed && isPathOutsideRoot(realAbs, realRoot))
215
224
  throw new Error(`symlink points outside workspace: ${abs}`)
216
225
  return realAbs
217
226
  }
@@ -959,37 +959,6 @@ async function syncMemoryFileToSupersession(
959
959
  await files.appendMemory(`[supersession] fact ${oldId} → ${newId} (superseded)`, cwd)
960
960
  }
961
961
 
962
- export async function supersedeMemoryByIds(
963
- oldIds: string[],
964
- newId: string,
965
- cwd?: string,
966
- ): Promise<number> {
967
- let n = 0
968
- const db = open(cwd)
969
- try {
970
- await withBusyRetry(() => {
971
- for (const oldId of oldIds) {
972
- const oldRow = memoryEntryHeadId(db, oldId)
973
- if (oldRow) {
974
- // Tombstone SELURUH entri lama (semua chunk berbagi kepala), bukan
975
- // hanya id chunk yang diberikan (D8: entri bisa diberikan lewat
976
- // chunk mana pun).
977
- db.prepare(`UPDATE memory SET superseded_by = ? WHERE id = ? OR parent = ?`).run(
978
- newId,
979
- oldRow.headId,
980
- oldRow.headId,
981
- )
982
- n++
983
- }
984
- }
985
- db.prepare(`UPDATE memory SET supersedes = ? WHERE id = ?`).run(oldIds.join(","), newId)
986
- })
987
- } finally {
988
- db.close()
989
- }
990
- return n
991
- }
992
-
993
962
  /**
994
963
  * [Fase 8 / 8.1] Rekam pelajaran KANDIDAT (belum terverifikasi).
995
964
  * Pelajaran disimpan `provisional` sehingga retrieval default tidak akan
@@ -6,6 +6,7 @@ import { createOpenAICompatProvider } from "#minicore/providers/openai-compat.ts
6
6
  import { LIMITS } from "../constants.ts"
7
7
  import { isPrivateHostWithDns } from "../lib/net.ts"
8
8
  import { minicodeEstimator } from "./context.ts"
9
+ import { applyMicrocompact } from "./microcompact.ts"
9
10
  import { scrubSecrets } from "./scrub.ts"
10
11
 
11
12
  /** F3.2: parse MINICODE_COMPACT_KEEP_TURNS (murni, diekspor untuk test).
@@ -295,6 +296,10 @@ function boundCompactionResult(
295
296
  }
296
297
 
297
298
  export function createLlmCompaction(opts: LlmCompactionOptions = {}): CompactionStrategy {
299
+ // Kill-switch: audit yang butuh output tool verbatim sepanjang sesi bisa
300
+ // mematikan lapisan ini. Default aktif — tanpa microcompact, payload sesi
301
+ // panjang dikuasai hasil tool lama yang sudah tak relevan.
302
+ const microcompactEnabled = (process.env.MINICODE_MICROCOMPACT ?? "") !== "0"
298
303
  const fallback = opts.fallback ?? {
299
304
  kind: "bounded-mechanical",
300
305
  compact(store: ContextStore, cOpts: { keepRecentTurns: number }) {
@@ -334,6 +339,28 @@ export function createLlmCompaction(opts: LlmCompactionOptions = {}): Compaction
334
339
  ): readonly import("#minicore/core/types.ts").Message[] {
335
340
  return fallback.compact(store, cOpts)
336
341
  },
342
+ /**
343
+ * Lapisan termurah: pangkas ISI hasil tool lama, tanpa LLM, tanpa membuang
344
+ * pesan. Dijalankan kernel sebelum kompaksi penuh; bila cukup menurunkan
345
+ * token, ringkasan mahal tak perlu dibuat sama sekali.
346
+ *
347
+ * `MINICODE_MICROCOMPACT=0` mematikannya (mis. saat audit butuh seluruh
348
+ * output tool verbatim sepanjang sesi).
349
+ */
350
+ microcompact(
351
+ store: ContextStore,
352
+ mOpts: { budgetTokens?: number },
353
+ ): readonly import("#minicore/core/types.ts").Message[] {
354
+ if (!microcompactEnabled) return store.messages
355
+ const result = applyMicrocompact(store.messages, {
356
+ keepRecent: LIMITS.MICROCOMPACT_KEEP_RECENT,
357
+ minSavingTokens: LIMITS.MICROCOMPACT_MIN_SAVING_TOKENS,
358
+ minToolShare: LIMITS.MICROCOMPACT_MIN_TOOL_SHARE,
359
+ estimateTokens: (text) => minicodeEstimator(text),
360
+ budgetTokens: mOpts.budgetTokens,
361
+ })
362
+ return result.clearedCount > 0 ? result.messages : store.messages
363
+ },
337
364
  async compactAsync(
338
365
  store: ContextStore,
339
366
  cOpts: { keepRecentTurns: number },