minicode-ai 0.27.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 +73 -44
- package/cli/context-command-run.ts +30 -18
- package/cli/index.ts +48 -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 +303 -100
- 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/environment.md +1 -2
- package/docs/policy-sandbox.md +3 -3
- package/docs/repl.md +2 -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 +4 -6
- 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 +31 -8
- package/src/policy/context.ts +62 -36
- 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/provision.ts +6 -56
- package/src/providers/responses.ts +1 -0
- 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 +47 -23
- package/src/ui/i18n/id.ts +76 -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 +82 -61
- 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
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type ChildProcess, type spawn, spawnSync } from "node:child_process"
|
|
2
|
+
import { resolveTrustedExecutable } from "./trusted-exec.ts"
|
|
3
|
+
|
|
4
|
+
/** Kirim sinyal ke grup proses POSIX (`-pid`, butuh `detached: true`). */
|
|
5
|
+
export function signalProcessGroup(p: Pick<ChildProcess, "pid">, signal: NodeJS.Signals): boolean {
|
|
6
|
+
if (process.platform === "win32" || p.pid === undefined) return false
|
|
7
|
+
try {
|
|
8
|
+
process.kill(-p.pid, signal)
|
|
9
|
+
return true
|
|
10
|
+
} catch {
|
|
11
|
+
return false
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Eskalasi sopan POSIX: SIGTERM ke process group, fallback ke proses tunggal. */
|
|
16
|
+
export function signalProcessGroupGraceful(
|
|
17
|
+
pid: number | undefined,
|
|
18
|
+
fallback: { kill: (signal?: NodeJS.Signals | number) => boolean },
|
|
19
|
+
opts: {
|
|
20
|
+
platform?: NodeJS.Platform
|
|
21
|
+
killGroup?: (pid: number, signal: NodeJS.Signals) => boolean
|
|
22
|
+
} = {},
|
|
23
|
+
): "group" | "fallback" {
|
|
24
|
+
const platform = opts.platform ?? process.platform
|
|
25
|
+
if (platform !== "win32" && typeof pid === "number" && pid > 0) {
|
|
26
|
+
const killGroup =
|
|
27
|
+
opts.killGroup ??
|
|
28
|
+
((target: number, signal: NodeJS.Signals): boolean => process.kill(target, signal))
|
|
29
|
+
try {
|
|
30
|
+
if (killGroup(-pid, "SIGTERM")) return "group"
|
|
31
|
+
} catch {}
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
fallback.kill("SIGTERM")
|
|
35
|
+
} catch {}
|
|
36
|
+
return "fallback"
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Hentikan proses beserta subtree: Windows `taskkill /T /F`, POSIX SIGKILL `-pid`. */
|
|
40
|
+
export function killProcessTree(
|
|
41
|
+
p: ReturnType<typeof spawn>,
|
|
42
|
+
opts: { env?: NodeJS.ProcessEnv } = {},
|
|
43
|
+
): void {
|
|
44
|
+
try {
|
|
45
|
+
if (process.platform === "win32" && p.pid !== undefined) {
|
|
46
|
+
const r = spawnSync(
|
|
47
|
+
resolveTrustedExecutable("taskkill"),
|
|
48
|
+
["/pid", String(p.pid), "/T", "/F"],
|
|
49
|
+
{
|
|
50
|
+
stdio: "ignore",
|
|
51
|
+
...(opts.env ? { env: opts.env } : {}),
|
|
52
|
+
},
|
|
53
|
+
)
|
|
54
|
+
if (r.status === 0) return
|
|
55
|
+
} else if (signalProcessGroup(p, "SIGKILL")) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
} catch {}
|
|
59
|
+
try {
|
|
60
|
+
p.kill("SIGKILL")
|
|
61
|
+
} catch {}
|
|
62
|
+
}
|
package/src/lib/public-http.ts
CHANGED
|
@@ -6,22 +6,34 @@ import { pipeline, Readable } from "node:stream"
|
|
|
6
6
|
import { createBrotliDecompress, createGunzip, createInflate } from "node:zlib"
|
|
7
7
|
import { isPrivateHost } from "./net.ts"
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
export interface PinnedRequestInit {
|
|
10
|
+
signal: AbortSignal
|
|
11
|
+
headers: Record<string, string>
|
|
12
|
+
method?: "GET" | "POST" | "DELETE"
|
|
13
|
+
body?: string
|
|
14
|
+
/** Izinkan host privat (khusus MCP lokal yang di-opt-in eksplisit); IP tetap di-pin ke socket. */
|
|
15
|
+
allowPrivateHost?: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolve once and pass only validated addresses to the socket lookup.
|
|
10
20
|
* The original URL retains HTTP Host and HTTPS certificate/SNI validation.
|
|
11
|
-
* No proxy, connection pool, or automatic redirect can bypass this lookup.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
init: { signal: AbortSignal; headers: Record<string, string> },
|
|
15
|
-
): Promise<Response> {
|
|
21
|
+
* No proxy, connection pool, or automatic redirect can bypass this lookup.
|
|
22
|
+
*/
|
|
23
|
+
export async function fetchPinnedUrl(url: URL, init: PinnedRequestInit): Promise<Response> {
|
|
16
24
|
init.signal.throwIfAborted()
|
|
17
25
|
if (url.protocol !== "http:" && url.protocol !== "https:") throw new Error("disallowed protocol")
|
|
26
|
+
const allowPrivate = init.allowPrivateHost === true
|
|
18
27
|
const hostname = url.hostname.replace(/^\[|\]$/g, "")
|
|
19
|
-
if (isPrivateHost(hostname))
|
|
28
|
+
if (!allowPrivate && isPrivateHost(hostname)) {
|
|
29
|
+
throw new Error(`blocked private host: ${hostname}`)
|
|
30
|
+
}
|
|
20
31
|
const family = isIP(hostname)
|
|
21
32
|
let addresses: { address: string; family: number }[]
|
|
22
33
|
if (family) addresses = [{ address: hostname, family }]
|
|
23
34
|
else {
|
|
24
|
-
|
|
35
|
+
// 4000ms memberi ruang untuk DNS dingin/VPN/Windows tanpa mengorbankan batas 10s web_fetch.
|
|
36
|
+
const signal = AbortSignal.any([init.signal, AbortSignal.timeout(4000)])
|
|
25
37
|
let onAbort!: () => void
|
|
26
38
|
try {
|
|
27
39
|
addresses = await Promise.race([
|
|
@@ -41,7 +53,9 @@ export async function fetchPublicUrl(
|
|
|
41
53
|
}
|
|
42
54
|
if (
|
|
43
55
|
!addresses.length ||
|
|
44
|
-
addresses.some(
|
|
56
|
+
addresses.some(
|
|
57
|
+
(entry) => !isIP(entry.address) || (!allowPrivate && isPrivateHost(entry.address)),
|
|
58
|
+
)
|
|
45
59
|
) {
|
|
46
60
|
throw new Error(`blocked private host: ${hostname}`)
|
|
47
61
|
}
|
|
@@ -49,17 +63,60 @@ export async function fetchPublicUrl(
|
|
|
49
63
|
const target = new URL(url)
|
|
50
64
|
target.username = ""
|
|
51
65
|
target.password = ""
|
|
66
|
+
const method = init.method ?? "GET"
|
|
67
|
+
const headers: Record<string, string> = { ...init.headers }
|
|
68
|
+
const payload = init.body !== undefined ? Buffer.from(init.body, "utf8") : undefined
|
|
69
|
+
if (payload && !Object.keys(headers).some((k) => k.toLowerCase() === "content-length")) {
|
|
70
|
+
headers["content-length"] = String(payload.byteLength)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Untuk HTTP polos (tanpa TLS SNI), pin IP tervalidasi langsung ke hostname URL + header Host
|
|
74
|
+
// lewat fetch native (redirect: "manual"). Ini menutup DNS rebinding sekaligus menghindari
|
|
75
|
+
// bug UAF callback custom `lookup` pada `node:http` di Bun Windows saat koneksi/abort bertubi-tubi.
|
|
76
|
+
if (target.protocol === "http:") {
|
|
77
|
+
const pinned = new URL(target)
|
|
78
|
+
const entry = addresses[0]!
|
|
79
|
+
pinned.hostname = entry.family === 6 ? `[${entry.address}]` : entry.address
|
|
80
|
+
if (!Object.keys(headers).some((k) => k.toLowerCase() === "host")) {
|
|
81
|
+
headers.host = target.host
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const res = await fetch(pinned, {
|
|
85
|
+
method,
|
|
86
|
+
headers,
|
|
87
|
+
...(init.body !== undefined ? { body: init.body } : {}),
|
|
88
|
+
signal: init.signal,
|
|
89
|
+
redirect: "manual",
|
|
90
|
+
})
|
|
91
|
+
if (res.status < 200 || res.status > 599) {
|
|
92
|
+
throw new Error(`invalid HTTP status: ${res.status}`)
|
|
93
|
+
}
|
|
94
|
+
return res
|
|
95
|
+
} catch (err) {
|
|
96
|
+
if (init.signal.aborted && init.signal.reason !== undefined) {
|
|
97
|
+
throw init.signal.reason
|
|
98
|
+
}
|
|
99
|
+
throw err
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
52
103
|
return new Promise<Response>((resolve, reject) => {
|
|
104
|
+
const rejectWithAbort = (err: unknown): void => {
|
|
105
|
+
if (init.signal.aborted && init.signal.reason !== undefined) {
|
|
106
|
+
reject(init.signal.reason)
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
reject(err)
|
|
110
|
+
}
|
|
53
111
|
const request = target.protocol === "https:" ? https.request : http.request
|
|
54
112
|
const req = request(
|
|
55
113
|
target,
|
|
56
114
|
{
|
|
57
|
-
method
|
|
58
|
-
headers
|
|
115
|
+
method,
|
|
116
|
+
headers,
|
|
59
117
|
signal: init.signal,
|
|
60
118
|
agent: false,
|
|
61
119
|
lookup: (_host, options, callback) => {
|
|
62
|
-
// Both Node and Bun may request all addresses for family autoselection.
|
|
63
120
|
if ((options as { all?: boolean }).all) {
|
|
64
121
|
callback(null, addresses as never)
|
|
65
122
|
} else {
|
|
@@ -71,22 +128,22 @@ export async function fetchPublicUrl(
|
|
|
71
128
|
(res) => {
|
|
72
129
|
let body: Readable = res
|
|
73
130
|
try {
|
|
74
|
-
const
|
|
131
|
+
const resHeaders = new Headers()
|
|
75
132
|
for (let i = 0; i < res.rawHeaders.length; i += 2) {
|
|
76
|
-
|
|
133
|
+
resHeaders.append(res.rawHeaders[i]!, res.rawHeaders[i + 1]!)
|
|
77
134
|
}
|
|
78
135
|
const status = res.statusCode ?? 502
|
|
79
136
|
if (status < 200 || status > 599) throw new Error(`invalid HTTP status: ${status}`)
|
|
80
137
|
const noBody = status === 204 || status === 205 || status === 304
|
|
81
138
|
if (noBody) res.resume()
|
|
82
139
|
else {
|
|
83
|
-
const encodings = (
|
|
140
|
+
const encodings = (resHeaders.get("content-encoding") ?? "")
|
|
84
141
|
.split(",")
|
|
85
142
|
.map((s) => s.trim().toLowerCase())
|
|
86
143
|
.filter((s) => s && s !== "identity")
|
|
87
144
|
for (const encoding of encodings.reverse()) {
|
|
88
145
|
const decoder =
|
|
89
|
-
encoding === "gzip"
|
|
146
|
+
encoding === "gzip" || encoding === "x-gzip"
|
|
90
147
|
? createGunzip()
|
|
91
148
|
: encoding === "deflate"
|
|
92
149
|
? createInflate()
|
|
@@ -98,8 +155,8 @@ export async function fetchPublicUrl(
|
|
|
98
155
|
body = decoder
|
|
99
156
|
}
|
|
100
157
|
if (encodings.length) {
|
|
101
|
-
|
|
102
|
-
|
|
158
|
+
resHeaders.delete("content-encoding")
|
|
159
|
+
resHeaders.delete("content-length")
|
|
103
160
|
}
|
|
104
161
|
}
|
|
105
162
|
resolve(
|
|
@@ -108,21 +165,29 @@ export async function fetchPublicUrl(
|
|
|
108
165
|
{
|
|
109
166
|
status,
|
|
110
167
|
statusText: res.statusMessage,
|
|
111
|
-
headers,
|
|
168
|
+
headers: resHeaders,
|
|
112
169
|
},
|
|
113
170
|
),
|
|
114
171
|
)
|
|
115
172
|
} catch (error) {
|
|
116
173
|
body.destroy()
|
|
117
174
|
res.destroy()
|
|
118
|
-
|
|
175
|
+
rejectWithAbort(error)
|
|
119
176
|
}
|
|
120
177
|
},
|
|
121
178
|
)
|
|
122
|
-
req.once("error",
|
|
123
|
-
req.end()
|
|
179
|
+
req.once("error", rejectWithAbort)
|
|
180
|
+
if (payload) req.end(payload)
|
|
181
|
+
else req.end()
|
|
124
182
|
})
|
|
125
183
|
}
|
|
126
184
|
|
|
185
|
+
export async function fetchPublicUrl(
|
|
186
|
+
url: URL,
|
|
187
|
+
init: { signal: AbortSignal; headers: Record<string, string> },
|
|
188
|
+
): Promise<Response> {
|
|
189
|
+
return fetchPinnedUrl(url, { ...init, method: "GET", allowPrivateHost: false })
|
|
190
|
+
}
|
|
191
|
+
|
|
127
192
|
// A module boundary for transport-level tests without opening real network sockets.
|
|
128
|
-
export const publicHttp = { fetch: fetchPublicUrl }
|
|
193
|
+
export const publicHttp = { fetch: fetchPublicUrl, request: fetchPinnedUrl }
|
package/src/lib/trusted-exec.ts
CHANGED
|
@@ -53,7 +53,9 @@ export function resolveTrustedExecutable(name: string): string {
|
|
|
53
53
|
const cached = cache.get(name)
|
|
54
54
|
if (cached) return cached
|
|
55
55
|
const resolved = resolveUncached(name)
|
|
56
|
-
cache
|
|
56
|
+
// Jangan pernah men-cache kegagalan resolusi (nama telanjang): bila PATH
|
|
57
|
+
// baru diisi belakangan, pemanggilan ulang harus tetap bisa menemukan biner absolut.
|
|
58
|
+
if (isAbsolute(resolved)) cache.set(name, resolved)
|
|
57
59
|
return resolved
|
|
58
60
|
}
|
|
59
61
|
|
|
@@ -85,3 +87,48 @@ function resolveUncached(name: string): string {
|
|
|
85
87
|
}
|
|
86
88
|
return name
|
|
87
89
|
}
|
|
90
|
+
|
|
91
|
+
function quoteWinCmdArg(arg: string): string {
|
|
92
|
+
if (/[\r\n\0]/.test(arg)) {
|
|
93
|
+
throw new Error("trusted-exec: unsafe control character in cmd.exe shim argument")
|
|
94
|
+
}
|
|
95
|
+
if (/^[A-Za-z0-9_./:=+-]+$/.test(arg)) return arg
|
|
96
|
+
// Pisahkan di `%` agar `cmd.exe` tidak melakukan ekspansi `%VAR%` di dalam
|
|
97
|
+
// tanda kutip ganda: tiap potongan aman dikutip, `%` disisip di luar kutip
|
|
98
|
+
// sebagai `^%` tanpa menghasilkan `""` berurutan yang dibaca MSVCRT sebagai `"`.
|
|
99
|
+
const quoteSegment = (seg: string): string =>
|
|
100
|
+
seg === "" ? "" : `"${seg.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, "$1$1")}"`
|
|
101
|
+
if (arg.includes("%")) {
|
|
102
|
+
const out = arg
|
|
103
|
+
.split("%")
|
|
104
|
+
.map((part) => quoteSegment(part))
|
|
105
|
+
.join("^%")
|
|
106
|
+
return out || '""'
|
|
107
|
+
}
|
|
108
|
+
return `"${arg.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, "$1$1")}"`
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Siapkan target `spawn()` lintas-OS yang aman dari hijack CWD sekaligus
|
|
113
|
+
* mampu menjalankan shim `.cmd`/`.bat` di Windows (Node/Bun menolak spawn
|
|
114
|
+
* langsung `.cmd` dengan EINVAL tanpa shell).
|
|
115
|
+
*/
|
|
116
|
+
export function resolveSpawnTarget(
|
|
117
|
+
command: string,
|
|
118
|
+
args: readonly string[],
|
|
119
|
+
opts: { platform?: NodeJS.Platform; resolveExec?: (name: string) => string } = {},
|
|
120
|
+
): { command: string; args: string[]; windowsVerbatimArguments?: boolean } {
|
|
121
|
+
const resolveExec = opts.resolveExec ?? resolveTrustedExecutable
|
|
122
|
+
const resolved = resolveExec(command)
|
|
123
|
+
const platform = opts.platform ?? process.platform
|
|
124
|
+
if (platform === "win32" && /\.(?:cmd|bat)$/i.test(resolved)) {
|
|
125
|
+
const comspec = resolveExec(process.env.ComSpec || "cmd.exe")
|
|
126
|
+
const line = [resolved, ...args].map(quoteWinCmdArg).join(" ")
|
|
127
|
+
return {
|
|
128
|
+
command: comspec,
|
|
129
|
+
args: ["/d", "/s", "/c", `"${line}"`],
|
|
130
|
+
windowsVerbatimArguments: true,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return { command: resolved, args: [...args] }
|
|
134
|
+
}
|
package/src/lsp/client.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Buffer } from "node:buffer"
|
|
2
|
-
import { type ChildProcess, spawn } from "node:child_process"
|
|
2
|
+
import { type ChildProcess, spawn, spawnSync } from "node:child_process"
|
|
3
3
|
import { extname, resolve as resolvePath } from "node:path"
|
|
4
4
|
import { pathToFileURL } from "node:url"
|
|
5
5
|
import { LIMITS } from "../constants.ts"
|
|
6
|
+
import { resolveSpawnTarget, resolveTrustedExecutable } from "../lib/trusted-exec.ts"
|
|
6
7
|
import { sanitizeSpawnEnv } from "../policy/scrub.ts"
|
|
7
8
|
import type {
|
|
8
9
|
ManagedChild,
|
|
@@ -91,6 +92,7 @@ class LspConnection {
|
|
|
91
92
|
private stopping = false
|
|
92
93
|
/** [P13.2] root workspace — dipakai lagi saat restart. */
|
|
93
94
|
private rootPath = ""
|
|
95
|
+
private winCmdTree = false
|
|
94
96
|
|
|
95
97
|
constructor(public entry: LspServerEntry) {}
|
|
96
98
|
|
|
@@ -110,11 +112,14 @@ class LspConnection {
|
|
|
110
112
|
this.rootPath = rootPath
|
|
111
113
|
this.stopping = false
|
|
112
114
|
this.killSignal = new AbortController()
|
|
113
|
-
|
|
115
|
+
const target = resolveSpawnTarget(this.entry.command, this.entry.args)
|
|
116
|
+
this.winCmdTree = target.windowsVerbatimArguments === true
|
|
117
|
+
this.proc = spawn(target.command, target.args, {
|
|
114
118
|
stdio: ["pipe", "pipe", "pipe"],
|
|
115
119
|
// env kredensial di-strip; env eksplisit config server menang setelahnya
|
|
116
120
|
env: sanitizeSpawnEnv(process.env, this.entry.env),
|
|
117
121
|
signal: this.killSignal.signal,
|
|
122
|
+
...(target.windowsVerbatimArguments ? { windowsVerbatimArguments: true } : {}),
|
|
118
123
|
})
|
|
119
124
|
this.proc.on("error", (err) => this.failAll(err))
|
|
120
125
|
// [P13.2] Tulis ke server yang sudah mati menghasilkan EPIPE. Itu kondisi
|
|
@@ -331,20 +336,35 @@ class LspConnection {
|
|
|
331
336
|
try {
|
|
332
337
|
this.proc.stdin?.end()
|
|
333
338
|
} catch {}
|
|
339
|
+
const p = this.proc
|
|
340
|
+
if (
|
|
341
|
+
this.winCmdTree &&
|
|
342
|
+
process.platform === "win32" &&
|
|
343
|
+
p.pid !== undefined &&
|
|
344
|
+
p.exitCode === null
|
|
345
|
+
) {
|
|
346
|
+
try {
|
|
347
|
+
spawnSync(resolveTrustedExecutable("taskkill"), ["/pid", String(p.pid), "/T", "/F"], {
|
|
348
|
+
stdio: "ignore",
|
|
349
|
+
env: sanitizeSpawnEnv(process.env),
|
|
350
|
+
})
|
|
351
|
+
} catch {}
|
|
352
|
+
}
|
|
334
353
|
this.killSignal.abort()
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
354
|
+
if (p.exitCode === null && p.signalCode === null) {
|
|
355
|
+
await new Promise<void>((r) => {
|
|
356
|
+
const t = setTimeout(() => {
|
|
357
|
+
try {
|
|
358
|
+
p.kill("SIGKILL")
|
|
359
|
+
} catch {}
|
|
360
|
+
r()
|
|
361
|
+
}, 2_000)
|
|
362
|
+
p.once("exit", () => {
|
|
363
|
+
clearTimeout(t)
|
|
364
|
+
r()
|
|
365
|
+
})
|
|
346
366
|
})
|
|
347
|
-
}
|
|
367
|
+
}
|
|
348
368
|
this.proc = null
|
|
349
369
|
this.started = false
|
|
350
370
|
this.starting = undefined
|
|
@@ -457,9 +477,34 @@ export function findSymbolPosition(
|
|
|
457
477
|
symbol: string,
|
|
458
478
|
): LspPositionResult["position"] | null {
|
|
459
479
|
const escaped = symbol.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
460
|
-
|
|
461
|
-
const
|
|
462
|
-
|
|
480
|
+
const wordRe = new RegExp(`\\b${escaped}\\b`, "g")
|
|
481
|
+
const declRe = new RegExp(
|
|
482
|
+
`^\\s*(?:export\\s+)?(?:default\\s+)?(?:async\\s+)?(?:(?:function|class|interface|type|enum|const|let|var|def|fn|struct|impl|trait)\\s+|(?:public|private|protected|static|readonly|override|pub(?:\\([^)]*\\))?)\\s+)*${escaped}\\b`,
|
|
483
|
+
)
|
|
484
|
+
const isCommentLine = (trimmed: string): boolean =>
|
|
485
|
+
trimmed.startsWith("//") ||
|
|
486
|
+
trimmed.startsWith("/*") ||
|
|
487
|
+
trimmed.startsWith("*") ||
|
|
488
|
+
trimmed.startsWith("#")
|
|
489
|
+
|
|
490
|
+
// Prioritaskan baris deklarasi non-komentar, lalu kemunculan word-boundary
|
|
491
|
+
// non-komentar pertama, baru fallback ke pencarian umum agar tidak nyasar
|
|
492
|
+
// ke komentar atau import statement di atas file.
|
|
493
|
+
const lines = text.split("\n")
|
|
494
|
+
let firstNonComment: LspPositionResult["position"] | null = null
|
|
495
|
+
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
|
|
496
|
+
const line = lines[lineIdx]!
|
|
497
|
+
const trimmed = line.trimStart()
|
|
498
|
+
if (isCommentLine(trimmed) || trimmed.startsWith("import ")) continue
|
|
499
|
+
wordRe.lastIndex = 0
|
|
500
|
+
const m = wordRe.exec(line)
|
|
501
|
+
if (!m) continue
|
|
502
|
+
if (!firstNonComment) firstNonComment = { line: lineIdx, character: m.index }
|
|
503
|
+
if (declRe.test(line)) return { line: lineIdx, character: m.index }
|
|
504
|
+
}
|
|
505
|
+
if (firstNonComment) return firstNonComment
|
|
506
|
+
|
|
507
|
+
const m = new RegExp(`\\b${escaped}\\b`).exec(text)
|
|
463
508
|
const idx = m?.index ?? text.indexOf(symbol)
|
|
464
509
|
if (idx === -1) return null
|
|
465
510
|
const before = text.slice(0, idx).split("\n")
|
package/src/mcp/client.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "
|
|
|
3
3
|
import { dirname, resolve } from "node:path"
|
|
4
4
|
import type { Tool, ToolContext } from "#minicore"
|
|
5
5
|
import { LIMITS } from "../constants.ts"
|
|
6
|
-
import { scrubSecrets } from "../policy/scrub.ts"
|
|
6
|
+
import { fenceUntrustedToolOutput, isAlreadyFenced, scrubSecrets } from "../policy/scrub.ts"
|
|
7
7
|
import { McpHttpTransport, type McpTransportLike } from "./http-transport.ts"
|
|
8
8
|
import { McpTransport } from "./transport.ts"
|
|
9
9
|
|
|
@@ -67,6 +67,28 @@ function sortObjectKeys(obj: Record<string, unknown>): Record<string, unknown> {
|
|
|
67
67
|
return out
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Normalisasi `inputSchema` dari server MCP agar provider LLM yang ketat
|
|
72
|
+
* (OpenAI/Gemini) tidak menolak tool saat server mengirim skema kosong atau
|
|
73
|
+
* tanpa `type: "object"`/`properties`. Tidak menyentuh objek asli agar pin SHA-256 utuh.
|
|
74
|
+
*/
|
|
75
|
+
export function normalizeMcpInputSchema(schema: unknown): Tool["parameters"] {
|
|
76
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
|
|
77
|
+
return { type: "object", properties: {}, additionalProperties: true }
|
|
78
|
+
}
|
|
79
|
+
const obj = schema as Record<string, unknown>
|
|
80
|
+
const typeVal = typeof obj.type === "string" && obj.type ? obj.type : "object"
|
|
81
|
+
if (typeVal === "object" && (!obj.properties || typeof obj.properties !== "object")) {
|
|
82
|
+
return {
|
|
83
|
+
...obj,
|
|
84
|
+
type: "object",
|
|
85
|
+
properties: {},
|
|
86
|
+
...(obj.additionalProperties === undefined ? { additionalProperties: true } : {}),
|
|
87
|
+
} as Tool["parameters"]
|
|
88
|
+
}
|
|
89
|
+
return { ...obj, type: typeVal } as Tool["parameters"]
|
|
90
|
+
}
|
|
91
|
+
|
|
70
92
|
/** P2.3: sleep yang dapat dibatalkan oleh signal sesi/koneksi. */
|
|
71
93
|
function abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
72
94
|
return new Promise((resolve, reject) => {
|
|
@@ -316,7 +338,9 @@ class McpConnection {
|
|
|
316
338
|
this.stdio = null
|
|
317
339
|
return this.http
|
|
318
340
|
}
|
|
319
|
-
|
|
341
|
+
// Beri ruang frame stdio 8 MiB agar screenshot/snapshot browser MCP tidak
|
|
342
|
+
// memicu SIGKILL pada server (uji unit McpTransport() tetap pakai default 1 MiB).
|
|
343
|
+
this.stdio = new McpTransport({ maxFrameBytes: LIMITS.MCP_HTTP_BODY_MAX_BYTES })
|
|
320
344
|
this.http = null
|
|
321
345
|
return this.stdio
|
|
322
346
|
}
|
|
@@ -380,27 +404,10 @@ class McpConnection {
|
|
|
380
404
|
}
|
|
381
405
|
|
|
382
406
|
let handshakeOk = false
|
|
383
|
-
// `
|
|
384
|
-
//
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
const disc = (await this.transport.request(
|
|
388
|
-
"server/discover",
|
|
389
|
-
{
|
|
390
|
-
_meta: {
|
|
391
|
-
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
|
|
392
|
-
"io.modelcontextprotocol/clientInfo": { name: "minicode", version: "0.1.0" },
|
|
393
|
-
"io.modelcontextprotocol/clientCapabilities": { tools: {} },
|
|
394
|
-
},
|
|
395
|
-
},
|
|
396
|
-
2_000,
|
|
397
|
-
)) as Record<string, unknown>
|
|
398
|
-
if (disc?.capabilities) handshakeOk = true
|
|
399
|
-
} catch {
|
|
400
|
-
// jatuh ke initialize di bawah
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
if (!handshakeOk) {
|
|
407
|
+
// Standar MCP mewajibkan `initialize` sebagai pesan pertama; mengirim
|
|
408
|
+
// `server/discover` lebih dulu membuang 2 dtk timeout dan membuat server
|
|
409
|
+
// ketat (Playwright/Puppeteer/SDK resmi) menutup koneksi.
|
|
410
|
+
try {
|
|
404
411
|
await this.transport.request(
|
|
405
412
|
"initialize",
|
|
406
413
|
{
|
|
@@ -417,6 +424,32 @@ class McpConnection {
|
|
|
417
424
|
// wajib per spec: notification initialized setelah initialize
|
|
418
425
|
this.transport.notify("notifications/initialized", {})
|
|
419
426
|
handshakeOk = true
|
|
427
|
+
} catch (initErr) {
|
|
428
|
+
const msg = (initErr as Error)?.message ?? ""
|
|
429
|
+
if (
|
|
430
|
+
this.kind === "stdio" &&
|
|
431
|
+
!this.transport.isClosed &&
|
|
432
|
+
/method not found|-32601/i.test(msg)
|
|
433
|
+
) {
|
|
434
|
+
try {
|
|
435
|
+
const disc = (await this.transport.request(
|
|
436
|
+
"server/discover",
|
|
437
|
+
{
|
|
438
|
+
_meta: {
|
|
439
|
+
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
|
|
440
|
+
"io.modelcontextprotocol/clientInfo": { name: "minicode", version: "0.1.0" },
|
|
441
|
+
"io.modelcontextprotocol/clientCapabilities": { tools: {} },
|
|
442
|
+
},
|
|
443
|
+
},
|
|
444
|
+
2_000,
|
|
445
|
+
)) as Record<string, unknown>
|
|
446
|
+
if (disc?.capabilities) handshakeOk = true
|
|
447
|
+
} catch {
|
|
448
|
+
throw initErr
|
|
449
|
+
}
|
|
450
|
+
} else {
|
|
451
|
+
throw initErr
|
|
452
|
+
}
|
|
420
453
|
}
|
|
421
454
|
if (!handshakeOk) throw new Error("handshake failed")
|
|
422
455
|
|
|
@@ -579,11 +612,7 @@ class McpConnection {
|
|
|
579
612
|
return {
|
|
580
613
|
name: `${serverId}.${t.name}`,
|
|
581
614
|
description: t.description ?? `MCP tool from ${serverId}`,
|
|
582
|
-
parameters: (t.inputSchema
|
|
583
|
-
type: "object",
|
|
584
|
-
properties: {},
|
|
585
|
-
additionalProperties: true,
|
|
586
|
-
}) as Tool["parameters"],
|
|
615
|
+
parameters: normalizeMcpInputSchema(t.inputSchema),
|
|
587
616
|
async execute(input: Record<string, unknown>, _ctx: ToolContext): Promise<string> {
|
|
588
617
|
// P2.3: pastikan koneksi hidup; reconnect bounded bila transport
|
|
589
618
|
// mati. Discovery dijalankan ulang, jadi verifikasi memakai definisi
|
|
@@ -603,7 +632,10 @@ class McpConnection {
|
|
|
603
632
|
_ctx.signal.throwIfAborted()
|
|
604
633
|
const result = await conn.callTool(t.name, input ?? {}, _ctx.signal)
|
|
605
634
|
const content = extractMcpText(result)
|
|
606
|
-
|
|
635
|
+
const capped = capMcpText(content)
|
|
636
|
+
return isAlreadyFenced(capped)
|
|
637
|
+
? capped
|
|
638
|
+
: fenceUntrustedToolOutput(`${serverId}.${t.name}`, capped)
|
|
607
639
|
},
|
|
608
640
|
}
|
|
609
641
|
})
|
|
@@ -629,7 +661,7 @@ async function ensureConnection(serverId: string, signal?: AbortSignal): Promise
|
|
|
629
661
|
return conn
|
|
630
662
|
}
|
|
631
663
|
|
|
632
|
-
/** Normalisasi konten tool MCP (string | blok {text}
|
|
664
|
+
/** Normalisasi konten tool MCP (string | blok {text} | image | resource) → teks. */
|
|
633
665
|
export function extractMcpText(result: unknown): string {
|
|
634
666
|
if (result == null) return ""
|
|
635
667
|
const r = result as { content?: unknown }
|
|
@@ -637,8 +669,26 @@ export function extractMcpText(result: unknown): string {
|
|
|
637
669
|
return r.content
|
|
638
670
|
.map((c) => {
|
|
639
671
|
if (typeof c === "string") return c
|
|
640
|
-
if (c && typeof c === "object"
|
|
641
|
-
|
|
672
|
+
if (c && typeof c === "object") {
|
|
673
|
+
const obj = c as {
|
|
674
|
+
type?: unknown
|
|
675
|
+
text?: unknown
|
|
676
|
+
data?: unknown
|
|
677
|
+
mimeType?: unknown
|
|
678
|
+
resource?: { text?: unknown; blob?: unknown; mimeType?: unknown; uri?: unknown }
|
|
679
|
+
}
|
|
680
|
+
if (typeof obj.text === "string") return obj.text
|
|
681
|
+
// Blok image/audio (mis. screenshot Playwright MCP): jangan serialize base64
|
|
682
|
+
// raksasa menjadi JSON teks yang lalu dipotong tengah di 16 KB.
|
|
683
|
+
if ((obj.type === "image" || obj.type === "audio") && typeof obj.data === "string") {
|
|
684
|
+
return `[${obj.type} ${String(obj.mimeType ?? "application/octet-stream")}, ${obj.data.length} base64 chars — binary skipped]`
|
|
685
|
+
}
|
|
686
|
+
if (obj.type === "resource" && obj.resource) {
|
|
687
|
+
if (typeof obj.resource.text === "string") return obj.resource.text
|
|
688
|
+
if (typeof obj.resource.blob === "string") {
|
|
689
|
+
return `[binary ${String(obj.resource.mimeType ?? "application/octet-stream")}, ${obj.resource.blob.length} base64 chars — skipped]`
|
|
690
|
+
}
|
|
691
|
+
}
|
|
642
692
|
}
|
|
643
693
|
return JSON.stringify(c)
|
|
644
694
|
})
|
|
@@ -717,8 +767,7 @@ export async function callMcpTool(
|
|
|
717
767
|
args: Record<string, unknown>,
|
|
718
768
|
signal?: AbortSignal,
|
|
719
769
|
): Promise<unknown> {
|
|
720
|
-
const conn =
|
|
721
|
-
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
770
|
+
const conn = await ensureConnection(serverId, signal)
|
|
722
771
|
// F8.2: verify skema ulang saat pemanggilan. Tool yang skemanya berubah
|
|
723
772
|
// tanpa persetujuan ditolak fail-closed sebelum request dikirim ke server.
|
|
724
773
|
const tool = conn.tools.find((t) => t.name === toolName)
|
|
@@ -735,8 +784,7 @@ export async function readMcpResource(
|
|
|
735
784
|
uri: string,
|
|
736
785
|
signal?: AbortSignal,
|
|
737
786
|
): Promise<string> {
|
|
738
|
-
const conn =
|
|
739
|
-
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
787
|
+
const conn = await ensureConnection(serverId, signal)
|
|
740
788
|
return conn.readResource(uri, signal)
|
|
741
789
|
}
|
|
742
790
|
|
|
@@ -746,8 +794,7 @@ export async function getMcpPrompt(
|
|
|
746
794
|
args: Record<string, unknown>,
|
|
747
795
|
signal?: AbortSignal,
|
|
748
796
|
): Promise<string> {
|
|
749
|
-
const conn =
|
|
750
|
-
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
797
|
+
const conn = await ensureConnection(serverId, signal)
|
|
751
798
|
return conn.getPrompt(name, args, signal)
|
|
752
799
|
}
|
|
753
800
|
|