minicode-ai 0.9.26
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/CHANGELOG.md +935 -0
- package/LICENSE +21 -0
- package/README.md +200 -0
- package/cli/args.ts +195 -0
- package/cli/auto-update.ts +108 -0
- package/cli/commands/auth.ts +166 -0
- package/cli/commands/config.ts +382 -0
- package/cli/commands/doctor.ts +128 -0
- package/cli/commands/exec.ts +179 -0
- package/cli/commands/mcp.ts +35 -0
- package/cli/commands/memory.ts +107 -0
- package/cli/commands/pricing.ts +100 -0
- package/cli/commands/providers.ts +162 -0
- package/cli/commands/sessions.ts +128 -0
- package/cli/commands/skills.ts +61 -0
- package/cli/commands/stats.ts +82 -0
- package/cli/commands.ts +317 -0
- package/cli/index.ts +539 -0
- package/cli/model-manager.ts +112 -0
- package/cli/provider-manager.ts +111 -0
- package/cli/repl.ts +734 -0
- package/cli/router.ts +124 -0
- package/cli/setup.ts +780 -0
- package/cli/wizard.ts +21 -0
- package/docs/ARCHITECTURE.html +1222 -0
- package/docs/README.md +109 -0
- package/docs/architecture.md +44 -0
- package/docs/changelog.md +3 -0
- package/package.json +91 -0
- package/src/agents/pool.ts +43 -0
- package/src/app/mentions.ts +50 -0
- package/src/app/provider-layer.ts +118 -0
- package/src/app/rag-layer.ts +105 -0
- package/src/app/session.ts +154 -0
- package/src/app/tool-layer.ts +56 -0
- package/src/config.ts +372 -0
- package/src/constants.ts +119 -0
- package/src/hooks/run.ts +89 -0
- package/src/lib/atomic-write.ts +88 -0
- package/src/lib/db-path.ts +36 -0
- package/src/lib/git-hardening.ts +91 -0
- package/src/lib/ignore.ts +58 -0
- package/src/lib/keystore.ts +202 -0
- package/src/lib/net.ts +70 -0
- package/src/lib/safe-open.ts +138 -0
- package/src/lib/session-id.ts +23 -0
- package/src/lib/trash.ts +38 -0
- package/src/lib/trusted-exec.ts +81 -0
- package/src/lsp/client.ts +395 -0
- package/src/mcp/client.ts +400 -0
- package/src/mcp/http-transport.ts +344 -0
- package/src/mcp/server.ts +538 -0
- package/src/mcp/transport.ts +150 -0
- package/src/memory/files.ts +159 -0
- package/src/memory/vector.ts +775 -0
- package/src/policy/allowlist.ts +67 -0
- package/src/policy/bash-guard.ts +465 -0
- package/src/policy/compaction.ts +271 -0
- package/src/policy/context.ts +161 -0
- package/src/policy/executor.ts +199 -0
- package/src/policy/jail.ts +127 -0
- package/src/policy/permission.ts +517 -0
- package/src/policy/pricing.ts +336 -0
- package/src/policy/ratelimit.ts +54 -0
- package/src/policy/sandbox-policy.ts +157 -0
- package/src/policy/scrub.ts +138 -0
- package/src/policy/update-check.ts +200 -0
- package/src/policy/usage.ts +292 -0
- package/src/policy/verifier.ts +181 -0
- package/src/providers/anthropic.ts +401 -0
- package/src/providers/auth-store.ts +139 -0
- package/src/providers/build.ts +136 -0
- package/src/providers/detect.ts +126 -0
- package/src/providers/effort.ts +165 -0
- package/src/providers/guards.ts +107 -0
- package/src/providers/oauth.ts +326 -0
- package/src/providers/presets.ts +122 -0
- package/src/providers/provision.ts +298 -0
- package/src/providers/responses.ts +371 -0
- package/src/providers/router.ts +247 -0
- package/src/repo/repomap.ts +470 -0
- package/src/sandbox/docker.ts +129 -0
- package/src/sandbox/os.ts +193 -0
- package/src/session/checkpoint.ts +543 -0
- package/src/session/journal.ts +1347 -0
- package/src/session/persistence.ts +446 -0
- package/src/session/shadow-git.ts +351 -0
- package/src/session/turn-marker.ts +118 -0
- package/src/skills/loader.ts +117 -0
- package/src/telemetry/trace.ts +283 -0
- package/src/tools/ask_user.ts +54 -0
- package/src/tools/bash.ts +400 -0
- package/src/tools/code_run.ts +96 -0
- package/src/tools/delete_file.ts +31 -0
- package/src/tools/edit.ts +204 -0
- package/src/tools/git.ts +301 -0
- package/src/tools/glob.ts +90 -0
- package/src/tools/grep.ts +281 -0
- package/src/tools/hashline.ts +66 -0
- package/src/tools/index.ts +103 -0
- package/src/tools/lsp.ts +346 -0
- package/src/tools/mcp_call.ts +163 -0
- package/src/tools/memory.ts +144 -0
- package/src/tools/move_file.ts +47 -0
- package/src/tools/patch.ts +86 -0
- package/src/tools/read_file.ts +130 -0
- package/src/tools/read_image.ts +80 -0
- package/src/tools/submit_result.ts +49 -0
- package/src/tools/task.ts +346 -0
- package/src/tools/todo.ts +201 -0
- package/src/tools/web_fetch.ts +161 -0
- package/src/tools/web_search.ts +156 -0
- package/src/tools/write_file.ts +42 -0
- package/src/ui/approval/prompt.ts +76 -0
- package/src/ui/assistant/simple.ts +568 -0
- package/src/ui/assistant/turn-status.ts +303 -0
- package/src/ui/contract.ts +55 -0
- package/src/ui/footer.ts +105 -0
- package/src/ui/input/input.ts +805 -0
- package/src/ui/input/prompt-engine.ts +606 -0
- package/src/ui/render/collapse.ts +84 -0
- package/src/ui/render/detail.ts +24 -0
- package/src/ui/render/diff.ts +181 -0
- package/src/ui/render/errors.ts +239 -0
- package/src/ui/render/format.ts +54 -0
- package/src/ui/render/highlight.ts +294 -0
- package/src/ui/render/markdown.ts +120 -0
- package/src/ui/render/money.ts +12 -0
- package/src/ui/render/reasoning.ts +20 -0
- package/src/ui/render/sanitize.ts +80 -0
- package/src/ui/render/table.ts +88 -0
- package/src/ui/render/theme.ts +339 -0
- package/src/ui/render/width.ts +256 -0
- package/src/ui/render/wrap.ts +120 -0
- package/src/ui/runtime/bus-debug.ts +83 -0
- package/src/ui/runtime/chrome.ts +301 -0
- package/src/ui/runtime/spinner.ts +77 -0
- package/src/ui/runtime/statusline.ts +227 -0
- package/src/ui/screens/model-manager.ts +434 -0
- package/src/ui/screens/overlay.ts +61 -0
- package/src/ui/screens/picker.ts +307 -0
- package/src/ui/screens/provider-manager.ts +450 -0
- package/src/ui/screens/wizard.ts +132 -0
- package/vendor/minicore/VENDOR.md +29 -0
- package/vendor/minicore/package.json +21 -0
- package/vendor/minicore/src/core/budget.ts +26 -0
- package/vendor/minicore/src/core/compact.ts +76 -0
- package/vendor/minicore/src/core/errors.ts +60 -0
- package/vendor/minicore/src/core/events.ts +74 -0
- package/vendor/minicore/src/core/executor.ts +139 -0
- package/vendor/minicore/src/core/history.ts +32 -0
- package/vendor/minicore/src/core/index.ts +42 -0
- package/vendor/minicore/src/core/loop.ts +344 -0
- package/vendor/minicore/src/core/permission.ts +24 -0
- package/vendor/minicore/src/core/provider.ts +39 -0
- package/vendor/minicore/src/core/recovery.ts +34 -0
- package/vendor/minicore/src/core/session.ts +327 -0
- package/vendor/minicore/src/core/snapshot.ts +142 -0
- package/vendor/minicore/src/core/tokens.ts +111 -0
- package/vendor/minicore/src/core/tool.ts +118 -0
- package/vendor/minicore/src/core/types.ts +50 -0
- package/vendor/minicore/src/providers/openai-compat.ts +366 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { appendFile, chmod, mkdir, readFile, unlink } from "node:fs/promises"
|
|
2
|
+
import { join, resolve } from "node:path"
|
|
3
|
+
import { LIMITS } from "../constants.ts"
|
|
4
|
+
import { atomicWriteText } from "../lib/atomic-write.ts"
|
|
5
|
+
import { scrubSecrets } from "../policy/scrub.ts"
|
|
6
|
+
|
|
7
|
+
export interface RunTrace {
|
|
8
|
+
sessionId: string
|
|
9
|
+
timestamp: string
|
|
10
|
+
prompt: string
|
|
11
|
+
durationMs: number
|
|
12
|
+
steps: number
|
|
13
|
+
turns: number
|
|
14
|
+
inputTokens: number
|
|
15
|
+
outputTokens: number
|
|
16
|
+
cost?: number
|
|
17
|
+
model?: string
|
|
18
|
+
ok: boolean
|
|
19
|
+
error?: string
|
|
20
|
+
/** P2.3: jumlah hit RAG memory yang di-inject ke system prompt run ini. */
|
|
21
|
+
memoryHits?: number
|
|
22
|
+
/** Harness-P0: true bila cost sesi melewati --budget (fail-open bila cost null). */
|
|
23
|
+
overBudget?: boolean
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Harness-P1: satu baris JSON per tool/step di .minicode/step-traces.jsonl.
|
|
27
|
+
// Trace per-run tak bisa menjawab "di mana contract gagal?" — ini bisa:
|
|
28
|
+
// tiap eksekusi tool (termasuk deny) + ringkasan tiap step tercatat.
|
|
29
|
+
export interface StepTrace {
|
|
30
|
+
sessionId: string
|
|
31
|
+
timestamp: string
|
|
32
|
+
kind: "tool" | "step"
|
|
33
|
+
step: number
|
|
34
|
+
tool?: string
|
|
35
|
+
ok?: boolean
|
|
36
|
+
/** true bila hasil diawali "permission denied"/"permission error". */
|
|
37
|
+
denied?: boolean
|
|
38
|
+
/** alasan deny yang sudah dinormalisasi (mis. bash-guard, jail, allowlist). */
|
|
39
|
+
denyReason?: string
|
|
40
|
+
durationMs?: number
|
|
41
|
+
/** ringkasan argumen yang di-scrub (tanpa isi file/kode penuh). */
|
|
42
|
+
args?: string
|
|
43
|
+
/** khusus kind=step: jumlah tool dan yang error. */
|
|
44
|
+
tools?: number
|
|
45
|
+
errors?: number
|
|
46
|
+
/** mode sandbox saat step berjalan — isolasi apa yang sebenarnya aktif. */
|
|
47
|
+
sandbox?: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Kunci argumen yang aman diringkas; `content`/`code`/body lain SENGAJA
|
|
51
|
+
// dibuang (bisa megabyte + berisi secret) — yang dicatat hanya pointer.
|
|
52
|
+
const ARG_KEYS = [
|
|
53
|
+
"path",
|
|
54
|
+
"from",
|
|
55
|
+
"to",
|
|
56
|
+
"cmd",
|
|
57
|
+
"pattern",
|
|
58
|
+
"query",
|
|
59
|
+
"prompt",
|
|
60
|
+
"file",
|
|
61
|
+
"id",
|
|
62
|
+
"url",
|
|
63
|
+
"dir",
|
|
64
|
+
"lang",
|
|
65
|
+
"name",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
export function summarizeArgs(args: unknown): string {
|
|
69
|
+
if (args == null) return ""
|
|
70
|
+
if (typeof args === "string") return scrubSecrets(args.slice(0, 120))
|
|
71
|
+
if (typeof args !== "object") return scrubSecrets(String(args).slice(0, 120))
|
|
72
|
+
const rec = args as Record<string, unknown>
|
|
73
|
+
const parts: string[] = []
|
|
74
|
+
for (const k of ARG_KEYS) {
|
|
75
|
+
const v = rec[k]
|
|
76
|
+
if (typeof v === "string" && v) parts.push(`${k}=${v.slice(0, 120)}`)
|
|
77
|
+
else if (typeof v === "number" || typeof v === "boolean") parts.push(`${k}=${v}`)
|
|
78
|
+
}
|
|
79
|
+
return scrubSecrets(parts.join(" ").slice(0, 300))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function resultText(content: unknown): string {
|
|
83
|
+
if (typeof content === "string") return content
|
|
84
|
+
if (Array.isArray(content)) {
|
|
85
|
+
return content
|
|
86
|
+
.map((p) =>
|
|
87
|
+
typeof p === "string" ? p : typeof p?.text === "string" ? (p.text as string) : "",
|
|
88
|
+
)
|
|
89
|
+
.join("\n")
|
|
90
|
+
}
|
|
91
|
+
return ""
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Klasifikasi observasi tool: deny permission adalah sinyal harness lapis-1
|
|
95
|
+
// (kuota, jail, allowlist) — tanpa ini deny tak terlihat di event stream
|
|
96
|
+
// karena kernel hanya meng-emit execution:* untuk call yang lolos gate.
|
|
97
|
+
export function classifyToolResult(result: {
|
|
98
|
+
isError?: boolean
|
|
99
|
+
content: unknown
|
|
100
|
+
}): "ok" | "denied" | "error" {
|
|
101
|
+
if (!result.isError) return "ok"
|
|
102
|
+
const t = resultText(result.content).toLowerCase()
|
|
103
|
+
if (t.startsWith("permission denied") || t.startsWith("permission error")) return "denied"
|
|
104
|
+
return "error"
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Alasan deny yang dinormalisasi untuk observability (SPEC). */
|
|
108
|
+
export function denyReasonOf(result: { isError?: boolean; content: unknown }): string | undefined {
|
|
109
|
+
const t = resultText(result.content)
|
|
110
|
+
if (!t.toLowerCase().startsWith("permission")) return undefined
|
|
111
|
+
// Ambil baris pertama, potong prefix "permission denied: "
|
|
112
|
+
const first = t.split("\n")[0] ?? t
|
|
113
|
+
const m = /permission (?:denied|error):?\s*(.*)/i.exec(first)
|
|
114
|
+
const reason = (m?.[1] ?? first).trim().slice(0, 80)
|
|
115
|
+
if (!reason) return "unknown"
|
|
116
|
+
// Normalisasi: jail, bash-guard, allowlist, sensitive, outside
|
|
117
|
+
if (/outside workspace|symlink/i.test(reason)) return "jail"
|
|
118
|
+
if (/sensitive/i.test(reason)) return "sensitive"
|
|
119
|
+
if (/allowlist/i.test(reason)) return "allowlist"
|
|
120
|
+
if (/bash|guard|destructive|interpreter|env-dump/i.test(reason)) return "bash-guard"
|
|
121
|
+
return reason.slice(0, 40)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Opt-out privasi: MINICODE_TELEMETRY=0/false/off → tidak ada file ditulis.
|
|
125
|
+
function telemetryEnabled(): boolean {
|
|
126
|
+
const v = (process.env.MINICODE_TELEMETRY ?? "").trim().toLowerCase()
|
|
127
|
+
return v !== "0" && v !== "false" && v !== "off"
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Telemetry ringan: satu baris JSON per run di .minicode/traces.jsonl.
|
|
131
|
+
// Tanpa OTel — cukup untuk agregasi manual / metrik sederhana.
|
|
132
|
+
export async function writeTrace(cwd: string | undefined, trace: RunTrace): Promise<void> {
|
|
133
|
+
if (!telemetryEnabled()) return
|
|
134
|
+
try {
|
|
135
|
+
const dir = resolve(cwd ?? ".", ".minicode")
|
|
136
|
+
await mkdir(dir, { recursive: true, mode: 0o700 }).catch(() => {})
|
|
137
|
+
const file = join(dir, "traces.jsonl")
|
|
138
|
+
// prompt/error/model di-redact sebelum persist — bisa berisi secret/PII
|
|
139
|
+
const safe: RunTrace = {
|
|
140
|
+
...trace,
|
|
141
|
+
prompt: scrubSecrets(trace.prompt.slice(0, 2000)),
|
|
142
|
+
...(trace.error ? { error: scrubSecrets(trace.error.slice(0, 1000)) } : {}),
|
|
143
|
+
...(trace.model ? { model: scrubSecrets(trace.model) } : {}),
|
|
144
|
+
}
|
|
145
|
+
await appendFile(file, `${JSON.stringify(safe)}\n`, "utf8")
|
|
146
|
+
await chmod(file, 0o600).catch(() => {})
|
|
147
|
+
// Rotate: keep TRACE_MAX_LINES baris terakhir (tmp+rename agar anti-korupsi)
|
|
148
|
+
try {
|
|
149
|
+
const txt = await readFile(file, "utf8")
|
|
150
|
+
const lines = txt.split("\n").filter(Boolean)
|
|
151
|
+
if (lines.length > LIMITS.TRACE_MAX_LINES) {
|
|
152
|
+
await atomicWriteText(file, `${lines.slice(-LIMITS.TRACE_MAX_LINES).join("\n")}\n`)
|
|
153
|
+
}
|
|
154
|
+
} catch {}
|
|
155
|
+
} catch {}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function writeStepTrace(cwd: string | undefined, step: StepTrace): Promise<void> {
|
|
159
|
+
if (!telemetryEnabled()) return
|
|
160
|
+
try {
|
|
161
|
+
const dir = resolve(cwd ?? ".", ".minicode")
|
|
162
|
+
await mkdir(dir, { recursive: true, mode: 0o700 }).catch(() => {})
|
|
163
|
+
const file = join(dir, "step-traces.jsonl")
|
|
164
|
+
const safe: StepTrace = { ...step, ...(step.args ? { args: scrubSecrets(step.args) } : {}) }
|
|
165
|
+
await appendFile(file, `${JSON.stringify(safe)}\n`, "utf8")
|
|
166
|
+
await chmod(file, 0o600).catch(() => {})
|
|
167
|
+
try {
|
|
168
|
+
const txt = await readFile(file, "utf8")
|
|
169
|
+
const lines = txt.split("\n").filter(Boolean)
|
|
170
|
+
if (lines.length > LIMITS.TRACE_MAX_LINES) {
|
|
171
|
+
await atomicWriteText(file, `${lines.slice(-LIMITS.TRACE_MAX_LINES).join("\n")}\n`)
|
|
172
|
+
}
|
|
173
|
+
} catch {}
|
|
174
|
+
} catch {}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Audit #10 §17: penghapusan sesi wajib mencakup jejak telemetry miliknya.
|
|
178
|
+
// traces.jsonl/step-traces.jsonl adalah berkas bersama per-workspace (untuk
|
|
179
|
+
// `minicode stats`), sehingga baris sesi yang dihapus bertahan sebagai
|
|
180
|
+
// residual reachable — reproducer: writeTrace marker → deleteSession → marker
|
|
181
|
+
// masih terbaca. Fungsi ini membuang HANYA baris dengan sessionId cocok via
|
|
182
|
+
// rewrite atomik; baris korup (tak ter-parse) DIPERTAHANKAN (fail-closed:
|
|
183
|
+
// jangan hapus yang tak bisa diatribusikan).
|
|
184
|
+
//
|
|
185
|
+
// Tradeoff jujur: rewrite read-filter-write balapan dengan append konkuren
|
|
186
|
+
// (trace sesi lain yang ditulis tepat di jendela ini bisa hilang — kehilangan
|
|
187
|
+
// observability, bukan kebocoran). Dipanggil best-effort dari deleteSession.
|
|
188
|
+
export async function purgeSessionTraces(
|
|
189
|
+
sessionId: string,
|
|
190
|
+
cwd: string | undefined,
|
|
191
|
+
): Promise<number> {
|
|
192
|
+
let removed = 0
|
|
193
|
+
for (const name of ["traces.jsonl", "step-traces.jsonl"]) {
|
|
194
|
+
try {
|
|
195
|
+
const file = join(resolve(cwd ?? ".", ".minicode"), name)
|
|
196
|
+
let txt: string
|
|
197
|
+
try {
|
|
198
|
+
txt = await readFile(file, "utf8")
|
|
199
|
+
} catch {
|
|
200
|
+
continue // tak ada berkas = tak ada residual
|
|
201
|
+
}
|
|
202
|
+
const lines = txt.split("\n").filter(Boolean)
|
|
203
|
+
if (lines.length === 0) continue
|
|
204
|
+
const keep: string[] = []
|
|
205
|
+
for (const line of lines) {
|
|
206
|
+
try {
|
|
207
|
+
const row = JSON.parse(line) as { sessionId?: unknown }
|
|
208
|
+
if (row.sessionId === sessionId) {
|
|
209
|
+
removed++
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
} catch {
|
|
213
|
+
// baris korup: pertahankan (lihat kontrak di atas)
|
|
214
|
+
}
|
|
215
|
+
keep.push(line)
|
|
216
|
+
}
|
|
217
|
+
if (keep.length === lines.length) continue // tak ada milik sesi ini
|
|
218
|
+
if (keep.length === 0) await unlink(file).catch(() => {})
|
|
219
|
+
else await atomicWriteText(file, `${keep.join("\n")}\n`)
|
|
220
|
+
} catch {}
|
|
221
|
+
}
|
|
222
|
+
return removed
|
|
223
|
+
}
|
|
224
|
+
// Harness-P3: agregat step-trace untuk `minicode stats` — data observability
|
|
225
|
+
// per-step (P1.2) kembali menjadi keputusan: deny-rate, tool paling sering
|
|
226
|
+
// ditolak/gagal, mode sandbox yang terlihat. Pure agar bisa diuji.
|
|
227
|
+
export interface StepSummary {
|
|
228
|
+
tools: number
|
|
229
|
+
ok: number
|
|
230
|
+
denied: number
|
|
231
|
+
errors: number
|
|
232
|
+
denyRate: number
|
|
233
|
+
topDenied: { tool: string; n: number }[]
|
|
234
|
+
topErrors: { tool: string; n: number }[]
|
|
235
|
+
sandboxes: string[]
|
|
236
|
+
topDenyReasons: { reason: string; n: number }[]
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function summarizeStepTraces(rows: StepTrace[]): StepSummary {
|
|
240
|
+
const tools = rows.filter((r) => r.kind === "tool")
|
|
241
|
+
let ok = 0
|
|
242
|
+
let denied = 0
|
|
243
|
+
let errors = 0
|
|
244
|
+
const deniedBy = new Map<string, number>()
|
|
245
|
+
const errBy = new Map<string, number>()
|
|
246
|
+
const reasonBy = new Map<string, number>()
|
|
247
|
+
const sandboxes = new Set<string>()
|
|
248
|
+
for (const t of tools) {
|
|
249
|
+
const name = t.tool ?? "?"
|
|
250
|
+
if (t.denied) {
|
|
251
|
+
denied++
|
|
252
|
+
deniedBy.set(name, (deniedBy.get(name) ?? 0) + 1)
|
|
253
|
+
const r = t.denyReason ?? "unknown"
|
|
254
|
+
reasonBy.set(r, (reasonBy.get(r) ?? 0) + 1)
|
|
255
|
+
} else if (t.ok === false) {
|
|
256
|
+
errors++
|
|
257
|
+
errBy.set(name, (errBy.get(name) ?? 0) + 1)
|
|
258
|
+
} else {
|
|
259
|
+
ok++
|
|
260
|
+
}
|
|
261
|
+
if (t.sandbox) sandboxes.add(t.sandbox)
|
|
262
|
+
}
|
|
263
|
+
const top = (m: Map<string, number>) =>
|
|
264
|
+
[...m.entries()]
|
|
265
|
+
.sort((a, b) => b[1] - a[1])
|
|
266
|
+
.slice(0, 3)
|
|
267
|
+
.map(([tool, n]) => ({ tool, n }))
|
|
268
|
+
const topReason = [...reasonBy.entries()]
|
|
269
|
+
.sort((a, b) => b[1] - a[1])
|
|
270
|
+
.slice(0, 3)
|
|
271
|
+
.map(([reason, n]) => ({ reason, n }))
|
|
272
|
+
return {
|
|
273
|
+
tools: tools.length,
|
|
274
|
+
ok,
|
|
275
|
+
denied,
|
|
276
|
+
errors,
|
|
277
|
+
denyRate: tools.length ? denied / tools.length : 0,
|
|
278
|
+
topDenied: top(deniedBy),
|
|
279
|
+
topErrors: top(errBy),
|
|
280
|
+
sandboxes: [...sandboxes],
|
|
281
|
+
topDenyReasons: topReason,
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Tool } from "#minicore"
|
|
2
|
+
|
|
3
|
+
// Tanya user di tengah run — untuk pilihan yang tidak bisa diputuskan model
|
|
4
|
+
// sendiri (ambiguitas kebutuhan, kredensial, "lanjut/tidak").
|
|
5
|
+
//
|
|
6
|
+
// DI seperti delegate_task: view pertanyaan di-inject dari composition root
|
|
7
|
+
// (cli/setup.ts), lapisan tool tidak mengimpor src/ui. Tanpa injeksi atau di
|
|
8
|
+
// luar TTY tool MENOLAK jalan (fail-closed) — model harus memilih default
|
|
9
|
+
// yang aman sendiri, bukan mengarang jawaban user.
|
|
10
|
+
|
|
11
|
+
export type AskTextFn = (question: string, options?: string[]) => Promise<string | null>
|
|
12
|
+
|
|
13
|
+
let askTextFn: AskTextFn | undefined
|
|
14
|
+
|
|
15
|
+
export function setAskTextFn(fn: AskTextFn | undefined): void {
|
|
16
|
+
askTextFn = fn
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const askUserTool: Tool = {
|
|
20
|
+
name: "ask_user",
|
|
21
|
+
description:
|
|
22
|
+
"Ask the user a clarifying question mid-run and wait for their answer. Use sparingly — only when blocked on a decision you cannot safely make alone. Fails outside an interactive terminal.",
|
|
23
|
+
parameters: {
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {
|
|
26
|
+
question: { type: "string", description: "single, clear question (max 2000 chars)" },
|
|
27
|
+
options: {
|
|
28
|
+
type: "array",
|
|
29
|
+
items: { type: "string" },
|
|
30
|
+
description: "suggested choices (may be empty); empty answer cancels",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
required: ["question"],
|
|
34
|
+
additionalProperties: false,
|
|
35
|
+
},
|
|
36
|
+
async execute({ question, options }, ctx) {
|
|
37
|
+
ctx.signal.throwIfAborted()
|
|
38
|
+
const q = String(question ?? "")
|
|
39
|
+
.trim()
|
|
40
|
+
.slice(0, 2000)
|
|
41
|
+
if (!q) throw new Error("question empty")
|
|
42
|
+
const opts = Array.isArray(options)
|
|
43
|
+
? (options as unknown[]).map((o) => String(o).slice(0, 200)).slice(0, 8)
|
|
44
|
+
: undefined
|
|
45
|
+
// Tanpa view ter-inject atau di luar TTY: tolak, jangan gantung.
|
|
46
|
+
// Model harus melanjutkan dengan asumsi paling aman, bukan menunggu.
|
|
47
|
+
if (!askTextFn)
|
|
48
|
+
throw new Error("ask_user unavailable: no question view injected (headless run?)")
|
|
49
|
+
if (!process.stdin.isTTY) throw new Error("ask_user unavailable: needs an interactive terminal")
|
|
50
|
+
const answer = await askTextFn(q, opts)
|
|
51
|
+
if (answer == null || !answer.trim()) throw new Error("ask_user cancelled (empty answer)")
|
|
52
|
+
return answer.trim().slice(0, 4000)
|
|
53
|
+
},
|
|
54
|
+
}
|