minicode-ai 0.13.1 → 0.14.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/CHANGELOG.md +17 -0
- package/LICENSE +21 -21
- package/README.md +4 -0
- package/cli/args.ts +50 -2
- package/cli/commands/acp.ts +18 -3
- package/cli/commands/daemon.ts +114 -19
- package/cli/commands/exec.ts +95 -7
- package/cli/commands/mcp.ts +66 -5
- package/cli/commands/memory.ts +46 -14
- package/cli/commands.ts +5 -0
- package/cli/index.ts +30 -5
- package/cli/setup.ts +365 -24
- package/cli/tui.ts +72 -27
- package/docs/TERMINAL_CONTRACT.md +91 -24
- package/docs/agents.md +2 -2
- package/docs/cli.md +1 -0
- package/docs/environment.md +1 -0
- package/docs/memory-sessions.md +5 -0
- package/package.json +2 -1
- package/src/app/session.ts +153 -35
- package/src/app/tool-layer.ts +67 -61
- package/src/daemon/approvals.ts +25 -1
- package/src/daemon/client.ts +51 -1
- package/src/daemon/control.ts +8 -0
- package/src/daemon/protocol.ts +47 -1
- package/src/daemon/server.ts +45 -9
- package/src/daemon/subscription.ts +25 -1
- package/src/daemon/telemetry.ts +4 -0
- package/src/hooks/run.ts +5 -1
- package/src/mcp/client.ts +349 -90
- package/src/mcp/http-transport.ts +7 -0
- package/src/mcp/transport.ts +5 -0
- package/src/memory/files.ts +69 -3
- package/src/memory/vector.ts +556 -160
- package/src/policy/approval-binding.ts +199 -0
- package/src/policy/context.ts +20 -1
- package/src/policy/executor.ts +44 -5
- package/src/policy/permission.ts +243 -26
- package/src/policy/tool-contract.ts +77 -0
- package/src/policy/usage.ts +3 -1
- package/src/policy/work-mode.ts +49 -1
- package/src/presentation/adapter.ts +157 -7
- package/src/presentation/events.ts +173 -1
- package/src/presentation/model.ts +37 -0
- package/src/presentation/projection.ts +107 -18
- package/src/presentation/reducer.ts +134 -10
- package/src/sandbox/os.ts +18 -1
- package/src/session/persistence.ts +153 -11
- package/src/task/store.ts +43 -3
- package/src/tools/memory.ts +70 -10
- package/src/tools/task.ts +7 -1
- package/src/tools/todo.ts +14 -4
- package/src/ui/approval/prompt.ts +52 -7
- package/src/ui/assistant/simple.ts +160 -57
- package/src/ui/contract.ts +46 -0
- package/src/ui/footer.ts +14 -5
- package/src/ui/i18n/en.ts +9 -0
- package/src/ui/i18n/id.ts +9 -0
- package/src/ui/render/collapse.ts +24 -52
- package/src/ui/render/detail.ts +14 -9
- package/src/ui/render/errors.ts +8 -0
- package/src/ui/render/reasoning.ts +13 -8
- package/src/ui/session-state.ts +243 -0
- package/src/ui/tui/app.ts +7 -3
- package/src/ui/tui/transcript.ts +271 -30
- package/vendor/minicore/src/core/budget.ts +25 -25
- package/vendor/minicore/src/core/compact.ts +75 -75
- package/vendor/minicore/src/core/errors.ts +59 -59
- package/vendor/minicore/src/core/events.ts +73 -73
- package/vendor/minicore/src/core/executor.ts +139 -139
- package/vendor/minicore/src/core/history.ts +31 -31
- package/vendor/minicore/src/core/index.ts +41 -41
- package/vendor/minicore/src/core/loop.ts +355 -355
- package/vendor/minicore/src/core/permission.ts +22 -22
- package/vendor/minicore/src/core/provider.ts +39 -39
- package/vendor/minicore/src/core/recovery.ts +35 -35
- package/vendor/minicore/src/core/session.ts +327 -327
- package/vendor/minicore/src/core/snapshot.ts +146 -146
- package/vendor/minicore/src/core/tokens.ts +107 -107
- package/vendor/minicore/src/core/tool.ts +115 -115
- package/vendor/minicore/src/core/types.ts +50 -50
- package/vendor/minicore/src/providers/openai-compat.ts +417 -417
package/src/app/session.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
+
import { resolve } from "node:path"
|
|
1
2
|
import type { RecoveryAction } from "#minicore/core/errors.ts"
|
|
2
3
|
import { ProviderError } from "#minicore/core/errors.ts"
|
|
3
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
ModelProvider,
|
|
6
|
+
PermissionHandler,
|
|
7
|
+
Session,
|
|
8
|
+
SessionConfig,
|
|
9
|
+
} from "#minicore/core/index.ts"
|
|
4
10
|
import { createSession as createCoreSession } from "#minicore/core/index.ts"
|
|
5
11
|
import { defaultRecoveryPolicy } from "#minicore/core/recovery.ts"
|
|
6
12
|
import { estimateMessages, estimateSystem, estimateTools } from "#minicore/core/tokens.ts"
|
|
7
13
|
import { LIMITS } from "../constants.ts"
|
|
14
|
+
// [Area B · PROMPT 06] Binding persetujuan ↔ operasi yang dieksekusi.
|
|
15
|
+
import { ApprovalBindingStore, verifyApprovalBinding } from "../policy/approval-binding.ts"
|
|
8
16
|
import {
|
|
9
17
|
buildSystemPrompt,
|
|
10
18
|
createCalibratedEstimator,
|
|
@@ -12,6 +20,7 @@ import {
|
|
|
12
20
|
} from "../policy/context.ts"
|
|
13
21
|
import { parallelExecutor } from "../policy/executor.ts"
|
|
14
22
|
import {
|
|
23
|
+
canonicalArgsHash,
|
|
15
24
|
createNoProgressDetector,
|
|
16
25
|
type NoProgressDetector,
|
|
17
26
|
type NoProgressOptions,
|
|
@@ -71,6 +80,38 @@ export type WorkModeControl = {
|
|
|
71
80
|
getWorkMode(): WorkMode
|
|
72
81
|
}
|
|
73
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Guidance system-prompt ber-keyed WorkMode (lapis intent), BUKAN PermissionMode.
|
|
85
|
+
*
|
|
86
|
+
* DISCUSS = investigasi baca-saja yang berorientasi diskusi: model membaca,
|
|
87
|
+
* mencari, dan bernalar, lalu MENYAJIKAN temuan/opsi/trade-off untuk didiskusikan
|
|
88
|
+
* dengan user — bukan menulis atau mengeksekusi apa pun. PLAN = investigasi/
|
|
89
|
+
* planning formal dengan prompt SENDIRI (independen dari `planHint` milik
|
|
90
|
+
* PermissionMode di bawah; lapisan intent ≠ lapisan permission). EXECUTE = tidak
|
|
91
|
+
* menambah apa pun (identitas).
|
|
92
|
+
*
|
|
93
|
+
* Ini *guidance* (behavior), bukan enforcement — enforcement tetap milik ceiling
|
|
94
|
+
* handler `work-mode.ts`. Diekspor agar teruji langsung (pola repo pure/export).
|
|
95
|
+
*/
|
|
96
|
+
export function workModeHint(workMode: WorkMode): string {
|
|
97
|
+
if (workMode === "DISCUSS") {
|
|
98
|
+
return (
|
|
99
|
+
"\n\nDISCUSS MODE: You are in read-only investigation and discussion mode. " +
|
|
100
|
+
"Do NOT modify files, run bash, or use write/edit tools. " +
|
|
101
|
+
"Read, search, and reason — investigate the question, then discuss findings, options, and trade-offs with the user. " +
|
|
102
|
+
"Do not implement or execute any change."
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
if (workMode === "PLAN") {
|
|
106
|
+
return (
|
|
107
|
+
"\n\nPLAN MODE: You are in planning mode. " +
|
|
108
|
+
"Do NOT modify files, run bash, or use write/edit tools. " +
|
|
109
|
+
"Only read, search, and reason — investigate thoroughly, then produce a concrete implementation plan."
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
return ""
|
|
113
|
+
}
|
|
114
|
+
|
|
74
115
|
export async function createMinicodeSession(
|
|
75
116
|
opts: Partial<
|
|
76
117
|
Omit<SessionConfig, "permissions" | "estimator" | "recovery" | "system" | "executor">
|
|
@@ -104,6 +145,15 @@ export async function createMinicodeSession(
|
|
|
104
145
|
noProgress?: NoProgressOptions | false
|
|
105
146
|
/** Hook opsional agar composition root dapat mereset riwayat osilasi saat verify hijau. */
|
|
106
147
|
onNoProgress?: (detector: NoProgressDetector) => void
|
|
148
|
+
/**
|
|
149
|
+
* [Area B · PROMPT 06] Penyimpanan binding persetujuan milik SESI.
|
|
150
|
+
*
|
|
151
|
+
* Default: dibuat baru per sesi (lihat implementasi). Composition root
|
|
152
|
+
* boleh mengoper instance sendiri bila perlu membagikannya dengan lapisan
|
|
153
|
+
* lain; bila tidak, tiap sesi punya store sendiri sehingga bukti
|
|
154
|
+
* persetujuan tak pernah lintas sesi.
|
|
155
|
+
*/
|
|
156
|
+
approvalBindings?: ApprovalBindingStore
|
|
107
157
|
/** Menerima handle kontrol mode permission. Kernel tidak mengekspos
|
|
108
158
|
* `config`, jadi satu-satunya cara mengubah mode saat runtime (mis.
|
|
109
159
|
* Shift+Tab di TUI) adalah menangkap handler di sini. */
|
|
@@ -160,13 +210,15 @@ export async function createMinicodeSession(
|
|
|
160
210
|
`createMinicodeSession: workMode ${resolvedWorkMode} dengan permissionHandler terinjeksi belum didukung (fail-closed S2.1)`,
|
|
161
211
|
)
|
|
162
212
|
}
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
213
|
+
const PLAN_PM_HINT =
|
|
214
|
+
"\n\nPLAN MODE: You are in read-only planning mode. Do NOT modify files, run bash, or use write/edit tools. Only read, search, and reason — then output a concrete implementation plan."
|
|
215
|
+
// P0.2 (G-PROMPT-RT): rakit bagian dasar sekali, lalu tambahkan `planHint`
|
|
216
|
+
// (PermissionMode) + `workModeHint` (WorkMode) secara LIVE per-turn lewat
|
|
217
|
+
// pembungkus provider supaya transisi runtime `/work-mode` maupun `/mode`
|
|
218
|
+
// tercermin langsung di StreamRequest.system tanpa mengedit vendor/minicore.
|
|
219
|
+
const baseSystem = await buildSystemPrompt({
|
|
168
220
|
cwd: opts.cwd,
|
|
169
|
-
extra:
|
|
221
|
+
extra: opts.systemExtra ?? "",
|
|
170
222
|
signal: (opts as unknown as { signal?: AbortSignal }).signal,
|
|
171
223
|
})
|
|
172
224
|
const {
|
|
@@ -189,9 +241,16 @@ export async function createMinicodeSession(
|
|
|
189
241
|
maxTurns,
|
|
190
242
|
noProgress,
|
|
191
243
|
onNoProgress,
|
|
244
|
+
approvalBindings: _approvalBindings,
|
|
192
245
|
...rest
|
|
193
246
|
} = opts
|
|
194
247
|
if (!provider) throw new Error("createMinicodeSession: provider is required")
|
|
248
|
+
// [Area B · PROMPT 06] Penyimpanan binding persetujuan milik SESI ini.
|
|
249
|
+
// Dibuat di sini (bukan global) supaya dua sesi dalam satu proses tak
|
|
250
|
+
// saling memakai bukti persetujuan. Root = cwd sesi (sama dengan yang
|
|
251
|
+
// dipakai tool untuk resolve path target).
|
|
252
|
+
const approvalBindings =
|
|
253
|
+
_approvalBindings ?? new ApprovalBindingStore(resolve(cwd ?? process.cwd()))
|
|
195
254
|
// F-09: sanitasi batas di lapisan app (mencakup pemanggil library langsung,
|
|
196
255
|
// bukan hanya CLI yang sudah sanitasi di cli/setup.ts + cli/index.ts).
|
|
197
256
|
// Tanpa ini: timeoutMs NaN/negatif = abort instan; maxSteps 0/negatif =
|
|
@@ -237,6 +296,9 @@ export async function createMinicodeSession(
|
|
|
237
296
|
ask,
|
|
238
297
|
allowLocalConfig,
|
|
239
298
|
onApprovalEvent,
|
|
299
|
+
// [Area B · PROMPT 06] Store per-sesi: persetujuan dari sesi A tak
|
|
300
|
+
// bisa dipakai sebagai bukti di sesi B.
|
|
301
|
+
approvalBindings,
|
|
240
302
|
}),
|
|
241
303
|
root: cwd,
|
|
242
304
|
})
|
|
@@ -250,7 +312,7 @@ export async function createMinicodeSession(
|
|
|
250
312
|
__getWorkMode?(): WorkMode
|
|
251
313
|
}
|
|
252
314
|
const registeredTools = rest.tools ?? []
|
|
253
|
-
const
|
|
315
|
+
const isAllToolsPureRead = (): boolean =>
|
|
254
316
|
registeredTools.length > 0 &&
|
|
255
317
|
registeredTools.every((t) => contractForTool(t.name)?.sideEffectTier === "PURE_READ")
|
|
256
318
|
const livePosture = (): NoProgressPosture => {
|
|
@@ -261,7 +323,7 @@ export async function createMinicodeSession(
|
|
|
261
323
|
const wm = wmCtl.__getWorkMode?.() ?? resolvedWorkMode
|
|
262
324
|
if (wm === "DISCUSS") return "readonly"
|
|
263
325
|
if (wm === "PLAN") return "plan"
|
|
264
|
-
if (
|
|
326
|
+
if (isAllToolsPureRead()) return "explore"
|
|
265
327
|
return "active"
|
|
266
328
|
}
|
|
267
329
|
const effectiveRoot = cwd ?? process.cwd()
|
|
@@ -278,24 +340,64 @@ export async function createMinicodeSession(
|
|
|
278
340
|
// 1. `detector.assertBeforeCall` memblokir panggilan yang terdeteksi
|
|
279
341
|
// no-progress / osilasi SEBELUM intent jurnal ditulis & sebelum efek.
|
|
280
342
|
// 2. `detector.capturePreMutation` merekam hash pra-efek untuk osilasi.
|
|
281
|
-
// 3. `beforeToolEffect`
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
343
|
+
// 3. `beforeToolEffect` writes intent jurnal secara awaited.
|
|
344
|
+
//
|
|
345
|
+
// [Area B · PROMPT 06] Barrier ini juga BATAS EKSEKUSI verifikasi
|
|
346
|
+
// persetujuan: setelah awaital intent jurnal + no-progress di atas, yang
|
|
347
|
+
// tersisa ke `tool.execute` hanya kode sinkron kernel
|
|
348
|
+
// (`permissions.check` → `validateArgs` → emit → execute), jadi verifikasi
|
|
349
|
+
// di sini efektif sama dengan memeriksa tepat sebelum eksekusi. Membungkus
|
|
350
|
+
// tool TIDAK dipakai: itu mengganti objek di array tool LIVE yang dipegang
|
|
351
|
+
// kernel/`setWorkMode` (regresi `test/work-mode-runtime.test.ts`).
|
|
352
|
+
//
|
|
353
|
+
// Alasan deny "approval stale" dicatat terpisah karena handler base
|
|
354
|
+
// menyimpan alasannya di peta privat; kernel membacanya lewat
|
|
355
|
+
// `describeDenial` yang kita override.
|
|
356
|
+
const staleDenyReasons = new Map<string, string>()
|
|
357
|
+
// Barrier selalu dipasang (bukan hanya bila detector/journal ada): di sini
|
|
358
|
+
// verifikasi persetujuan terjadi, dan ia harus berlaku di SETIAP konfigurasi
|
|
359
|
+
// — termasuk sesi tanpa jurnal dan tanpa detektor.
|
|
360
|
+
const withEffectBarrier: PermissionHandler = {
|
|
361
|
+
...permissions,
|
|
362
|
+
check: async (call, deps) => {
|
|
363
|
+
const decision = await permissions.check(call, deps)
|
|
364
|
+
if (decision !== "allow") return decision
|
|
365
|
+
// Ambil (konsumsi) bukti persetujuan milik call ini — sekali saja.
|
|
366
|
+
// Diambil SEBELUM await supaya tak ada binding tersisa kalau barrier
|
|
367
|
+
// memblokir call (tak ada deny lalu tak ada jejak).
|
|
368
|
+
const binding = approvalBindings.take(call.name, canonicalArgsHash(call.args))
|
|
369
|
+
if (detector !== null) {
|
|
370
|
+
await detector.beforeToolEffectCheck(call, effectiveRoot)
|
|
371
|
+
}
|
|
372
|
+
if (beforeToolEffect !== undefined) {
|
|
373
|
+
await beforeToolEffect(call)
|
|
374
|
+
}
|
|
375
|
+
// Verifikasi lapis-2 (batas eksekusi): fail-closed saat target berubah
|
|
376
|
+
// sejak user menyetujui.
|
|
377
|
+
const verdict = verifyApprovalBinding(binding)
|
|
378
|
+
if (verdict.kind === "stale") {
|
|
379
|
+
const id = (call as { id?: string } | null)?.id
|
|
380
|
+
if (id) {
|
|
381
|
+
if (staleDenyReasons.size > 500)
|
|
382
|
+
staleDenyReasons.delete(staleDenyReasons.keys().next().value as string)
|
|
383
|
+
staleDenyReasons.set(id, `approval stale: ${verdict.reason}`)
|
|
384
|
+
}
|
|
385
|
+
return "deny"
|
|
386
|
+
}
|
|
387
|
+
return "allow"
|
|
388
|
+
},
|
|
389
|
+
describeDenial: (call) => {
|
|
390
|
+
const id = (call as { id?: string } | null)?.id
|
|
391
|
+
if (id) {
|
|
392
|
+
const reason = staleDenyReasons.get(id)
|
|
393
|
+
if (reason) {
|
|
394
|
+
staleDenyReasons.delete(id)
|
|
395
|
+
return reason
|
|
298
396
|
}
|
|
397
|
+
}
|
|
398
|
+
return permissions.describeDenial?.(call)
|
|
399
|
+
},
|
|
400
|
+
}
|
|
299
401
|
// [PHASE 6U] An INJECTED handler is not a mode, and has no mode control.
|
|
300
402
|
//
|
|
301
403
|
// [DESIGN DECISION] `onPermissions` and `livePermissionMode` are deliberately NOT
|
|
@@ -326,17 +428,33 @@ export async function createMinicodeSession(
|
|
|
326
428
|
// Teruskan mode live ke kernel agar ToolContext.permissionMode selalu
|
|
327
429
|
// mencerminkan Shift+Tab saat itu (bukan snapshot saat sesi dibuat).
|
|
328
430
|
const livePermissionMode = (): PermissionMode => (injected ? "readonly" : withMode.__getMode())
|
|
431
|
+
const computeLiveSystem = (): string => {
|
|
432
|
+
const pm = injected ? opts.permissionMode : withMode.__getMode()
|
|
433
|
+
const wm = injected ? resolvedWorkMode : (wmCtl.__getWorkMode?.() ?? resolvedWorkMode)
|
|
434
|
+
const planHint = pm === "plan" ? PLAN_PM_HINT : ""
|
|
435
|
+
return baseSystem + planHint + workModeHint(wm)
|
|
436
|
+
}
|
|
437
|
+
const dynamicProvider: ModelProvider = {
|
|
438
|
+
id: provider.id,
|
|
439
|
+
get models() {
|
|
440
|
+
return provider.models
|
|
441
|
+
},
|
|
442
|
+
stream(req, signal) {
|
|
443
|
+
return provider.stream({ ...req, system: computeLiveSystem() }, signal)
|
|
444
|
+
},
|
|
445
|
+
}
|
|
329
446
|
// [Fase 3 / CTX-03] Estimator terkalibrasi terhadap usage aktual provider.
|
|
330
447
|
const calibration = createCalibratedEstimator()
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
448
|
+
const liveToolSchemas = () =>
|
|
449
|
+
registeredTools.map((t) => ({
|
|
450
|
+
name: t.name,
|
|
451
|
+
description: t.description,
|
|
452
|
+
parameters: t.parameters,
|
|
453
|
+
}))
|
|
336
454
|
const core = createCoreSession({
|
|
337
455
|
...rest,
|
|
338
|
-
provider,
|
|
339
|
-
system,
|
|
456
|
+
provider: dynamicProvider,
|
|
457
|
+
system: computeLiveSystem(),
|
|
340
458
|
permissions: withEffectBarrier,
|
|
341
459
|
permissionMode: livePermissionMode,
|
|
342
460
|
estimator: calibration.estimator,
|
|
@@ -354,8 +472,8 @@ export async function createMinicodeSession(
|
|
|
354
472
|
const d = e.data as { inputTokens?: number; totalTokens?: number } | undefined
|
|
355
473
|
if (!d) return
|
|
356
474
|
const baseLocal =
|
|
357
|
-
estimateSystem(
|
|
358
|
-
estimateTools(
|
|
475
|
+
estimateSystem(computeLiveSystem(), minicodeEstimator) +
|
|
476
|
+
estimateTools(liveToolSchemas(), minicodeEstimator) +
|
|
359
477
|
estimateMessages(core.state.history, minicodeEstimator)
|
|
360
478
|
calibration.observeProviderUsage({
|
|
361
479
|
localEstimatedTokens: baseLocal,
|
package/src/app/tool-layer.ts
CHANGED
|
@@ -2,40 +2,60 @@ import type { Tool } from "#minicore"
|
|
|
2
2
|
import type { MinicodeConfig } from "../config.ts"
|
|
3
3
|
import { configureServers as lspConfigure } from "../lsp/client.ts"
|
|
4
4
|
import { connectAll as mcpConnectAll } from "../mcp/client.ts"
|
|
5
|
+
import {
|
|
6
|
+
EXPLORE_TOOL_NAMES,
|
|
7
|
+
LOCAL_READONLY_TOOL_NAMES,
|
|
8
|
+
READONLY_TOOL_NAMES,
|
|
9
|
+
} from "../policy/tool-contract.ts"
|
|
5
10
|
import { isMcpToolName } from "../presentation/label.ts"
|
|
6
11
|
import type { CanonicalScope } from "../session/verification.ts"
|
|
7
12
|
import { createBashJobTools } from "../tools/bash.ts"
|
|
8
13
|
import { withEvidence } from "../tools/evidence.ts"
|
|
9
14
|
import { allTools, withMcpTools } from "../tools/index.ts"
|
|
10
|
-
import { EXPLORE_TOOL_NAMES } from "../tools/task.ts"
|
|
11
15
|
|
|
12
16
|
const errMsg = (e: unknown) => (e instanceof Error ? e.message : String(e))
|
|
13
17
|
|
|
14
18
|
export type ToolScope = "full" | "explore"
|
|
15
19
|
|
|
16
20
|
const PLAN_EXTRA = new Set(["todo_write", "delegate_task", "submit_result"])
|
|
21
|
+
const LOCAL_READONLY_SET: ReadonlySet<string> = new Set(LOCAL_READONLY_TOOL_NAMES)
|
|
17
22
|
|
|
18
|
-
// WorkMode ceiling (P12 S2.2) — lapisan intent di ATAS permission mode.
|
|
19
|
-
// Invariant INV-WM1: WorkMode TIDAK PERNAH memperluas akses;
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
export function workModeVisible(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
// WorkMode ceiling (P12 S2.2 + P0.2) — lapisan intent di ATAS permission mode.
|
|
24
|
+
// Invariant INV-WM1 & #7: WorkMode TIDAK PERNAH memperluas akses; `visible`
|
|
25
|
+
// diselaraskan penuh dengan `callable` di handler:
|
|
26
|
+
// - DISCUSS: tepat 16 tool PURE_READ lokal (`LOCAL_READONLY_TOOL_NAMES`);
|
|
27
|
+
// `web_fetch` & `web_search` ditolak dan disembunyikan.
|
|
28
|
+
// - PLAN: 18 `READONLY_TOOL_NAMES` (16 lokal + 2 network read ber-approval) +
|
|
29
|
+
// `PLAN_EXTRA` (kecuali bila `permissionMode` adalah `readonly`/`allowlist`
|
|
30
|
+
// yang ceiling-nya `readonly`).
|
|
31
|
+
// - EXECUTE: tanpa ceiling WorkMode tambahan; mengikuti `permissionMode`.
|
|
32
|
+
export function workModeVisible(
|
|
33
|
+
name: string,
|
|
34
|
+
workMode: "DISCUSS" | "PLAN" | "EXECUTE",
|
|
35
|
+
permissionMode?: string,
|
|
36
|
+
): boolean {
|
|
37
|
+
if (isMcpToolName(name)) {
|
|
38
|
+
return workMode === "EXECUTE" && permissionMode !== "readonly" && permissionMode !== "plan"
|
|
39
|
+
}
|
|
40
|
+
if (workMode === "DISCUSS") return LOCAL_READONLY_SET.has(name)
|
|
41
|
+
if (workMode === "PLAN") {
|
|
42
|
+
if (permissionMode === "readonly" || permissionMode === "allowlist") {
|
|
43
|
+
return READONLY_TOOL_NAMES.has(name)
|
|
44
|
+
}
|
|
45
|
+
return READONLY_TOOL_NAMES.has(name) || PLAN_EXTRA.has(name)
|
|
46
|
+
}
|
|
47
|
+
// EXECUTE = tanpa ceiling WorkMode tambahan; hormati filter PermissionMode.
|
|
48
|
+
if (permissionMode === "readonly") return READONLY_TOOL_NAMES.has(name)
|
|
49
|
+
if (permissionMode === "plan") return READONLY_TOOL_NAMES.has(name) || PLAN_EXTRA.has(name)
|
|
31
50
|
return true
|
|
32
51
|
}
|
|
33
52
|
|
|
34
53
|
export function applyWorkModeFilter<T extends Tool>(
|
|
35
54
|
tools: readonly T[],
|
|
36
55
|
workMode: "DISCUSS" | "PLAN" | "EXECUTE",
|
|
56
|
+
permissionMode?: string,
|
|
37
57
|
): T[] {
|
|
38
|
-
return tools.filter((t) => workModeVisible(t.name, workMode))
|
|
58
|
+
return tools.filter((t) => workModeVisible(t.name, workMode, permissionMode))
|
|
39
59
|
}
|
|
40
60
|
|
|
41
61
|
export type ToolEvidenceMode = "events" | "canonical"
|
|
@@ -47,31 +67,22 @@ export async function setupToolLayer(
|
|
|
47
67
|
verification?: CanonicalScope,
|
|
48
68
|
jobOwnerSid?: string,
|
|
49
69
|
workMode: "DISCUSS" | "PLAN" | "EXECUTE" = "EXECUTE",
|
|
70
|
+
cwd?: string,
|
|
50
71
|
): Promise<{
|
|
51
72
|
sessionTools: Tool[]
|
|
52
73
|
evidenceMode: ToolEvidenceMode
|
|
53
74
|
/** P12 S2.2 Slice 5 - set WorkMode runtime (visibilitas tool). */
|
|
54
75
|
setWorkMode?: (m: "DISCUSS" | "PLAN" | "EXECUTE") => void
|
|
76
|
+
/** P0.2 - set PermissionMode runtime agar visibilitas tetap sinkron saat /mode atau Shift+Tab. */
|
|
77
|
+
setPermissionMode?: (pm: string) => void
|
|
55
78
|
}> {
|
|
56
79
|
let sessionTools: Tool[] = allTools
|
|
57
|
-
// Harness-P2: scope explore = subset read-only (
|
|
58
|
-
// MCP runtime ikut terpotong (nama bertitik tak ada di daftar) — least privilege.
|
|
80
|
+
// Harness-P2: scope explore = subset read-only sub-agen (tetap 12 EXPLORE_TOOL_NAMES).
|
|
59
81
|
if (scope === "explore")
|
|
60
82
|
sessionTools = allTools.filter((t) => EXPLORE_TOOL_NAMES.includes(t.name))
|
|
61
|
-
// Paket perbaikan (1): plan/readonly sembunyikan tool tulis dari daftar agar
|
|
62
|
-
// model tak buang langkah mencoba yang pasti ditolak. Daftar fixed per sesi
|
|
63
|
-
// (ikuti docs/HARNESS.md batasan sadar).
|
|
64
|
-
if (permissionMode === "readonly") {
|
|
65
|
-
sessionTools = sessionTools.filter((t) => EXPLORE_TOOL_NAMES.includes(t.name))
|
|
66
|
-
} else if (permissionMode === "plan") {
|
|
67
|
-
sessionTools = sessionTools.filter(
|
|
68
|
-
(t) => EXPLORE_TOOL_NAMES.includes(t.name) || PLAN_EXTRA.has(t.name),
|
|
69
|
-
)
|
|
70
|
-
}
|
|
71
83
|
// F-03 (P5 §C2): background bash job diikat ke sid kanonik sesi ini.
|
|
72
|
-
// Rebind
|
|
73
|
-
//
|
|
74
|
-
// daftar — filter explore/plan di atas tetap berlaku (least privilege).
|
|
84
|
+
// Rebind dilakukan pada kandidat penuh sebelum filter mode supaya transisi
|
|
85
|
+
// runtime DISCUSS/PLAN/readonly → EXECUTE tetap mendapat bash terikat sid.
|
|
75
86
|
if (jobOwnerSid) {
|
|
76
87
|
const owned = createBashJobTools(jobOwnerSid)
|
|
77
88
|
const rebind = new Map<string, Tool>([
|
|
@@ -82,10 +93,10 @@ export async function setupToolLayer(
|
|
|
82
93
|
sessionTools = sessionTools.map((t) => rebind.get(t.name) ?? t)
|
|
83
94
|
}
|
|
84
95
|
try {
|
|
85
|
-
if (cfg.mcpServers?.length) {
|
|
86
|
-
|
|
96
|
+
if (cfg.mcpServers?.length && scope !== "explore") {
|
|
97
|
+
// P2.3: bawa cwd agar pin skema tersimpan per-workspace.
|
|
98
|
+
const mcpTools = await mcpConnectAll(cfg.mcpServers, cwd)
|
|
87
99
|
if (mcpTools.length) {
|
|
88
|
-
// Plan/readonly: filter MCP juga, bukan balik ke allTools (bug lama).
|
|
89
100
|
sessionTools = withMcpTools(sessionTools, mcpTools)
|
|
90
101
|
}
|
|
91
102
|
}
|
|
@@ -95,9 +106,6 @@ export async function setupToolLayer(
|
|
|
95
106
|
try {
|
|
96
107
|
if (cfg.lspServers?.length) lspConfigure(cfg.lspServers)
|
|
97
108
|
} catch (e) {
|
|
98
|
-
// Label "[lsp]" — BUKAN "[mcp]": mengarang label yang salah membuat
|
|
99
|
-
// operator mengira MCP yang rusak, padahal yang gagal adalah registrasi
|
|
100
|
-
// server LSP.
|
|
101
109
|
process.stderr.write(`[lsp] init failed: ${errMsg(e)}\n`)
|
|
102
110
|
}
|
|
103
111
|
if (verification) {
|
|
@@ -105,40 +113,38 @@ export async function setupToolLayer(
|
|
|
105
113
|
// registry invocation. Special paths keep their explicit instrumentation.
|
|
106
114
|
sessionTools = sessionTools.map((tool) => withEvidence(tool, verification))
|
|
107
115
|
}
|
|
108
|
-
// P12 S2.2
|
|
109
|
-
//
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// ia menebak namanya. Yang MENOLAK tetap permission ceiling handler
|
|
119
|
-
// (WorkMode non-EXECUTE) - visibilitas hanya lapisan UX/least-privilege,
|
|
120
|
-
// bukan gerbang keamanan.
|
|
121
|
-
const preWorkMode = sessionTools
|
|
122
|
-
const mcpBlockedByPermission = permissionMode === "plan" || permissionMode === "readonly"
|
|
123
|
-
const computeVisible = (wm: "DISCUSS" | "PLAN" | "EXECUTE"): Tool[] => {
|
|
124
|
-
let list = preWorkMode
|
|
125
|
-
// Tool MCP runtime ber-nama bertitik ditolak pada plan/readonly (kontrak
|
|
126
|
-
// lama) dan pada WorkMode non-EXECUTE (ceiling tak memuat tool MCP).
|
|
127
|
-
if (mcpBlockedByPermission || wm !== "EXECUTE") {
|
|
128
|
-
list = list.filter((t) => !isMcpToolName(t.name) || EXPLORE_TOOL_NAMES.includes(t.name))
|
|
116
|
+
// P12 S2.2 + P0.2 - visibilitas tool runtime (sinkron terhadap WorkMode DAN
|
|
117
|
+
// PermissionMode). `parallelExecutor` meresolusi `registry.get(name)` dari
|
|
118
|
+
// `list()` yang sama sehingga `callable === visible` di kedua arah transisi.
|
|
119
|
+
const candidates = sessionTools
|
|
120
|
+
let currentWm: "DISCUSS" | "PLAN" | "EXECUTE" = workMode
|
|
121
|
+
let currentPm: string | undefined = permissionMode
|
|
122
|
+
const computeVisible = (wm: "DISCUSS" | "PLAN" | "EXECUTE", pm?: string): Tool[] => {
|
|
123
|
+
if (scope === "explore") {
|
|
124
|
+
// Scope explore sudah dibatasi ke EXPLORE_TOOL_NAMES; tetap terapkan irisan WorkMode.
|
|
125
|
+
return applyWorkModeFilter(candidates, wm, pm)
|
|
129
126
|
}
|
|
130
|
-
return applyWorkModeFilter(
|
|
127
|
+
return applyWorkModeFilter(candidates, wm, pm)
|
|
131
128
|
}
|
|
132
129
|
// Array LIVE: identitas objek ini yang dipegang kernel.
|
|
133
|
-
const visible: Tool[] = computeVisible(
|
|
134
|
-
const
|
|
135
|
-
const next = computeVisible(
|
|
130
|
+
const visible: Tool[] = computeVisible(currentWm, currentPm)
|
|
131
|
+
const syncVisible = (): void => {
|
|
132
|
+
const next = computeVisible(currentWm, currentPm)
|
|
136
133
|
visible.length = 0
|
|
137
134
|
visible.push(...next)
|
|
138
135
|
}
|
|
136
|
+
const setWorkMode = (wm: "DISCUSS" | "PLAN" | "EXECUTE"): void => {
|
|
137
|
+
currentWm = wm
|
|
138
|
+
syncVisible()
|
|
139
|
+
}
|
|
140
|
+
const setPermissionMode = (pm: string): void => {
|
|
141
|
+
currentPm = pm
|
|
142
|
+
syncVisible()
|
|
143
|
+
}
|
|
139
144
|
return {
|
|
140
145
|
sessionTools: visible,
|
|
141
146
|
evidenceMode: verification ? "canonical" : "events",
|
|
142
147
|
setWorkMode,
|
|
148
|
+
setPermissionMode,
|
|
143
149
|
}
|
|
144
150
|
}
|
package/src/daemon/approvals.ts
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
import { randomUUID } from "node:crypto"
|
|
16
16
|
import type { PermissionAsk } from "../policy/permission.ts"
|
|
17
|
+
import { scrubSecrets } from "../policy/scrub.ts"
|
|
17
18
|
import { hashArgs } from "../session/journal.ts"
|
|
18
19
|
import type { ApprovalView, RefusalBody } from "./protocol.ts"
|
|
19
20
|
import { sanitizeLabel } from "./sanitize.ts"
|
|
@@ -338,7 +339,11 @@ export class ApprovalBroker {
|
|
|
338
339
|
sid,
|
|
339
340
|
runId: opts.runId ? opts.runId() : null,
|
|
340
341
|
tool,
|
|
341
|
-
|
|
342
|
+
// [Area B · PROMPT 06] Bukti yang sama dengan prompt TUI: preview
|
|
343
|
+
// per-tool yang SUDAH di-scrub secret oleh policy + tier risiko.
|
|
344
|
+
// Sebelumnya daemon melihat JSON args mentah (hanya sanitizeLabel) —
|
|
345
|
+
// konsumen daemon bisa melihat secret yang tak pernah tampil di TUI.
|
|
346
|
+
summary: approvalSummary(call?.riskTier, call?.diffPreview, call?.args),
|
|
342
347
|
// [Fase 2 / LOOP-INV-08] Ikat keputusan ke argumen kanonis + id call.
|
|
343
348
|
argsHash: hashArgs(call?.args ?? {}),
|
|
344
349
|
invocationId: typeof call?.id === "string" && call.id !== "" ? call.id : null,
|
|
@@ -348,6 +353,25 @@ export class ApprovalBroker {
|
|
|
348
353
|
}
|
|
349
354
|
}
|
|
350
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Ringkasan approval untuk konsumen daemon.
|
|
358
|
+
*
|
|
359
|
+
* Urutan prioritas: preview per-tool dari policy (sudah di-scrub) → fallback
|
|
360
|
+
* JSON args yang juga di-scrub. Tier risiko didahulukan sebagai tag agar
|
|
361
|
+
* konsumen tanpa renderer diff tetap tahu efeknya. Tak ada jalur yang
|
|
362
|
+
* mengirim args mentah ke luar proses.
|
|
363
|
+
*/
|
|
364
|
+
function approvalSummary(
|
|
365
|
+
riskTier: string | undefined,
|
|
366
|
+
diffPreview: string | undefined,
|
|
367
|
+
args: unknown,
|
|
368
|
+
): string {
|
|
369
|
+
const body = diffPreview?.trim() || scrubSecrets(summarizeArgs(args)).trim()
|
|
370
|
+
const tier = riskTier?.trim() ? riskTier.trim() : ""
|
|
371
|
+
if (!tier) return body
|
|
372
|
+
return body ? `[${tier}] ${body}` : `[${tier}]`
|
|
373
|
+
}
|
|
374
|
+
|
|
351
375
|
/** Ringkasan args untuk ditampilkan konsumen — dipangkas, dibasuh, dibatasi. */
|
|
352
376
|
function summarizeArgs(args: unknown): string {
|
|
353
377
|
if (args === undefined || args === null) return ""
|
package/src/daemon/client.ts
CHANGED
|
@@ -9,7 +9,12 @@
|
|
|
9
9
|
import { connect, type Socket } from "node:net"
|
|
10
10
|
import { type DiscoveryResult, discover, readEndpoint } from "./discovery.ts"
|
|
11
11
|
import { encodeFrame, FrameDecoder } from "./framing.ts"
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
DAEMON_PROTOCOL,
|
|
14
|
+
type DaemonStreamPhase,
|
|
15
|
+
type ServerMessage,
|
|
16
|
+
type SubscribeFrom,
|
|
17
|
+
} from "./protocol.ts"
|
|
13
18
|
|
|
14
19
|
export interface ConnectTarget {
|
|
15
20
|
host: string
|
|
@@ -96,6 +101,14 @@ export class DaemonClient {
|
|
|
96
101
|
#closed = false
|
|
97
102
|
welcome: Extract<ServerMessage, { t: "welcome" }> | null = null
|
|
98
103
|
#pendingControl = new Map<string, (msg: ServerMessage) => void>()
|
|
104
|
+
/**
|
|
105
|
+
* [Area C · PROMPT 06] Fase stream per-sesi, dilacak dari wire.
|
|
106
|
+
*
|
|
107
|
+
* Sumber: `subscribed.phase` (fase awal) lalu `tail_marker.phase` (fase
|
|
108
|
+
* live). Tanpa pelacakan ini, konsumen tak bisa menampilkan badge
|
|
109
|
+
* replay→live tanpa harus menebak dari urutan pesan.
|
|
110
|
+
*/
|
|
111
|
+
readonly #phases = new Map<string, DaemonStreamPhase>()
|
|
99
112
|
|
|
100
113
|
private constructor(sock: Socket) {
|
|
101
114
|
this.#sock = sock
|
|
@@ -113,6 +126,9 @@ export class DaemonClient {
|
|
|
113
126
|
for (const raw of msgs) {
|
|
114
127
|
if (!raw || typeof raw !== "object" || !("t" in raw)) continue
|
|
115
128
|
const msg = raw as ServerMessage
|
|
129
|
+
// [Area C] Latch fase stream per-sesi (sumber: wire, bukan tebakan).
|
|
130
|
+
if (msg.t === "subscribed") this.#phases.set(msg.sid, msg.phase)
|
|
131
|
+
else if (msg.t === "tail_marker") this.#phases.set(msg.sid, msg.phase)
|
|
116
132
|
if (msg.t === "control_result") {
|
|
117
133
|
const waiter = this.#pendingControl.get(msg.idem)
|
|
118
134
|
if (waiter) {
|
|
@@ -207,6 +223,36 @@ export class DaemonClient {
|
|
|
207
223
|
this.#send({ t: "subscribe", sid, from, consumer: this.welcome?.consumer ?? "" })
|
|
208
224
|
}
|
|
209
225
|
|
|
226
|
+
/**
|
|
227
|
+
* [Area C · PROMPT 06] Fase stream terakhir yang diketahui untuk satu sesi
|
|
228
|
+
* (`replay` sebelum `tail_marker`, `live` sesudahnya). `undefined` = belum
|
|
229
|
+
* subscribe / belum ada fase yang dilaporkan.
|
|
230
|
+
*/
|
|
231
|
+
phaseOf(sid: string): DaemonStreamPhase | undefined {
|
|
232
|
+
return this.#phases.get(sid)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Mapping EKPLISIT dari kosakata wire daemon ke kosakata provenance
|
|
237
|
+
* TAMPILAN (`live`/`replay`, lowercase — `src/presentation/projection.ts`).
|
|
238
|
+
*
|
|
239
|
+
* Dua kosakata itu sengaja terpisah (lihat catatan di `protocol.ts`):
|
|
240
|
+
* provenance wire = "dari mana event dibaca"; provenance tampilan = "status
|
|
241
|
+
* rebuild terhadap watershed". Yang dipetakan di sini adalah FASE stream,
|
|
242
|
+
* yang happens punya nilai yang sama — tapi mappingnya tetap ditulis
|
|
243
|
+
* eksplisit supaya tak ada perbandingan diam-diam antar kosakata.
|
|
244
|
+
*
|
|
245
|
+
* Badge yang dirender memakai penanda provenans yang SUDAH ada di lapisan
|
|
246
|
+
* presentasi (`provenanceMark`): fase "replay" → " [replay]", fase "live" →
|
|
247
|
+
* tanpa badge (live tak pernah diberi badge).
|
|
248
|
+
*/
|
|
249
|
+
streamProvenance(sid: string): "live" | "replay" | undefined {
|
|
250
|
+
const phase = this.#phases.get(sid)
|
|
251
|
+
if (phase === "replay") return "replay"
|
|
252
|
+
if (phase === "live") return "live"
|
|
253
|
+
return undefined
|
|
254
|
+
}
|
|
255
|
+
|
|
210
256
|
ack(sid: string, seq: number): void {
|
|
211
257
|
this.#send({ t: "ack", sid, seq, consumer: this.welcome?.consumer ?? "" })
|
|
212
258
|
}
|
|
@@ -247,6 +293,10 @@ export class DaemonClient {
|
|
|
247
293
|
if (this.#closed) return
|
|
248
294
|
this.#send({ t: "goodbye" })
|
|
249
295
|
this.#closed = true
|
|
296
|
+
// [Area C] Fase stream tak boleh bertahan hidup setelah socket tutup —
|
|
297
|
+
// consumer yang masih memegang klien tak boleh menampilkan "replay"
|
|
298
|
+
// untuk stream yang sudah mati.
|
|
299
|
+
this.#phases.clear()
|
|
250
300
|
try {
|
|
251
301
|
this.#sock.end()
|
|
252
302
|
} catch {
|
package/src/daemon/control.ts
CHANGED
|
@@ -21,6 +21,14 @@ export interface HostedSessionInfo {
|
|
|
21
21
|
runId: string | null
|
|
22
22
|
runStatus: string | null
|
|
23
23
|
presentationHead: number
|
|
24
|
+
/** P0.2 (Q5): WorkMode efektif sesi yang di-host (bila diketahui host). */
|
|
25
|
+
workMode?: "DISCUSS" | "PLAN" | "EXECUTE"
|
|
26
|
+
/**
|
|
27
|
+
* P2.3: hasil recovery run terakhir yang durable (mis. UNKNOWN setelah
|
|
28
|
+
* restart). Operator dapat membedakan LIVE vs reconstructed/interrupted
|
|
29
|
+
* tanpa membaca log. Absen = belum ada run.
|
|
30
|
+
*/
|
|
31
|
+
recoveryStatus?: string | null
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
/**
|