minicode-ai 0.26.0 → 0.28.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/cli/args.ts +120 -16
- package/cli/commands/acp.ts +38 -28
- 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 +11 -12
- package/cli/commands/exec.ts +35 -73
- 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 +96 -46
- package/cli/context-command-run.ts +171 -0
- package/cli/context-command.ts +87 -0
- package/cli/index.ts +96 -98
- package/cli/model-manager.ts +7 -2
- package/cli/provider-manager.ts +33 -18
- package/cli/router.ts +41 -12
- package/cli/setup.ts +246 -123
- package/cli/tui.ts +311 -109
- package/docs/TERMINAL_CONTRACT.md +254 -69
- package/docs/UI_RENDER_PIPELINE.md +9 -1
- package/docs/agents.md +2 -2
- package/docs/architecture.md +3 -3
- package/docs/choosing-mode.md +7 -9
- package/docs/cli.md +3 -5
- package/docs/config.md +16 -0
- package/docs/environment.md +3 -3
- package/docs/policy-sandbox.md +3 -3
- package/docs/repl.md +3 -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 +119 -66
- package/src/app/tool-layer.ts +12 -17
- package/src/config.ts +64 -104
- package/src/constants.ts +23 -9
- 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/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 +109 -0
- package/src/policy/context.ts +78 -37
- package/src/policy/executor.ts +17 -7
- 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/guards.ts +41 -2
- package/src/providers/provision.ts +6 -60
- package/src/providers/responses.ts +1 -0
- package/src/repo/repomap.ts +180 -23
- 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/open-work.ts +26 -3
- package/src/session/persistence.ts +25 -58
- package/src/session/verification.ts +170 -20
- package/src/skills/loader.ts +31 -16
- package/src/task/production-scheduler.ts +13 -1
- package/src/task/store.ts +20 -37
- 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 +59 -23
- package/src/ui/i18n/id.ts +88 -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 +88 -62
- 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 +154 -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 +28 -19
package/src/policy/work-mode.ts
CHANGED
|
@@ -13,13 +13,16 @@
|
|
|
13
13
|
import type { PermissionHandler, ToolCall } from "#minicore"
|
|
14
14
|
import type { PermissionMode } from "./permission.ts"
|
|
15
15
|
import {
|
|
16
|
-
LOCAL_READONLY_TOOL_NAMES,
|
|
17
16
|
NETWORK_READ_TOOL_NAMES,
|
|
18
17
|
PLAN_EXTRA_TOOL_NAMES,
|
|
19
18
|
READONLY_TOOL_NAMES,
|
|
20
19
|
} from "./tool-contract.ts"
|
|
21
20
|
|
|
22
|
-
export type WorkMode = "DISCUSS" | "
|
|
21
|
+
export type WorkMode = "DISCUSS" | "EXECUTE"
|
|
22
|
+
|
|
23
|
+
export function normalizeWorkMode(mode?: WorkMode | string | null): WorkMode {
|
|
24
|
+
return mode === "EXECUTE" ? "EXECUTE" : "DISCUSS"
|
|
25
|
+
}
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
28
|
* Error kebijakan startup saat `--verify` digabung dengan WorkMode non-EXECUTE
|
|
@@ -28,7 +31,7 @@ export type WorkMode = "DISCUSS" | "PLAN" | "EXECUTE"
|
|
|
28
31
|
*/
|
|
29
32
|
export class VerifyWorkModeConflictError extends Error {
|
|
30
33
|
readonly workMode: Exclude<WorkMode, "EXECUTE">
|
|
31
|
-
constructor(workMode: Exclude<WorkMode, "EXECUTE">) {
|
|
34
|
+
constructor(workMode: Exclude<WorkMode, "EXECUTE"> = "DISCUSS") {
|
|
32
35
|
super(
|
|
33
36
|
`[work-mode] --verify cannot be combined with WorkMode ${workMode} (verification runs shell commands and self-heal turns; use --work-mode BUILD or omit --verify)`,
|
|
34
37
|
)
|
|
@@ -63,7 +66,7 @@ export type WorkModePermissionHandler = PermissionHandler &
|
|
|
63
66
|
function requireModeControl(base: PermissionHandler): ModeControl {
|
|
64
67
|
const ctrl = base as Partial<ModeControl>
|
|
65
68
|
if (typeof ctrl.__getMode !== "function" || typeof ctrl.__setMode !== "function") {
|
|
66
|
-
// Fail-closed: tanpa kontrol mode, ceiling
|
|
69
|
+
// Fail-closed: tanpa kontrol mode, ceiling DISCUSS tak bisa mengikuti mode PM
|
|
67
70
|
// saat runtime — lebih baik menolak konstruksi daripada memakai snapshot
|
|
68
71
|
// basi yang bisa mencabut ceiling setelah `__setMode`.
|
|
69
72
|
throw new Error(
|
|
@@ -74,27 +77,21 @@ function requireModeControl(base: PermissionHandler): ModeControl {
|
|
|
74
77
|
}
|
|
75
78
|
|
|
76
79
|
// Seleksi ceiling per mode PM SAAT INI (bukan snapshot konstruksi):
|
|
77
|
-
// DISCUSS
|
|
78
|
-
// ketat (`allowlist`/`readonly`), selain itu ceiling plan.
|
|
79
|
-
function ceilingFor(
|
|
80
|
-
if (workMode === "DISCUSS") return "readonly"
|
|
80
|
+
// DISCUSS (gabungan diskusi + perencanaan) memakai ceiling read-only saat PM sudah
|
|
81
|
+
// lebih ketat (`allowlist`/`readonly`), selain itu ceiling plan (18 readonly + 3 plan extra).
|
|
82
|
+
function ceilingFor(_workMode: RestrictiveWorkMode, baseMode: PermissionMode): "readonly" | "plan" {
|
|
81
83
|
return baseMode === "allowlist" || baseMode === "readonly" ? "readonly" : "plan"
|
|
82
84
|
}
|
|
83
85
|
|
|
84
86
|
// Tool Access Policy — ceiling capability WorkMode dihitung LANGSUNG dari
|
|
85
|
-
// registry kontrak tool (satu sumber)
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
// - DISCUSS → tepat 16 `LOCAL_READONLY_TOOL_NAMES` (network read ditolak
|
|
89
|
-
// terpisah di check() dengan alasan khusus).
|
|
90
|
-
// - PLAN → 18 `READONLY_TOOL_NAMES` (+ `PLAN_EXTRA_TOOL_NAMES`), atau
|
|
87
|
+
// registry kontrak tool (satu sumber):
|
|
88
|
+
// - DISCUSS → 18 `READONLY_TOOL_NAMES` (+ `PLAN_EXTRA_TOOL_NAMES`), atau
|
|
91
89
|
// read-only saja bila PM sudah `readonly`/`allowlist` (lebih ketat).
|
|
92
90
|
function ceilingAllows(
|
|
93
91
|
name: string,
|
|
94
|
-
|
|
92
|
+
_workMode: RestrictiveWorkMode,
|
|
95
93
|
baseMode: PermissionMode,
|
|
96
94
|
): boolean {
|
|
97
|
-
if (workMode === "DISCUSS") return LOCAL_READONLY_TOOL_NAMES.includes(name)
|
|
98
95
|
if (baseMode === "readonly" || baseMode === "allowlist") return READONLY_TOOL_NAMES.has(name)
|
|
99
96
|
return READONLY_TOOL_NAMES.has(name) || PLAN_EXTRA_TOOL_NAMES.has(name)
|
|
100
97
|
}
|
|
@@ -105,13 +102,7 @@ export function createWorkModePermissionHandler(opts: {
|
|
|
105
102
|
root?: string
|
|
106
103
|
}): WorkModePermissionHandler {
|
|
107
104
|
const ctrl = requireModeControl(opts.base)
|
|
108
|
-
|
|
109
|
-
// mengkonsultasi handler per tool call, jadi __setWorkMode runtime menggeser
|
|
110
|
-
// ceiling tanpa mengganti handler. EXECUTE = delegasi base (perilaku identik).
|
|
111
|
-
let wm: WorkMode = opts.workMode
|
|
112
|
-
// Handler deny ceiling — hanya untuk seam describeDenial (alasan yang sama
|
|
113
|
-
// dengan perilaku lama: "read-only mode" / "plan mode"). Tidak ada lagi
|
|
114
|
-
// handler PermissionMode `readonly`/`plan` sebagai primitif ceiling.
|
|
105
|
+
let wm: WorkMode = normalizeWorkMode(opts.workMode)
|
|
115
106
|
const ceilingDenyHandlers: Record<"readonly" | "plan", PermissionHandler> = {
|
|
116
107
|
readonly: {
|
|
117
108
|
async check() {
|
|
@@ -176,41 +167,27 @@ export function createWorkModePermissionHandler(opts: {
|
|
|
176
167
|
|
|
177
168
|
const handler = {
|
|
178
169
|
async check(call: ToolCall, _deps?: unknown): Promise<"allow" | "deny"> {
|
|
179
|
-
// EXECUTE = identitas: delegasi penuh ke base tanpa ceiling (perilaku
|
|
180
|
-
// lama). Memungkinkan upgrade runtime EXECUTE→DISCUSS/PLAN walau komposisi
|
|
181
|
-
// awal EXECUTE (compose mengembalikan base) TIDAK menyediakan __setWorkMode —
|
|
182
|
-
// jalur ini dipakai createMinicodeSession yang SELALU membungkus.
|
|
183
170
|
if (wm === "EXECUTE") return opts.base.check(call, _deps as never)
|
|
184
|
-
// P0.2 (Q2): DISCUSS melarang network read (`web_fetch`, `web_search`) di
|
|
185
|
-
// SEMUA PermissionMode tanpa pernah menyentuh base (0 prompt).
|
|
186
|
-
if (wm === "DISCUSS" && NETWORK_READ_TOOL_NAMES.has(call.name)) {
|
|
187
|
-
rememberDeny(call, discussNetworkDeny)
|
|
188
|
-
return "deny"
|
|
189
|
-
}
|
|
190
|
-
// Baca mode PM sekarang: `__setMode` runtime menggeser seleksi ceiling
|
|
191
|
-
// (PLAN × allowlist/readonly = read-only), bukan membekukan yang lama.
|
|
192
|
-
// Baca WorkMode sekarang (`wm` mutable) — __setWorkMode runtime menggeser
|
|
193
|
-
// ceiling DISCUSS↔PLAN.
|
|
194
171
|
const baseMode = ctrl.__getMode()
|
|
195
172
|
const ceilingKind = ceilingFor(wm, baseMode)
|
|
196
|
-
// Ceiling DULU (Tool Access Policy): operasi di luar ceiling tak boleh
|
|
197
|
-
// memicu prompt base (requirement S2.1 #1) — ceiling tak pernah prompt.
|
|
198
173
|
if (!ceilingAllows(call.name, wm, baseMode)) {
|
|
199
174
|
rememberDeny(call, ceilingDenyHandlers[ceilingKind])
|
|
200
175
|
return "deny"
|
|
201
176
|
}
|
|
202
|
-
// Di dalam ceiling:
|
|
203
|
-
//
|
|
204
|
-
const
|
|
177
|
+
// Di dalam ceiling DISCUSS: paksa enforceJail=true agar `DISCUSS × unrestricted`
|
|
178
|
+
// tidak melompati jail workspace maupun proteksi berkas sensitif `.env` (INV-WM1).
|
|
179
|
+
const baseDeps =
|
|
180
|
+
_deps && typeof _deps === "object"
|
|
181
|
+
? { ...(_deps as Record<string, unknown>), enforceJail: true }
|
|
182
|
+
: { enforceJail: true }
|
|
183
|
+
const decision = await opts.base.check(call, baseDeps as never)
|
|
205
184
|
if (decision !== "allow") {
|
|
206
185
|
rememberDeny(call, opts.base)
|
|
207
186
|
return decision
|
|
208
187
|
}
|
|
209
|
-
//
|
|
210
|
-
//
|
|
211
|
-
|
|
212
|
-
// terjaga (WorkMode tak pernah melonggarkan penolakan base).
|
|
213
|
-
if (wm === "PLAN" && NETWORK_READ_TOOL_NAMES.has(call.name)) {
|
|
188
|
+
// Di DISCUSS, network read (`web_fetch`, `web_search`) wajib melewati
|
|
189
|
+
// gerbang approval prompt eksplisit (headless/tanpa __promptGated = fail-closed deny).
|
|
190
|
+
if (NETWORK_READ_TOOL_NAMES.has(call.name)) {
|
|
214
191
|
if (typeof ctrl.__promptGated !== "function") {
|
|
215
192
|
rememberDeny(call, discussNetworkDeny)
|
|
216
193
|
return "deny"
|
|
@@ -225,19 +202,15 @@ export function createWorkModePermissionHandler(opts: {
|
|
|
225
202
|
ctrl.__setMode(m)
|
|
226
203
|
},
|
|
227
204
|
__getMode(): PermissionMode {
|
|
228
|
-
// Metadata yang dilaporkan tetap mode PM asli; WorkMode tidak bocor.
|
|
229
205
|
return ctrl.__getMode()
|
|
230
206
|
},
|
|
231
207
|
__setWorkMode(m: WorkMode): void {
|
|
232
|
-
wm = m
|
|
208
|
+
wm = normalizeWorkMode(m)
|
|
233
209
|
},
|
|
234
210
|
__getWorkMode(): WorkMode {
|
|
235
211
|
return wm
|
|
236
212
|
},
|
|
237
213
|
describeDenial(call: ToolCall): Promise<string | undefined> | string | undefined {
|
|
238
|
-
// Cek owner map lebih dulu: bila call ditolak saat mode non-EXECUTE lalu
|
|
239
|
-
// WorkMode diubah ke EXECUTE sebelum describeDenial dipanggil, alasan
|
|
240
|
-
// deny tetap diambil dari pemilik yang mencatatnya.
|
|
241
214
|
const owner = takeDenyOwner(call)
|
|
242
215
|
if (owner) {
|
|
243
216
|
const seam = owner.describeDenial
|
|
@@ -259,9 +232,10 @@ export function composeWorkModePermissionHandler(opts: {
|
|
|
259
232
|
base: PermissionHandler
|
|
260
233
|
root?: string
|
|
261
234
|
}): PermissionHandler {
|
|
262
|
-
|
|
235
|
+
const wm = normalizeWorkMode(opts.workMode)
|
|
236
|
+
if (wm === "EXECUTE") return opts.base
|
|
263
237
|
return createWorkModePermissionHandler({
|
|
264
|
-
workMode:
|
|
238
|
+
workMode: wm,
|
|
265
239
|
base: opts.base,
|
|
266
240
|
root: opts.root,
|
|
267
241
|
})
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
import { type CanonicalAssignmentTable, decodeCanonicalAssignments } from "../task/assignment.ts"
|
|
16
16
|
import { classifyToolResult, denyReasonOf, summarizeArgs } from "../telemetry/trace.ts"
|
|
17
|
-
import { normalizeTodos } from "../tools/todo.ts"
|
|
17
|
+
import { extractCommittedTodoStatuses, normalizeTodos } from "../tools/todo.ts"
|
|
18
18
|
import type {
|
|
19
19
|
ApprovalEventHook,
|
|
20
20
|
ApprovalHookEvent,
|
|
@@ -483,6 +483,7 @@ export function createPresentationAdapter(
|
|
|
483
483
|
* back to the legacy positional shape rather than claim `v2`.
|
|
484
484
|
*/
|
|
485
485
|
assignment?: CanonicalAssignmentTable,
|
|
486
|
+
resultContent?: unknown,
|
|
486
487
|
):
|
|
487
488
|
| {
|
|
488
489
|
steps: PlanStep[]
|
|
@@ -501,21 +502,27 @@ export function createPresentationAdapter(
|
|
|
501
502
|
return undefined
|
|
502
503
|
}
|
|
503
504
|
if (list.length === 0) return undefined
|
|
504
|
-
const
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
t.
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
505
|
+
const committed = extractCommittedTodoStatuses(resultContent, list.length)
|
|
506
|
+
const steps: PlanStep[] = list.map((t, i) => {
|
|
507
|
+
const effectiveStatus = committed?.[i] ?? t.status
|
|
508
|
+
return {
|
|
509
|
+
stepId: String(i + 1),
|
|
510
|
+
// PHASE 4A.4 - the declared id is passed to the resolver as a HINT only. It
|
|
511
|
+
// is kept on the step solely when TaskStore confirms that row exists for
|
|
512
|
+
// this session; otherwise it is stripped below, so an unverified id can
|
|
513
|
+
// never appear on a published plan.
|
|
514
|
+
...(t.taskId ? { taskId: t.taskId } : {}),
|
|
515
|
+
title: t.content,
|
|
516
|
+
status:
|
|
517
|
+
effectiveStatus === "completed" ||
|
|
518
|
+
effectiveStatus === "cancelled" ||
|
|
519
|
+
effectiveStatus === "blocked"
|
|
520
|
+
? effectiveStatus
|
|
521
|
+
: effectiveStatus === "in_progress"
|
|
522
|
+
? "active"
|
|
523
|
+
: "pending",
|
|
524
|
+
}
|
|
525
|
+
})
|
|
519
526
|
const status = steps.every((step) => step.status === "completed")
|
|
520
527
|
? "completed"
|
|
521
528
|
: steps.every((step) => step.status === "cancelled")
|
|
@@ -946,6 +953,7 @@ export function createPresentationAdapter(
|
|
|
946
953
|
call.args,
|
|
947
954
|
planSessionId,
|
|
948
955
|
decodeCanonicalAssignments(result.content, { sessionId: planSessionId }),
|
|
956
|
+
result.content,
|
|
949
957
|
)
|
|
950
958
|
if (plan) {
|
|
951
959
|
publish({
|
package/src/providers/guards.ts
CHANGED
|
@@ -19,6 +19,8 @@ export function withStreamGuards(
|
|
|
19
19
|
provider: ModelProvider,
|
|
20
20
|
opts: {
|
|
21
21
|
timeoutMs?: number
|
|
22
|
+
/** Batas diam (tanpa byte) sebelum request dianggap mati. 0 = nonaktif. */
|
|
23
|
+
idleTimeoutMs?: number
|
|
22
24
|
maxTextChars?: number
|
|
23
25
|
maxReasonChars?: number
|
|
24
26
|
maxToolArgBytes?: number
|
|
@@ -32,6 +34,11 @@ export function withStreamGuards(
|
|
|
32
34
|
configuredTimeout > 0
|
|
33
35
|
? Math.min(LIMITS.MAX_TIMER_DELAY_MS, Math.max(1, Math.floor(configuredTimeout)))
|
|
34
36
|
: LIMITS.PROVIDER_REQUEST_TIMEOUT_MS
|
|
37
|
+
const configuredIdle = opts.idleTimeoutMs
|
|
38
|
+
const idleTimeoutMs =
|
|
39
|
+
typeof configuredIdle === "number" && Number.isFinite(configuredIdle) && configuredIdle >= 0
|
|
40
|
+
? Math.min(LIMITS.MAX_TIMER_DELAY_MS, Math.floor(configuredIdle))
|
|
41
|
+
: LIMITS.PROVIDER_IDLE_TIMEOUT_MS
|
|
35
42
|
const maxTextChars = opts.maxTextChars ?? LIMITS.PROVIDER_TEXT_MAX_CHARS
|
|
36
43
|
// Reasoning = bukan bagian jawaban; cap agresif (2MB logis ≈ 500k token —
|
|
37
44
|
// jauh di atas reasoning sah mana pun, jauh di bawah OOM).
|
|
@@ -67,6 +74,28 @@ export function withStreamGuards(
|
|
|
67
74
|
timedOut = true
|
|
68
75
|
ctl.abort(new Error(`provider request timed out after ${timeoutMs}ms`))
|
|
69
76
|
}, timeoutMs)
|
|
77
|
+
// Timeout DIAM (audit freeze 2026-11-16): timer total di atas membiarkan
|
|
78
|
+
// provider yang tak pernah mengirim byte menahan turn sampai 5 menit —
|
|
79
|
+
// user tak bisa membedakannya dari hang. Timer ini di-reset setiap ada
|
|
80
|
+
// frame masuk, jadi hanya koneksi yang benar-benar berhenti bicara yang
|
|
81
|
+
// dipotong; stream panjang (reasoning, output besar) tidak tersentuh.
|
|
82
|
+
const idleMs = idleTimeoutMs > 0 ? idleTimeoutMs : 0
|
|
83
|
+
let idleFired = false
|
|
84
|
+
let idleTimer: ReturnType<typeof setTimeout> | undefined
|
|
85
|
+
const armIdle = () => {
|
|
86
|
+
if (!idleMs) return
|
|
87
|
+
if (idleTimer) clearTimeout(idleTimer)
|
|
88
|
+
idleTimer = setTimeout(() => {
|
|
89
|
+
idleFired = true
|
|
90
|
+
ctl.abort(new Error(`provider sent nothing for ${idleMs}ms (connection stalled)`))
|
|
91
|
+
}, idleMs)
|
|
92
|
+
}
|
|
93
|
+
armIdle()
|
|
94
|
+
const stopTimers = () => {
|
|
95
|
+
clearTimeout(timer)
|
|
96
|
+
if (idleTimer) clearTimeout(idleTimer)
|
|
97
|
+
idleTimer = undefined
|
|
98
|
+
}
|
|
70
99
|
let textChars = 0
|
|
71
100
|
let reasonChars = 0
|
|
72
101
|
let toolArgumentBytes = 0
|
|
@@ -81,6 +110,8 @@ export function withStreamGuards(
|
|
|
81
110
|
while (true) {
|
|
82
111
|
const next = await nextOrAbort(iterator, ctl.signal)
|
|
83
112
|
if (next.done) break
|
|
113
|
+
// Ada frame masuk = koneksi masih hidup; jadwal diam dimulai ulang.
|
|
114
|
+
armIdle()
|
|
84
115
|
const ev = next.value
|
|
85
116
|
if (
|
|
86
117
|
finishReason !== undefined &&
|
|
@@ -186,14 +217,22 @@ export function withStreamGuards(
|
|
|
186
217
|
invalidated = true
|
|
187
218
|
yield attemptEvent("invalidated", {
|
|
188
219
|
hadVisibleOutput,
|
|
189
|
-
reason: timedOut ? "timeout" : "stream-error",
|
|
220
|
+
reason: timedOut ? "timeout" : idleFired ? "stalled" : "stream-error",
|
|
190
221
|
})
|
|
191
222
|
}
|
|
192
223
|
if (timedOut)
|
|
193
224
|
throw new ProviderError("server", `provider request timed out after ${timeoutMs}ms`)
|
|
225
|
+
// Diam total = kegagalan sisi server yang bisa diulang, bukan abort user.
|
|
226
|
+
// Diklasifikasikan "server" supaya jalur pemulihan bersama (anggaran
|
|
227
|
+
// T-009) yang menanganinya, dengan pesan yang menyebut SEBABNYA.
|
|
228
|
+
if (idleFired)
|
|
229
|
+
throw new ProviderError(
|
|
230
|
+
"server",
|
|
231
|
+
`provider sent nothing for ${idleTimeoutMs}ms (connection stalled)`,
|
|
232
|
+
)
|
|
194
233
|
throw e
|
|
195
234
|
} finally {
|
|
196
|
-
|
|
235
|
+
stopTimers()
|
|
197
236
|
signal.removeEventListener("abort", onParentAbort)
|
|
198
237
|
if (!ctl.signal.aborted && finishReason !== "stop" && finishReason !== "tool_calls") {
|
|
199
238
|
ctl.abort(new DOMException("Provider stream closed", "AbortError"))
|
|
@@ -12,10 +12,10 @@ import {
|
|
|
12
12
|
type MinicodeConfig,
|
|
13
13
|
normalizeConfig,
|
|
14
14
|
type ProviderEntry,
|
|
15
|
+
updateConfigAtomic,
|
|
15
16
|
withConfigLock,
|
|
16
17
|
writeConfigAtomic,
|
|
17
18
|
} from "../config.ts"
|
|
18
|
-
import { atomicWriteText } from "../lib/atomic-write.ts"
|
|
19
19
|
import { getSecret } from "../lib/keystore.ts"
|
|
20
20
|
import { clearDetectCache, type DetectResult, detectModels } from "./detect.ts"
|
|
21
21
|
import { GATEWAY_PRESETS } from "./presets.ts"
|
|
@@ -39,48 +39,11 @@ export async function saveProvider(
|
|
|
39
39
|
const needsKey = entry.auth !== "oauth"
|
|
40
40
|
if (!entry.id || !entry.baseUrl || (needsKey && !entry.apiKey))
|
|
41
41
|
throw new Error("provider id/baseUrl/apiKey required")
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
const path =
|
|
45
|
-
(opts.global ?? true) ? globalConfigPath() : resolve(opts.cwd ?? process.cwd(), LOCAL)
|
|
46
|
-
// Kunci per-path SAMA dengan saveMcpServer/saveLspServer (audit #07):
|
|
47
|
-
// tanpa ini baca-modifikasi-tulis paralel last-wins dan menelan entri.
|
|
48
|
-
return withConfigLock(path, async () => {
|
|
49
|
-
let cfg: MinicodeConfig = { providers: [] }
|
|
50
|
-
try {
|
|
51
|
-
cfg = normalizeConfig(JSON.parse(await readFile(path, "utf8")))
|
|
52
|
-
} catch (e) {
|
|
53
|
-
// Audit #08 P1: reset diam-diam saat berkas korup MENGHAPUS semua
|
|
54
|
-
// provider + MCP/LSP/verify/allowlist lain saat tulis berikutnya.
|
|
55
|
-
// ENOENT (belum ada file) = mulai kosong; korup = backup + gagal keras
|
|
56
|
-
// seperti saveMcpServer; error lain (EACCES/…) = teruskan, jangan timpa.
|
|
57
|
-
if (e instanceof SyntaxError) {
|
|
58
|
-
const raw = await readFile(path, "utf8").catch(() => "")
|
|
59
|
-
const backup = `${path}.corrupt.${Date.now()}`
|
|
60
|
-
await atomicWriteText(backup, raw).catch(() => {})
|
|
61
|
-
throw new Error(`config corrupt: ${path} — backup to ${backup}: ${e.message}`)
|
|
62
|
-
}
|
|
63
|
-
if ((e as NodeJS.ErrnoException).code !== "ENOENT") throw e
|
|
64
|
-
}
|
|
42
|
+
// Entri baru menang mutlak agar perintah `auto` bisa menghapus kunci jendela sebelumnya.
|
|
43
|
+
return updateConfigAtomic(opts, (cfg) => {
|
|
65
44
|
const idx = cfg.providers.findIndex((p) => p.id === entry.id)
|
|
66
|
-
if (idx >= 0) {
|
|
67
|
-
|
|
68
|
-
cfg.providers[idx] = {
|
|
69
|
-
...entry,
|
|
70
|
-
...(entry.contextWindows === undefined && previous.contextWindows
|
|
71
|
-
? { contextWindows: previous.contextWindows }
|
|
72
|
-
: {}),
|
|
73
|
-
// Sumber harus ikut diselamatkan bersama nilainya. Tanpa ini, entri
|
|
74
|
-
// yang ditulis tanpa `contextWindows` (mis. hasil `sync`) kehilangan
|
|
75
|
-
// label `user` sementara angkanya dipertahankan — nilai operator lalu
|
|
76
|
-
// dianggap milik endpoint dan boleh ditimpa pada sync berikutnya,
|
|
77
|
-
// yaitu persis hal yang label itu ada untuk mencegahnya.
|
|
78
|
-
...(entry.contextWindowSources === undefined && previous.contextWindowSources
|
|
79
|
-
? { contextWindowSources: previous.contextWindowSources }
|
|
80
|
-
: {}),
|
|
81
|
-
}
|
|
82
|
-
} else cfg.providers.push(entry)
|
|
83
|
-
await writeConfigAtomic(path, cfg)
|
|
45
|
+
if (idx >= 0) cfg.providers[idx] = { ...entry }
|
|
46
|
+
else cfg.providers.push(entry)
|
|
84
47
|
})
|
|
85
48
|
}
|
|
86
49
|
|
|
@@ -259,25 +222,8 @@ export async function detectAndSave(
|
|
|
259
222
|
}
|
|
260
223
|
|
|
261
224
|
export async function removeProvider(id: string, opts: { global?: boolean; cwd?: string } = {}) {
|
|
262
|
-
|
|
263
|
-
(opts.global ?? true) ? globalConfigPath() : resolve(opts.cwd ?? process.cwd(), LOCAL)
|
|
264
|
-
return withConfigLock(path, async () => {
|
|
265
|
-
let cfg: MinicodeConfig = { providers: [] }
|
|
266
|
-
try {
|
|
267
|
-
cfg = normalizeConfig(JSON.parse(await readFile(path, "utf8")))
|
|
268
|
-
} catch (e) {
|
|
269
|
-
// Sama seperti saveProvider (audit #08 P1): korup = backup + gagal
|
|
270
|
-
// keras, bukan reset diam-diam yang menghapus seluruh config.
|
|
271
|
-
if (e instanceof SyntaxError) {
|
|
272
|
-
const raw = await readFile(path, "utf8").catch(() => "")
|
|
273
|
-
const backup = `${path}.corrupt.${Date.now()}`
|
|
274
|
-
await atomicWriteText(backup, raw).catch(() => {})
|
|
275
|
-
throw new Error(`config corrupt: ${path} — backup to ${backup}: ${e.message}`)
|
|
276
|
-
}
|
|
277
|
-
if ((e as NodeJS.ErrnoException).code !== "ENOENT") throw e
|
|
278
|
-
}
|
|
225
|
+
return updateConfigAtomic(opts, (cfg) => {
|
|
279
226
|
cfg.providers = cfg.providers.filter((p) => p.id !== id)
|
|
280
|
-
await writeConfigAtomic(path, cfg)
|
|
281
227
|
})
|
|
282
228
|
}
|
|
283
229
|
|
|
@@ -287,6 +287,7 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
|
|
|
287
287
|
outputTokens:
|
|
288
288
|
typeof u.output_tokens === "number" ? u.output_tokens : undefined,
|
|
289
289
|
totalTokens: typeof u.total_tokens === "number" ? u.total_tokens : undefined,
|
|
290
|
+
cacheIncluded: true,
|
|
290
291
|
...(typeof cached === "number" ? { cacheReadTokens: cached } : {}),
|
|
291
292
|
},
|
|
292
293
|
}
|