minicode-ai 0.14.4 → 0.14.6
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 +32 -7
- package/cli/tui.ts +4 -1
- package/package.json +1 -1
- package/src/app/session.ts +31 -4
- package/src/policy/context-window-catalog.ts +64 -0
- package/src/policy/context-window.ts +101 -1
- package/src/policy/permission.ts +24 -0
- package/src/policy/pricing.ts +52 -0
- package/src/ui/footer.ts +13 -3
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"
|
|
@@ -1151,9 +1151,11 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1151
1151
|
}
|
|
1152
1152
|
|
|
1153
1153
|
// [mode-architecture] `--plan` legacy bukan lagi permission mode — pemetaannya
|
|
1154
|
-
// ke WorkMode PLAN terjadi di effectiveWorkMode (di atas).
|
|
1155
|
-
//
|
|
1156
|
-
|
|
1154
|
+
// ke WorkMode PLAN terjadi di effectiveWorkMode (di atas). `--allow-all` tetap
|
|
1155
|
+
// profil privileged. `--allowlist`/fallback no-sandbox TIDAK lagi mengubah
|
|
1156
|
+
// PermissionMode: ia jadi `restrictBash` (lapis kapabilitas bash terpisah).
|
|
1157
|
+
const permissionMode = allowAll ? "allow-all" : ask ? "ask" : "auto"
|
|
1158
|
+
const restrictBash = allowlist
|
|
1157
1159
|
|
|
1158
1160
|
// Home guard: sesi dibuka di home berisiko (glob rame, write berbahaya).
|
|
1159
1161
|
// Standar industri: percaya-tapi-ingatkan, jangan tolak diam-diam maupun
|
|
@@ -1238,6 +1240,9 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1238
1240
|
return resolved.source === "safe-default" ? undefined : resolved.tokens
|
|
1239
1241
|
}
|
|
1240
1242
|
router.updateContextWindowResolver(getContextWindowTokens)
|
|
1243
|
+
// Muat overlay jendela konteks models.dev sekali sebelum resolusi dipakai;
|
|
1244
|
+
// bersifat best-effort (cache mungkin belum ada — itu normal).
|
|
1245
|
+
await loadContextWindowOverlay().catch(() => undefined)
|
|
1241
1246
|
// Emit the fallback warning immediately for the startup-selected model.
|
|
1242
1247
|
getContextWindowTokens()
|
|
1243
1248
|
const {
|
|
@@ -1894,6 +1899,7 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1894
1899
|
tools: sessionTools,
|
|
1895
1900
|
cwd,
|
|
1896
1901
|
permissionMode,
|
|
1902
|
+
restrictBash,
|
|
1897
1903
|
allowLocalConfig,
|
|
1898
1904
|
// P12 S2.2 + P0.2 — WorkMode efektif sesi ini (termasuk pulihan resume).
|
|
1899
1905
|
workMode: effectiveWorkMode,
|
|
@@ -1927,6 +1933,7 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1927
1933
|
setToolPermissionMode?.(m)
|
|
1928
1934
|
},
|
|
1929
1935
|
getMode: () => ctl.getMode(),
|
|
1936
|
+
getRestrictBash: () => ctl.getRestrictBash?.() ?? false,
|
|
1930
1937
|
}
|
|
1931
1938
|
},
|
|
1932
1939
|
// P12 S2.2 Slice 5 — handle kontrol WorkMode runtime. Setelah sesi
|
|
@@ -2779,7 +2786,14 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2779
2786
|
const stopContextGuard = session.events.on("provider:extension", (event) => {
|
|
2780
2787
|
if (event.kind !== "context-window-limit") return
|
|
2781
2788
|
const data = event.data as
|
|
2782
|
-
| {
|
|
2789
|
+
| {
|
|
2790
|
+
inputTokens?: unknown
|
|
2791
|
+
outputTokens?: unknown
|
|
2792
|
+
windowTokens?: unknown
|
|
2793
|
+
factor?: unknown
|
|
2794
|
+
localInputTokens?: unknown
|
|
2795
|
+
storeMessages?: unknown
|
|
2796
|
+
}
|
|
2783
2797
|
| undefined
|
|
2784
2798
|
const input = typeof data?.inputTokens === "number" ? data.inputTokens : 0
|
|
2785
2799
|
const output = typeof data?.outputTokens === "number" ? data.outputTokens : 0
|
|
@@ -2788,14 +2802,25 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2788
2802
|
? data.windowTokens
|
|
2789
2803
|
: getContextWindowTokens()
|
|
2790
2804
|
const usedTokens = input + output
|
|
2805
|
+
// Diagnostik inflasi kalibrasi: factor > 1 & localInput kecil = cutoff
|
|
2806
|
+
// palsu. Bila factor besar dan localInput ≈ usage nyata (footer),
|
|
2807
|
+
// biang-nya kalibrasi, bukan konteks yang benar-benar penuh.
|
|
2808
|
+
const localInput =
|
|
2809
|
+
typeof data?.localInputTokens === "number" ? Math.round(data.localInputTokens) : null
|
|
2810
|
+
const factor = typeof data?.factor === "number" ? Math.round(data.factor * 100) / 100 : null
|
|
2811
|
+
const storeMessages = typeof data?.storeMessages === "number" ? data.storeMessages : null
|
|
2812
|
+
const diag =
|
|
2813
|
+
localInput !== null || factor !== null || storeMessages !== null
|
|
2814
|
+
? ` (factor=${factor ?? "?"}, localInput=${localInput ?? "?"}, messages=${storeMessages ?? "?"})`
|
|
2815
|
+
: ""
|
|
2791
2816
|
presentation?.noteContextCutoff({
|
|
2792
|
-
reason: `context guard stopped request: input ${input} + output ${output} (window ${windowTokens})`,
|
|
2817
|
+
reason: `context guard stopped request: input ${input} + output ${output} (window ${windowTokens})${diag}`,
|
|
2793
2818
|
usedTokens,
|
|
2794
2819
|
windowTokens,
|
|
2795
2820
|
turnId: session.state.turnCount,
|
|
2796
2821
|
})
|
|
2797
2822
|
process.stderr.write(
|
|
2798
|
-
`[context] request stopped at context budget: input ${input} + output ${output} (window ${windowTokens}).\n`,
|
|
2823
|
+
`[context] request stopped at context budget: input ${input} + output ${output} (window ${windowTokens})${diag}.\n`,
|
|
2799
2824
|
)
|
|
2800
2825
|
})
|
|
2801
2826
|
try {
|
package/cli/tui.ts
CHANGED
|
@@ -638,7 +638,10 @@ export async function runTui(ctx: CliSession): Promise<void> {
|
|
|
638
638
|
getStatus: () => {
|
|
639
639
|
const status = {
|
|
640
640
|
footer: {
|
|
641
|
-
|
|
641
|
+
// Normalisasi: sumbu persetujuan hanya AUTO/ASK. Profil warisan
|
|
642
|
+
// (allow-all/allowlist) tak lagi bocor sebagai label mode.
|
|
643
|
+
mode: mode === "ask" ? "ask" : "auto",
|
|
644
|
+
strictBash: permissions?.getRestrictBash?.() ?? false,
|
|
642
645
|
// P12 S2.2 Slice 5 - WorkMode LIVE (bukan snapshot boot): footer
|
|
643
646
|
// mencerminkan mode aktif saat frame digambar.
|
|
644
647
|
workMode: workModeControl?.getWorkMode() ?? "DISCUSS",
|
package/package.json
CHANGED
package/src/app/session.ts
CHANGED
|
@@ -74,6 +74,8 @@ export const cappedRecovery = {
|
|
|
74
74
|
export type PermissionControl = {
|
|
75
75
|
setMode(m: "auto" | "readonly" | "plan" | "allow-all" | "ask" | "allowlist"): void
|
|
76
76
|
getMode(): "auto" | "readonly" | "plan" | "allow-all" | "ask" | "allowlist"
|
|
77
|
+
/** Bila true, bash dibatasi ke allow-list (lapis kapabilitas, terpisah mode). */
|
|
78
|
+
getRestrictBash?(): boolean
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
/**
|
|
@@ -129,6 +131,11 @@ export async function createMinicodeSession(
|
|
|
129
131
|
/** Live model identity used for per-model token calibration. */
|
|
130
132
|
getContextModel?: () => string | undefined
|
|
131
133
|
permissionMode?: "auto" | "readonly" | "plan" | "allow-all" | "ask" | "allowlist"
|
|
134
|
+
/**
|
|
135
|
+
* [mode-architecture] Lapis kapabilitas bash TERPISAH dari permission mode.
|
|
136
|
+
* Bila true, `auto` membatasi bash ke allow-list (fallback "no-sandbox").
|
|
137
|
+
*/
|
|
138
|
+
restrictBash?: boolean
|
|
132
139
|
/** S2.1: WorkMode adalah lapis intent TERPISAH dari PermissionMode.
|
|
133
140
|
* Default EXECUTE = perilaku lama (base handler apa adanya). Nilai selain
|
|
134
141
|
* EXECUTE memasang ceiling tipis di atas handler PM (lihat work-mode.ts);
|
|
@@ -237,6 +244,7 @@ export async function createMinicodeSession(
|
|
|
237
244
|
concurrency,
|
|
238
245
|
writeConcurrency,
|
|
239
246
|
permissionMode,
|
|
247
|
+
restrictBash,
|
|
240
248
|
workMode: _workMode,
|
|
241
249
|
allowLocalConfig,
|
|
242
250
|
systemExtra: _extra,
|
|
@@ -313,6 +321,7 @@ export async function createMinicodeSession(
|
|
|
313
321
|
root: defaultCwd,
|
|
314
322
|
ask,
|
|
315
323
|
allowLocalConfig,
|
|
324
|
+
restrictBash,
|
|
316
325
|
onApprovalEvent,
|
|
317
326
|
authorizedTools,
|
|
318
327
|
// [Area B · PROMPT 06] Store per-sesi: persetujuan dari sesi A tak
|
|
@@ -433,6 +442,9 @@ export async function createMinicodeSession(
|
|
|
433
442
|
onPermissions({
|
|
434
443
|
setMode: (m) => withMode.__setMode(m),
|
|
435
444
|
getMode: () => withMode.__getMode(),
|
|
445
|
+
// restrictBash adalah opsi tetap (bukan state handler) — dibaca langsung
|
|
446
|
+
// dari closure, bukan lewat wrapper WorkMode yang tak meneruskannya.
|
|
447
|
+
getRestrictBash: () => restrictBash === true,
|
|
436
448
|
})
|
|
437
449
|
}
|
|
438
450
|
// P12 S2.2 Slice 5 — handle kontrol WorkMode runtime. Hanya di-wire bila
|
|
@@ -538,13 +550,28 @@ export async function createMinicodeSession(
|
|
|
538
550
|
const contextLimitEvent = (input: number, output: number): ProviderEvent => ({
|
|
539
551
|
type: "extension",
|
|
540
552
|
kind: "context-window-limit",
|
|
541
|
-
data: {
|
|
553
|
+
data: {
|
|
554
|
+
inputTokens: input,
|
|
555
|
+
outputTokens: output,
|
|
556
|
+
windowTokens: limit,
|
|
557
|
+
// Diagnostik: factor kalibrasi (>=1) dan estimasi lokal tanpa
|
|
558
|
+
// kalibrasi (chars/4). Bila factor besar sementara localInput kecil,
|
|
559
|
+
// cutoff ini palsu (inflasi kalibrasi) — bukan konteks benar-benar penuh.
|
|
560
|
+
factor: calibration.snapshot().factor,
|
|
561
|
+
localInputTokens: localInputTokens,
|
|
562
|
+
storeMessages: req.messages.length,
|
|
563
|
+
},
|
|
542
564
|
})
|
|
543
|
-
const overflow = (actualInput = inputTokens, actualOutput = outputTokens) =>
|
|
544
|
-
|
|
545
|
-
|
|
565
|
+
const overflow = (actualInput = inputTokens, actualOutput = outputTokens) => {
|
|
566
|
+
// context_length_exceeded (bukan budget_exceeded) supaya kernel menempuh
|
|
567
|
+
// jalur recovery `force_compact_and_retry`: meringkas lalu coba lagi,
|
|
568
|
+
// alih-alih mati `✗ Context window exceeded`. Bounded — loop recovery
|
|
569
|
+
// menolak bila kompaksi no-op (compactedForRecovery sudah set).
|
|
570
|
+
return new ProviderError(
|
|
571
|
+
"context_length_exceeded",
|
|
546
572
|
`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
573
|
)
|
|
574
|
+
}
|
|
548
575
|
const requestController = new AbortController()
|
|
549
576
|
const forwardAbort = () => requestController.abort(signal.reason)
|
|
550
577
|
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:
|
|
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",
|
package/src/policy/permission.ts
CHANGED
|
@@ -342,9 +342,18 @@ export function createPermissionHandler(
|
|
|
342
342
|
* ini yang boleh auto-allow. Absen = fail-closed (semua MCP bertitik deny).
|
|
343
343
|
*/
|
|
344
344
|
authorizedTools?: ReadonlySet<string> | null
|
|
345
|
+
/**
|
|
346
|
+
* [mode-architecture] Pembatas bash TERPISAH dari sumbu persetujuan.
|
|
347
|
+
* Bila true, mode `auto` juga membatasi `bash` ke allow-list
|
|
348
|
+
* (DEFAULT_BASH_ALLOWLIST / MINICODE_BASH_ALLOWLIST) — pola "no-sandbox"
|
|
349
|
+
* (Windows) yang dulu diselipkan sebagai `mode: "allowlist"`. Mode
|
|
350
|
+
* permission tetap `auto`/`ask`; ini hanya lapis kapabilitas bash.
|
|
351
|
+
*/
|
|
352
|
+
restrictBash?: boolean
|
|
345
353
|
} = {},
|
|
346
354
|
): PermissionHandler {
|
|
347
355
|
const state = { mode: (opts.mode ?? "auto") as PermissionMode }
|
|
356
|
+
const restrictBash = opts.restrictBash === true
|
|
348
357
|
const root = resolve(opts.root ?? cwd())
|
|
349
358
|
const approvalBindings = opts.approvalBindings
|
|
350
359
|
const askUser = opts.ask
|
|
@@ -555,6 +564,17 @@ export function createPermissionHandler(
|
|
|
555
564
|
const cmd = (args?.cmd as string) ?? ""
|
|
556
565
|
const br = bashDenyReason(cmd)
|
|
557
566
|
if (br) return deny(call, `bash-guard: ${br}`)
|
|
567
|
+
// restrictBash: bash hanya boleh pola allow-list (fallback no-sandbox).
|
|
568
|
+
if (restrictBash) {
|
|
569
|
+
const matched = bashAllowlist.filter((pat) => matchBashAllowlist(cmd, pat))
|
|
570
|
+
if (matched.length === 0)
|
|
571
|
+
return deny(
|
|
572
|
+
call,
|
|
573
|
+
`allowlist: no matching pattern for "${cmd.slice(0, 80)}" — file work: use read_file/write_file/edit; full shell: rerun with --allow-all / --sandbox docker / MINICODE_BASH_ALLOWLIST`,
|
|
574
|
+
)
|
|
575
|
+
if (matched.some((p) => /^(npx|npm exec|bun x|bun run)\b/i.test(p)) && !npmNpxSafe(cmd))
|
|
576
|
+
return deny(call, "allowlist: npm/npx unsafe")
|
|
577
|
+
}
|
|
558
578
|
return "allow"
|
|
559
579
|
}
|
|
560
580
|
return deny(call, "not authorized in AUTO mode")
|
|
@@ -701,6 +721,9 @@ export function createPermissionHandler(
|
|
|
701
721
|
__getMode(): PermissionMode {
|
|
702
722
|
return state.mode
|
|
703
723
|
},
|
|
724
|
+
__getRestrictBash(): boolean {
|
|
725
|
+
return restrictBash
|
|
726
|
+
},
|
|
704
727
|
// Seam kernel describeDenial: alasan deny terakhir call ini (dicatat
|
|
705
728
|
// check() via deny()). Hapus-saat-baca; tak ada catatan = undefined =
|
|
706
729
|
// pesan deny polos seperti dulu.
|
|
@@ -720,6 +743,7 @@ export function createPermissionHandler(
|
|
|
720
743
|
return returned as unknown as PermissionHandler & {
|
|
721
744
|
__setMode(m: PermissionMode): void
|
|
722
745
|
__getMode(): PermissionMode
|
|
746
|
+
__getRestrictBash(): boolean
|
|
723
747
|
describeDenial(call: ToolCall): string | undefined
|
|
724
748
|
__promptGated(call: ToolCall, deps?: unknown): Promise<"allow" | "deny">
|
|
725
749
|
}
|
package/src/policy/pricing.ts
CHANGED
|
@@ -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
|
|
package/src/ui/footer.ts
CHANGED
|
@@ -16,6 +16,8 @@ export type TimerHighlight = "seconds" | "minutes" | "hours"
|
|
|
16
16
|
export interface FooterStatus {
|
|
17
17
|
/** Permission mode efektif sesi ini (umumnya AUTO atau ASK). */
|
|
18
18
|
mode: string
|
|
19
|
+
/** Bila true, bash dibatasi ke allow-list (lapis kapabilitas, bukan mode). */
|
|
20
|
+
strictBash?: boolean
|
|
19
21
|
/** WorkMode aktif sesi; identitas internal EXECUTE ditampilkan sebagai Build. */
|
|
20
22
|
workMode?: string
|
|
21
23
|
model: string
|
|
@@ -97,6 +99,11 @@ export function paintPermissionMode(mode: string, style: ModeLabelStyle = "full"
|
|
|
97
99
|
return `${c.gray(prefix)}${c.info(value)}`
|
|
98
100
|
}
|
|
99
101
|
|
|
102
|
+
/** Badge kecil pembatas bash allow-list (lapis kapabilitas, terpisah mode). */
|
|
103
|
+
export function paintBashStrict(strictBash?: boolean): string {
|
|
104
|
+
return strictBash ? c.yellow("bash:strict") : ""
|
|
105
|
+
}
|
|
106
|
+
|
|
100
107
|
/** Spark glyph: redup saat idle, pulse putih↔abu saat busy (frame genap/ganjil). */
|
|
101
108
|
function sparkGlyph(frame: number): string {
|
|
102
109
|
const g = glyphs.thinkingIcon
|
|
@@ -173,7 +180,9 @@ export function renderFooter(s: FooterStatus, columns: number): string[] {
|
|
|
173
180
|
return `${lead}${paintWorkMode(s.workMode, style)}`
|
|
174
181
|
}
|
|
175
182
|
const rightFor = (style: ModeLabelStyle): string =>
|
|
176
|
-
[...contextParts, paintPermissionMode(s.mode, style)
|
|
183
|
+
[...contextParts, paintPermissionMode(s.mode, style), paintBashStrict(s.strictBash)]
|
|
184
|
+
.filter(Boolean)
|
|
185
|
+
.join(fieldGap)
|
|
177
186
|
const align = (left: string, right: string): string | undefined => {
|
|
178
187
|
const leftWidth = displayWidth(left)
|
|
179
188
|
const rightWidth = displayWidth(right)
|
|
@@ -224,15 +233,16 @@ export function renderFooter(s: FooterStatus, columns: number): string[] {
|
|
|
224
233
|
// timer/model/CWD/label sebelum konteks; potong konteks lebih dahulu.
|
|
225
234
|
const workValue = paintWorkMode(s.workMode, "value")
|
|
226
235
|
const permissionValue = paintPermissionMode(s.mode, "value")
|
|
236
|
+
const strictValue = paintBashStrict(s.strictBash)
|
|
227
237
|
const shortWorkValue = workValue.trimEnd()
|
|
228
|
-
const modeOnly = `${spark} ${shortWorkValue} ${permissionValue}`
|
|
238
|
+
const modeOnly = `${spark} ${shortWorkValue} ${[permissionValue, strictValue].filter(Boolean).join(" ")}`
|
|
229
239
|
if (ctx || metrics) {
|
|
230
240
|
const context = [ctx, metrics].filter(Boolean).join(" ")
|
|
231
241
|
const baseWidth = displayWidth(modeOnly)
|
|
232
242
|
const contextBudget = target - baseWidth - 2
|
|
233
243
|
if (contextBudget > 0) {
|
|
234
244
|
const shortenedContext = truncateToWidth(context, contextBudget, "…")
|
|
235
|
-
const withContext = `${spark} ${shortWorkValue} ${c.gray(shortenedContext)} ${permissionValue}`
|
|
245
|
+
const withContext = `${spark} ${shortWorkValue} ${c.gray(shortenedContext)} ${[permissionValue, strictValue].filter(Boolean).join(" ")}`
|
|
236
246
|
if (displayWidth(withContext) <= target) return [withContext]
|
|
237
247
|
}
|
|
238
248
|
}
|