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,470 @@
|
|
|
1
|
+
import { execFile } from "node:child_process"
|
|
2
|
+
import { createHash } from "node:crypto"
|
|
3
|
+
import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs"
|
|
4
|
+
import { readdir, readFile, stat } from "node:fs/promises"
|
|
5
|
+
import { join, relative, resolve } from "node:path"
|
|
6
|
+
import { fileURLToPath } from "node:url"
|
|
7
|
+
import { promisify } from "node:util"
|
|
8
|
+
import { LIMITS } from "../constants.ts"
|
|
9
|
+
import { homeDir } from "../lib/db-path.ts"
|
|
10
|
+
import { GIT_SAFE_BASE } from "../lib/git-hardening.ts"
|
|
11
|
+
import { resolveTrustedExecutable } from "../lib/trusted-exec.ts"
|
|
12
|
+
import { isRealPathOutsideRoot } from "../policy/jail.ts"
|
|
13
|
+
|
|
14
|
+
const execFileAsync = promisify(execFile)
|
|
15
|
+
|
|
16
|
+
const SOURCE_EXTS = new Set([
|
|
17
|
+
".ts",
|
|
18
|
+
".tsx",
|
|
19
|
+
".js",
|
|
20
|
+
".jsx",
|
|
21
|
+
".mjs",
|
|
22
|
+
".cjs",
|
|
23
|
+
".py",
|
|
24
|
+
".go",
|
|
25
|
+
".rs",
|
|
26
|
+
".java",
|
|
27
|
+
".c",
|
|
28
|
+
".cpp",
|
|
29
|
+
".h",
|
|
30
|
+
".hpp",
|
|
31
|
+
".cs",
|
|
32
|
+
".rb",
|
|
33
|
+
".php",
|
|
34
|
+
])
|
|
35
|
+
const MAX_FILES = LIMITS.REPOMAP_MAX_FILES
|
|
36
|
+
const MAX_FILE_BYTES = LIMITS.REPOMAP_MAX_FILE_BYTES
|
|
37
|
+
const MAX_REPOMAP_CHARS = LIMITS.REPOMAP_MAX_CHARS
|
|
38
|
+
const MAX_SYMBOLS_PER_FILE = LIMITS.REPOMAP_MAX_SYMBOLS_PER_FILE
|
|
39
|
+
|
|
40
|
+
function langFor(file: string): string {
|
|
41
|
+
const ext = file.slice(file.lastIndexOf(".")).toLowerCase()
|
|
42
|
+
switch (ext) {
|
|
43
|
+
case ".ts":
|
|
44
|
+
case ".tsx":
|
|
45
|
+
case ".js":
|
|
46
|
+
case ".jsx":
|
|
47
|
+
case ".mjs":
|
|
48
|
+
case ".cjs":
|
|
49
|
+
return "ts"
|
|
50
|
+
case ".py":
|
|
51
|
+
return "py"
|
|
52
|
+
case ".go":
|
|
53
|
+
return "go"
|
|
54
|
+
case ".rs":
|
|
55
|
+
return "rs"
|
|
56
|
+
case ".java":
|
|
57
|
+
return "java"
|
|
58
|
+
case ".c":
|
|
59
|
+
case ".cpp":
|
|
60
|
+
case ".h":
|
|
61
|
+
case ".hpp":
|
|
62
|
+
return "c"
|
|
63
|
+
case ".cs":
|
|
64
|
+
return "cs"
|
|
65
|
+
case ".rb":
|
|
66
|
+
return "rb"
|
|
67
|
+
case ".php":
|
|
68
|
+
return "php"
|
|
69
|
+
default:
|
|
70
|
+
return ""
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface LinePattern {
|
|
75
|
+
re: RegExp
|
|
76
|
+
render: (m: RegExpExecArray) => string
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const PATTERNS: Record<string, LinePattern[]> = {
|
|
80
|
+
ts: [
|
|
81
|
+
{
|
|
82
|
+
re: /^export\s+(?:async\s+)?function\s+([A-Za-z_$][\w$]*)/,
|
|
83
|
+
render: (m) => `function ${m[1]}(...)`,
|
|
84
|
+
},
|
|
85
|
+
{ re: /^export\s+class\s+([A-Za-z_$][\w$]*)/, render: (m) => `class ${m[1]}` },
|
|
86
|
+
{ re: /^export\s+interface\s+([A-Za-z_$][\w$]*)/, render: (m) => `interface ${m[1]}` },
|
|
87
|
+
{ re: /^export\s+(type|enum)\s+([A-Za-z_$][\w$]*)/, render: (m) => `${m[1]} ${m[2]}` },
|
|
88
|
+
{ re: /^export\s+const\s+([A-Za-z_$][\w$]*)/, render: (m) => `const ${m[1]}` },
|
|
89
|
+
{
|
|
90
|
+
re: /^(?:export\s+)?(?:async\s+)?function\s+([A-Za-z_$][\w$]*)/,
|
|
91
|
+
render: (m) => `function ${m[1]}(...)`,
|
|
92
|
+
},
|
|
93
|
+
{ re: /^(?:export\s+)?class\s+([A-Za-z_$][\w$]*)/, render: (m) => `class ${m[1]}` },
|
|
94
|
+
{ re: /^(?:export\s+)?interface\s+([A-Za-z_$][\w$]*)/, render: (m) => `interface ${m[1]}` },
|
|
95
|
+
],
|
|
96
|
+
py: [
|
|
97
|
+
{ re: /^(?:async\s+)?def\s+(\w+)\s*\(/, render: (m) => `def ${m[1]}(...)` },
|
|
98
|
+
{ re: /^class\s+(\w+)/, render: (m) => `class ${m[1]}` },
|
|
99
|
+
],
|
|
100
|
+
go: [
|
|
101
|
+
{ re: /^func\s+([\w*[\]].]+)\s*\(/, render: (m) => `func ${m[1]}(...)` },
|
|
102
|
+
{ re: /^type\s+(\w+)\s+(?:struct|interface)\b/, render: (m) => `type ${m[1]}` },
|
|
103
|
+
],
|
|
104
|
+
rs: [
|
|
105
|
+
{ re: /^(?:pub\s+)?fn\s+(\w+)\s*\(/, render: (m) => `fn ${m[1]}(...)` },
|
|
106
|
+
{ re: /^(?:pub\s+)?struct\s+(\w+)/, render: (m) => `struct ${m[1]}` },
|
|
107
|
+
{
|
|
108
|
+
re: /^(?:pub\s+)?(?:enum|trait)\s+(\w+)/,
|
|
109
|
+
render: (m) => `${m[0].includes("trait") ? "trait" : "enum"} ${m[1]}`,
|
|
110
|
+
},
|
|
111
|
+
{ re: /^(?:pub\s+)?impl\s+(\w+)/, render: (m) => `impl ${m[1]}` },
|
|
112
|
+
],
|
|
113
|
+
java: [
|
|
114
|
+
{
|
|
115
|
+
re: /^(?:public|private|protected|static|final|abstract|\s)*\b(?:class|interface|enum)\s+(\w+)/,
|
|
116
|
+
render: (m) => `type ${m[1]}`,
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
re: /^\s+(?:public|private|protected)\s+(?:static\s+)?[\w<>[\],. ]+\s+(\w+)\s*\(/,
|
|
120
|
+
render: (m) => `method ${m[1]}(...)`,
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
c: [
|
|
124
|
+
{ re: /^[A-Za-z_][\w\s*]*\b(\w+)\s*\([^)]*\)\s*\{/, render: (m) => `function ${m[1]}(...)` },
|
|
125
|
+
{
|
|
126
|
+
re: /^(?:struct|enum)\s+(\w+)/,
|
|
127
|
+
render: (m) => `${m[0].startsWith("struct") ? "struct" : "enum"} ${m[1]}`,
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
cs: [
|
|
131
|
+
{
|
|
132
|
+
re: /^(?:public|private|protected|internal|static|sealed|abstract|partial|\s)*\bclass\s+(\w+)/,
|
|
133
|
+
render: (m) => `class ${m[1]}`,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
re: /^(?:public|private|protected|internal|\s)*\binterface\s+(\w+)/,
|
|
137
|
+
render: (m) => `interface ${m[1]}`,
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
re: /^\s*(?:public|private|protected|internal|static|\s)*\b\w+\s+(\w+)\s*\(/,
|
|
141
|
+
render: (m) => `method ${m[1]}(...)`,
|
|
142
|
+
},
|
|
143
|
+
{ re: /^\s*namespace\s+([\w.]+)/, render: (m) => `namespace ${m[1]}` },
|
|
144
|
+
],
|
|
145
|
+
rb: [
|
|
146
|
+
{ re: /^\s*class\s+(\w+)/, render: (m) => `class ${m[1]}` },
|
|
147
|
+
{ re: /^\s*module\s+(\w+)/, render: (m) => `module ${m[1]}` },
|
|
148
|
+
{ re: /^\s*def\s+(\w+)/, render: (m) => `def ${m[1]}(...)` },
|
|
149
|
+
],
|
|
150
|
+
php: [
|
|
151
|
+
{ re: /^\s*(?:abstract|final)?\s*class\s+(\w+)/, render: (m) => `class ${m[1]}` },
|
|
152
|
+
{ re: /^\s*interface\s+(\w+)/, render: (m) => `interface ${m[1]}` },
|
|
153
|
+
{ re: /^\s*trait\s+(\w+)/, render: (m) => `trait ${m[1]}` },
|
|
154
|
+
{
|
|
155
|
+
re: /^\s*(?:public|private|protected|static)?\s*function\s+(\w+)\s*\(/,
|
|
156
|
+
render: (m) => `function ${m[1]}(...)`,
|
|
157
|
+
},
|
|
158
|
+
{ re: /^\s*namespace\s+([\w\\]+)/, render: (m) => `namespace ${m[1]}` },
|
|
159
|
+
],
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Ekstraksi simbol berbasis regex per-bahasa.
|
|
163
|
+
//
|
|
164
|
+
// Keputusan Fase 3.2 (PLAN_V4): `src/repo/tree-sitter.ts` DIHAPUS, bukan
|
|
165
|
+
// diimplementasikan. Alasannya diukur, bukan diasumsikan.
|
|
166
|
+
//
|
|
167
|
+
// Prototipe `web-tree-sitter` + `tree-sitter-typescript` berjalan dan cepat
|
|
168
|
+
// (init 89 ms, load grammar 19 ms, parse 6 ms), tapi perbandingan pada 5 file
|
|
169
|
+
// nyata di repo ini menunjukkan selisihnya kecil dan hampir seluruhnya berupa
|
|
170
|
+
// **member kelas** (`constructor`, `append`, `execute`, `check`, `__setMode`)
|
|
171
|
+
// dan variabel lokal — bukan simbol top-level yang berguna untuk orientasi:
|
|
172
|
+
//
|
|
173
|
+
// file tree-sitter regex yang terlewat regex
|
|
174
|
+
// src/policy/bash-guard.ts 6 5 1 (helper lokal)
|
|
175
|
+
// src/session/shadow-git.ts 14 13 1 (helper lokal)
|
|
176
|
+
// src/tools/bash.ts 11 9 5 (method kelas)
|
|
177
|
+
// src/repo/repomap.ts 14 14 0
|
|
178
|
+
// src/policy/permission.ts 12 9 3 (method)
|
|
179
|
+
//
|
|
180
|
+
// Biayanya: dua dependensi (~1,4 MB wasm per bahasa, dikali jumlah bahasa yang
|
|
181
|
+
// didukung), grammar terpisah untuk tiap bahasa dari sembilan yang ada, dan
|
|
182
|
+
// jalur async baru. Sementara repo-map sudah **mencapai cap 2.500 char** pada
|
|
183
|
+
// repo ini, jadi simbol tambahan tidak akan sampai ke prompt — ia justru
|
|
184
|
+
// menggeser simbol top-level yang lebih penting.
|
|
185
|
+
//
|
|
186
|
+
// Menyimpan stub yang selalu `return null` lebih buruk daripada keduanya: ia
|
|
187
|
+
// membuat pembaca (dan dokumen) percaya kemampuan yang tidak ada. Bila suatu
|
|
188
|
+
// saat repo-map dipisah per-file atau cap dinaikkan, tree-sitter layak
|
|
189
|
+
// dipertimbangkan ulang — dengan pengukuran baru, bukan asumsi.
|
|
190
|
+
export async function extractSymbolsAsync(content: string, lang: string): Promise<string[]> {
|
|
191
|
+
return extractSymbols(content, lang)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Ekstrak simbol dari konten file berdasarkan bahasa. Deterministik & cepat (regex).
|
|
195
|
+
export function extractSymbols(content: string, lang: string): string[] {
|
|
196
|
+
const patterns = PATTERNS[lang]
|
|
197
|
+
if (!patterns) return []
|
|
198
|
+
const symbols: string[] = []
|
|
199
|
+
const seen = new Set<string>()
|
|
200
|
+
const lines = content.split("\n")
|
|
201
|
+
for (const rawLine of lines) {
|
|
202
|
+
const t = rawLine.trim()
|
|
203
|
+
if (!t || t.startsWith("//") || t.startsWith("#") || t.startsWith("/*") || t.startsWith("*"))
|
|
204
|
+
continue
|
|
205
|
+
for (const p of patterns) {
|
|
206
|
+
const m = p.re.exec(t)
|
|
207
|
+
if (m) {
|
|
208
|
+
const sig = p.render(m)
|
|
209
|
+
const key = sig.replace(/\s+/g, " ").trim()
|
|
210
|
+
if (!seen.has(key)) {
|
|
211
|
+
seen.add(key)
|
|
212
|
+
symbols.push(sig)
|
|
213
|
+
}
|
|
214
|
+
break
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (symbols.length >= MAX_SYMBOLS_PER_FILE) break
|
|
218
|
+
}
|
|
219
|
+
return symbols
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function isSourceFile(f: string): boolean {
|
|
223
|
+
const dot = f.lastIndexOf(".")
|
|
224
|
+
return dot !== -1 && SOURCE_EXTS.has(f.slice(dot).toLowerCase())
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Baca konten file sumber (async) — dipakai sekali untuk ranking + ekstraksi simbol.
|
|
228
|
+
async function readSourceContents(
|
|
229
|
+
files: string[],
|
|
230
|
+
cwd: string,
|
|
231
|
+
): Promise<{ path: string; content: string }[]> {
|
|
232
|
+
const out: { path: string; content: string }[] = []
|
|
233
|
+
for (const f of files) {
|
|
234
|
+
try {
|
|
235
|
+
const abs = resolve(cwd, f)
|
|
236
|
+
const st = await stat(abs)
|
|
237
|
+
if (!st.isFile() || st.size > MAX_FILE_BYTES) continue
|
|
238
|
+
const content = await readFile(abs, "utf8")
|
|
239
|
+
out.push({ path: f, content })
|
|
240
|
+
} catch {}
|
|
241
|
+
}
|
|
242
|
+
return out
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Ranking sederhana ala PageRank: file yang banyak di-import file lain
|
|
246
|
+
// mendapat skor lebih tinggi → tampil lebih awal di repo-map (hemat token).
|
|
247
|
+
function rankByImport(files: { path: string; content: string }[]): string[] {
|
|
248
|
+
const basenameToFiles = new Map<string, string[]>()
|
|
249
|
+
for (const { path: f } of files) {
|
|
250
|
+
const base = f.slice(f.lastIndexOf("/") + 1).replace(/\.[^.]+$/, "")
|
|
251
|
+
if (!base) continue
|
|
252
|
+
const list = basenameToFiles.get(base) ?? []
|
|
253
|
+
list.push(f)
|
|
254
|
+
basenameToFiles.set(base, list)
|
|
255
|
+
}
|
|
256
|
+
const scores = new Map<string, number>()
|
|
257
|
+
for (const { path: f } of files) scores.set(f, 0)
|
|
258
|
+
for (const { path: f, content } of files) {
|
|
259
|
+
for (const [base, targets] of basenameToFiles) {
|
|
260
|
+
if (f === targets[0] && targets.length === 1) continue // jangan hit diri sendiri bila unik
|
|
261
|
+
const importRe = new RegExp(
|
|
262
|
+
`(?:import|from|require)\\s*["'\`][^"'\`]*${base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}[^"'\`]*["'\`]`,
|
|
263
|
+
"i",
|
|
264
|
+
)
|
|
265
|
+
if (importRe.test(content)) {
|
|
266
|
+
for (const t of targets) scores.set(t, (scores.get(t) ?? 0) + 1)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return [...files.map((f) => f.path)].sort((a, b) => (scores.get(b) ?? 0) - (scores.get(a) ?? 0))
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async function walkForFiles(
|
|
274
|
+
root: string,
|
|
275
|
+
rel: string,
|
|
276
|
+
out: string[],
|
|
277
|
+
limit: number,
|
|
278
|
+
): Promise<void> {
|
|
279
|
+
if (out.length >= limit) return
|
|
280
|
+
const dir = rel ? join(root, rel) : root
|
|
281
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
|
|
282
|
+
for (const e of entries) {
|
|
283
|
+
if (out.length >= limit) break
|
|
284
|
+
if (e.name.startsWith(".") || e.name === "node_modules") continue
|
|
285
|
+
const r = rel ? `${rel}/${e.name}` : e.name
|
|
286
|
+
// Symlink escape (temuan audit #06): walk fallback mengikuti symlink;
|
|
287
|
+
// tolak yang realpath-nya keluar workspace (simbol saja yang bocor,
|
|
288
|
+
// tetapi tetap data asing tanpa provenance).
|
|
289
|
+
if (isRealPathOutsideRoot(join(root, r), root)) continue
|
|
290
|
+
if (e.isDirectory()) await walkForFiles(root, r, out, limit)
|
|
291
|
+
else if (isSourceFile(e.name)) out.push(r)
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Daftar file sumber (git ls-files dulu → fallback walk).
|
|
296
|
+
async function listSourceFiles(cwd: string, limit: number): Promise<string[]> {
|
|
297
|
+
try {
|
|
298
|
+
// Audit #10: ls-files memicu fsmonitor repo & bisa menjalankan `git.bat`
|
|
299
|
+
// repo (Windows cwd-first). Resolve absolut + netralisasi.
|
|
300
|
+
const { stdout } = await execFileAsync(
|
|
301
|
+
resolveTrustedExecutable("git"),
|
|
302
|
+
[...GIT_SAFE_BASE, "ls-files"],
|
|
303
|
+
{
|
|
304
|
+
cwd,
|
|
305
|
+
timeout: 3000,
|
|
306
|
+
encoding: "utf8",
|
|
307
|
+
},
|
|
308
|
+
)
|
|
309
|
+
const files = stdout
|
|
310
|
+
.split("\n")
|
|
311
|
+
.map((f) => f.replace(/\\/g, "/"))
|
|
312
|
+
.filter(Boolean)
|
|
313
|
+
if (files.length) return files.filter(isSourceFile).slice(0, limit)
|
|
314
|
+
} catch {}
|
|
315
|
+
const out: string[] = []
|
|
316
|
+
await walkForFiles(cwd, "", out, limit)
|
|
317
|
+
return out
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function cachePath(cwd: string): string {
|
|
321
|
+
// Audit #10 P1: cache di repo (.minicode/repomap.json) dapat ditulis
|
|
322
|
+
// penyerang — validasi apa pun yang bisa dihitung pembaca (mtime, hash)
|
|
323
|
+
// bisa dipalsukan penulis, sehingga peta BOHONG tersaji. Simpan di home
|
|
324
|
+
// operator (di luar jangkauan konten repo), kunci = hash realpath cwd.
|
|
325
|
+
// Sig isi tetap dipakai untuk deteksi basi (staleness), bukan trust.
|
|
326
|
+
try {
|
|
327
|
+
const key = createHash("sha256").update(resolve(cwd)).digest("hex").slice(0, 32)
|
|
328
|
+
return join(homeDir(), ".minicode", `repomap-${key}.json`)
|
|
329
|
+
} catch {
|
|
330
|
+
return resolve(cwd, ".minicode", "repomap.json")
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Bangun repo-map compact dari daftar file.
|
|
335
|
+
export async function buildRepoMap(
|
|
336
|
+
cwd: string = process.cwd(),
|
|
337
|
+
opts: { limit?: number } = {},
|
|
338
|
+
): Promise<string> {
|
|
339
|
+
const limit = opts.limit ?? MAX_FILES
|
|
340
|
+
const files = await listSourceFiles(cwd, limit)
|
|
341
|
+
// baca sekali → dipakai untuk ranking DAN simbol (tanpa double-read)
|
|
342
|
+
const contents = await readSourceContents(files, cwd)
|
|
343
|
+
const ranked = rankByImport(contents)
|
|
344
|
+
const byPath = new Map(contents.map((c) => [c.path, c.content]))
|
|
345
|
+
const parts: string[] = []
|
|
346
|
+
for (const f of ranked) {
|
|
347
|
+
const content = byPath.get(f)
|
|
348
|
+
if (content == null) continue
|
|
349
|
+
const symbols = extractSymbols(content, langFor(f))
|
|
350
|
+
if (symbols.length) parts.push(`${f}:\n${symbols.map((s) => ` ${s}`).join("\n")}`)
|
|
351
|
+
}
|
|
352
|
+
return parts.join("\n").slice(0, MAX_REPOMAP_CHARS)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Signature ISI (audit #10 P1): hash path+konten file terpilih — BUKAN
|
|
356
|
+
// mtime+size. Cache repomap.json tinggal di repo (dapat ditulis penyerang);
|
|
357
|
+
// sig mtime+size dapat dipalsukan via touch+padding sehingga peta BOHONG
|
|
358
|
+
// disajikan tanpa baca ulang. Dengan sig isi, cache palsu hanya cocok bila
|
|
359
|
+
// isinya benar-benar sama — dan bila sama, petanya pun benar. Cache lama
|
|
360
|
+
// (format mtime) otomatis miss → dibangun ulang sekali, tanpa migrasi.
|
|
361
|
+
function signature(files: string[], cwd: string): string {
|
|
362
|
+
const h = createHash("sha256")
|
|
363
|
+
for (const f of files.slice(0, 80)) {
|
|
364
|
+
try {
|
|
365
|
+
const abs = resolve(cwd, f)
|
|
366
|
+
const st = statSync(abs)
|
|
367
|
+
if (!st.isFile() || st.size > MAX_FILE_BYTES) continue
|
|
368
|
+
h.update(f)
|
|
369
|
+
h.update(readFileSync(abs))
|
|
370
|
+
} catch {}
|
|
371
|
+
}
|
|
372
|
+
return `v2:${h.digest("hex")}`
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Coba bangun repo-map via LSP workspace/symbol (lebih akurat dari regex).
|
|
376
|
+
// Best-effort: timeout cepat, fallback ke regex bila LSP tak tersedia/kosong.
|
|
377
|
+
async function buildRepoMapLsp(cwd: string): Promise<string | null> {
|
|
378
|
+
try {
|
|
379
|
+
const { getConfiguredExts, workspaceSymbols } = await import("../lsp/client.ts")
|
|
380
|
+
if (getConfiguredExts().length === 0) return null
|
|
381
|
+
const symbols = await workspaceSymbols("", 4000, cwd)
|
|
382
|
+
if (!symbols.length) return null
|
|
383
|
+
const KIND = [
|
|
384
|
+
"File",
|
|
385
|
+
"Module",
|
|
386
|
+
"Namespace",
|
|
387
|
+
"Package",
|
|
388
|
+
"Class",
|
|
389
|
+
"Method",
|
|
390
|
+
"Property",
|
|
391
|
+
"Field",
|
|
392
|
+
"Constructor",
|
|
393
|
+
"Enum",
|
|
394
|
+
"Interface",
|
|
395
|
+
"Function",
|
|
396
|
+
"Variable",
|
|
397
|
+
"Constant",
|
|
398
|
+
"String",
|
|
399
|
+
"Number",
|
|
400
|
+
"Boolean",
|
|
401
|
+
"Array",
|
|
402
|
+
"Object",
|
|
403
|
+
"Key",
|
|
404
|
+
"Null",
|
|
405
|
+
"EnumMember",
|
|
406
|
+
"Struct",
|
|
407
|
+
"Event",
|
|
408
|
+
"Operator",
|
|
409
|
+
"TypeParameter",
|
|
410
|
+
]
|
|
411
|
+
const byFile = new Map<string, string[]>()
|
|
412
|
+
for (const s of symbols.slice(0, 200)) {
|
|
413
|
+
let file: string
|
|
414
|
+
try {
|
|
415
|
+
file = relative(cwd, fileURLToPath(s.location.uri)).replace(/\\/g, "/")
|
|
416
|
+
} catch {
|
|
417
|
+
continue
|
|
418
|
+
}
|
|
419
|
+
const kind = KIND[(s.kind ?? 1) - 1] ?? "?"
|
|
420
|
+
const label = `${kind} ${s.name}${s.containerName ? ` (${s.containerName})` : ""}`
|
|
421
|
+
const list = byFile.get(file) ?? []
|
|
422
|
+
if (list.length < 8 && !list.includes(label)) list.push(label)
|
|
423
|
+
byFile.set(file, list)
|
|
424
|
+
}
|
|
425
|
+
if (byFile.size === 0) return null
|
|
426
|
+
const parts: string[] = []
|
|
427
|
+
for (const [file, syms] of byFile)
|
|
428
|
+
parts.push(`${file}:\n${syms.map((x) => ` ${x}`).join("\n")}`)
|
|
429
|
+
const map = parts.join("\n").slice(0, MAX_REPOMAP_CHARS)
|
|
430
|
+
return map || null
|
|
431
|
+
} catch {
|
|
432
|
+
return null
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Load repo-map, pakai cache di .minicode/repomap.json bila file tak berubah.
|
|
437
|
+
export async function loadRepoMap(cwd: string = process.cwd()): Promise<string> {
|
|
438
|
+
// Env MINICODE_REPOMAP=regex → skip LSP (paksa regex, lebih cepat)
|
|
439
|
+
const forceRegex = process.env.MINICODE_REPOMAP === "regex"
|
|
440
|
+
const files = await listSourceFiles(cwd, MAX_FILES)
|
|
441
|
+
if (files.length === 0) return ""
|
|
442
|
+
const sig = signature(files, cwd)
|
|
443
|
+
const cp = cachePath(cwd)
|
|
444
|
+
// Cache hit → langsung pakai (tanpa LSP, cepat)
|
|
445
|
+
try {
|
|
446
|
+
const cached = JSON.parse(readFileSync(cp, "utf8")) as {
|
|
447
|
+
sig?: string
|
|
448
|
+
map?: string
|
|
449
|
+
lsp?: boolean
|
|
450
|
+
}
|
|
451
|
+
if (cached.sig === sig && typeof cached.map === "string") return cached.map
|
|
452
|
+
} catch {}
|
|
453
|
+
// Cache miss → coba LSP dulu (best-effort), fallback ke regex
|
|
454
|
+
if (!forceRegex) {
|
|
455
|
+
const lspMap = await buildRepoMapLsp(cwd)
|
|
456
|
+
if (lspMap) {
|
|
457
|
+
try {
|
|
458
|
+
mkdirSync(join(homeDir(), ".minicode"), { recursive: true })
|
|
459
|
+
writeFileSync(cp, JSON.stringify({ sig, map: lspMap, lsp: true }))
|
|
460
|
+
} catch {}
|
|
461
|
+
return lspMap
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
const map = await buildRepoMap(cwd, { limit: MAX_FILES })
|
|
465
|
+
try {
|
|
466
|
+
mkdirSync(join(homeDir(), ".minicode"), { recursive: true })
|
|
467
|
+
writeFileSync(cp, JSON.stringify({ sig, map, lsp: false }))
|
|
468
|
+
} catch {}
|
|
469
|
+
return map
|
|
470
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process"
|
|
2
|
+
import { resolve } from "node:path"
|
|
3
|
+
import { LIMITS } from "../constants.ts"
|
|
4
|
+
import { resolveTrustedExecutable } from "../lib/trusted-exec.ts"
|
|
5
|
+
import { sanitizeSpawnEnv } from "../policy/scrub.ts"
|
|
6
|
+
|
|
7
|
+
let dockerOk: boolean | null = null
|
|
8
|
+
|
|
9
|
+
// Cek docker CLI tersedia (cache hasilnya).
|
|
10
|
+
export function dockerAvailable(): boolean {
|
|
11
|
+
if (dockerOk !== null) return dockerOk
|
|
12
|
+
try {
|
|
13
|
+
// F-23: resolve absolut dari PATH terpercaya — di Windows spawn mencari
|
|
14
|
+
// CWD lebih dulu sehingga docker.bat di workspace bisa membajak cek ini.
|
|
15
|
+
const r = spawnSync(
|
|
16
|
+
resolveTrustedExecutable("docker"),
|
|
17
|
+
["version", "--format", "{{.Server.Version}}"],
|
|
18
|
+
{
|
|
19
|
+
stdio: "ignore",
|
|
20
|
+
timeout: 5000,
|
|
21
|
+
},
|
|
22
|
+
)
|
|
23
|
+
dockerOk = r.status === 0
|
|
24
|
+
} catch {
|
|
25
|
+
dockerOk = false
|
|
26
|
+
}
|
|
27
|
+
return dockerOk
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Konversi path workspace untuk -v mount. Windows: C:\x → //c/x (Docker Desktop / MSYS).
|
|
31
|
+
function workspaceMount(cwd: string): string {
|
|
32
|
+
const abs = resolve(cwd)
|
|
33
|
+
if (process.platform === "win32") {
|
|
34
|
+
const m = /^([A-Za-z]):[\\/](.*)$/.exec(abs)
|
|
35
|
+
if (m) return `//${m[1]!.toLowerCase()}/${m[2]!.replace(/\\/g, "/")}`
|
|
36
|
+
}
|
|
37
|
+
return abs
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface DockerRunOptions {
|
|
41
|
+
image?: string
|
|
42
|
+
network?: "none" | "bridge"
|
|
43
|
+
memory?: string
|
|
44
|
+
cpus?: number
|
|
45
|
+
env?: Record<string, string>
|
|
46
|
+
timeoutMs?: number
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Run command di dalam container ephemeral, terisolasi (--network none,
|
|
50
|
+
// memori/CPU cap), mount workspace read-write. Return { code, output }.
|
|
51
|
+
export function runInDocker(
|
|
52
|
+
command: string,
|
|
53
|
+
cwd: string,
|
|
54
|
+
opts: DockerRunOptions = {},
|
|
55
|
+
): Promise<{ code: number | null; output: string }> {
|
|
56
|
+
const image = opts.image ?? process.env.MINICODE_SANDBOX_IMAGE ?? "node:22-alpine"
|
|
57
|
+
const mount = workspaceMount(cwd)
|
|
58
|
+
const args = [
|
|
59
|
+
"run",
|
|
60
|
+
"--rm",
|
|
61
|
+
"-i",
|
|
62
|
+
"--read-only",
|
|
63
|
+
"--cap-drop",
|
|
64
|
+
"ALL",
|
|
65
|
+
"--security-opt",
|
|
66
|
+
"no-new-privileges:true",
|
|
67
|
+
"--pids-limit",
|
|
68
|
+
"32",
|
|
69
|
+
"--tmpfs",
|
|
70
|
+
"/tmp",
|
|
71
|
+
"-v",
|
|
72
|
+
`${mount}:${mount}`,
|
|
73
|
+
"-w",
|
|
74
|
+
mount,
|
|
75
|
+
"--network",
|
|
76
|
+
"none",
|
|
77
|
+
"--memory",
|
|
78
|
+
opts.memory ?? process.env.MINICODE_SANDBOX_MEMORY ?? "512m",
|
|
79
|
+
"--cpus",
|
|
80
|
+
String(opts.cpus ?? 1),
|
|
81
|
+
"--user",
|
|
82
|
+
"1000:1000",
|
|
83
|
+
image,
|
|
84
|
+
"sh",
|
|
85
|
+
"-c",
|
|
86
|
+
command,
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
return new Promise((resolveResult) => {
|
|
90
|
+
// env selalu disanitasi dari hasil merge final — secret (API_KEY/TOKEN/...)
|
|
91
|
+
// tidak pernah diwarisi container walau caller lupa strip.
|
|
92
|
+
const p = spawn(resolveTrustedExecutable("docker"), args, {
|
|
93
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
94
|
+
env: sanitizeSpawnEnv(process.env, opts.env),
|
|
95
|
+
})
|
|
96
|
+
// Cap buffer saat streaming — output container raksasa tidak menumpuk di RAM.
|
|
97
|
+
const OUT_CAP = LIMITS.DOCKER_OUTPUT_MAX_CHARS
|
|
98
|
+
let out = "",
|
|
99
|
+
err = ""
|
|
100
|
+
let truncated = false
|
|
101
|
+
const appendCapped = (target: "out" | "err", d: Buffer | string): void => {
|
|
102
|
+
const s = typeof d === "string" ? d : d.toString()
|
|
103
|
+
const cur = target === "out" ? out : err
|
|
104
|
+
if (cur.length >= OUT_CAP) {
|
|
105
|
+
truncated = true
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
const room = OUT_CAP - cur.length
|
|
109
|
+
if (s.length > room) truncated = true
|
|
110
|
+
const piece = s.slice(0, Math.max(0, room))
|
|
111
|
+
if (target === "out") out += piece
|
|
112
|
+
else err += piece
|
|
113
|
+
}
|
|
114
|
+
p.stdout.on("data", (d) => appendCapped("out", d))
|
|
115
|
+
p.stderr.on("data", (d) => appendCapped("err", d))
|
|
116
|
+
const timeout = opts.timeoutMs ?? LIMITS.DOCKER_TIMEOUT_MS
|
|
117
|
+
const t = setTimeout(() => p.kill("SIGKILL"), timeout)
|
|
118
|
+
p.on("error", (e) => {
|
|
119
|
+
clearTimeout(t)
|
|
120
|
+
resolveResult({ code: null, output: `[docker] ${e.message}` })
|
|
121
|
+
})
|
|
122
|
+
p.on("close", (code) => {
|
|
123
|
+
clearTimeout(t)
|
|
124
|
+
const marker = truncated ? "\n… [output truncated]" : ""
|
|
125
|
+
const output = (out + (err ? `\n[stderr]\n${err}` : "")).trim() + marker
|
|
126
|
+
resolveResult({ code, output })
|
|
127
|
+
})
|
|
128
|
+
})
|
|
129
|
+
}
|