minicode-ai 0.14.1 → 0.14.4
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/README.md +34 -178
- package/cli/args.ts +10 -18
- package/cli/auto-update.ts +32 -2
- package/cli/commands/acp.ts +20 -27
- package/cli/commands/daemon.ts +6 -7
- package/cli/commands/exec.ts +12 -10
- package/cli/commands.ts +6 -9
- package/cli/index.ts +40 -17
- package/cli/provider-manager.ts +1 -3
- package/cli/setup.ts +174 -145
- package/cli/tui.ts +33 -38
- package/docs/CONTROL-PLANE-MAP.md +145 -0
- package/docs/OUTPUT_EVENT_MODEL.md +1 -1
- package/docs/OUTPUT_PROTOCOL_SPEC.md +8 -2
- package/docs/OUTPUT_RENDERING_SPEC.md +35 -6
- package/docs/OUTPUT_UX_RULES.md +1 -1
- package/docs/README.md +14 -102
- package/docs/SUMMARY.md +53 -0
- package/docs/TERMINAL_CONTRACT.md +136 -77
- package/docs/UI_RENDER_PIPELINE.md +204 -0
- package/docs/agents.md +5 -5
- package/docs/architecture.md +23 -12
- package/docs/choosing-mode.md +7 -7
- package/docs/cli.md +22 -29
- package/docs/concepts.md +1 -1
- package/docs/config.md +10 -5
- package/docs/contributing.md +6 -6
- package/docs/environment.md +3 -1
- package/docs/exec.md +5 -5
- package/docs/getting-started.md +14 -12
- package/docs/glossary.md +9 -7
- package/docs/mcp-lsp.md +1 -1
- package/docs/memory-sessions.md +7 -5
- package/docs/policy-sandbox.md +23 -15
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +4 -3
- package/docs/security-model.md +16 -17
- package/docs/security.md +15 -30
- package/docs/skills.md +1 -1
- package/docs/tools.md +6 -6
- package/docs/verify-benchmark.md +16 -0
- package/package.json +5 -5
- package/scripts/vendor-overlays/minicode-context.patch +109 -0
- package/src/app/mentions.ts +4 -3
- package/src/app/provider-layer.ts +13 -1
- package/src/app/session.ts +229 -22
- package/src/app/tool-layer.ts +6 -0
- package/src/config.ts +30 -3
- package/src/constants.ts +19 -0
- package/src/daemon/control.ts +1 -1
- package/src/lib/git-hardening.ts +27 -22
- package/src/lib/safe-open.ts +73 -19
- package/src/lib/trusted-exec.ts +7 -4
- package/src/mcp/client.ts +34 -9
- package/src/mcp/server.ts +2 -3
- package/src/mcp/transport.ts +96 -42
- package/src/memory/files.ts +15 -4
- package/src/memory/vector.ts +5 -1
- package/src/policy/compaction.ts +272 -16
- package/src/policy/context-window.ts +73 -0
- package/src/policy/context.ts +76 -16
- package/src/policy/ratelimit.ts +47 -18
- package/src/policy/tool-contract.ts +4 -10
- package/src/policy/update-check.ts +83 -6
- package/src/policy/usage.ts +31 -14
- package/src/policy/work-mode.ts +1 -1
- package/src/presentation/adapter.ts +64 -2
- package/src/presentation/events.ts +6 -0
- package/src/presentation/model.ts +1 -0
- package/src/presentation/projection.ts +2 -0
- package/src/presentation/reducer.ts +1 -0
- package/src/providers/anthropic.ts +52 -19
- package/src/providers/guards.ts +158 -16
- package/src/providers/provision.ts +12 -3
- package/src/providers/responses.ts +61 -23
- package/src/providers/router.ts +56 -23
- package/src/providers/sse-bounds.ts +170 -0
- package/src/repo/repomap.ts +10 -11
- package/src/runtime/execution-backend.ts +7 -8
- package/src/sandbox/docker.ts +113 -9
- package/src/session/checkpoint.ts +9 -2
- package/src/session/context-projection.ts +14 -0
- package/src/session/context-selector.ts +58 -20
- package/src/session/persistence.ts +6 -3
- package/src/session/verification.ts +55 -16
- package/src/skills/loader.ts +16 -3
- package/src/task/model.ts +21 -70
- package/src/tools/bash.ts +6 -3
- package/src/tools/code_run.ts +6 -5
- package/src/tools/context.ts +8 -0
- package/src/tools/delete_file.ts +2 -1
- package/src/tools/edit.ts +2 -1
- package/src/tools/git.ts +128 -54
- package/src/tools/glob.ts +2 -1
- package/src/tools/grep.ts +31 -6
- package/src/tools/lsp.ts +5 -4
- package/src/tools/mcp_call.ts +3 -1
- package/src/tools/memory.ts +175 -180
- package/src/tools/move_file.ts +2 -1
- package/src/tools/patch.ts +2 -1
- package/src/tools/read_file.ts +94 -1
- package/src/tools/read_image.ts +2 -1
- package/src/tools/task.ts +9 -1
- package/src/tools/todo.ts +4 -5
- package/src/tools/web_fetch.ts +28 -12
- package/src/tools/web_search.ts +28 -9
- package/src/tools/write_file.ts +2 -1
- package/src/ui/assistant/simple.ts +72 -3
- package/src/ui/footer.ts +131 -66
- package/src/ui/i18n/en.ts +3 -11
- package/src/ui/i18n/id.ts +3 -11
- package/src/ui/input/input.ts +2 -3
- package/src/ui/input/prompt-engine.ts +2 -5
- package/src/ui/render/width.ts +107 -79
- package/src/ui/runtime/screen.ts +49 -37
- package/src/ui/screens/model-manager.ts +3 -10
- package/src/ui/screens/picker.ts +23 -40
- package/src/ui/screens/provider-manager.ts +12 -33
- package/src/ui/screens/wizard.ts +0 -1
- package/src/ui/tui/app.ts +265 -211
- package/src/ui/tui/transcript.ts +87 -8
- package/vendor/minicore/VENDOR.md +9 -5
- package/vendor/minicore/src/core/loop.ts +47 -12
- package/vendor/minicore/src/core/provider.ts +4 -0
- package/vendor/minicore/src/core/session.ts +14 -1
- package/CHANGELOG.md +0 -1185
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +0 -489
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +0 -391
- package/docs/PLAN_UIUX_V6.md +0 -209
- package/docs/changelog.md +0 -3
package/src/lib/safe-open.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { constants } from "node:fs"
|
|
2
2
|
import type { FileHandle } from "node:fs/promises"
|
|
3
|
-
import { open, realpath } from "node:fs/promises"
|
|
3
|
+
import { open, realpath, stat as statPath } from "node:fs/promises"
|
|
4
4
|
import { homedir, tmpdir } from "node:os"
|
|
5
5
|
import { basename, dirname, isAbsolute, join, parse, relative, resolve } from "node:path"
|
|
6
6
|
import { isPathOutsideRoot, isSensitive } from "../policy/jail.ts"
|
|
7
7
|
|
|
8
|
-
// O_NOFOLLOW mencegah open mengikuti symlink
|
|
9
|
-
//
|
|
10
|
-
|
|
8
|
+
// O_NOFOLLOW mencegah open mengikuti symlink pada POSIX. Windows tidak
|
|
9
|
+
// menyediakan flag Node yang setara; semua platform juga membandingkan dev+ino
|
|
10
|
+
// stat sebelum open dengan identitas handle setelah open.
|
|
11
|
+
const O_NOFOLLOW: number = (constants as unknown as { O_NOFOLLOW?: number }).O_NOFOLLOW ?? 0
|
|
11
12
|
const O_RDONLY = constants.O_RDONLY
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -39,17 +40,19 @@ export async function resolveSafePath(
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
/**
|
|
42
|
-
* Buka file dengan O_NOFOLLOW pada
|
|
43
|
+
* Buka file dengan O_NOFOLLOW pada POSIX dan pemeriksaan identitas handle pada
|
|
44
|
+
* semua platform.
|
|
43
45
|
*
|
|
44
|
-
* Urutan: realpath
|
|
46
|
+
* Urutan: realpath + boundary check → stat identitas → realpath ulang +
|
|
47
|
+
* boundary check → open target canonical → fstat dan cocokkan dev+ino.
|
|
45
48
|
* Membuka hasil resolusi (bukan abs asli) berarti symlink internal tetap bisa
|
|
46
|
-
* dibaca
|
|
47
|
-
*
|
|
49
|
+
* dibaca. O_NOFOLLOW menutup swap leaf pada POSIX; pencocokan identitas handle
|
|
50
|
+
* menolak target berbeda yang terbuka setelah komponen path berubah, termasuk
|
|
51
|
+
* pada Windows yang tidak memiliki O_NOFOLLOW di API Node.
|
|
48
52
|
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* POSIX — lihat test/tool-toctou.test.ts (skip bila symlink EPERM).
|
|
53
|
+
* File system yang tidak menyediakan inode non-zero ditolak karena identitas
|
|
54
|
+
* handle tidak dapat dibuktikan. Stress test symlink tetap bergantung pada
|
|
55
|
+
* privilege platform; lihat test/tool-toctou.test.ts.
|
|
53
56
|
*/
|
|
54
57
|
export async function safeOpenRead(
|
|
55
58
|
abs: string,
|
|
@@ -64,31 +67,50 @@ export async function safeOpenRead(
|
|
|
64
67
|
// di sini agar semua pembaca via open (read_file/read_image/stat) mewarisinya.
|
|
65
68
|
if (isSensitive(preReal)) throw new Error(`blocked sensitive file: ${abs}`)
|
|
66
69
|
|
|
70
|
+
// Ambil identitas sebelum open, lalu periksa kembali boundary setelah stat.
|
|
71
|
+
// Ini menutup race di mana parent directory berubah menjadi junction/symlink
|
|
72
|
+
// keluar sesudah realpath pertama tetapi sebelum open.
|
|
73
|
+
const expected = await statPath(preReal, { bigint: true })
|
|
74
|
+
const checkedReal = await realpath(preReal)
|
|
75
|
+
if (isPathOutsideRoot(checkedReal, realRoot))
|
|
76
|
+
throw new Error(`symlink points outside workspace: ${abs}`)
|
|
77
|
+
if (isSensitive(checkedReal)) throw new Error(`blocked sensitive file: ${abs}`)
|
|
78
|
+
if (expected.ino === 0n)
|
|
79
|
+
throw new Error(`unable to verify opened file identity on this filesystem: ${abs}`)
|
|
80
|
+
|
|
67
81
|
// Buka path terverifikasi — bukan abs (yang bisa di-swap setelah realpath).
|
|
68
82
|
let handle: FileHandle
|
|
69
83
|
try {
|
|
70
|
-
handle = await open(
|
|
84
|
+
handle = await open(checkedReal, O_RDONLY | O_NOFOLLOW)
|
|
71
85
|
} catch (e) {
|
|
72
86
|
const code = (e as NodeJS.ErrnoException).code
|
|
73
87
|
if (code === "ELOOP") throw new Error(`symlink swapped during open (O_NOFOLLOW): ${abs}`)
|
|
74
|
-
// EINVAL: O_NOFOLLOW tidak didukung → buka biasa
|
|
88
|
+
// EINVAL: O_NOFOLLOW tidak didukung → buka biasa; fstat di bawah tetap
|
|
89
|
+
// memastikan file yang dibuka sama dengan identitas yang sudah diperiksa.
|
|
75
90
|
if (code === "EINVAL" && O_NOFOLLOW !== 0) {
|
|
76
|
-
handle = await open(
|
|
91
|
+
handle = await open(checkedReal, O_RDONLY)
|
|
77
92
|
} else throw e
|
|
78
93
|
}
|
|
94
|
+
const opened = await handle.stat({ bigint: true }).catch(async (error: unknown) => {
|
|
95
|
+
await handle.close().catch(() => {})
|
|
96
|
+
throw error
|
|
97
|
+
})
|
|
98
|
+
if (opened.ino === 0n || opened.dev !== expected.dev || opened.ino !== expected.ino) {
|
|
99
|
+
await handle.close().catch(() => {})
|
|
100
|
+
throw new Error(`file changed during safe open; refusing to read unverified target: ${abs}`)
|
|
101
|
+
}
|
|
79
102
|
// Hardlink ke luar workspace tak terlihat oleh realpath (semua nama setara)
|
|
80
103
|
// — baca via hardlink = baca konten luar. fstat pada handle yang SUDAH
|
|
81
104
|
// terbuka: tepat untuk file ini, bebas race dengan swap nama. File normal
|
|
82
105
|
// nlink=1; tolak sisanya dengan pesan yang bisa ditindaklanjuti.
|
|
83
|
-
const st = await handle.stat().catch(() => null)
|
|
84
106
|
// Hardlink check hanya untuk file regular: direktori punya nlink >= 2
|
|
85
107
|
// (`.` dan `..`) dan bukan target hardlink — biarkan tool yang menolak
|
|
86
108
|
// direktori dengan pesan yang user-friendly.
|
|
87
|
-
if (
|
|
109
|
+
if (!opened.isDirectory() && opened.nlink > 1n) {
|
|
88
110
|
await handle.close().catch(() => {})
|
|
89
|
-
throw new Error(`refusing to read file with multiple hardlinks (nlink=${
|
|
111
|
+
throw new Error(`refusing to read file with multiple hardlinks (nlink=${opened.nlink}): ${abs}`)
|
|
90
112
|
}
|
|
91
|
-
return { handle, realPath:
|
|
113
|
+
return { handle, realPath: checkedReal }
|
|
92
114
|
}
|
|
93
115
|
|
|
94
116
|
export async function safeReadFile(abs: string, root: string): Promise<string> {
|
|
@@ -100,6 +122,38 @@ export async function safeReadFile(abs: string, root: string): Promise<string> {
|
|
|
100
122
|
}
|
|
101
123
|
}
|
|
102
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Baca file tepercaya dengan batas byte sebelum alokasi tumbuh mengikuti file.
|
|
127
|
+
* Satu byte tambahan membedakan file tepat pada batas dari file yang dipotong.
|
|
128
|
+
*/
|
|
129
|
+
export async function safeReadFileCapped(
|
|
130
|
+
abs: string,
|
|
131
|
+
root: string,
|
|
132
|
+
maxBytes: number,
|
|
133
|
+
): Promise<{ text: string; truncated: boolean }> {
|
|
134
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1)
|
|
135
|
+
throw new RangeError("maxBytes must be a positive safe integer")
|
|
136
|
+
const { handle } = await safeOpenRead(abs, root)
|
|
137
|
+
try {
|
|
138
|
+
const st = await handle.stat()
|
|
139
|
+
if (!st.isFile()) throw new Error(`refusing to read non-regular file: ${abs}`)
|
|
140
|
+
const buffer = Buffer.alloc(maxBytes + 1)
|
|
141
|
+
let bytesRead = 0
|
|
142
|
+
while (bytesRead < buffer.length) {
|
|
143
|
+
const result = await handle.read(buffer, bytesRead, buffer.length - bytesRead, bytesRead)
|
|
144
|
+
if (result.bytesRead === 0) break
|
|
145
|
+
bytesRead += result.bytesRead
|
|
146
|
+
}
|
|
147
|
+
const truncated = bytesRead > maxBytes || st.size > maxBytes
|
|
148
|
+
return {
|
|
149
|
+
text: buffer.subarray(0, Math.min(bytesRead, maxBytes)).toString("utf8"),
|
|
150
|
+
truncated,
|
|
151
|
+
}
|
|
152
|
+
} finally {
|
|
153
|
+
await handle.close().catch(() => {})
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
103
157
|
export async function safeStat(abs: string, root: string) {
|
|
104
158
|
const { handle } = await safeOpenRead(abs, root)
|
|
105
159
|
try {
|
package/src/lib/trusted-exec.ts
CHANGED
|
@@ -15,7 +15,9 @@ import { delimiter, isAbsolute, join } from "node:path"
|
|
|
15
15
|
// nama telanjang (perilaku lama) supaya kegagalan tetap fail-open seperti
|
|
16
16
|
// semula, tetapi kasus umum (git terpasang di PATH) jadi aman dari hijack.
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
function isWindows(): boolean {
|
|
19
|
+
return process.platform === "win32"
|
|
20
|
+
}
|
|
19
21
|
|
|
20
22
|
function validPathEntries(): string[] {
|
|
21
23
|
const raw = process.env.PATH ?? process.env.Path ?? ""
|
|
@@ -32,7 +34,7 @@ function isExecutableFile(p: string): boolean {
|
|
|
32
34
|
try {
|
|
33
35
|
const st = statSync(p)
|
|
34
36
|
if (!st.isFile()) return false
|
|
35
|
-
if (
|
|
37
|
+
if (isWindows()) return true
|
|
36
38
|
accessSync(p, constants.X_OK)
|
|
37
39
|
return true
|
|
38
40
|
} catch {
|
|
@@ -60,14 +62,15 @@ function resolveUncached(name: string): string {
|
|
|
60
62
|
// "tools\\run.exe") adalah pilihan eksplisit pemanggil — jangan diubah.
|
|
61
63
|
if (isAbsolute(name)) return name
|
|
62
64
|
if (name.includes("/") || name.includes("\\")) return name
|
|
63
|
-
const
|
|
65
|
+
const windows = isWindows()
|
|
66
|
+
const exts = windows
|
|
64
67
|
? (process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD")
|
|
65
68
|
.split(";")
|
|
66
69
|
.map((e) => e.trim())
|
|
67
70
|
.filter(Boolean)
|
|
68
71
|
: [""]
|
|
69
72
|
for (const dir of validPathEntries()) {
|
|
70
|
-
if (
|
|
73
|
+
if (windows) {
|
|
71
74
|
for (const ext of exts) {
|
|
72
75
|
const candidate = join(dir, name.endsWith(ext.toLowerCase()) ? name : name + ext)
|
|
73
76
|
if (isExecutableFile(candidate)) return candidate
|
package/src/mcp/client.ts
CHANGED
|
@@ -7,6 +7,22 @@ import { scrubSecrets } from "../policy/scrub.ts"
|
|
|
7
7
|
import { McpHttpTransport, type McpTransportLike } from "./http-transport.ts"
|
|
8
8
|
import { McpTransport } from "./transport.ts"
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Transport interruption after a tools/call request can race with remote
|
|
12
|
+
* execution. Callers must not report this as a clean failure or retry blindly.
|
|
13
|
+
*/
|
|
14
|
+
export class McpCallOutcomeUnknownError extends Error {
|
|
15
|
+
readonly code = "MCP_CALL_OUTCOME_UNKNOWN"
|
|
16
|
+
|
|
17
|
+
constructor(toolName: string, cause: unknown) {
|
|
18
|
+
const detail = scrubSecrets(cause instanceof Error ? cause.message : String(cause))
|
|
19
|
+
super(
|
|
20
|
+
`MCP tool ${toolName} outcome is unknown: ${detail}. The server may have accepted or completed the operation; inspect remote state before retrying.`,
|
|
21
|
+
)
|
|
22
|
+
this.name = "McpCallOutcomeUnknownError"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
10
26
|
// [Fase 8 / 8.2] MCP Schema Fingerprint Pinning.
|
|
11
27
|
//
|
|
12
28
|
// Bahaya yang ditutup: server MCP bisa mengubah skema toolnya SETELAH pin
|
|
@@ -515,15 +531,24 @@ class McpConnection {
|
|
|
515
531
|
args: Record<string, unknown>,
|
|
516
532
|
signal?: AbortSignal,
|
|
517
533
|
): Promise<unknown> {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
534
|
+
// Abort before entering transport is known not to have sent a request.
|
|
535
|
+
signal?.throwIfAborted()
|
|
536
|
+
let result: Record<string, unknown>
|
|
537
|
+
try {
|
|
538
|
+
result = (await this.transport.request(
|
|
539
|
+
"tools/call",
|
|
540
|
+
{
|
|
541
|
+
name,
|
|
542
|
+
arguments: args,
|
|
543
|
+
},
|
|
544
|
+
undefined,
|
|
545
|
+
signal,
|
|
546
|
+
)) as Record<string, unknown>
|
|
547
|
+
} catch (error) {
|
|
548
|
+
// Once the transport call begins, timeout/abort/connection failure does
|
|
549
|
+
// not prove the remote server stopped or rolled back the operation.
|
|
550
|
+
throw new McpCallOutcomeUnknownError(name, error)
|
|
551
|
+
}
|
|
527
552
|
if (result && typeof result.isError === "boolean" && result.isError) {
|
|
528
553
|
const content = Array.isArray(result.content)
|
|
529
554
|
? result.content.map((c: any) => c.text ?? JSON.stringify(c)).join("\n")
|
package/src/mcp/server.ts
CHANGED
|
@@ -240,9 +240,8 @@ async function invokeTool(
|
|
|
240
240
|
* the whole process. As a durable task namespace that is unsafe: it is a
|
|
241
241
|
* constant, so every MCP context that has ever run against the same `root`
|
|
242
242
|
* shares one identity space, and it can also collide with anything else that
|
|
243
|
-
* ever uses that string. `
|
|
244
|
-
*
|
|
245
|
-
* overwrite each other's files last-writer-wins.
|
|
243
|
+
* ever uses that string. Because `mcp serve` is single-tenant, concurrent
|
|
244
|
+
* requests must not share this durable namespace and overwrite files last-writer-wins.
|
|
246
245
|
*
|
|
247
246
|
* WHAT THIS IDENTIFIES. One server instance = one logical MCP context. That
|
|
248
247
|
* matches this implementation's actual lifecycle, which was verified rather
|
package/src/mcp/transport.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { type ChildProcess, spawn } from "node:child_process"
|
|
2
|
-
import { createInterface } from "node:readline"
|
|
3
2
|
import { LIMITS } from "../constants.ts"
|
|
4
3
|
import { sanitizeSpawnEnv } from "../policy/scrub.ts"
|
|
5
4
|
|
|
6
5
|
export class McpTransport {
|
|
7
6
|
private proc: ChildProcess | null = null
|
|
8
|
-
private
|
|
7
|
+
private stdoutFrame = Buffer.alloc(0)
|
|
9
8
|
private pending = new Map<
|
|
10
9
|
string | number,
|
|
11
10
|
{
|
|
@@ -37,35 +36,22 @@ export class McpTransport {
|
|
|
37
36
|
})
|
|
38
37
|
|
|
39
38
|
this.proc.on("error", (err) => this.failAll(err))
|
|
39
|
+
this.proc.stdin?.on("error", (err) => {
|
|
40
|
+
// Server dapat keluar tepat sebelum request ditulis. EPIPE pada stdin
|
|
41
|
+
// tetap harus menjadi penolakan request, bukan uncaught exception proses.
|
|
42
|
+
this.closed = true
|
|
43
|
+
this.writeQueue = []
|
|
44
|
+
this.draining = false
|
|
45
|
+
this.failAll(err)
|
|
46
|
+
})
|
|
40
47
|
this.proc.on("exit", (code) => {
|
|
41
48
|
if (!this.closed) this.failAll(new Error(`MCP server exited with code ${code}`))
|
|
42
49
|
})
|
|
43
50
|
this.proc.stderr?.on("data", (d: Buffer) =>
|
|
44
51
|
process.stderr.write(`[mcp:${command}] ${d.toString().trim()}\n`),
|
|
45
52
|
)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
this.rl.close()
|
|
50
|
-
} catch {}
|
|
51
|
-
|
|
52
|
-
this.rl = createInterface({ input: this.proc.stdout! })
|
|
53
|
-
this.rl.on("line", (line) => {
|
|
54
|
-
if (!line.trim()) return
|
|
55
|
-
try {
|
|
56
|
-
const msg = JSON.parse(line)
|
|
57
|
-
if (msg.id != null && this.pending.has(msg.id)) {
|
|
58
|
-
const p = this.pending.get(msg.id)!
|
|
59
|
-
clearTimeout(p.timer)
|
|
60
|
-
p.unsubscribe?.()
|
|
61
|
-
this.pending.delete(msg.id)
|
|
62
|
-
if (msg.error) p.reject(new Error(msg.error.message || JSON.stringify(msg.error)))
|
|
63
|
-
else p.resolve(msg.result)
|
|
64
|
-
}
|
|
65
|
-
} catch (e) {
|
|
66
|
-
process.stderr.write(`[mcp] ignoring invalid JSON from server: ${(e as Error).message}\n`)
|
|
67
|
-
}
|
|
68
|
-
})
|
|
53
|
+
this.stdoutFrame = Buffer.alloc(0)
|
|
54
|
+
this.proc.stdout!.on("data", (chunk: Buffer) => this.consumeStdout(chunk))
|
|
69
55
|
}
|
|
70
56
|
|
|
71
57
|
async request(
|
|
@@ -107,36 +93,43 @@ export class McpTransport {
|
|
|
107
93
|
}
|
|
108
94
|
|
|
109
95
|
async close(): Promise<void> {
|
|
110
|
-
if (this.closed) return
|
|
96
|
+
if (this.closed && !this.proc) return
|
|
111
97
|
this.closed = true
|
|
112
98
|
this.writeQueue = []
|
|
113
99
|
this.draining = false
|
|
114
100
|
this.failAll(new Error("transport closed"))
|
|
115
|
-
if (this.rl) this.rl.close()
|
|
116
101
|
try {
|
|
117
102
|
this.proc?.stdin?.end()
|
|
118
103
|
} catch {}
|
|
119
104
|
this.killSignal.abort()
|
|
120
105
|
if (this.proc) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
106
|
+
const p = this.proc
|
|
107
|
+
if (p.exitCode === null && p.signalCode === null) {
|
|
108
|
+
await new Promise<void>((r) => {
|
|
109
|
+
const t = setTimeout(() => {
|
|
110
|
+
try {
|
|
111
|
+
p.kill("SIGKILL")
|
|
112
|
+
} catch {}
|
|
113
|
+
r()
|
|
114
|
+
}, 2_000)
|
|
115
|
+
p.once("exit", () => {
|
|
116
|
+
clearTimeout(t)
|
|
117
|
+
r()
|
|
118
|
+
})
|
|
132
119
|
})
|
|
133
|
-
}
|
|
120
|
+
}
|
|
134
121
|
}
|
|
135
122
|
this.proc = null
|
|
136
123
|
}
|
|
137
124
|
|
|
138
125
|
private write(msg: unknown) {
|
|
139
|
-
|
|
126
|
+
const proc = this.proc
|
|
127
|
+
if (!proc?.stdin) throw new Error("transport not connected")
|
|
128
|
+
if (this.closed || proc.exitCode !== null || proc.stdin.destroyed) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`MCP transport is closed${proc.exitCode === null ? "" : ` (exit ${proc.exitCode})`}`,
|
|
131
|
+
)
|
|
132
|
+
}
|
|
140
133
|
let line: string
|
|
141
134
|
try {
|
|
142
135
|
line = `${JSON.stringify(msg)}\n`
|
|
@@ -147,10 +140,10 @@ export class McpTransport {
|
|
|
147
140
|
this.writeQueue.push(line)
|
|
148
141
|
return
|
|
149
142
|
}
|
|
150
|
-
const ok =
|
|
143
|
+
const ok = proc.stdin.write(line)
|
|
151
144
|
if (!ok) {
|
|
152
145
|
this.draining = true
|
|
153
|
-
|
|
146
|
+
proc.stdin.once("drain", () => {
|
|
154
147
|
this.draining = false
|
|
155
148
|
this.flushQueue()
|
|
156
149
|
})
|
|
@@ -173,6 +166,67 @@ export class McpTransport {
|
|
|
173
166
|
}
|
|
174
167
|
}
|
|
175
168
|
|
|
169
|
+
/** Frame parsing stays incremental so a peer cannot make readline buffer an unlimited line. */
|
|
170
|
+
private consumeStdout(chunk: Buffer): void {
|
|
171
|
+
let start = 0
|
|
172
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
173
|
+
if (chunk[i] !== 0x0a) continue
|
|
174
|
+
const part = chunk.subarray(start, i)
|
|
175
|
+
if (this.stdoutFrame.length + part.length > LIMITS.MCP_STDIO_FRAME_MAX_BYTES) {
|
|
176
|
+
this.rejectOversizedFrame(this.stdoutFrame.length + part.length)
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
const frame = this.stdoutFrame.length === 0 ? part : Buffer.concat([this.stdoutFrame, part])
|
|
180
|
+
this.stdoutFrame = Buffer.alloc(0)
|
|
181
|
+
this.handleFrame(frame)
|
|
182
|
+
if (this.closed) return
|
|
183
|
+
start = i + 1
|
|
184
|
+
}
|
|
185
|
+
if (start < chunk.length) {
|
|
186
|
+
const remainder = chunk.subarray(start)
|
|
187
|
+
if (this.stdoutFrame.length + remainder.length > LIMITS.MCP_STDIO_FRAME_MAX_BYTES) {
|
|
188
|
+
this.rejectOversizedFrame(this.stdoutFrame.length + remainder.length)
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
this.stdoutFrame =
|
|
192
|
+
this.stdoutFrame.length === 0
|
|
193
|
+
? Buffer.from(remainder)
|
|
194
|
+
: Buffer.concat([this.stdoutFrame, remainder])
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private handleFrame(frame: Buffer): void {
|
|
199
|
+
if (frame.length === 0) return
|
|
200
|
+
try {
|
|
201
|
+
const msg = JSON.parse(frame.toString("utf8"))
|
|
202
|
+
if (msg.id != null && this.pending.has(msg.id)) {
|
|
203
|
+
const p = this.pending.get(msg.id)!
|
|
204
|
+
clearTimeout(p.timer)
|
|
205
|
+
p.unsubscribe?.()
|
|
206
|
+
this.pending.delete(msg.id)
|
|
207
|
+
if (msg.error) p.reject(new Error(msg.error.message || "MCP server returned an error"))
|
|
208
|
+
else p.resolve(msg.result)
|
|
209
|
+
}
|
|
210
|
+
} catch {
|
|
211
|
+
process.stderr.write("[mcp] ignoring invalid JSON from server\n")
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private rejectOversizedFrame(size: number): void {
|
|
216
|
+
const error = new Error(
|
|
217
|
+
`MCP stdio frame exceeds ${LIMITS.MCP_STDIO_FRAME_MAX_BYTES} bytes (at least ${size})`,
|
|
218
|
+
)
|
|
219
|
+
this.closed = true
|
|
220
|
+
this.stdoutFrame = Buffer.alloc(0)
|
|
221
|
+
this.writeQueue = []
|
|
222
|
+
this.draining = false
|
|
223
|
+
this.failAll(error)
|
|
224
|
+
this.killSignal.abort(error)
|
|
225
|
+
try {
|
|
226
|
+
this.proc?.kill("SIGKILL")
|
|
227
|
+
} catch {}
|
|
228
|
+
}
|
|
229
|
+
|
|
176
230
|
private failAll(err: Error) {
|
|
177
231
|
for (const p of this.pending.values()) {
|
|
178
232
|
clearTimeout(p.timer)
|
package/src/memory/files.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { homedir } from "node:os"
|
|
|
3
3
|
import { dirname, join, resolve } from "node:path"
|
|
4
4
|
import { LIMITS } from "../constants.ts"
|
|
5
5
|
import { homeDir } from "../lib/db-path.ts"
|
|
6
|
+
import { safeReadFileCapped } from "../lib/safe-open.ts"
|
|
6
7
|
import { isRealPathOutsideRoot } from "../policy/jail.ts"
|
|
7
8
|
import { scrubSecrets } from "../policy/scrub.ts"
|
|
8
9
|
|
|
@@ -36,8 +37,15 @@ export async function loadMemoryFiles(cwd = process.cwd()): Promise<string> {
|
|
|
36
37
|
// symlink keluar workspace. globalMem (home) sengaja dikecualikan —
|
|
37
38
|
// di luar cwd by design. Samakan rules/ di bawah ke realpath.
|
|
38
39
|
if (p !== globalMem && isRealPathOutsideRoot(p, cwd)) continue
|
|
39
|
-
const
|
|
40
|
-
|
|
40
|
+
const { text, truncated } = await safeReadFileCapped(
|
|
41
|
+
p,
|
|
42
|
+
p === globalMem ? dirname(p) : cwd,
|
|
43
|
+
6000,
|
|
44
|
+
)
|
|
45
|
+
if (text.trim())
|
|
46
|
+
parts.push(
|
|
47
|
+
`# ${tildePath(p)}\n${scrubSecrets(text)}${truncated ? "\n… [truncated: showing first 6000 bytes]" : ""}`,
|
|
48
|
+
)
|
|
41
49
|
} catch {}
|
|
42
50
|
}
|
|
43
51
|
// Load .minicode/rules/*.md (Kiro steering style)
|
|
@@ -57,8 +65,11 @@ export async function loadMemoryFiles(cwd = process.cwd()): Promise<string> {
|
|
|
57
65
|
try {
|
|
58
66
|
const full = join(rulesDir, f as string)
|
|
59
67
|
if (isRealPathOutsideRoot(full, cwd)) continue
|
|
60
|
-
const
|
|
61
|
-
if (
|
|
68
|
+
const { text, truncated } = await safeReadFileCapped(full, cwd, 3000)
|
|
69
|
+
if (text.trim())
|
|
70
|
+
parts.push(
|
|
71
|
+
`# rules/${f}\n${scrubSecrets(text)}${truncated ? "\n… [truncated: showing first 3000 bytes]" : ""}`,
|
|
72
|
+
)
|
|
62
73
|
} catch {}
|
|
63
74
|
}
|
|
64
75
|
} catch {}
|
package/src/memory/vector.ts
CHANGED
|
@@ -1278,7 +1278,11 @@ export async function searchHybrid(
|
|
|
1278
1278
|
if (rows.length === 0) return []
|
|
1279
1279
|
|
|
1280
1280
|
let queryVec: number[] | null = null
|
|
1281
|
-
|
|
1281
|
+
// F-07: bila semua kandidat hanya memiliki teks (mis. memori manual atau
|
|
1282
|
+
// fallback provider tanpa embedding), vektor query tidak dapat mengubah
|
|
1283
|
+
// skor satu pun. Jangan melakukan DNS/HTTP embedding yang hasilnya tak dipakai.
|
|
1284
|
+
const hasVectorCandidates = rows.some((row) => row.embedding !== null)
|
|
1285
|
+
if (hasVectorCandidates && opts.baseUrl && opts.apiKey) {
|
|
1282
1286
|
const vecs = await embedTexts(opts.baseUrl, opts.apiKey, [query], opts.embeddingModel)
|
|
1283
1287
|
if (vecs?.[0]) queryVec = vecs[0]
|
|
1284
1288
|
}
|