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.
Files changed (139) hide show
  1. package/cli/args.ts +120 -16
  2. package/cli/commands/acp.ts +38 -28
  3. package/cli/commands/auth.ts +17 -17
  4. package/cli/commands/config.ts +65 -66
  5. package/cli/commands/daemon.ts +44 -16
  6. package/cli/commands/doctor.ts +11 -12
  7. package/cli/commands/exec.ts +35 -73
  8. package/cli/commands/mcp.ts +25 -8
  9. package/cli/commands/memory.ts +16 -24
  10. package/cli/commands/openwork.ts +6 -2
  11. package/cli/commands/pricing.ts +14 -12
  12. package/cli/commands/providers.ts +36 -57
  13. package/cli/commands/sessions.ts +24 -18
  14. package/cli/commands/skills.ts +33 -18
  15. package/cli/commands/stats.ts +15 -30
  16. package/cli/commands.ts +96 -46
  17. package/cli/context-command-run.ts +171 -0
  18. package/cli/context-command.ts +87 -0
  19. package/cli/index.ts +96 -98
  20. package/cli/model-manager.ts +7 -2
  21. package/cli/provider-manager.ts +33 -18
  22. package/cli/router.ts +41 -12
  23. package/cli/setup.ts +246 -123
  24. package/cli/tui.ts +311 -109
  25. package/docs/TERMINAL_CONTRACT.md +254 -69
  26. package/docs/UI_RENDER_PIPELINE.md +9 -1
  27. package/docs/agents.md +2 -2
  28. package/docs/architecture.md +3 -3
  29. package/docs/choosing-mode.md +7 -9
  30. package/docs/cli.md +3 -5
  31. package/docs/config.md +16 -0
  32. package/docs/environment.md +3 -3
  33. package/docs/policy-sandbox.md +3 -3
  34. package/docs/repl.md +3 -2
  35. package/docs/tools.md +1 -1
  36. package/package.json +1 -1
  37. package/src/app/mentions.ts +60 -13
  38. package/src/app/rag-layer.ts +17 -20
  39. package/src/app/session.ts +119 -66
  40. package/src/app/tool-layer.ts +12 -17
  41. package/src/config.ts +64 -104
  42. package/src/constants.ts +23 -9
  43. package/src/daemon/capability.ts +8 -1
  44. package/src/daemon/control.ts +3 -2
  45. package/src/daemon/server.ts +57 -0
  46. package/src/hooks/run.ts +73 -41
  47. package/src/lib/db-path.ts +47 -0
  48. package/src/lib/process-tree.ts +62 -0
  49. package/src/lib/public-http.ts +88 -23
  50. package/src/lib/trusted-exec.ts +48 -1
  51. package/src/lsp/client.ts +62 -17
  52. package/src/mcp/client.ts +85 -38
  53. package/src/mcp/http-transport.ts +28 -16
  54. package/src/mcp/transport.ts +42 -6
  55. package/src/memory/files.ts +164 -134
  56. package/src/memory/vector.ts +371 -420
  57. package/src/policy/approval-binding.ts +122 -45
  58. package/src/policy/bash-guard.ts +18 -7
  59. package/src/policy/compaction.ts +51 -18
  60. package/src/policy/context-window.ts +109 -0
  61. package/src/policy/context.ts +78 -37
  62. package/src/policy/executor.ts +17 -7
  63. package/src/policy/microcompact.ts +4 -3
  64. package/src/policy/no-progress.ts +103 -22
  65. package/src/policy/permission.ts +36 -12
  66. package/src/policy/tool-contract.ts +74 -416
  67. package/src/policy/usage.ts +24 -2
  68. package/src/policy/verifier.ts +50 -2
  69. package/src/policy/work-mode.ts +28 -54
  70. package/src/presentation/adapter.ts +24 -16
  71. package/src/providers/guards.ts +41 -2
  72. package/src/providers/provision.ts +6 -60
  73. package/src/providers/responses.ts +1 -0
  74. package/src/repo/repomap.ts +180 -23
  75. package/src/runtime/execution-journal.ts +4 -29
  76. package/src/sandbox/docker.ts +58 -34
  77. package/src/sandbox/mask.ts +157 -0
  78. package/src/sandbox/os.ts +107 -73
  79. package/src/session/authority.ts +29 -10
  80. package/src/session/context-projection.ts +6 -5
  81. package/src/session/open-work.ts +26 -3
  82. package/src/session/persistence.ts +25 -58
  83. package/src/session/verification.ts +170 -20
  84. package/src/skills/loader.ts +31 -16
  85. package/src/task/production-scheduler.ts +13 -1
  86. package/src/task/store.ts +20 -37
  87. package/src/telemetry/trace.ts +27 -0
  88. package/src/tools/ask_user.ts +1 -13
  89. package/src/tools/bash.ts +27 -92
  90. package/src/tools/code_run.ts +1 -1
  91. package/src/tools/context.ts +133 -0
  92. package/src/tools/delete_file.ts +3 -2
  93. package/src/tools/edit.ts +37 -72
  94. package/src/tools/git.ts +86 -9
  95. package/src/tools/glob.ts +26 -51
  96. package/src/tools/grep.ts +67 -57
  97. package/src/tools/hashline.ts +4 -0
  98. package/src/tools/lsp.ts +11 -8
  99. package/src/tools/mcp_call.ts +9 -6
  100. package/src/tools/memory.ts +56 -33
  101. package/src/tools/move_file.ts +36 -10
  102. package/src/tools/patch.ts +13 -21
  103. package/src/tools/read_file.ts +18 -25
  104. package/src/tools/read_image.ts +35 -33
  105. package/src/tools/submit_result.ts +29 -8
  106. package/src/tools/task.ts +74 -17
  107. package/src/tools/todo.ts +181 -74
  108. package/src/tools/web_fetch.ts +42 -2
  109. package/src/tools/web_search.ts +47 -8
  110. package/src/ui/approval/prompt.ts +206 -47
  111. package/src/ui/assistant/turn-status.ts +14 -5
  112. package/src/ui/footer.ts +4 -4
  113. package/src/ui/i18n/en.ts +59 -23
  114. package/src/ui/i18n/id.ts +88 -51
  115. package/src/ui/input/input.ts +4 -3
  116. package/src/ui/input/prompt-engine.ts +35 -5
  117. package/src/ui/render/activity-panel.ts +4 -2
  118. package/src/ui/render/hint.ts +7 -4
  119. package/src/ui/render/list.ts +32 -0
  120. package/src/ui/render/markdown.ts +6 -3
  121. package/src/ui/render/notice.ts +19 -0
  122. package/src/ui/render/table-grid.ts +7 -8
  123. package/src/ui/render/theme.ts +113 -39
  124. package/src/ui/runtime/screen.ts +74 -10
  125. package/src/ui/runtime/spinner.ts +19 -6
  126. package/src/ui/screens/dialog.ts +19 -1
  127. package/src/ui/screens/form.ts +80 -35
  128. package/src/ui/screens/model-manager.ts +88 -62
  129. package/src/ui/screens/output-window.ts +263 -0
  130. package/src/ui/screens/picker.ts +94 -81
  131. package/src/ui/screens/provider-manager.ts +100 -69
  132. package/src/ui/screens/wizard.ts +117 -28
  133. package/src/ui/tui/app.ts +196 -33
  134. package/src/ui/tui/transcript.ts +154 -25
  135. package/vendor/minicore/src/core/executor.ts +2 -0
  136. package/vendor/minicore/src/core/loop.ts +17 -6
  137. package/vendor/minicore/src/core/session.ts +3 -0
  138. package/vendor/minicore/src/core/tool.ts +1 -0
  139. package/vendor/minicore/src/providers/openai-compat.ts +28 -19
@@ -1,10 +1,9 @@
1
- import { execFile } from "node:child_process"
1
+ import { spawn } from "node:child_process"
2
2
  import { createHash } from "node:crypto"
3
- import { mkdirSync, readFileSync, writeFileSync } from "node:fs"
3
+ import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs"
4
4
  import { readdir } from "node:fs/promises"
5
5
  import { join, relative, resolve } from "node:path"
6
6
  import { fileURLToPath } from "node:url"
7
- import { promisify } from "node:util"
8
7
  import { LIMITS, LSP_SYMBOL_KINDS } from "../constants.ts"
9
8
  import { homeDir } from "../lib/db-path.ts"
10
9
  import { GIT_SAFE_BASE } from "../lib/git-hardening.ts"
@@ -12,8 +11,6 @@ import { safeReadFileCapped } from "../lib/safe-open.ts"
12
11
  import { resolveTrustedExecutable } from "../lib/trusted-exec.ts"
13
12
  import { isRealPathOutsideRoot } from "../policy/jail.ts"
14
13
 
15
- const execFileAsync = promisify(execFile)
16
-
17
14
  const SOURCE_EXTS = new Set([
18
15
  ".ts",
19
16
  ".tsx",
@@ -34,6 +31,24 @@ const SOURCE_EXTS = new Set([
34
31
  ".php",
35
32
  ])
36
33
  const MAX_FILES = LIMITS.REPOMAP_MAX_FILES
34
+ /**
35
+ * Batas waktu `git ls-files` untuk repo-map startup.
36
+ *
37
+ * [Audit freeze 2026-11-16] Terukur 31-90 ms di luar TUI tetapi 1000-1150 ms
38
+ * di dalam TUI. Sebab sesungguhnya bukan CPU: child mewarisi stdin TTY dalam
39
+ * raw mode dan hasilnya dibaca dari `child.stdout` yang kosong, sehingga daftar
40
+ * selalu jatuh ke walk. Batas ini tetap dipertahankan sebagai jaring: provider
41
+ * git yang benar-benar lambat (repo jaringan, index besar) tidak boleh menahan
42
+ * prompt.
43
+ */
44
+ const REPOMAP_GIT_TIMEOUT_MS = 250
45
+ /**
46
+ * Batas waktu walk direktori (jalur cadangan). Terukur 982 ms pada repo ini
47
+ * saat TUI aktif — tanpa batas, kegagalan git berubah menjadi startup lambat
48
+ * yang sama parahnya. Hasil parsial tetap dipakai: peta repo hanya pelengkap
49
+ * prompt, jadi 60 berkas cukup dan jauh lebih baik daripada menahan 1 dtk.
50
+ */
51
+ const REPOMAP_WALK_DEADLINE_MS = 400
37
52
  const MAX_FILE_BYTES = LIMITS.REPOMAP_MAX_FILE_BYTES
38
53
  const MAX_REPOMAP_CHARS = LIMITS.REPOMAP_MAX_CHARS
39
54
  const MAX_SYMBOLS_PER_FILE = LIMITS.REPOMAP_MAX_SYMBOLS_PER_FILE
@@ -274,46 +289,122 @@ async function walkForFiles(
274
289
  rel: string,
275
290
  out: string[],
276
291
  limit: number,
292
+ deadline = 0,
277
293
  ): Promise<void> {
278
294
  if (out.length >= limit) return
295
+ // Batas waktu berlaku untuk SELURUH penjelajahan (bukan per direktori):
296
+ // `deadline` adalah batas waktu absolut yang dibagikan ke semua rekursi.
297
+ if (deadline && Date.now() > deadline) return
279
298
  const dir = rel ? join(root, rel) : root
280
299
  const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
281
300
  for (const e of entries) {
282
301
  if (out.length >= limit) break
302
+ if (deadline && Date.now() > deadline) return
283
303
  if (e.name.startsWith(".") || e.name === "node_modules") continue
284
304
  const r = rel ? `${rel}/${e.name}` : e.name
285
305
  // Symlink escape (temuan audit #06): walk fallback mengikuti symlink;
286
306
  // tolak yang realpath-nya keluar workspace (simbol saja yang bocor,
287
307
  // tetapi tetap data asing tanpa provenance).
288
308
  if (isRealPathOutsideRoot(join(root, r), root)) continue
289
- if (e.isDirectory()) await walkForFiles(root, r, out, limit)
309
+ if (e.isDirectory()) await walkForFiles(root, r, out, limit, deadline)
290
310
  else if (isSourceFile(e.name)) out.push(r)
291
311
  }
292
312
  }
293
313
 
294
314
  // Daftar file sumber (git ls-files dulu → fallback walk).
315
+ //
316
+ // [Audit freeze 2026-11-16] Di dalam TUI, `git ls-files` terukur 1000-1150 ms
317
+ // padahal 31-90 ms saat dijalankan sendiri — pada Windows, spawn subproses
318
+ // bersaing CPU dengan loop render/IO TUI dan kalah. Karena daftar berkas hanya
319
+ // bahan repo-map (bukan kebenaran yang wajib), jalur startup memakai batas
320
+ // waktu KETAT: bila git tak selesai dalam `REPOMAP_GIT_TIMEOUT_MS`, jatuh ke
321
+ // walk direktori yang berjalan di proses yang sama. Walk tidak sesempurna
322
+ // gitignore, tetapi lebih baik daripada menahan prompt 1+ detik.
295
323
  async function listSourceFiles(cwd: string, limit: number): Promise<string[]> {
324
+ const fromGit = await gitListFiles(cwd, limit)
325
+ if (fromGit) return fromGit
326
+ const out: string[] = []
327
+ await walkForFiles(cwd, "", out, limit, Date.now() + REPOMAP_WALK_DEADLINE_MS)
328
+ return out
329
+ }
330
+
331
+ /**
332
+ * `git ls-files` dengan batas waktu yang BENAR-BENAR ditegakkan.
333
+ *
334
+ * [Audit freeze 2026-11-16] Tiga cacat nyata yang membuat startup TUI menahan
335
+ * ~1,2 dtk, semuanya terukur:
336
+ *
337
+ * 1. `execFile({timeout})` tidak mematuhi batasnya di Windows — timeout 250 ms
338
+ * tapi durasi nyata 1100 ms, karena ia menunggu pipe tertutup setelah kill.
339
+ * Karena itu `spawn` dipakai langsung di sini.
340
+ * 2. Child mewarisi stdin TTY TUI yang dalam raw mode: tak pernah EOF, sehingga
341
+ * git bisa menunggu input. Ditutup tegas (`stdin: "ignore"`).
342
+ * 3. Membaca dari `child.stdout` mengembalikan **0 byte** saat TUI memegang
343
+ * alt-screen — hasilnya selalu "git kosong", lalu jatuh ke walk yang jauh
344
+ * lebih lambat. Sekarang pipe dibuat EKSPLISIT dan dibaca dari handle pipe
345
+ * itu, bukan dari properti `stdout` yang bisa kosong di lingkungan TTY.
346
+ */
347
+ async function gitListFiles(cwd: string, limit: number): Promise<string[] | null> {
296
348
  try {
297
349
  // Audit #10: ls-files memicu fsmonitor repo & bisa menjalankan `git.bat`
298
350
  // repo (Windows cwd-first). Resolve absolut + netralisasi.
299
- const { stdout } = await execFileAsync(
300
- resolveTrustedExecutable("git"),
301
- [...GIT_SAFE_BASE, "ls-files"],
302
- {
351
+ const result = await new Promise<string | null>((res) => {
352
+ let settled = false
353
+ const done = (v: string | null) => {
354
+ if (settled) return
355
+ settled = true
356
+ res(v)
357
+ }
358
+ const child = spawn(resolveTrustedExecutable("git"), [...GIT_SAFE_BASE, "ls-files"], {
303
359
  cwd,
304
- timeout: 3000,
305
- encoding: "utf8",
306
- },
307
- )
308
- const files = stdout
309
- .split("\n")
310
- .map((f) => f.replace(/\\/g, "/"))
360
+ windowsHide: true,
361
+ stdio: ["ignore", "pipe", "ignore"],
362
+ })
363
+ // Pipe dibaca dari handle yang kita buat sendiri; `child.stdout` bisa
364
+ // null/terputus pada beberapa kondisi TTY.
365
+ const out = child.stdout
366
+ if (!out) {
367
+ try {
368
+ child.kill("SIGKILL")
369
+ } catch {}
370
+ done(null)
371
+ return
372
+ }
373
+ const chunks: string[] = []
374
+ const timer = setTimeout(() => {
375
+ try {
376
+ child.kill("SIGKILL")
377
+ } catch {}
378
+ done(null)
379
+ }, REPOMAP_GIT_TIMEOUT_MS)
380
+ out.on("data", (d: Buffer) => chunks.push(d.toString("utf8")))
381
+ const finish = () => {
382
+ clearTimeout(timer)
383
+ done(null)
384
+ }
385
+ child.on("error", finish)
386
+ child.on("close", () => {
387
+ clearTimeout(timer)
388
+ if (!chunks.length) {
389
+ done(null)
390
+ return
391
+ }
392
+ done(chunks.join(""))
393
+ })
394
+ })
395
+ if (!result) return null
396
+ const files = String(result)
397
+ .split(String.fromCharCode(10))
398
+ .map((f) => f.split(String.fromCharCode(92)).join("/"))
311
399
  .filter(Boolean)
312
- if (files.length) return files.filter(isSourceFile).slice(0, limit)
313
- } catch {}
314
- const out: string[] = []
315
- await walkForFiles(cwd, "", out, limit)
316
- return out
400
+ .filter(isSourceFile)
401
+ .slice(0, limit)
402
+ // git yang menjawab tapi semuanya terfilter = repon tanpa sumber git-tracked;
403
+ // biarkan null supaya walk memutuskan.
404
+ return files.length ? files : null
405
+ } catch {
406
+ return null
407
+ }
317
408
  }
318
409
 
319
410
  function cachePath(cwd: string): string {
@@ -406,11 +497,67 @@ async function buildRepoMapLsp(cwd: string): Promise<string | null> {
406
497
  }
407
498
 
408
499
  // Load repo-map, pakai cache di .minicode/repomap.json bila file tak berubah.
500
+ //
501
+ // [Audit freeze 2026-11-16] Tambahan cache DALAM-PROSES: `signature()` membaca
502
+ // isi 80 berkas (~99 ms) dan `git ls-files` (~31 ms) pada SETIAP panggilan,
503
+ // termasuk saat cache disk sudah segar — karena cache disk masih harus
504
+ // divalidasi dengan cara yang sama mahalnya. Sesi hanya butuh satu peta per
505
+ // cwd, jadi hasil per-cwd diingat di memori proses. Invalidasi: TTL pendek
506
+ // ditambah pembanding signature murah (jumlah berkas + mtime direktori kerja
507
+ // tidak dipakai karena bisa palsu) — di sini cukup TTL karena peta hanya
508
+ // memperkaya prompt; sesi panjang yang ingin peta segar memanggil `/context`
509
+ // atau memulai sesi baru.
510
+ const repomapMemo = new Map<string, { map: string; at: number; stamp: string; files: string[] }>()
511
+
512
+ /**
513
+ * Sidik "murah" untuk mendeteksi perubahan isi repo tanpa membaca 80 berkas.
514
+ *
515
+ * Kenapa bukan TTL saja: TTL akan menyajikan peta BOHONG selama satu menit
516
+ * setelah berkas berubah — regresi nyata yang tertangkap
517
+ * `test/supply-chain.test.ts` ("repomap basi dibangun ulang saat berkas
518
+ * berubah"). Staleness harus tetap menang atas kecepatan.
519
+ *
520
+ * Yang dipakai: (jumlah entry, mtime maksimum) dari direktori kerja. Jauh lebih
521
+ * murah daripada `signature()` (baca isi 80 berkas ≈ 99 ms) dan cukup untuk
522
+ * kasus nyata (menambah/menyunting berkas mengubah mtime direktori). Bila
523
+ * sidik ini cocok, peta lama masih dianggap berlaku; bila tidak, jalur normal
524
+ * (signature + cache disk) yang memutuskan.
525
+ */
526
+ function quickStamp(cwd: string, files: string[]): string {
527
+ // (mtime direktori kerja, mtime+size berkas teratas). Menyunting ISI berkas
528
+ // tidak mengubah mtime direktori, jadi mtime berkas ikut disertakan — inilah
529
+ // yang menjaga `test/supply-chain.test.ts` tetap hijau. Batasnya 16 berkas
530
+ // teratas: cukup untuk mendeteksi suntingan pada berkas yang masuk peta.
531
+ const parts: string[] = []
532
+ try {
533
+ parts.push(String(statSync(cwd).mtimeMs))
534
+ } catch {
535
+ return ""
536
+ }
537
+ for (const f of files.slice(0, 16)) {
538
+ try {
539
+ const st = statSync(resolve(cwd, f))
540
+ parts.push(`${st.mtimeMs}:${st.size}`)
541
+ } catch {
542
+ parts.push("-")
543
+ }
544
+ }
545
+ return parts.join("|")
546
+ }
547
+
409
548
  export async function loadRepoMap(cwd: string = process.cwd()): Promise<string> {
549
+ const memoKey = resolve(cwd)
410
550
  // Env MINICODE_REPOMAP=regex → skip LSP (paksa regex, lebih cepat)
411
551
  const forceRegex = process.env.MINICODE_REPOMAP === "regex"
552
+ // Sidik murah dihitung SEBELUM `git ls-files` (yang justru bagian mahalnya),
553
+ // dengan berkas yang diingat dari panggilan sebelumnya. Bila cocok, tak ada
554
+ // git/disk yang disentuh sama sekali.
555
+ const remembered = repomapMemo.get(memoKey)
556
+ const stamp = quickStamp(cwd, remembered?.files ?? [])
557
+ if (remembered && stamp && remembered.stamp === stamp) return remembered.map
412
558
  const files = await listSourceFiles(cwd, MAX_FILES)
413
559
  if (files.length === 0) return ""
560
+ const freshStamp = quickStamp(cwd, files)
414
561
  const sig = await signature(files, cwd)
415
562
  const cp = cachePath(cwd)
416
563
  // Cache hit → langsung pakai (tanpa LSP, cepat)
@@ -420,7 +567,15 @@ export async function loadRepoMap(cwd: string = process.cwd()): Promise<string>
420
567
  map?: string
421
568
  lsp?: boolean
422
569
  }
423
- if (cached.sig === sig && typeof cached.map === "string") return cached.map
570
+ if (cached.sig === sig && typeof cached.map === "string") {
571
+ repomapMemo.set(memoKey, {
572
+ map: cached.map,
573
+ at: Date.now(),
574
+ stamp: freshStamp || stamp,
575
+ files,
576
+ })
577
+ return cached.map
578
+ }
424
579
  } catch {}
425
580
  // Cache miss → coba LSP dulu (best-effort), fallback ke regex
426
581
  if (!forceRegex) {
@@ -430,6 +585,7 @@ export async function loadRepoMap(cwd: string = process.cwd()): Promise<string>
430
585
  mkdirSync(join(homeDir(), ".minicode"), { recursive: true })
431
586
  writeFileSync(cp, JSON.stringify({ sig, map: lspMap, lsp: true }))
432
587
  } catch {}
588
+ repomapMemo.set(memoKey, { map: lspMap, at: Date.now(), stamp: freshStamp || stamp, files })
433
589
  return lspMap
434
590
  }
435
591
  }
@@ -438,5 +594,6 @@ export async function loadRepoMap(cwd: string = process.cwd()): Promise<string>
438
594
  mkdirSync(join(homeDir(), ".minicode"), { recursive: true })
439
595
  writeFileSync(cp, JSON.stringify({ sig, map, lsp: false }))
440
596
  } catch {}
597
+ repomapMemo.set(memoKey, { map, at: Date.now(), stamp: freshStamp || stamp, files })
441
598
  return map
442
599
  }
@@ -37,6 +37,7 @@ import { createHash } from "node:crypto"
37
37
  import { mkdirSync } from "node:fs"
38
38
  import { dirname } from "node:path"
39
39
  import { LIMITS } from "../constants.ts"
40
+ import { isSqliteContention, withSqliteBusyRetrySync } from "../lib/db-path.ts"
40
41
  import type { ExecutionEvent } from "./execution-events.ts"
41
42
 
42
43
  /** Versi skema jurnal (bentuk/encoding) — BUKAN executionVersion. */
@@ -336,36 +337,10 @@ export async function withJournalBusyRetry<T>(
336
337
  throw last instanceof Error ? last : new Error(`SQLITE_BUSY after ${attempts} attempts`)
337
338
  }
338
339
 
339
- // [Phase 12.3] Predikat kontensi SQLite transien lintas-proses — SELARAS dengan
340
- // persistence.ts / store.ts / vector.ts (Fase 12.2). Bun menaruh kode simbolik di
341
- // `error.code` (mis. "SQLITE_IOERR_TRUNCATE") dan teks manusiawi di `error.message`
342
- // ("disk I/O error"), jadi keduanya diperiksa. SENGAJA TIDAK di-retry:
343
- // SQLITE_CORRUPT / SQLITE_NOTADB / constraint error.
344
- const SQLITE_CONTENTION_RE =
345
- /SQLITE_BUSY|database is locked|database table is locked|SQLITE_LOCKED|SQLITE_PROTOCOL|SQLITE_IOERR_TRUNCATE|SQLITE_IOERR_SHMOPEN|SQLITE_IOERR_LOCK|disk I\/O error/i
346
-
347
- function isSqliteContention(e: unknown): boolean {
348
- const code = (e as { code?: unknown } | null)?.code
349
- if (typeof code === "string" && SQLITE_CONTENTION_RE.test(code)) return true
350
- const msg = e instanceof Error ? String(e.message ?? e) : String(e)
351
- return SQLITE_CONTENTION_RE.test(msg)
352
- }
353
-
354
- const DB_INIT_ATTEMPTS = 8
355
- const DB_INIT_BACKOFF_MS = 15
356
-
357
340
  /** Retry sinkron untuk setup PRAGMA/WAL saat open (jalur sinkron openExecutionJournal). */
358
- function withBusyRetrySync<T>(fn: () => T, attempts = DB_INIT_ATTEMPTS): T | null {
359
- for (let i = 0; i < attempts; i++) {
360
- try {
361
- return fn()
362
- } catch (e) {
363
- if (!isSqliteContention(e)) throw e
364
- Bun.sleepSync(DB_INIT_BACKOFF_MS * 2 ** i)
365
- }
366
- }
367
- return null
368
- }
341
+ const DB_INIT_ATTEMPTS = 8
342
+ const withBusyRetrySync = <T>(fn: () => T, attempts = DB_INIT_ATTEMPTS): T | null =>
343
+ withSqliteBusyRetrySync(fn, { attempts })
369
344
 
370
345
  function isCorruptionError(e: unknown): boolean {
371
346
  const msg = (e instanceof Error ? e.message : String(e)).toLowerCase()
@@ -4,6 +4,11 @@ import { resolve } from "node:path"
4
4
  import { LIMITS } from "../constants.ts"
5
5
  import { resolveTrustedExecutable } from "../lib/trusted-exec.ts"
6
6
  import { sanitizeSpawnEnv } from "../policy/scrub.ts"
7
+ import {
8
+ allocateEmptyMaskFile,
9
+ createCappedOutputCollector,
10
+ discoverSandboxMaskTargets,
11
+ } from "./mask.ts"
7
12
 
8
13
  let dockerOk: boolean | null = null
9
14
 
@@ -86,24 +91,26 @@ function removeContainer(name: string, env: NodeJS.ProcessEnv): Promise<boolean>
86
91
  })
87
92
  }
88
93
 
89
- // Run command di dalam container ephemeral, terisolasi (--network none,
90
- // memori/CPU cap), mount workspace read-write. Return { code, output }.
91
- export function runInDocker(
94
+ /** Susun argumen `docker run` termasuk masking `.minicode/` & `.env*` sensitif. */
95
+ export function buildDockerRunArgs(
92
96
  command: string,
93
97
  cwd: string,
94
- opts: DockerRunOptions = {},
95
- ): Promise<{ code: number | null; output: string }> {
96
- if (opts.signal?.aborted) {
97
- return Promise.resolve({ code: null, output: "[docker] aborted before container start" })
98
- }
98
+ opts: DockerRunOptions,
99
+ mask: {
100
+ containerName: string
101
+ hasMinicode: boolean
102
+ envFiles: readonly string[]
103
+ emptyMaskHostPath: string | null
104
+ },
105
+ ): string[] {
99
106
  const image = opts.image ?? process.env.MINICODE_SANDBOX_IMAGE ?? "node:22-alpine"
100
107
  const mount = workspaceMount(cwd)
101
- const containerName = `minicode-${randomUUID()}`
108
+ const net = opts.network === "bridge" ? "bridge" : "none"
102
109
  const args = [
103
110
  "run",
104
111
  "--rm",
105
112
  "--name",
106
- containerName,
113
+ mask.containerName,
107
114
  "-i",
108
115
  "--read-only",
109
116
  "--cap-drop",
@@ -116,10 +123,22 @@ export function runInDocker(
116
123
  "/tmp",
117
124
  "-v",
118
125
  `${mount}:${mount}`,
126
+ ]
127
+ // Sembunyikan state minicode (.minicode/) dan rahasia workspace (.env*) di dalam container.
128
+ if (mask.hasMinicode) {
129
+ args.push("--tmpfs", `${mount}/.minicode:ro,size=64k`)
130
+ }
131
+ if (mask.emptyMaskHostPath) {
132
+ const emptyMount = workspaceMount(mask.emptyMaskHostPath)
133
+ for (const rel of mask.envFiles) {
134
+ args.push("-v", `${emptyMount}:${mount}/${rel}:ro`)
135
+ }
136
+ }
137
+ args.push(
119
138
  "-w",
120
139
  mount,
121
140
  "--network",
122
- "none",
141
+ net,
123
142
  "--memory",
124
143
  opts.memory ?? process.env.MINICODE_SANDBOX_MEMORY ?? "512m",
125
144
  "--cpus",
@@ -130,7 +149,29 @@ export function runInDocker(
130
149
  "sh",
131
150
  "-c",
132
151
  command,
133
- ]
152
+ )
153
+ return args
154
+ }
155
+
156
+ // Run command di dalam container ephemeral, terisolasi (--network none default,
157
+ // memori/CPU cap, masking .env/.minicode), mount workspace read-write. Return { code, output }.
158
+ export function runInDocker(
159
+ command: string,
160
+ cwd: string,
161
+ opts: DockerRunOptions = {},
162
+ ): Promise<{ code: number | null; output: string }> {
163
+ if (opts.signal?.aborted) {
164
+ return Promise.resolve({ code: null, output: "[docker] aborted before container start" })
165
+ }
166
+ const containerName = `minicode-${randomUUID()}`
167
+ const targets = discoverSandboxMaskTargets(cwd)
168
+ const emptyMask = allocateEmptyMaskFile(targets.envFiles.length > 0)
169
+ const args = buildDockerRunArgs(command, cwd, opts, {
170
+ containerName,
171
+ hasMinicode: targets.hasMinicode,
172
+ envFiles: targets.envFiles,
173
+ emptyMaskHostPath: emptyMask.path,
174
+ })
134
175
 
135
176
  return new Promise((resolveResult) => {
136
177
  // env selalu disanitasi dari hasil merge final — secret (API_KEY/TOKEN/...)
@@ -141,25 +182,9 @@ export function runInDocker(
141
182
  env,
142
183
  })
143
184
  // Cap buffer saat streaming — output container raksasa tidak menumpuk di RAM.
144
- const OUT_CAP = LIMITS.DOCKER_OUTPUT_MAX_CHARS
145
- let out = "",
146
- err = ""
147
- let truncated = false
148
- const appendCapped = (target: "out" | "err", d: Buffer | string): void => {
149
- const s = typeof d === "string" ? d : d.toString()
150
- const cur = target === "out" ? out : err
151
- if (cur.length >= OUT_CAP) {
152
- truncated = true
153
- return
154
- }
155
- const room = OUT_CAP - cur.length
156
- if (s.length > room) truncated = true
157
- const piece = s.slice(0, Math.max(0, room))
158
- if (target === "out") out += piece
159
- else err += piece
160
- }
161
- p.stdout.on("data", (d) => appendCapped("out", d))
162
- p.stderr.on("data", (d) => appendCapped("err", d))
185
+ const collector = createCappedOutputCollector(LIMITS.DOCKER_OUTPUT_MAX_CHARS)
186
+ p.stdout.on("data", (d) => collector.append("out", d))
187
+ p.stderr.on("data", (d) => collector.append("err", d))
163
188
  const timeout =
164
189
  typeof opts.timeoutMs === "number" && Number.isFinite(opts.timeoutMs) && opts.timeoutMs > 0
165
190
  ? opts.timeoutMs
@@ -175,9 +200,8 @@ export function runInDocker(
175
200
  settled = true
176
201
  clearTimeout(t)
177
202
  signal?.removeEventListener("abort", onAbort)
178
- const marker = truncated ? "\n… [output truncated]" : ""
179
- const output = (out + (err ? `\n[stderr]\n${err}` : "")).trim() + marker + suffix
180
- resolveResult({ code, output })
203
+ emptyMask.cleanup()
204
+ resolveResult({ code, output: collector.format().output + suffix })
181
205
  }
182
206
  const requestStop = (reason: "timeout" | "abort") => {
183
207
  if (stopReason || settled) return
@@ -0,0 +1,157 @@
1
+ import {
2
+ chmodSync,
3
+ existsSync,
4
+ lstatSync,
5
+ mkdtempSync,
6
+ readdirSync,
7
+ rmSync,
8
+ writeFileSync,
9
+ } from "node:fs"
10
+ import { tmpdir } from "node:os"
11
+ import { join, relative, resolve } from "node:path"
12
+
13
+ /** Template env publik non-rahasia yang tetap diizinkan terbaca di sandbox (Opsi A). */
14
+ export const SAFE_ENV_TEMPLATES: ReadonlySet<string> = new Set([
15
+ ".env.example",
16
+ ".env.sample",
17
+ ".env.template",
18
+ ])
19
+
20
+ const SKIP_DIRS: ReadonlySet<string> = new Set([
21
+ ".git",
22
+ "node_modules",
23
+ ".minicode",
24
+ "dist",
25
+ "build",
26
+ ])
27
+ const MAX_SCAN_DIRS = 500
28
+ const MAX_SCAN_DEPTH = 3
29
+
30
+ /** Apakah nama file termasuk berkas `.env*` sensitif yang wajib dimasking. */
31
+ export function isSensitiveEnvFileName(name: string): boolean {
32
+ const lower = name.toLowerCase()
33
+ if (SAFE_ENV_TEMPLATES.has(lower)) return false
34
+ return lower === ".env" || lower.startsWith(".env.")
35
+ }
36
+
37
+ export interface SandboxMaskTargets {
38
+ readonly hasMinicode: boolean
39
+ /** Path relatif berformat POSIX terhadap workspace root (`".env"`, `"apps/a/.env.local"`). */
40
+ readonly envFiles: readonly string[]
41
+ }
42
+
43
+ /**
44
+ * Temukan `.minicode/` dan file `.env*` sensitif yang BENAR-BENAR ada di workspace.
45
+ * Hanya mengembalikan path yang sudah eksis agar bind-mount read-only tidak pernah
46
+ * menciptakan entri `.env` kosong di host.
47
+ */
48
+ export function discoverSandboxMaskTargets(cwd: string): SandboxMaskTargets {
49
+ const abs = resolve(cwd)
50
+ let hasMinicode = false
51
+ try {
52
+ hasMinicode = existsSync(join(abs, ".minicode"))
53
+ } catch {
54
+ hasMinicode = false
55
+ }
56
+ const envFiles: string[] = []
57
+ const queue: { dir: string; depth: number }[] = [{ dir: abs, depth: 0 }]
58
+ let visitedDirs = 0
59
+
60
+ while (queue.length > 0 && visitedDirs < MAX_SCAN_DIRS) {
61
+ const { dir, depth } = queue.shift()!
62
+ visitedDirs++
63
+ let entries: string[]
64
+ try {
65
+ entries = readdirSync(dir)
66
+ } catch {
67
+ continue
68
+ }
69
+ for (const name of entries) {
70
+ const full = join(dir, name)
71
+ let st: ReturnType<typeof lstatSync>
72
+ try {
73
+ st = lstatSync(full)
74
+ } catch {
75
+ continue
76
+ }
77
+ if (st.isDirectory()) {
78
+ if (depth < MAX_SCAN_DEPTH && !SKIP_DIRS.has(name)) {
79
+ queue.push({ dir: full, depth: depth + 1 })
80
+ }
81
+ continue
82
+ }
83
+ if ((st.isFile() || st.isSymbolicLink()) && isSensitiveEnvFileName(name)) {
84
+ const rel = relative(abs, full).replace(/\\/g, "/")
85
+ if (rel && !rel.startsWith("..")) envFiles.push(rel)
86
+ }
87
+ }
88
+ }
89
+ envFiles.sort()
90
+ return { hasMinicode, envFiles }
91
+ }
92
+
93
+ /**
94
+ * Buat satu berkas kosong read-only (0 byte) di tempdir host untuk memasking
95
+ * file `.env*` di dalam Docker/bwrap, lengkap dengan fungsi pembersihan idempoten.
96
+ */
97
+ export function allocateEmptyMaskFile(needed: boolean): {
98
+ path: string | null
99
+ cleanup: () => void
100
+ } {
101
+ if (!needed) return { path: null, cleanup: () => {} }
102
+ let tempDir: string | null = null
103
+ let cleaned = false
104
+ try {
105
+ tempDir = mkdtempSync(join(tmpdir(), "mc-sb-mask-"))
106
+ const maskFile = join(tempDir, "empty-mask")
107
+ writeFileSync(maskFile, "", { mode: 0o444 })
108
+ try {
109
+ chmodSync(maskFile, 0o444)
110
+ } catch {}
111
+ return {
112
+ path: maskFile,
113
+ cleanup: () => {
114
+ if (cleaned || !tempDir) return
115
+ cleaned = true
116
+ try {
117
+ rmSync(tempDir, { recursive: true, force: true })
118
+ } catch {}
119
+ },
120
+ }
121
+ } catch {
122
+ if (tempDir) {
123
+ try {
124
+ rmSync(tempDir, { recursive: true, force: true })
125
+ } catch {}
126
+ }
127
+ return { path: null, cleanup: () => {} }
128
+ }
129
+ }
130
+
131
+ /** Kolektor output proses (stdout+stderr) berbatas karakter untuk runner sandbox. */
132
+ export function createCappedOutputCollector(cap: number) {
133
+ let out = ""
134
+ let err = ""
135
+ let truncated = false
136
+ const append = (target: "out" | "err", d: Buffer | string): void => {
137
+ const s = typeof d === "string" ? d : d.toString()
138
+ const cur = target === "out" ? out : err
139
+ if (cur.length >= cap) {
140
+ truncated = true
141
+ return
142
+ }
143
+ const room = cap - cur.length
144
+ if (s.length > room) truncated = true
145
+ const piece = s.slice(0, Math.max(0, room))
146
+ if (target === "out") out += piece
147
+ else err += piece
148
+ }
149
+ const format = () => {
150
+ const marker = truncated ? "\n… [output truncated]" : ""
151
+ return {
152
+ marker,
153
+ output: (out + (err ? `\n[stderr]\n${err}` : "")).trim() + marker,
154
+ }
155
+ }
156
+ return { append, format }
157
+ }