minicode-ai 0.27.0 → 0.30.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.
- package/README.md +1 -1
- package/cli/args.ts +184 -57
- package/cli/bootstrap/presentation-bridge.ts +383 -0
- package/cli/commands/acp.ts +45 -29
- package/cli/commands/auth.ts +17 -17
- package/cli/commands/config.ts +65 -66
- package/cli/commands/daemon.ts +44 -16
- package/cli/commands/doctor.ts +18 -15
- package/cli/commands/exec.ts +41 -89
- package/cli/commands/mcp.ts +25 -8
- package/cli/commands/memory.ts +16 -24
- package/cli/commands/openwork.ts +6 -2
- package/cli/commands/pricing.ts +14 -12
- package/cli/commands/providers.ts +36 -57
- package/cli/commands/sessions.ts +24 -18
- package/cli/commands/skills.ts +33 -18
- package/cli/commands/stats.ts +15 -30
- package/cli/commands.ts +73 -44
- package/cli/context-command-run.ts +30 -18
- package/cli/index.ts +51 -100
- package/cli/model-manager.ts +7 -2
- package/cli/provider-manager.ts +33 -18
- package/cli/router.ts +41 -12
- package/cli/setup.ts +241 -485
- package/cli/tui.ts +303 -100
- package/docs/TERMINAL_CONTRACT.md +258 -73
- package/docs/UI_RENDER_PIPELINE.md +9 -1
- package/docs/agents.md +2 -2
- package/docs/architecture.md +17 -32
- package/docs/choosing-mode.md +7 -9
- package/docs/cli.md +3 -5
- package/docs/environment.md +1 -2
- package/docs/policy-sandbox.md +3 -3
- package/docs/repl.md +2 -2
- package/docs/tools.md +1 -1
- package/package.json +1 -1
- package/src/app/mentions.ts +60 -13
- package/src/app/rag-layer.ts +17 -20
- package/src/app/session.ts +144 -68
- package/src/app/tool-layer.ts +38 -16
- package/src/config.ts +64 -104
- package/src/constants.ts +4 -6
- package/src/daemon/capability.ts +8 -1
- package/src/daemon/control.ts +3 -2
- package/src/daemon/server.ts +57 -0
- package/src/hooks/run.ts +73 -41
- package/src/lib/atomic-write.ts +31 -59
- package/src/lib/db-path.ts +47 -0
- package/src/lib/process-tree.ts +62 -0
- package/src/lib/public-http.ts +88 -23
- package/src/lib/trusted-exec.ts +48 -1
- package/src/lsp/client.ts +62 -17
- package/src/mcp/client.ts +85 -38
- package/src/mcp/http-transport.ts +28 -16
- package/src/mcp/transport.ts +42 -6
- package/src/memory/files.ts +164 -134
- package/src/memory/vector.ts +371 -420
- package/src/policy/approval-binding.ts +122 -45
- package/src/policy/bash-guard.ts +18 -7
- package/src/policy/compaction.ts +51 -18
- package/src/policy/context-window.ts +31 -8
- package/src/policy/context.ts +62 -36
- package/src/policy/executor.ts +17 -7
- package/src/policy/jail.ts +6 -8
- package/src/policy/microcompact.ts +4 -3
- package/src/policy/no-progress.ts +103 -22
- package/src/policy/permission.ts +36 -12
- package/src/policy/tool-contract.ts +74 -416
- package/src/policy/usage.ts +24 -2
- package/src/policy/verifier.ts +50 -2
- package/src/policy/work-mode.ts +28 -54
- package/src/presentation/adapter.ts +24 -16
- package/src/providers/anthropic.ts +16 -5
- package/src/providers/provision.ts +6 -56
- package/src/providers/responses.ts +38 -7
- package/src/providers/sse-bounds.ts +6 -4
- package/src/runtime/execution-journal.ts +4 -29
- package/src/sandbox/docker.ts +58 -34
- package/src/sandbox/mask.ts +157 -0
- package/src/sandbox/os.ts +107 -73
- package/src/session/authority.ts +29 -10
- package/src/session/context-projection.ts +6 -5
- package/src/session/db/runs.ts +397 -0
- package/src/session/db/schema.ts +358 -0
- package/src/session/open-work.ts +26 -3
- package/src/session/persistence.ts +74 -998
- package/src/session/verification.ts +170 -20
- package/src/skills/loader.ts +31 -16
- package/src/task/db/schema.ts +325 -0
- package/src/task/db/types.ts +269 -0
- package/src/task/production-scheduler.ts +13 -1
- package/src/task/store.ts +92 -1021
- package/src/telemetry/trace.ts +27 -0
- package/src/tools/ask_user.ts +1 -13
- package/src/tools/bash.ts +27 -92
- package/src/tools/code_run.ts +1 -1
- package/src/tools/context.ts +133 -0
- package/src/tools/delete_file.ts +3 -2
- package/src/tools/edit.ts +37 -72
- package/src/tools/git.ts +86 -9
- package/src/tools/glob.ts +26 -51
- package/src/tools/grep.ts +67 -57
- package/src/tools/hashline.ts +4 -0
- package/src/tools/lsp.ts +11 -8
- package/src/tools/mcp_call.ts +9 -6
- package/src/tools/memory.ts +56 -33
- package/src/tools/move_file.ts +36 -10
- package/src/tools/patch.ts +13 -21
- package/src/tools/read_file.ts +18 -25
- package/src/tools/read_image.ts +35 -33
- package/src/tools/submit_result.ts +29 -8
- package/src/tools/task.ts +74 -17
- package/src/tools/todo.ts +181 -74
- package/src/tools/web_fetch.ts +42 -2
- package/src/tools/web_search.ts +47 -8
- package/src/ui/approval/prompt.ts +206 -47
- package/src/ui/assistant/turn-status.ts +14 -5
- package/src/ui/footer.ts +4 -4
- package/src/ui/i18n/en.ts +47 -23
- package/src/ui/i18n/id.ts +76 -51
- package/src/ui/input/input.ts +4 -3
- package/src/ui/input/prompt-engine.ts +35 -5
- package/src/ui/render/activity-panel.ts +4 -2
- package/src/ui/render/hint.ts +7 -4
- package/src/ui/render/list.ts +32 -0
- package/src/ui/render/markdown.ts +6 -3
- package/src/ui/render/notice.ts +19 -0
- package/src/ui/render/table-grid.ts +7 -8
- package/src/ui/render/theme.ts +113 -39
- package/src/ui/runtime/screen.ts +74 -10
- package/src/ui/runtime/spinner.ts +19 -6
- package/src/ui/screens/dialog.ts +19 -1
- package/src/ui/screens/form.ts +80 -35
- package/src/ui/screens/model-manager.ts +82 -61
- package/src/ui/screens/output-window.ts +263 -0
- package/src/ui/screens/picker.ts +94 -81
- package/src/ui/screens/provider-manager.ts +100 -69
- package/src/ui/screens/wizard.ts +117 -28
- package/src/ui/tui/app.ts +196 -33
- package/src/ui/tui/transcript.ts +158 -25
- package/vendor/minicore/src/core/executor.ts +2 -0
- package/vendor/minicore/src/core/loop.ts +17 -6
- package/vendor/minicore/src/core/session.ts +3 -0
- package/vendor/minicore/src/core/tool.ts +1 -0
- package/vendor/minicore/src/providers/openai-compat.ts +35 -19
package/src/app/session.ts
CHANGED
|
@@ -51,26 +51,47 @@ import { DEFAULT_SESSION_MAX_STEPS, nextStepLimit } from "../policy/step-escalat
|
|
|
51
51
|
import { getCachedTokenizer, loadTokenizer } from "../policy/tokenizer-registry.ts"
|
|
52
52
|
import { contractForTool } from "../policy/tool-contract.ts"
|
|
53
53
|
import { maxTurnsExceededError } from "../policy/usage.ts"
|
|
54
|
-
import {
|
|
54
|
+
import {
|
|
55
|
+
createWorkModePermissionHandler,
|
|
56
|
+
normalizeWorkMode,
|
|
57
|
+
type WorkMode,
|
|
58
|
+
} from "../policy/work-mode.ts"
|
|
55
59
|
import type { ApprovalEventHook } from "../presentation/events.ts"
|
|
56
60
|
|
|
57
|
-
// Kebijakan kompaksi lebih agresif: ringkas lebih DINI
|
|
58
|
-
//
|
|
59
|
-
//
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
61
|
+
// Kebijakan kompaksi lebih agresif: ringkas lebih DINI daripada default kernel
|
|
62
|
+
// (75%/90%), supaya konteks jarang menyentuh dinding. Untuk jendela model
|
|
63
|
+
// nyata (>= 16k), selaraskan ambang medium/high dengan `contextBudgetFor` agar
|
|
64
|
+
// kompaksi berjalan SEBELUM `limit - outputReserve` (mis. 60% pada 32k tanpa
|
|
65
|
+
// maxOutputTokens) memicu overflow guard di dynamicProvider; jendela sintetis
|
|
66
|
+
// kecil (< 16k di test) mempertahankan rasio 65%/85%.
|
|
67
|
+
export function createAggressiveBudgetPolicy(
|
|
68
|
+
getMaxOutputTokens?: () => number | undefined,
|
|
69
|
+
): BudgetPolicy {
|
|
70
|
+
return {
|
|
71
|
+
evaluate({ usedTokens, limitTokens }) {
|
|
72
|
+
const limit = Math.max(1, limitTokens)
|
|
73
|
+
if (usedTokens >= limit) return "critical"
|
|
74
|
+
if (limit >= 16_000) {
|
|
75
|
+
const b = contextBudgetFor(limit, getMaxOutputTokens?.())
|
|
76
|
+
const highAt = Math.min(Math.floor(limit * 0.85), b.blockingLimit)
|
|
77
|
+
const mediumAt = Math.min(Math.floor(limit * 0.65), b.compactTrigger)
|
|
78
|
+
if (usedTokens >= highAt) return "high"
|
|
79
|
+
if (usedTokens >= mediumAt) return "medium"
|
|
80
|
+
return "low"
|
|
81
|
+
}
|
|
82
|
+
const ratio = usedTokens / limit
|
|
83
|
+
if (ratio >= 0.85) return "high"
|
|
84
|
+
if (ratio >= 0.65) return "medium"
|
|
85
|
+
return "low"
|
|
86
|
+
},
|
|
87
|
+
shouldCompact(pressure) {
|
|
88
|
+
return pressure !== "low"
|
|
89
|
+
},
|
|
90
|
+
}
|
|
72
91
|
}
|
|
73
92
|
|
|
93
|
+
export const aggressiveBudgetPolicy: BudgetPolicy = createAggressiveBudgetPolicy()
|
|
94
|
+
|
|
74
95
|
// P2 cap wrapper: limit retryAfter to 30s without mutating original error.
|
|
75
96
|
//
|
|
76
97
|
// [T-009 OBSERVATION.md] Anggaran pemulihan TIDAK lagi dinyatakan sebagai "berapa
|
|
@@ -159,6 +180,14 @@ export function createRecoveryLedger(now: () => number = Date.now): RecoveryLedg
|
|
|
159
180
|
export const cappedRecovery = {
|
|
160
181
|
onError(error: ProviderError, attempt: number): RecoveryAction {
|
|
161
182
|
const category = error.category
|
|
183
|
+
// 402 / kuota habis tidak akan sembuh dengan retry — fail-fast.
|
|
184
|
+
if (
|
|
185
|
+
category === "unknown" &&
|
|
186
|
+
(/\b402\b/.test(error.message) ||
|
|
187
|
+
/quota_exceeded|insufficient_quota|exceeded its quota/i.test(error.message))
|
|
188
|
+
) {
|
|
189
|
+
return { type: "throw" }
|
|
190
|
+
}
|
|
162
191
|
const ra = error.retryAfterMs
|
|
163
192
|
const saneRetryAfter =
|
|
164
193
|
ra != null && Number.isFinite(ra) && ra >= 0
|
|
@@ -239,19 +268,12 @@ export type WorkModeControl = {
|
|
|
239
268
|
export function workModeHint(workMode: WorkMode): string {
|
|
240
269
|
if (workMode === "DISCUSS") {
|
|
241
270
|
return (
|
|
242
|
-
"\n\nDISCUSS MODE: You are in read-only investigation and
|
|
271
|
+
"\n\nDISCUSS MODE: You are in read-only investigation, discussion, and planning mode. " +
|
|
243
272
|
"Do NOT modify files, run bash, or use write/edit tools. " +
|
|
244
|
-
"Read, search, and reason — investigate the question,
|
|
273
|
+
"Read, search, and reason — investigate the question thoroughly, discuss findings, options, and trade-offs with the user, and produce a concrete implementation plan. " +
|
|
245
274
|
"Do not implement or execute any change."
|
|
246
275
|
)
|
|
247
276
|
}
|
|
248
|
-
if (workMode === "PLAN") {
|
|
249
|
-
return (
|
|
250
|
-
"\n\nPLAN MODE: You are in planning mode. " +
|
|
251
|
-
"Do NOT modify files, run bash, or use write/edit tools. " +
|
|
252
|
-
"Only read, search, and reason — investigate thoroughly, then produce a concrete implementation plan."
|
|
253
|
-
)
|
|
254
|
-
}
|
|
255
277
|
return ""
|
|
256
278
|
}
|
|
257
279
|
|
|
@@ -283,6 +305,8 @@ export async function createMinicodeSession(
|
|
|
283
305
|
* EXECUTE memasang ceiling tipis di atas handler PM (lihat work-mode.ts);
|
|
284
306
|
* belum ada permukaan CLI/TUI — wiring itu slice integrasi berikutnya. */
|
|
285
307
|
workMode?: WorkMode
|
|
308
|
+
/** Izinkan callback `ask` non-TTY (mis. broker persetujuan daemon) untuk tool gated. */
|
|
309
|
+
allowNonTtyAsk?: boolean
|
|
286
310
|
/** Teruskan flag --allow-local-config ke permission handler (allowlist
|
|
287
311
|
* lokal). Default deny — lihat loadAllowlist. */
|
|
288
312
|
allowLocalConfig?: boolean
|
|
@@ -304,6 +328,10 @@ export async function createMinicodeSession(
|
|
|
304
328
|
noProgress?: NoProgressOptions | false
|
|
305
329
|
/** Hook opsional agar composition root dapat mereset riwayat osilasi saat verify hijau. */
|
|
306
330
|
onNoProgress?: (detector: NoProgressDetector) => void
|
|
331
|
+
/** Mengembalikan `true` jika batas jendela model dipin eksplisit oleh user
|
|
332
|
+
* (`--context-window` atau `origin: "user"`), sehingga guard estimasi
|
|
333
|
+
* lokal tidak boleh menaikkan jendela tanpa bukti error dari provider. */
|
|
334
|
+
isExplicitContextWindow?: (model?: string) => boolean
|
|
307
335
|
/**
|
|
308
336
|
* Dipanggil saat jendela konteks dinaikkan otomatis setelah overflow
|
|
309
337
|
* terverifikasi. Composition root memakainya untuk memberi tahu user
|
|
@@ -381,6 +409,9 @@ export async function createMinicodeSession(
|
|
|
381
409
|
* ini perilaku identik seperti sebelumnya.
|
|
382
410
|
*/
|
|
383
411
|
beforeToolEffect?: (call: { id: string; name: string; args: unknown }) => Promise<void>
|
|
412
|
+
/** Tool tak-tersedia yang skemanya disembunyikan pada request wire LLM
|
|
413
|
+
* (tidak mengubah registry `tools`/permission kanonik). */
|
|
414
|
+
hiddenWireTools?: ReadonlySet<string>
|
|
384
415
|
},
|
|
385
416
|
): Promise<Session> {
|
|
386
417
|
// Normalisasi root sekali di composition agar ToolContext selalu memiliki path tepercaya.
|
|
@@ -388,7 +419,7 @@ export async function createMinicodeSession(
|
|
|
388
419
|
// Fail-closed lebih awal (sebelum provider/system prompt): WorkMode non-
|
|
389
420
|
// EXECUTE di atas handler terinjeksi belum punya komposisi yang terbukti
|
|
390
421
|
// aman — jangan pernah membungkus atau melewati policy yang diinjeksi 6S.
|
|
391
|
-
const resolvedWorkMode: WorkMode = opts.workMode ?? "EXECUTE"
|
|
422
|
+
const resolvedWorkMode: WorkMode = normalizeWorkMode(opts.workMode ?? "EXECUTE")
|
|
392
423
|
if (opts.permissionHandler !== undefined && resolvedWorkMode !== "EXECUTE") {
|
|
393
424
|
throw new Error(
|
|
394
425
|
`createMinicodeSession: workMode ${resolvedWorkMode} dengan permissionHandler terinjeksi belum didukung (fail-closed S2.1)`,
|
|
@@ -405,6 +436,7 @@ export async function createMinicodeSession(
|
|
|
405
436
|
extra: opts.systemExtra ?? "",
|
|
406
437
|
signal: (opts as unknown as { signal?: AbortSignal }).signal,
|
|
407
438
|
lang: opts.lang,
|
|
439
|
+
allowLocalConfig: opts.allowLocalConfig,
|
|
408
440
|
})
|
|
409
441
|
const {
|
|
410
442
|
concurrency,
|
|
@@ -412,6 +444,7 @@ export async function createMinicodeSession(
|
|
|
412
444
|
permissionMode,
|
|
413
445
|
restrictBash,
|
|
414
446
|
workMode: _workMode,
|
|
447
|
+
allowNonTtyAsk,
|
|
415
448
|
allowLocalConfig,
|
|
416
449
|
systemExtra: _extra,
|
|
417
450
|
getContextModel: _getContextModel,
|
|
@@ -428,6 +461,7 @@ export async function createMinicodeSession(
|
|
|
428
461
|
noProgress,
|
|
429
462
|
onNoProgress,
|
|
430
463
|
approvalBindings: _approvalBindings,
|
|
464
|
+
hiddenWireTools,
|
|
431
465
|
...rest
|
|
432
466
|
} = opts
|
|
433
467
|
if (!provider) throw new Error("createMinicodeSession: provider is required")
|
|
@@ -486,7 +520,8 @@ export async function createMinicodeSession(
|
|
|
486
520
|
mode: permissionMode ?? "auto",
|
|
487
521
|
root: defaultCwd,
|
|
488
522
|
ask,
|
|
489
|
-
|
|
523
|
+
allowNonTtyAsk,
|
|
524
|
+
allowLocalConfig: allowLocalConfig === true,
|
|
490
525
|
restrictBash,
|
|
491
526
|
onApprovalEvent,
|
|
492
527
|
authorizedTools,
|
|
@@ -515,8 +550,7 @@ export async function createMinicodeSession(
|
|
|
515
550
|
if (pm === "readonly") return "readonly"
|
|
516
551
|
if (pm === "plan") return "plan"
|
|
517
552
|
const wm = wmCtl.__getWorkMode?.() ?? resolvedWorkMode
|
|
518
|
-
if (wm === "DISCUSS") return "
|
|
519
|
-
if (wm === "PLAN") return "plan"
|
|
553
|
+
if (wm === "DISCUSS") return "plan"
|
|
520
554
|
if (isAllToolsPureRead()) return "explore"
|
|
521
555
|
return "active"
|
|
522
556
|
}
|
|
@@ -559,32 +593,44 @@ export async function createMinicodeSession(
|
|
|
559
593
|
// mengubah salinan MiniCore yang wajib disinkronkan dari repo sumber.
|
|
560
594
|
// Handler ASK yang sah tetap menunggu approval lalu mengembalikan allow/deny.
|
|
561
595
|
if (decision !== "allow") return "deny"
|
|
596
|
+
const recordStaleDeny = (reason: string): "deny" => {
|
|
597
|
+
const id = (call as { id?: string } | null)?.id
|
|
598
|
+
if (id) {
|
|
599
|
+
if (staleDenyReasons.size > 500)
|
|
600
|
+
staleDenyReasons.delete(staleDenyReasons.keys().next().value as string)
|
|
601
|
+
staleDenyReasons.set(id, reason)
|
|
602
|
+
}
|
|
603
|
+
return "deny"
|
|
604
|
+
}
|
|
562
605
|
// Ambil (konsumsi) bukti persetujuan milik call ini — sekali saja.
|
|
563
606
|
// Diambil SEBELUM await supaya tak ada binding tersisa kalau barrier
|
|
564
607
|
// memblokir call (tak ada deny lalu tak ada jejak).
|
|
565
|
-
const binding = approvalBindings.take(call.name, canonicalArgsHash(call.args))
|
|
608
|
+
const binding = approvalBindings.take(call.name, canonicalArgsHash(call.args), call.id)
|
|
609
|
+
// Verifikasi awal SEBELUM preflightIntent menulis record `pending` ke
|
|
610
|
+
// jurnal agar persetujuan basi / pergeseran mode tidak menodai jurnal.
|
|
611
|
+
const preVerdict = verifyApprovalBinding(binding)
|
|
612
|
+
if (authorizedVersion !== authorizationVersion || preVerdict.kind === "stale") {
|
|
613
|
+
return recordStaleDeny(
|
|
614
|
+
authorizedVersion !== authorizationVersion
|
|
615
|
+
? "authorization changed while preparing tool execution; request fresh approval"
|
|
616
|
+
: `approval stale: ${preVerdict.kind === "stale" ? preVerdict.reason : "changed"}`,
|
|
617
|
+
)
|
|
618
|
+
}
|
|
566
619
|
if (detector !== null) {
|
|
567
620
|
await detector.beforeToolEffectCheck(call, effectiveRoot)
|
|
568
621
|
}
|
|
569
622
|
if (beforeToolEffect !== undefined) {
|
|
570
623
|
await beforeToolEffect(call)
|
|
571
624
|
}
|
|
572
|
-
// Verifikasi lapis-2 (batas eksekusi): fail-closed
|
|
573
|
-
//
|
|
625
|
+
// Verifikasi lapis-2 (batas eksekusi): fail-closed bila target atau mode
|
|
626
|
+
// berubah selama await no-progress / intent jurnal berlangsung.
|
|
574
627
|
const verdict = verifyApprovalBinding(binding)
|
|
575
628
|
if (authorizedVersion !== authorizationVersion || verdict.kind === "stale") {
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
id,
|
|
582
|
-
authorizedVersion !== authorizationVersion
|
|
583
|
-
? "authorization changed while preparing tool execution; request fresh approval"
|
|
584
|
-
: `approval stale: ${verdict.kind === "stale" ? verdict.reason : "changed"}`,
|
|
585
|
-
)
|
|
586
|
-
}
|
|
587
|
-
return "deny"
|
|
629
|
+
return recordStaleDeny(
|
|
630
|
+
authorizedVersion !== authorizationVersion
|
|
631
|
+
? "authorization changed while preparing tool execution; request fresh approval"
|
|
632
|
+
: `approval stale: ${verdict.kind === "stale" ? verdict.reason : "changed"}`,
|
|
633
|
+
)
|
|
588
634
|
}
|
|
589
635
|
return "allow"
|
|
590
636
|
},
|
|
@@ -630,8 +676,9 @@ export async function createMinicodeSession(
|
|
|
630
676
|
}
|
|
631
677
|
onWorkMode({
|
|
632
678
|
setWorkMode: (m) => {
|
|
633
|
-
|
|
634
|
-
wm.
|
|
679
|
+
const norm = normalizeWorkMode(m)
|
|
680
|
+
if (wm.__getWorkMode() !== norm) authorizationVersion++
|
|
681
|
+
wm.__setWorkMode(norm)
|
|
635
682
|
},
|
|
636
683
|
getWorkMode: () => wm.__getWorkMode(),
|
|
637
684
|
})
|
|
@@ -750,7 +797,15 @@ export async function createMinicodeSession(
|
|
|
750
797
|
* satu eskalasi. Namun ia tetap BOUNDED: satu langkah per kejadian, tak
|
|
751
798
|
* pernah melebihi batas atas, dan hasilnya hanya berlaku untuk sesi ini.
|
|
752
799
|
*/
|
|
753
|
-
const escalateContextWindow = (
|
|
800
|
+
const escalateContextWindow = (
|
|
801
|
+
model: string | undefined,
|
|
802
|
+
observedTokens: number,
|
|
803
|
+
fromClientGuard = false,
|
|
804
|
+
) => {
|
|
805
|
+
// Jika operator mem-pin jendela eksplisit (--context-window / origin user),
|
|
806
|
+
// guard kalibrasi lokal saja tidak boleh menaikkan pin tersebut tanpa
|
|
807
|
+
// bukti error kapasitas dari provider.
|
|
808
|
+
if (fromClientGuard && rest.isExplicitContextWindow?.(model)) return
|
|
754
809
|
const key = escalationKey(model)
|
|
755
810
|
const base = escalatedWindows.get(key) ?? contextWindowBase(model)
|
|
756
811
|
// Bila pengamatan sudah jelas melampaui jendela dasar, naik dari nilai
|
|
@@ -800,12 +855,16 @@ export async function createMinicodeSession(
|
|
|
800
855
|
const system = computeLiveSystem()
|
|
801
856
|
const model = req.model ?? currentContextModel()
|
|
802
857
|
let limit = contextWindowForModel(model)
|
|
803
|
-
|
|
858
|
+
let maxOutputTokens = maxOutputTokensForModel(model)
|
|
804
859
|
const estimatorModel = model?.includes("::") ? model : (currentContextModel() ?? model)
|
|
805
860
|
let calibration = calibrationFor(estimatorModel)
|
|
806
861
|
let estimator = (text: string) => preciseEstimator(estimatorModel, text)
|
|
807
862
|
let admittedFactor = calibration.snapshot().factor
|
|
808
|
-
const
|
|
863
|
+
const rawTools = req.tools ?? []
|
|
864
|
+
const tools =
|
|
865
|
+
hiddenWireTools && hiddenWireTools.size > 0
|
|
866
|
+
? rawTools.filter((t) => !hiddenWireTools.has(t.name))
|
|
867
|
+
: rawTools
|
|
809
868
|
const localInputTokens =
|
|
810
869
|
estimateMessages(req.messages, minicodeEstimator) +
|
|
811
870
|
estimateSystem(system, minicodeEstimator) +
|
|
@@ -855,22 +914,21 @@ export async function createMinicodeSession(
|
|
|
855
914
|
storeMessages: req.messages.length,
|
|
856
915
|
},
|
|
857
916
|
})
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
//
|
|
865
|
-
//
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
// 429/402/auth/jaringan.
|
|
869
|
-
escalateContextWindow(model, actualInput + actualOutput)
|
|
870
|
-
return new ProviderError(
|
|
917
|
+
let clientOverflowErr: ProviderError | undefined
|
|
918
|
+
const overflow = (
|
|
919
|
+
actualInput = inputTokens,
|
|
920
|
+
actualOutput = outputTokens,
|
|
921
|
+
clientGuard = false,
|
|
922
|
+
) => {
|
|
923
|
+
// context_length_exceeded agar kernel menempuh force_compact_and_retry,
|
|
924
|
+
// plus eskalasi adaptif terikat (hanya jalur overflow, bukan 429/402/auth).
|
|
925
|
+
escalateContextWindow(model, actualInput + actualOutput, clientGuard)
|
|
926
|
+
const err = new ProviderError(
|
|
871
927
|
"context_length_exceeded",
|
|
872
928
|
`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`,
|
|
873
929
|
)
|
|
930
|
+
if (clientGuard) clientOverflowErr = err
|
|
931
|
+
return err
|
|
874
932
|
}
|
|
875
933
|
const requestController = new AbortController()
|
|
876
934
|
const forwardAbort = () => requestController.abort(signal.reason)
|
|
@@ -878,13 +936,20 @@ export async function createMinicodeSession(
|
|
|
878
936
|
else signal.addEventListener("abort", forwardAbort, { once: true })
|
|
879
937
|
try {
|
|
880
938
|
if (inputTokens + reserve >= limit) {
|
|
881
|
-
const error = overflow(inputTokens, 0)
|
|
939
|
+
const error = overflow(inputTokens, 0, true)
|
|
882
940
|
requestController.abort(error)
|
|
883
941
|
yield contextLimitEvent(inputTokens, 0)
|
|
884
942
|
throw error
|
|
885
943
|
}
|
|
886
944
|
|
|
887
|
-
for await (const event of provider.stream(
|
|
945
|
+
for await (const event of provider.stream(
|
|
946
|
+
{
|
|
947
|
+
...req,
|
|
948
|
+
system,
|
|
949
|
+
...(hiddenWireTools && hiddenWireTools.size > 0 ? { tools } : {}),
|
|
950
|
+
},
|
|
951
|
+
requestController.signal,
|
|
952
|
+
)) {
|
|
888
953
|
if (event.type === "extension" && event.kind === "effective-model") {
|
|
889
954
|
const effective = event.data as { effective?: unknown; provider?: unknown } | undefined
|
|
890
955
|
if (
|
|
@@ -896,6 +961,7 @@ export async function createMinicodeSession(
|
|
|
896
961
|
// a failed primary provider cannot silently widen the budget.
|
|
897
962
|
const effectiveModel = `${effective.provider}::${effective.effective}`
|
|
898
963
|
const effectiveLimit = contextWindowForModel(effectiveModel)
|
|
964
|
+
maxOutputTokens = maxOutputTokensForModel(effectiveModel) ?? maxOutputTokens
|
|
899
965
|
limit = Math.min(limit, effectiveLimit)
|
|
900
966
|
calibration = calibrationFor(effectiveModel)
|
|
901
967
|
estimator = (text: string) => preciseEstimator(effectiveModel, text)
|
|
@@ -934,7 +1000,7 @@ export async function createMinicodeSession(
|
|
|
934
1000
|
}
|
|
935
1001
|
}
|
|
936
1002
|
if (added > 0 && outputTokens + added > outputAllowance) {
|
|
937
|
-
const error = overflow(inputTokens, outputTokens + added)
|
|
1003
|
+
const error = overflow(inputTokens, outputTokens + added, true)
|
|
938
1004
|
requestController.abort(error)
|
|
939
1005
|
yield contextLimitEvent(inputTokens, outputTokens + added)
|
|
940
1006
|
throw error
|
|
@@ -943,6 +1009,13 @@ export async function createMinicodeSession(
|
|
|
943
1009
|
yield event as ProviderEvent
|
|
944
1010
|
}
|
|
945
1011
|
} catch (error) {
|
|
1012
|
+
if (error instanceof ProviderError && error.category === "context_length_exceeded") {
|
|
1013
|
+
escalateContextWindow(
|
|
1014
|
+
model,
|
|
1015
|
+
Math.max(inputTokens + outputTokens, limit),
|
|
1016
|
+
error === clientOverflowErr,
|
|
1017
|
+
)
|
|
1018
|
+
}
|
|
946
1019
|
// Generic 400 "invalid_request" dari gateway (tiprouter dll) yang BISA
|
|
947
1020
|
// sebenarnya konteks kepanjangan meski teksnya tak menyebut "context".
|
|
948
1021
|
// Reklasifikasi sebagai context_length_exceeded supaya kernel menempuh
|
|
@@ -958,7 +1031,7 @@ export async function createMinicodeSession(
|
|
|
958
1031
|
(error.category === "invalid_request" || error.category === "unknown") &&
|
|
959
1032
|
localInputTokens >= contextBudgetFor(limit, maxOutputTokens).reclassifyThreshold
|
|
960
1033
|
) {
|
|
961
|
-
throw overflow(inputTokens, outputTokens)
|
|
1034
|
+
throw overflow(inputTokens, outputTokens, false)
|
|
962
1035
|
}
|
|
963
1036
|
throw error
|
|
964
1037
|
} finally {
|
|
@@ -986,7 +1059,7 @@ export async function createMinicodeSession(
|
|
|
986
1059
|
maxSteps: effectiveMaxSteps,
|
|
987
1060
|
maxStepsForRun: () => effectiveMaxSteps,
|
|
988
1061
|
// Kompaksi lebih agresif (host-side): ringkas mulai 65% jendela, bukan 75%.
|
|
989
|
-
budget:
|
|
1062
|
+
budget: createAggressiveBudgetPolicy(() => maxOutputTokensForModel(currentContextModel())),
|
|
990
1063
|
provider: dynamicProvider,
|
|
991
1064
|
system: computeLiveSystem(),
|
|
992
1065
|
systemForContext: computeLiveSystem,
|
|
@@ -1007,10 +1080,12 @@ export async function createMinicodeSession(
|
|
|
1007
1080
|
lastProviderInputTokens = undefined
|
|
1008
1081
|
})
|
|
1009
1082
|
core.events.on("provider:extension", (e) => {
|
|
1083
|
+
if ((e as { forwardedChild?: unknown }).forwardedChild != null) return
|
|
1010
1084
|
if (e.kind !== "usage") return
|
|
1011
1085
|
const d = e.data as
|
|
1012
1086
|
| {
|
|
1013
1087
|
inputTokens?: number
|
|
1088
|
+
outputTokens?: number
|
|
1014
1089
|
totalTokens?: number
|
|
1015
1090
|
model?: string
|
|
1016
1091
|
provider?: string
|
|
@@ -1040,6 +1115,7 @@ export async function createMinicodeSession(
|
|
|
1040
1115
|
calibrationFor(reportedModel).observeProviderUsage({
|
|
1041
1116
|
localEstimatedTokens,
|
|
1042
1117
|
providerInputTokens: d.inputTokens,
|
|
1118
|
+
providerOutputTokens: d.outputTokens,
|
|
1043
1119
|
providerTotalTokens: d.totalTokens,
|
|
1044
1120
|
})
|
|
1045
1121
|
})
|
|
@@ -1140,7 +1216,7 @@ export async function createMinicodeSession(
|
|
|
1140
1216
|
if (reason === "fixpoint" || reason === "critical") {
|
|
1141
1217
|
const used =
|
|
1142
1218
|
typeof error.details?.fixedTokens === "number" ? error.details.fixedTokens : 0
|
|
1143
|
-
escalateContextWindow(modelForRun, used)
|
|
1219
|
+
escalateContextWindow(modelForRun, used, true)
|
|
1144
1220
|
}
|
|
1145
1221
|
}
|
|
1146
1222
|
// [T-008] Turn mentok batas langkah: naik satu tangga untuk turn
|
package/src/app/tool-layer.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { configureServers as lspConfigure } from "../lsp/client.ts"
|
|
|
4
4
|
import { connectAll as mcpConnectAll } from "../mcp/client.ts"
|
|
5
5
|
import {
|
|
6
6
|
EXPLORE_TOOL_NAMES,
|
|
7
|
-
LOCAL_READONLY_TOOL_NAMES,
|
|
8
7
|
PLAN_EXTRA_TOOL_NAMES,
|
|
9
8
|
READONLY_TOOL_NAMES,
|
|
10
9
|
} from "../policy/tool-contract.ts"
|
|
@@ -19,27 +18,50 @@ const errMsg = (e: unknown) => (e instanceof Error ? e.message : String(e))
|
|
|
19
18
|
|
|
20
19
|
export type ToolScope = "full" | "explore"
|
|
21
20
|
|
|
22
|
-
const
|
|
21
|
+
const MCP_WIRE_TOOLS = ["mcp_list", "mcp_call", "mcp_read", "mcp_prompt"] as const
|
|
22
|
+
const LSP_WIRE_TOOLS = [
|
|
23
|
+
"lsp_diagnostics",
|
|
24
|
+
"lsp_definition",
|
|
25
|
+
"lsp_references",
|
|
26
|
+
"lsp_hover",
|
|
27
|
+
"lsp_symbols",
|
|
28
|
+
"lsp_workspace_symbols",
|
|
29
|
+
] as const
|
|
23
30
|
|
|
24
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Daftar tool yang skemanya aman disembunyikan dari wire API ketika kapabilitas
|
|
33
|
+
* runtime-nya tidak aktif (MCP/LSP kosong, sandbox none, atau non-interaktif),
|
|
34
|
+
* agar token prompt awal hemat tanpa mengubah registry izin/WorkMode kanonik.
|
|
35
|
+
*/
|
|
36
|
+
export function computeUnavailableWireTools(
|
|
37
|
+
cfg: Pick<MinicodeConfig, "mcpServers" | "lspServers">,
|
|
38
|
+
opts: { sandboxMode?: string; interactiveAsk?: boolean } = {},
|
|
39
|
+
): Set<string> {
|
|
40
|
+
const hidden = new Set<string>()
|
|
41
|
+
if (!cfg.mcpServers?.length) for (const n of MCP_WIRE_TOOLS) hidden.add(n)
|
|
42
|
+
if (!cfg.lspServers?.length) for (const n of LSP_WIRE_TOOLS) hidden.add(n)
|
|
43
|
+
if (!opts.sandboxMode || opts.sandboxMode === "none") hidden.add("code_run")
|
|
44
|
+
if (opts.interactiveAsk === false) hidden.add("ask_user")
|
|
45
|
+
return hidden
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// WorkMode ceiling (DISCUSS menyatukan diskusi + perencanaan; EXECUTE = BUILD).
|
|
25
49
|
// Invariant INV-WM1 & #7: WorkMode TIDAK PERNAH memperluas akses; `visible`
|
|
26
50
|
// diselaraskan penuh dengan `callable` di handler:
|
|
27
|
-
// - DISCUSS:
|
|
28
|
-
// `web_fetch` & `web_search` ditolak dan disembunyikan.
|
|
29
|
-
// - PLAN: 18 `READONLY_TOOL_NAMES` (16 lokal + 2 network read ber-approval) +
|
|
51
|
+
// - DISCUSS: 18 `READONLY_TOOL_NAMES` (16 lokal + 2 network read ber-approval) +
|
|
30
52
|
// `PLAN_EXTRA` (kecuali bila `permissionMode` adalah `readonly`/`allowlist`
|
|
31
53
|
// yang ceiling-nya `readonly`).
|
|
32
54
|
// - EXECUTE: tanpa ceiling WorkMode tambahan; mengikuti `permissionMode`.
|
|
33
55
|
export function workModeVisible(
|
|
34
56
|
name: string,
|
|
35
|
-
workMode: "DISCUSS" | "
|
|
57
|
+
workMode: "DISCUSS" | "EXECUTE",
|
|
36
58
|
permissionMode?: string,
|
|
37
59
|
): boolean {
|
|
60
|
+
const wm = workMode === "EXECUTE" ? "EXECUTE" : "DISCUSS"
|
|
38
61
|
if (isMcpToolName(name)) {
|
|
39
|
-
return
|
|
62
|
+
return wm === "EXECUTE" && permissionMode !== "readonly" && permissionMode !== "plan"
|
|
40
63
|
}
|
|
41
|
-
if (
|
|
42
|
-
if (workMode === "PLAN") {
|
|
64
|
+
if (wm === "DISCUSS") {
|
|
43
65
|
if (permissionMode === "readonly" || permissionMode === "allowlist") {
|
|
44
66
|
return READONLY_TOOL_NAMES.has(name)
|
|
45
67
|
}
|
|
@@ -54,7 +76,7 @@ export function workModeVisible(
|
|
|
54
76
|
|
|
55
77
|
export function applyWorkModeFilter<T extends Tool>(
|
|
56
78
|
tools: readonly T[],
|
|
57
|
-
workMode: "DISCUSS" | "
|
|
79
|
+
workMode: "DISCUSS" | "EXECUTE",
|
|
58
80
|
permissionMode?: string,
|
|
59
81
|
): T[] {
|
|
60
82
|
return tools.filter((t) => workModeVisible(t.name, workMode, permissionMode))
|
|
@@ -68,13 +90,13 @@ export async function setupToolLayer(
|
|
|
68
90
|
permissionMode?: string,
|
|
69
91
|
verification?: CanonicalScope,
|
|
70
92
|
jobOwnerSid?: string,
|
|
71
|
-
workMode: "DISCUSS" | "
|
|
93
|
+
workMode: "DISCUSS" | "EXECUTE" = "EXECUTE",
|
|
72
94
|
cwd?: string,
|
|
73
95
|
): Promise<{
|
|
74
96
|
sessionTools: Tool[]
|
|
75
97
|
evidenceMode: ToolEvidenceMode
|
|
76
98
|
/** P12 S2.2 Slice 5 - set WorkMode runtime (visibilitas tool). */
|
|
77
|
-
setWorkMode?: (m: "DISCUSS" | "
|
|
99
|
+
setWorkMode?: (m: "DISCUSS" | "EXECUTE") => void
|
|
78
100
|
/** P0.2 - set PermissionMode runtime agar visibilitas tetap sinkron saat /mode atau Shift+Tab. */
|
|
79
101
|
setPermissionMode?: (pm: string) => void
|
|
80
102
|
}> {
|
|
@@ -124,9 +146,9 @@ export async function setupToolLayer(
|
|
|
124
146
|
// PermissionMode). `parallelExecutor` meresolusi `registry.get(name)` dari
|
|
125
147
|
// `list()` yang sama sehingga `callable === visible` di kedua arah transisi.
|
|
126
148
|
const candidates = sessionTools
|
|
127
|
-
let currentWm: "DISCUSS" | "
|
|
149
|
+
let currentWm: "DISCUSS" | "EXECUTE" = workMode
|
|
128
150
|
let currentPm: string | undefined = permissionMode
|
|
129
|
-
const computeVisible = (wm: "DISCUSS" | "
|
|
151
|
+
const computeVisible = (wm: "DISCUSS" | "EXECUTE", pm?: string): Tool[] => {
|
|
130
152
|
if (scope === "explore") {
|
|
131
153
|
// Scope explore sudah dibatasi ke EXPLORE_TOOL_NAMES; tetap terapkan irisan WorkMode.
|
|
132
154
|
return applyWorkModeFilter(candidates, wm, pm)
|
|
@@ -140,7 +162,7 @@ export async function setupToolLayer(
|
|
|
140
162
|
visible.length = 0
|
|
141
163
|
visible.push(...next)
|
|
142
164
|
}
|
|
143
|
-
const setWorkMode = (wm: "DISCUSS" | "
|
|
165
|
+
const setWorkMode = (wm: "DISCUSS" | "EXECUTE"): void => {
|
|
144
166
|
currentWm = wm
|
|
145
167
|
syncVisible()
|
|
146
168
|
}
|