minicode-ai 0.14.1 → 0.14.4
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 +34 -178
- package/cli/args.ts +10 -18
- package/cli/auto-update.ts +32 -2
- package/cli/commands/acp.ts +20 -27
- package/cli/commands/daemon.ts +6 -7
- package/cli/commands/exec.ts +12 -10
- package/cli/commands.ts +6 -9
- package/cli/index.ts +40 -17
- package/cli/provider-manager.ts +1 -3
- package/cli/setup.ts +174 -145
- package/cli/tui.ts +33 -38
- package/docs/CONTROL-PLANE-MAP.md +145 -0
- package/docs/OUTPUT_EVENT_MODEL.md +1 -1
- package/docs/OUTPUT_PROTOCOL_SPEC.md +8 -2
- package/docs/OUTPUT_RENDERING_SPEC.md +35 -6
- package/docs/OUTPUT_UX_RULES.md +1 -1
- package/docs/README.md +14 -102
- package/docs/SUMMARY.md +53 -0
- package/docs/TERMINAL_CONTRACT.md +136 -77
- package/docs/UI_RENDER_PIPELINE.md +204 -0
- package/docs/agents.md +5 -5
- package/docs/architecture.md +23 -12
- package/docs/choosing-mode.md +7 -7
- package/docs/cli.md +22 -29
- package/docs/concepts.md +1 -1
- package/docs/config.md +10 -5
- package/docs/contributing.md +6 -6
- package/docs/environment.md +3 -1
- package/docs/exec.md +5 -5
- package/docs/getting-started.md +14 -12
- package/docs/glossary.md +9 -7
- package/docs/mcp-lsp.md +1 -1
- package/docs/memory-sessions.md +7 -5
- package/docs/policy-sandbox.md +23 -15
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +4 -3
- package/docs/security-model.md +16 -17
- package/docs/security.md +15 -30
- package/docs/skills.md +1 -1
- package/docs/tools.md +6 -6
- package/docs/verify-benchmark.md +16 -0
- package/package.json +5 -5
- package/scripts/vendor-overlays/minicode-context.patch +109 -0
- package/src/app/mentions.ts +4 -3
- package/src/app/provider-layer.ts +13 -1
- package/src/app/session.ts +229 -22
- package/src/app/tool-layer.ts +6 -0
- package/src/config.ts +30 -3
- package/src/constants.ts +19 -0
- package/src/daemon/control.ts +1 -1
- package/src/lib/git-hardening.ts +27 -22
- package/src/lib/safe-open.ts +73 -19
- package/src/lib/trusted-exec.ts +7 -4
- package/src/mcp/client.ts +34 -9
- package/src/mcp/server.ts +2 -3
- package/src/mcp/transport.ts +96 -42
- package/src/memory/files.ts +15 -4
- package/src/memory/vector.ts +5 -1
- package/src/policy/compaction.ts +272 -16
- package/src/policy/context-window.ts +73 -0
- package/src/policy/context.ts +76 -16
- package/src/policy/ratelimit.ts +47 -18
- package/src/policy/tool-contract.ts +4 -10
- package/src/policy/update-check.ts +83 -6
- package/src/policy/usage.ts +31 -14
- package/src/policy/work-mode.ts +1 -1
- package/src/presentation/adapter.ts +64 -2
- package/src/presentation/events.ts +6 -0
- package/src/presentation/model.ts +1 -0
- package/src/presentation/projection.ts +2 -0
- package/src/presentation/reducer.ts +1 -0
- package/src/providers/anthropic.ts +52 -19
- package/src/providers/guards.ts +158 -16
- package/src/providers/provision.ts +12 -3
- package/src/providers/responses.ts +61 -23
- package/src/providers/router.ts +56 -23
- package/src/providers/sse-bounds.ts +170 -0
- package/src/repo/repomap.ts +10 -11
- package/src/runtime/execution-backend.ts +7 -8
- package/src/sandbox/docker.ts +113 -9
- package/src/session/checkpoint.ts +9 -2
- package/src/session/context-projection.ts +14 -0
- package/src/session/context-selector.ts +58 -20
- package/src/session/persistence.ts +6 -3
- package/src/session/verification.ts +55 -16
- package/src/skills/loader.ts +16 -3
- package/src/task/model.ts +21 -70
- package/src/tools/bash.ts +6 -3
- package/src/tools/code_run.ts +6 -5
- package/src/tools/context.ts +8 -0
- package/src/tools/delete_file.ts +2 -1
- package/src/tools/edit.ts +2 -1
- package/src/tools/git.ts +128 -54
- package/src/tools/glob.ts +2 -1
- package/src/tools/grep.ts +31 -6
- package/src/tools/lsp.ts +5 -4
- package/src/tools/mcp_call.ts +3 -1
- package/src/tools/memory.ts +175 -180
- package/src/tools/move_file.ts +2 -1
- package/src/tools/patch.ts +2 -1
- package/src/tools/read_file.ts +94 -1
- package/src/tools/read_image.ts +2 -1
- package/src/tools/task.ts +9 -1
- package/src/tools/todo.ts +4 -5
- package/src/tools/web_fetch.ts +28 -12
- package/src/tools/web_search.ts +28 -9
- package/src/tools/write_file.ts +2 -1
- package/src/ui/assistant/simple.ts +72 -3
- package/src/ui/footer.ts +131 -66
- package/src/ui/i18n/en.ts +3 -11
- package/src/ui/i18n/id.ts +3 -11
- package/src/ui/input/input.ts +2 -3
- package/src/ui/input/prompt-engine.ts +2 -5
- package/src/ui/render/width.ts +107 -79
- package/src/ui/runtime/screen.ts +49 -37
- package/src/ui/screens/model-manager.ts +3 -10
- package/src/ui/screens/picker.ts +23 -40
- package/src/ui/screens/provider-manager.ts +12 -33
- package/src/ui/screens/wizard.ts +0 -1
- package/src/ui/tui/app.ts +265 -211
- package/src/ui/tui/transcript.ts +87 -8
- package/vendor/minicore/VENDOR.md +9 -5
- package/vendor/minicore/src/core/loop.ts +47 -12
- package/vendor/minicore/src/core/provider.ts +4 -0
- package/vendor/minicore/src/core/session.ts +14 -1
- package/CHANGELOG.md +0 -1185
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +0 -489
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +0 -391
- package/docs/PLAN_UIUX_V6.md +0 -209
- package/docs/changelog.md +0 -3
package/src/app/session.ts
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
import { resolve } from "node:path"
|
|
2
2
|
import type { RecoveryAction } from "#minicore/core/errors.ts"
|
|
3
|
-
import { ProviderError } from "#minicore/core/errors.ts"
|
|
3
|
+
import { AgentError, ProviderError } from "#minicore/core/errors.ts"
|
|
4
4
|
import type {
|
|
5
5
|
ModelProvider,
|
|
6
6
|
PermissionHandler,
|
|
7
|
+
ProviderEvent,
|
|
7
8
|
Session,
|
|
8
9
|
SessionConfig,
|
|
9
10
|
} from "#minicore/core/index.ts"
|
|
10
11
|
import { createSession as createCoreSession } from "#minicore/core/index.ts"
|
|
11
12
|
import { defaultRecoveryPolicy } from "#minicore/core/recovery.ts"
|
|
12
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
estimateMessages,
|
|
15
|
+
estimateSystem,
|
|
16
|
+
estimateTools,
|
|
17
|
+
safeStringify,
|
|
18
|
+
} from "#minicore/core/tokens.ts"
|
|
13
19
|
import { LIMITS } from "../constants.ts"
|
|
14
20
|
// [Area B · PROMPT 06] Binding persetujuan ↔ operasi yang dieksekusi.
|
|
15
21
|
import { ApprovalBindingStore, verifyApprovalBinding } from "../policy/approval-binding.ts"
|
|
16
22
|
import {
|
|
17
23
|
buildSystemPrompt,
|
|
18
24
|
createCalibratedEstimator,
|
|
25
|
+
createIncrementalTokenCounter,
|
|
19
26
|
minicodeEstimator,
|
|
20
27
|
} from "../policy/context.ts"
|
|
28
|
+
import { DEFAULT_FALLBACK_CONTEXT_WINDOW_TOKENS } from "../policy/context-window.ts"
|
|
21
29
|
import { parallelExecutor } from "../policy/executor.ts"
|
|
22
30
|
import {
|
|
23
31
|
canonicalArgsHash,
|
|
@@ -118,6 +126,8 @@ export async function createMinicodeSession(
|
|
|
118
126
|
> & {
|
|
119
127
|
systemExtra?: string
|
|
120
128
|
cwd?: string
|
|
129
|
+
/** Live model identity used for per-model token calibration. */
|
|
130
|
+
getContextModel?: () => string | undefined
|
|
121
131
|
permissionMode?: "auto" | "readonly" | "plan" | "allow-all" | "ask" | "allowlist"
|
|
122
132
|
/** S2.1: WorkMode adalah lapis intent TERPISAH dari PermissionMode.
|
|
123
133
|
* Default EXECUTE = perilaku lama (base handler apa adanya). Nilai selain
|
|
@@ -201,6 +211,8 @@ export async function createMinicodeSession(
|
|
|
201
211
|
beforeToolEffect?: (call: { id: string; name: string; args: unknown }) => Promise<void>
|
|
202
212
|
},
|
|
203
213
|
): Promise<Session> {
|
|
214
|
+
// Normalisasi root sekali di composition agar ToolContext selalu memiliki path tepercaya.
|
|
215
|
+
const defaultCwd = resolve(opts.cwd ?? process.cwd())
|
|
204
216
|
// Fail-closed lebih awal (sebelum provider/system prompt): WorkMode non-
|
|
205
217
|
// EXECUTE di atas handler terinjeksi belum punya komposisi yang terbukti
|
|
206
218
|
// aman — jangan pernah membungkus atau melewati policy yang diinjeksi 6S.
|
|
@@ -217,18 +229,18 @@ export async function createMinicodeSession(
|
|
|
217
229
|
// pembungkus provider supaya transisi runtime `/work-mode` maupun `/mode`
|
|
218
230
|
// tercermin langsung di StreamRequest.system tanpa mengedit vendor/minicore.
|
|
219
231
|
const baseSystem = await buildSystemPrompt({
|
|
220
|
-
cwd:
|
|
232
|
+
cwd: defaultCwd,
|
|
221
233
|
extra: opts.systemExtra ?? "",
|
|
222
234
|
signal: (opts as unknown as { signal?: AbortSignal }).signal,
|
|
223
235
|
})
|
|
224
236
|
const {
|
|
225
237
|
concurrency,
|
|
226
238
|
writeConcurrency,
|
|
227
|
-
cwd,
|
|
228
239
|
permissionMode,
|
|
229
240
|
workMode: _workMode,
|
|
230
241
|
allowLocalConfig,
|
|
231
242
|
systemExtra: _extra,
|
|
243
|
+
getContextModel: _getContextModel,
|
|
232
244
|
provider,
|
|
233
245
|
onPermissions,
|
|
234
246
|
onWorkMode,
|
|
@@ -249,8 +261,7 @@ export async function createMinicodeSession(
|
|
|
249
261
|
// Dibuat di sini (bukan global) supaya dua sesi dalam satu proses tak
|
|
250
262
|
// saling memakai bukti persetujuan. Root = cwd sesi (sama dengan yang
|
|
251
263
|
// dipakai tool untuk resolve path target).
|
|
252
|
-
const approvalBindings =
|
|
253
|
-
_approvalBindings ?? new ApprovalBindingStore(resolve(cwd ?? process.cwd()))
|
|
264
|
+
const approvalBindings = _approvalBindings ?? new ApprovalBindingStore(defaultCwd)
|
|
254
265
|
// F-09: sanitasi batas di lapisan app (mencakup pemanggil library langsung,
|
|
255
266
|
// bukan hanya CLI yang sudah sanitasi di cli/setup.ts + cli/index.ts).
|
|
256
267
|
// Tanpa ini: timeoutMs NaN/negatif = abort instan; maxSteps 0/negatif =
|
|
@@ -261,6 +272,8 @@ export async function createMinicodeSession(
|
|
|
261
272
|
const t = (rest as Record<string, unknown>).timeoutMs
|
|
262
273
|
if (typeof t === "number" && (!Number.isFinite(t) || t < 0))
|
|
263
274
|
delete (rest as Record<string, unknown>).timeoutMs
|
|
275
|
+
else if (typeof t === "number" && t > LIMITS.MAX_TIMER_DELAY_MS)
|
|
276
|
+
(rest as Record<string, unknown>).timeoutMs = LIMITS.MAX_TIMER_DELAY_MS
|
|
264
277
|
const m = (rest as Record<string, unknown>).maxSteps
|
|
265
278
|
if (typeof m !== "number" || !Number.isFinite(m) || m < 1)
|
|
266
279
|
delete (rest as Record<string, unknown>).maxSteps
|
|
@@ -297,7 +310,7 @@ export async function createMinicodeSession(
|
|
|
297
310
|
workMode: resolvedWorkMode,
|
|
298
311
|
base: createPermissionHandler({
|
|
299
312
|
mode: permissionMode ?? "auto",
|
|
300
|
-
root:
|
|
313
|
+
root: defaultCwd,
|
|
301
314
|
ask,
|
|
302
315
|
allowLocalConfig,
|
|
303
316
|
onApprovalEvent,
|
|
@@ -306,7 +319,7 @@ export async function createMinicodeSession(
|
|
|
306
319
|
// bisa dipakai sebagai bukti di sesi B.
|
|
307
320
|
approvalBindings,
|
|
308
321
|
}),
|
|
309
|
-
root:
|
|
322
|
+
root: defaultCwd,
|
|
310
323
|
})
|
|
311
324
|
const injected = opts.permissionHandler !== undefined
|
|
312
325
|
const withMode = permissions as typeof permissions & {
|
|
@@ -332,7 +345,7 @@ export async function createMinicodeSession(
|
|
|
332
345
|
if (isAllToolsPureRead()) return "explore"
|
|
333
346
|
return "active"
|
|
334
347
|
}
|
|
335
|
-
const effectiveRoot =
|
|
348
|
+
const effectiveRoot = defaultCwd
|
|
336
349
|
const detector: NoProgressDetector | null =
|
|
337
350
|
noProgress === false
|
|
338
351
|
? null
|
|
@@ -367,7 +380,11 @@ export async function createMinicodeSession(
|
|
|
367
380
|
...permissions,
|
|
368
381
|
check: async (call, deps) => {
|
|
369
382
|
const decision = await permissions.check(call, deps)
|
|
370
|
-
|
|
383
|
+
// Kernel vendored saat ini hanya memblokir literal "deny". Validasi di
|
|
384
|
+
// composition root ini menjaga production dispatch fail-closed tanpa
|
|
385
|
+
// mengubah salinan MiniCore yang wajib disinkronkan dari repo sumber.
|
|
386
|
+
// Handler ASK yang sah tetap menunggu approval lalu mengembalikan allow/deny.
|
|
387
|
+
if (decision !== "allow") return "deny"
|
|
371
388
|
// Ambil (konsumsi) bukti persetujuan milik call ini — sekali saja.
|
|
372
389
|
// Diambil SEBELUM await supaya tak ada binding tersisa kalau barrier
|
|
373
390
|
// memblokir call (tak ada deny lalu tak ada jejak).
|
|
@@ -440,17 +457,172 @@ export async function createMinicodeSession(
|
|
|
440
457
|
const planHint = pm === "plan" ? PLAN_PM_HINT : ""
|
|
441
458
|
return baseSystem + planHint + workModeHint(wm)
|
|
442
459
|
}
|
|
460
|
+
// [CTX] Kalibrasi dipisahkan per identitas model agar berpindah ke model
|
|
461
|
+
// lain tidak membawa rasio tokenizer/provider dari sesi sebelumnya.
|
|
462
|
+
const calibrations = new Map<string, ReturnType<typeof createCalibratedEstimator>>()
|
|
463
|
+
const contextModelKey = (model?: string): string => model?.trim() || "__default__"
|
|
464
|
+
const calibrationFor = (model?: string) => {
|
|
465
|
+
const key = contextModelKey(model)
|
|
466
|
+
let calibration = calibrations.get(key)
|
|
467
|
+
if (!calibration) {
|
|
468
|
+
calibration = createCalibratedEstimator()
|
|
469
|
+
calibrations.set(key, calibration)
|
|
470
|
+
}
|
|
471
|
+
return calibration
|
|
472
|
+
}
|
|
473
|
+
let activeContextModel = opts.model
|
|
474
|
+
const currentContextModel = () => {
|
|
475
|
+
const selected = _getContextModel?.() ?? activeContextModel
|
|
476
|
+
try {
|
|
477
|
+
return provider.contextModelIdentity?.(selected) ?? selected
|
|
478
|
+
} catch {
|
|
479
|
+
return selected
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const calibratedEstimator = (text: string) =>
|
|
483
|
+
calibrationFor(currentContextModel()).estimator(text)
|
|
484
|
+
let lastRequestLocalEstimate: { model: string; tokens: number } | undefined
|
|
485
|
+
const contextWindowForModel = (model?: string): number => {
|
|
486
|
+
const explicit = rest.contextWindowTokens
|
|
487
|
+
if (
|
|
488
|
+
!rest.contextWindowTokensForModel &&
|
|
489
|
+
typeof explicit === "number" &&
|
|
490
|
+
Number.isSafeInteger(explicit) &&
|
|
491
|
+
explicit > 0
|
|
492
|
+
) {
|
|
493
|
+
return explicit
|
|
494
|
+
}
|
|
495
|
+
try {
|
|
496
|
+
const resolved =
|
|
497
|
+
rest.contextWindowTokensForModel?.(model) ?? provider.contextWindowTokensForModel?.(model)
|
|
498
|
+
if (typeof resolved === "number" && Number.isSafeInteger(resolved) && resolved > 0)
|
|
499
|
+
return resolved
|
|
500
|
+
} catch {}
|
|
501
|
+
return typeof explicit === "number" && Number.isSafeInteger(explicit) && explicit > 0
|
|
502
|
+
? explicit
|
|
503
|
+
: DEFAULT_FALLBACK_CONTEXT_WINDOW_TOKENS
|
|
504
|
+
}
|
|
443
505
|
const dynamicProvider: ModelProvider = {
|
|
444
506
|
id: provider.id,
|
|
445
507
|
get models() {
|
|
446
508
|
return provider.models
|
|
447
509
|
},
|
|
448
|
-
stream(req, signal) {
|
|
449
|
-
|
|
510
|
+
async *stream(req, signal) {
|
|
511
|
+
const system = computeLiveSystem()
|
|
512
|
+
const model = req.model ?? currentContextModel()
|
|
513
|
+
let limit = contextWindowForModel(model)
|
|
514
|
+
const estimatorModel = model?.includes("::") ? model : (currentContextModel() ?? model)
|
|
515
|
+
let calibration = calibrationFor(estimatorModel)
|
|
516
|
+
let estimator = calibration.estimator
|
|
517
|
+
let admittedFactor = calibration.snapshot().factor
|
|
518
|
+
const tools = req.tools ?? []
|
|
519
|
+
const localInputTokens =
|
|
520
|
+
estimateMessages(req.messages, minicodeEstimator) +
|
|
521
|
+
estimateSystem(system, minicodeEstimator) +
|
|
522
|
+
estimateTools(tools, minicodeEstimator)
|
|
523
|
+
let inputTokens =
|
|
524
|
+
estimateMessages(req.messages, estimator) +
|
|
525
|
+
estimateSystem(system, estimator) +
|
|
526
|
+
estimateTools(tools, estimator)
|
|
527
|
+
lastRequestLocalEstimate = {
|
|
528
|
+
model: contextModelKey(estimatorModel),
|
|
529
|
+
tokens: localInputTokens,
|
|
530
|
+
}
|
|
531
|
+
// Sisakan ruang framing dan respons minimum. Tanpa reserve, request yang
|
|
532
|
+
// tepat menyentuh batas provider tetap ditolak setelah biaya dibakar.
|
|
533
|
+
let reserve = Math.min(256, Math.floor(limit / 10))
|
|
534
|
+
let outputAllowance = Math.max(0, limit - inputTokens - reserve)
|
|
535
|
+
let outputTokens = 0
|
|
536
|
+
const streamedText = createIncrementalTokenCounter()
|
|
537
|
+
const streamedReasoning = createIncrementalTokenCounter()
|
|
538
|
+
const contextLimitEvent = (input: number, output: number): ProviderEvent => ({
|
|
539
|
+
type: "extension",
|
|
540
|
+
kind: "context-window-limit",
|
|
541
|
+
data: { inputTokens: input, outputTokens: output, windowTokens: limit },
|
|
542
|
+
})
|
|
543
|
+
const overflow = (actualInput = inputTokens, actualOutput = outputTokens) =>
|
|
544
|
+
new AgentError(
|
|
545
|
+
"budget_exceeded",
|
|
546
|
+
`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
|
+
)
|
|
548
|
+
const requestController = new AbortController()
|
|
549
|
+
const forwardAbort = () => requestController.abort(signal.reason)
|
|
550
|
+
if (signal.aborted) forwardAbort()
|
|
551
|
+
else signal.addEventListener("abort", forwardAbort, { once: true })
|
|
552
|
+
try {
|
|
553
|
+
if (inputTokens + reserve >= limit) {
|
|
554
|
+
const error = overflow(inputTokens, 0)
|
|
555
|
+
requestController.abort(error)
|
|
556
|
+
yield contextLimitEvent(inputTokens, 0)
|
|
557
|
+
throw error
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
for await (const event of provider.stream({ ...req, system }, requestController.signal)) {
|
|
561
|
+
if (event.type === "extension" && event.kind === "effective-model") {
|
|
562
|
+
const effective = event.data as { effective?: unknown; provider?: unknown } | undefined
|
|
563
|
+
if (
|
|
564
|
+
typeof effective?.effective === "string" &&
|
|
565
|
+
typeof effective.provider === "string"
|
|
566
|
+
) {
|
|
567
|
+
// Router fallback/substitution is a different model boundary.
|
|
568
|
+
// Keep the smaller of the requested and effective capacities so
|
|
569
|
+
// a failed primary provider cannot silently widen the budget.
|
|
570
|
+
const effectiveModel = `${effective.provider}::${effective.effective}`
|
|
571
|
+
const effectiveLimit = contextWindowForModel(effectiveModel)
|
|
572
|
+
limit = Math.min(limit, effectiveLimit)
|
|
573
|
+
calibration = calibrationFor(effectiveModel)
|
|
574
|
+
estimator = calibration.estimator
|
|
575
|
+
admittedFactor = calibration.snapshot().factor
|
|
576
|
+
inputTokens = Math.max(
|
|
577
|
+
inputTokens,
|
|
578
|
+
estimateMessages(req.messages, estimator) +
|
|
579
|
+
estimateSystem(system, estimator) +
|
|
580
|
+
estimateTools(tools, estimator),
|
|
581
|
+
)
|
|
582
|
+
reserve = Math.min(256, Math.floor(limit / 10))
|
|
583
|
+
outputAllowance = Math.max(0, limit - inputTokens - reserve)
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
const currentFactor = calibration.snapshot().factor
|
|
587
|
+
if (currentFactor > admittedFactor) {
|
|
588
|
+
inputTokens = Math.max(
|
|
589
|
+
inputTokens,
|
|
590
|
+
estimateMessages(req.messages, estimator) +
|
|
591
|
+
estimateSystem(system, estimator) +
|
|
592
|
+
estimateTools(tools, estimator),
|
|
593
|
+
)
|
|
594
|
+
reserve = Math.min(256, Math.floor(limit / 10))
|
|
595
|
+
outputAllowance = Math.max(0, limit - inputTokens - reserve)
|
|
596
|
+
}
|
|
597
|
+
admittedFactor = currentFactor
|
|
598
|
+
let added = 0
|
|
599
|
+
if (event.type === "text") {
|
|
600
|
+
added = streamedText.add(event.text, calibration.snapshot().factor)
|
|
601
|
+
} else if (event.type === "tool_call") {
|
|
602
|
+
added = estimator(event.name) + estimator(safeStringify(event.args))
|
|
603
|
+
} else if (event.type === "extension" && event.kind === "reasoning") {
|
|
604
|
+
const reasoning = (event.data as { text?: unknown } | undefined)?.text
|
|
605
|
+
if (typeof reasoning === "string") {
|
|
606
|
+
added = streamedReasoning.add(reasoning, calibration.snapshot().factor)
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
if (added > 0 && outputTokens + added > outputAllowance) {
|
|
610
|
+
const error = overflow(inputTokens, outputTokens + added)
|
|
611
|
+
requestController.abort(error)
|
|
612
|
+
yield contextLimitEvent(inputTokens, outputTokens + added)
|
|
613
|
+
throw error
|
|
614
|
+
}
|
|
615
|
+
outputTokens += added
|
|
616
|
+
yield event as ProviderEvent
|
|
617
|
+
}
|
|
618
|
+
} finally {
|
|
619
|
+
signal.removeEventListener("abort", forwardAbort)
|
|
620
|
+
if (!requestController.signal.aborted) {
|
|
621
|
+
requestController.abort(new AgentError("aborted", "provider stream closed"))
|
|
622
|
+
}
|
|
623
|
+
}
|
|
450
624
|
},
|
|
451
625
|
}
|
|
452
|
-
// [Fase 3 / CTX-03] Estimator terkalibrasi terhadap usage aktual provider.
|
|
453
|
-
const calibration = createCalibratedEstimator()
|
|
454
626
|
const liveToolSchemas = () =>
|
|
455
627
|
registeredTools.map((t) => ({
|
|
456
628
|
name: t.name,
|
|
@@ -459,30 +631,48 @@ export async function createMinicodeSession(
|
|
|
459
631
|
}))
|
|
460
632
|
const core = createCoreSession({
|
|
461
633
|
...rest,
|
|
634
|
+
contextWindowTokens: contextWindowForModel(currentContextModel()),
|
|
635
|
+
contextWindowTokensForModel: contextWindowForModel,
|
|
462
636
|
provider: dynamicProvider,
|
|
463
637
|
system: computeLiveSystem(),
|
|
638
|
+
systemForContext: computeLiveSystem,
|
|
464
639
|
permissions: withEffectBarrier,
|
|
465
640
|
permissionMode: livePermissionMode,
|
|
466
|
-
estimator:
|
|
641
|
+
estimator: calibratedEstimator,
|
|
467
642
|
recovery: cappedRecovery,
|
|
468
643
|
executor: parallelExecutor({
|
|
469
644
|
concurrency: safeConcurrency ?? LIMITS.EXECUTOR_CONCURRENCY,
|
|
470
645
|
writeConcurrency: safeWriteConcurrency ?? LIMITS.EXECUTOR_WRITE_CONCURRENCY,
|
|
471
646
|
}),
|
|
472
|
-
cwd,
|
|
647
|
+
cwd: defaultCwd,
|
|
473
648
|
...(turnCount !== undefined ? { turnCount } : {}),
|
|
474
649
|
...(stepCount !== undefined ? { stepCount } : {}),
|
|
475
650
|
})
|
|
476
651
|
core.events.on("provider:extension", (e) => {
|
|
477
652
|
if (e.kind !== "usage") return
|
|
478
|
-
const d = e.data as
|
|
479
|
-
|
|
480
|
-
|
|
653
|
+
const d = e.data as
|
|
654
|
+
| {
|
|
655
|
+
inputTokens?: number
|
|
656
|
+
totalTokens?: number
|
|
657
|
+
model?: string
|
|
658
|
+
provider?: string
|
|
659
|
+
source?: string
|
|
660
|
+
}
|
|
661
|
+
| undefined
|
|
662
|
+
if (!d || d.source === "compaction") return
|
|
663
|
+
const reportedModel =
|
|
664
|
+
typeof d.model === "string" && d.model.length > 0
|
|
665
|
+
? typeof d.provider === "string" && d.provider.length > 0
|
|
666
|
+
? `${d.provider}::${d.model}`
|
|
667
|
+
: d.model
|
|
668
|
+
: currentContextModel()
|
|
669
|
+
const fallbackLocal =
|
|
481
670
|
estimateSystem(computeLiveSystem(), minicodeEstimator) +
|
|
482
671
|
estimateTools(liveToolSchemas(), minicodeEstimator) +
|
|
483
672
|
estimateMessages(core.state.history, minicodeEstimator)
|
|
484
|
-
|
|
485
|
-
|
|
673
|
+
const localEstimatedTokens = lastRequestLocalEstimate?.tokens ?? fallbackLocal
|
|
674
|
+
calibrationFor(reportedModel).observeProviderUsage({
|
|
675
|
+
localEstimatedTokens,
|
|
486
676
|
providerInputTokens: d.inputTokens,
|
|
487
677
|
providerTotalTokens: d.totalTokens,
|
|
488
678
|
})
|
|
@@ -526,5 +716,22 @@ export async function createMinicodeSession(
|
|
|
526
716
|
return origRun(input, runOpts)
|
|
527
717
|
}
|
|
528
718
|
}
|
|
529
|
-
return
|
|
719
|
+
return {
|
|
720
|
+
get state() {
|
|
721
|
+
return core.state
|
|
722
|
+
},
|
|
723
|
+
get events() {
|
|
724
|
+
return core.events
|
|
725
|
+
},
|
|
726
|
+
get contextTokens() {
|
|
727
|
+
return core.contextTokens
|
|
728
|
+
},
|
|
729
|
+
run(input: string, runOpts?: { signal?: AbortSignal; model?: string }) {
|
|
730
|
+
activeContextModel = runOpts?.model ?? opts.model
|
|
731
|
+
return core.run(input, runOpts)
|
|
732
|
+
},
|
|
733
|
+
abort() {
|
|
734
|
+
core.abort()
|
|
735
|
+
},
|
|
736
|
+
}
|
|
530
737
|
}
|
package/src/app/tool-layer.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type { CanonicalScope } from "../session/verification.ts"
|
|
|
13
13
|
import { createBashJobTools } from "../tools/bash.ts"
|
|
14
14
|
import { withEvidence } from "../tools/evidence.ts"
|
|
15
15
|
import { allTools, withMcpTools } from "../tools/index.ts"
|
|
16
|
+
import { createWriteMemoryTool } from "../tools/memory.ts"
|
|
16
17
|
|
|
17
18
|
const errMsg = (e: unknown) => (e instanceof Error ? e.message : String(e))
|
|
18
19
|
|
|
@@ -78,6 +79,11 @@ export async function setupToolLayer(
|
|
|
78
79
|
setPermissionMode?: (pm: string) => void
|
|
79
80
|
}> {
|
|
80
81
|
let sessionTools: Tool[] = allTools
|
|
82
|
+
if (jobOwnerSid) {
|
|
83
|
+
sessionTools = sessionTools.map((tool) =>
|
|
84
|
+
tool.name === "write_memory" ? createWriteMemoryTool(jobOwnerSid) : tool,
|
|
85
|
+
)
|
|
86
|
+
}
|
|
81
87
|
// Harness-P2: scope explore = subset read-only sub-agen (tetap 12 EXPLORE_TOOL_NAMES).
|
|
82
88
|
if (scope === "explore")
|
|
83
89
|
sessionTools = allTools.filter((t) => EXPLORE_TOOL_NAMES.includes(t.name))
|
package/src/config.ts
CHANGED
|
@@ -37,6 +37,10 @@ export interface ProviderEntry {
|
|
|
37
37
|
/** API key. Kosong bila provider memakai OAuth (`auth: "oauth"`). */
|
|
38
38
|
apiKey: string
|
|
39
39
|
models: string[]
|
|
40
|
+
/** Context window tokens by exact model id; `*` is a provider-wide fallback.
|
|
41
|
+
* Values are operator-supplied because gateways often expose aliases whose
|
|
42
|
+
* actual model capacity cannot be discovered reliably at runtime. */
|
|
43
|
+
contextWindows?: Record<string, number>
|
|
40
44
|
providerHint?: string
|
|
41
45
|
/** Knob generik reasoning effort — dipetakan per-wire (openai reasoning_effort, anthropic thinking, gemini). */
|
|
42
46
|
reasoningEffort?: "low" | "medium" | "high"
|
|
@@ -104,9 +108,32 @@ export function localConfigNotice(cwd: string, allowLocal: boolean): string | un
|
|
|
104
108
|
export function normalizeConfig(raw: unknown): MinicodeConfig {
|
|
105
109
|
const cfg = raw as Record<string, unknown>
|
|
106
110
|
const providers = Array.isArray(cfg?.providers)
|
|
107
|
-
? (cfg.providers as ProviderEntry[])
|
|
108
|
-
(p) => p && typeof p.id === "string" && typeof p.baseUrl === "string"
|
|
109
|
-
|
|
111
|
+
? (cfg.providers as ProviderEntry[])
|
|
112
|
+
.filter((p) => p && typeof p.id === "string" && typeof p.baseUrl === "string")
|
|
113
|
+
.map((p) => {
|
|
114
|
+
const source = (p as { contextWindows?: unknown }).contextWindows
|
|
115
|
+
const validContextWindows: Record<string, number> = {}
|
|
116
|
+
if (source && typeof source === "object" && !Array.isArray(source)) {
|
|
117
|
+
for (const [model, tokens] of Object.entries(source as Record<string, unknown>)) {
|
|
118
|
+
if (
|
|
119
|
+
(model === "*" || model.length > 0) &&
|
|
120
|
+
typeof tokens === "number" &&
|
|
121
|
+
Number.isSafeInteger(tokens) &&
|
|
122
|
+
tokens > 0 &&
|
|
123
|
+
tokens <= 20_000_000
|
|
124
|
+
) {
|
|
125
|
+
validContextWindows[model] = tokens
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const contextWindows =
|
|
130
|
+
Object.keys(validContextWindows).length > 0 ? validContextWindows : undefined
|
|
131
|
+
return {
|
|
132
|
+
...p,
|
|
133
|
+
contextWindows:
|
|
134
|
+
contextWindows && Object.keys(contextWindows).length > 0 ? contextWindows : undefined,
|
|
135
|
+
}
|
|
136
|
+
})
|
|
110
137
|
: []
|
|
111
138
|
// Server MCP sah bila punya `command` (stdio) ATAU `url` (HTTP). Entri tanpa
|
|
112
139
|
// keduanya dibuang di sini supaya kegagalan tampak saat config dibaca, bukan
|
package/src/constants.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** Centralized magic numbers — full English naming. Single source for limits. */
|
|
2
2
|
export const LIMITS = {
|
|
3
|
+
/** Maksimum timer JS; nilai lebih besar diklem agar tidak berubah jadi 1ms. */
|
|
4
|
+
MAX_TIMER_DELAY_MS: 2_147_483_647,
|
|
3
5
|
/** File size limits */
|
|
4
6
|
READ_FILE_MAX_BYTES: 2_000_000,
|
|
5
7
|
WRITE_FILE_MAX_CHARS: 5_000_000,
|
|
@@ -14,11 +16,17 @@ export const LIMITS = {
|
|
|
14
16
|
BASH_BACKGROUND_MAX_JOBS: 8,
|
|
15
17
|
BASH_BACKGROUND_MAX_LIFETIME_MS: 3_600_000,
|
|
16
18
|
DOCKER_OUTPUT_MAX_CHARS: 100_000,
|
|
19
|
+
/** Batas untuk meminta daemon Docker menghapus container yang timeout/abort. */
|
|
20
|
+
DOCKER_CLEANUP_TIMEOUT_MS: 5_000,
|
|
17
21
|
MCP_OUTPUT_MAX_CHARS: 100_000,
|
|
22
|
+
/** Batas frame JSON-RPC stdio sebelum decode/parse; satu respons tools/list bisa besar. */
|
|
23
|
+
MCP_STDIO_FRAME_MAX_BYTES: 1_048_576,
|
|
18
24
|
MCP_REQUEST_TIMEOUT_MS: 30_000,
|
|
19
25
|
TASK_SUB_AGENT_MAX_TOKENS: 2_000,
|
|
20
26
|
/** Context & memory */
|
|
21
27
|
SYSTEM_PROMPT_MAX_CHARS: 8_000,
|
|
28
|
+
/** Batas baca per skill sebelum dirender sebagai instruksi pada prompt. */
|
|
29
|
+
SKILL_FILE_MAX_BYTES: 32_000,
|
|
22
30
|
MEMORY_FILE_MAX_CHARS: 4_000,
|
|
23
31
|
MEMORY_FILE_MAX_BYTES: 200_000,
|
|
24
32
|
MEMORY_TRUNCATE_KEEP_BYTES: 150_000,
|
|
@@ -85,6 +93,16 @@ export const LIMITS = {
|
|
|
85
93
|
/** Investigasi Phase 5: cap reasoning stream (terbukti TANPA cap = akumulasi
|
|
86
94
|
* tak terbatas — satu-satunya jalur loop `reasoning +=` tanpa batas). */
|
|
87
95
|
PROVIDER_REASON_MAX_CHARS: 2_000_000,
|
|
96
|
+
/** Frame SSE provider longgar untuk JSON tool besar (write_file menerima 5 MB chars). */
|
|
97
|
+
PROVIDER_SSE_FRAME_MAX_BYTES: 16_777_216,
|
|
98
|
+
/** Batas total satu respons stream provider, termasuk reasoning/tool args. */
|
|
99
|
+
PROVIDER_SSE_TOTAL_MAX_BYTES: 67_108_864,
|
|
100
|
+
/** Argumen tool besar tetap didukung; envelope JSON dapat menggandakan escape chars. */
|
|
101
|
+
PROVIDER_TOOL_ARGS_MAX_BYTES: 12_582_912,
|
|
102
|
+
/** Batas agregat argumen yang masih tertahan dari beberapa tool call. */
|
|
103
|
+
PROVIDER_TOOL_ARGS_PENDING_MAX_BYTES: 25_165_824,
|
|
104
|
+
/** Respons error provider cukup untuk diagnosis tanpa membaca body arbitrer. */
|
|
105
|
+
PROVIDER_ERROR_BODY_MAX_BYTES: 65_536,
|
|
88
106
|
DOCKER_TIMEOUT_MS: 30_000,
|
|
89
107
|
DETECT_MODELS_TIMEOUT_MS: 4_000,
|
|
90
108
|
DETECT_GLOBAL_TIMEOUT_MS: 6_000,
|
|
@@ -99,6 +117,7 @@ export const LIMITS = {
|
|
|
99
117
|
LSP_INIT_TIMEOUT_MS: 15_000,
|
|
100
118
|
VERIFY_DEFAULT_TIMEOUT_MS: 30_000,
|
|
101
119
|
WEB_FETCH_BODY_HARD_CAP_CHARS: 2_000_000,
|
|
120
|
+
WEB_RESPONSE_BODY_HARD_CAP_BYTES: 2_000_000,
|
|
102
121
|
WEB_FETCH_MAX_REDIRECTS: 5,
|
|
103
122
|
/** Glob/grep */
|
|
104
123
|
SEARCH_DEFAULT_LIMIT: 100,
|
package/src/daemon/control.ts
CHANGED
|
@@ -22,7 +22,7 @@ export interface HostedSessionInfo {
|
|
|
22
22
|
runStatus: string | null
|
|
23
23
|
presentationHead: number
|
|
24
24
|
/** P0.2 (Q5): WorkMode efektif sesi yang di-host (bila diketahui host). */
|
|
25
|
-
workMode?: "DISCUSS" | "PLAN" | "
|
|
25
|
+
workMode?: "DISCUSS" | "PLAN" | "BUILD"
|
|
26
26
|
/**
|
|
27
27
|
* P2.3: hasil recovery run terakhir yang durable (mis. UNKNOWN setelah
|
|
28
28
|
* restart). Operator dapat membedakan LIVE vs reconstructed/interrupted
|
package/src/lib/git-hardening.ts
CHANGED
|
@@ -17,7 +17,7 @@ import { resolveTrustedExecutable } from "./trusted-exec.ts"
|
|
|
17
17
|
// index user (LFS/semantik clean milik user, lihat git.ts). Pengecualian:
|
|
18
18
|
// protokol `filter.<d>.process` diutamakan git di atas clean/smudge, jadi
|
|
19
19
|
// neutralizer selalu mematikannya (nilai kosong = fallback ke clean/smudge)
|
|
20
|
-
// di SEMUA jalur plumbing;
|
|
20
|
+
// di SEMUA jalur plumbing; `git_commit` menolak filter sebelum staging.
|
|
21
21
|
|
|
22
22
|
/** Netralisasi universal: tanpa hooks, tanpa fsmonitor, tanpa pager. */
|
|
23
23
|
export const GIT_SAFE_BASE: readonly string[] = [
|
|
@@ -31,7 +31,7 @@ export const GIT_SAFE_BASE: readonly string[] = [
|
|
|
31
31
|
/** Netralisasi driver konten untuk diff/log (tanpa eksekusi repo). */
|
|
32
32
|
export const GIT_NO_DIFF_DRIVERS: readonly string[] = ["--no-ext-diff", "--no-textconv"]
|
|
33
33
|
|
|
34
|
-
const DRIVER_RE = /^filter\.([A-Za-z0-9_.-]+)\.(
|
|
34
|
+
const DRIVER_RE = /^filter\.([A-Za-z0-9_.-]+)\.(clean|smudge|process)$/i
|
|
35
35
|
|
|
36
36
|
export interface FilterDriver {
|
|
37
37
|
name: string
|
|
@@ -42,30 +42,35 @@ export interface FilterDriver {
|
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* Daftar driver filter yang TERKONFIGURASI di repo (discovery read-only via
|
|
45
|
-
* `git config`, tanpa hook/filter/pager).
|
|
46
|
-
*
|
|
47
|
-
*
|
|
45
|
+
* `git config`, tanpa hook/filter/pager). Kegagalan discovery melempar error:
|
|
46
|
+
* pemanggil tidak boleh menganggap ketidakmampuan memeriksa sebagai tidak ada
|
|
47
|
+
* filter. Dipakai neutralizer plumbing dan preflight `git_commit`.
|
|
48
48
|
*/
|
|
49
49
|
export async function discoverFilterDrivers(cwd: string): Promise<FilterDriver[]> {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
50
|
+
const r = spawnSync(
|
|
51
|
+
resolveTrustedExecutable("git"),
|
|
52
|
+
["config", "--name-only", "--get-regexp", "^filter\\..*\\.(clean|smudge|process)$"],
|
|
53
|
+
{ cwd, encoding: "utf8", timeout: 10_000 },
|
|
54
|
+
)
|
|
55
|
+
if (r.error) throw new Error(`unable to inspect Git filter configuration: ${r.error.message}`)
|
|
56
|
+
if (r.status === 1 && !r.stdout?.trim()) return []
|
|
57
|
+
if (r.status !== 0) {
|
|
58
|
+
throw new Error(`unable to inspect Git filter configuration (git exited ${String(r.status)})`)
|
|
59
|
+
}
|
|
60
|
+
if (!r.stdout?.trim()) return []
|
|
61
|
+
const drivers = new Map<string, FilterDriver>()
|
|
62
|
+
for (const line of r.stdout.split("\n")) {
|
|
63
|
+
const key = line.trim()
|
|
64
|
+
if (!key) continue
|
|
65
|
+
const m = DRIVER_RE.exec(key)
|
|
66
|
+
if (!m?.[1] || !m[2]) {
|
|
67
|
+
throw new Error("unable to safely parse Git filter configuration")
|
|
64
68
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
const d = drivers.get(m[1]) ?? { name: m[1], hasProcess: false }
|
|
70
|
+
if (m[2].toLowerCase() === "process") d.hasProcess = true
|
|
71
|
+
drivers.set(m[1], d)
|
|
68
72
|
}
|
|
73
|
+
return [...drivers.values()].sort((a, b) => (a.name < b.name ? -1 : 1))
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
/**
|