minicode-ai 0.11.0 → 0.13.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 +98 -0
- package/LICENSE +21 -21
- package/README.md +4 -4
- package/cli/args.ts +46 -0
- package/cli/commands/acp.ts +163 -10
- package/cli/commands/config.ts +33 -18
- package/cli/commands/daemon.ts +515 -0
- package/cli/commands/exec.ts +145 -53
- package/cli/commands/openwork.ts +986 -0
- package/cli/commands/sessions.ts +4 -1
- package/cli/commands/stats.ts +34 -4
- package/cli/commands.ts +217 -2
- package/cli/index.ts +146 -14
- package/cli/recovery-warning.ts +115 -0
- package/cli/router.ts +32 -1
- package/cli/setup.ts +2332 -171
- package/cli/tui.ts +113 -7
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +489 -0
- package/docs/OUTPUT_EVENT_MODEL.md +289 -0
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +391 -0
- package/docs/OUTPUT_PROTOCOL_SPEC.md +285 -0
- package/docs/OUTPUT_RENDERING_SPEC.md +248 -0
- package/docs/OUTPUT_UX_RULES.md +449 -0
- package/docs/PLAN_UIUX_V6.md +2 -2
- package/docs/README.md +4 -3
- package/docs/TERMINAL_CONTRACT.md +278 -66
- package/docs/architecture.md +89 -7
- package/docs/changelog.md +1 -1
- package/docs/choosing-mode.md +2 -2
- package/docs/cli.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +9 -2
- package/docs/environment.md +6 -2
- package/docs/exec.md +46 -5
- package/docs/memory-sessions.md +2 -0
- package/docs/policy-sandbox.md +1 -1
- package/docs/pricing-budget.md +1 -1
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +6 -2
- package/docs/security-model.md +1 -1
- package/docs/security.md +1 -1
- package/docs/skills.md +2 -2
- package/docs/terminal.md +9 -5
- package/docs/tools.md +1 -1
- package/package.json +35 -5
- package/src/app/session.ts +261 -15
- package/src/app/tool-layer.ts +94 -7
- package/src/daemon/approvals.ts +361 -0
- package/src/daemon/capability.ts +145 -0
- package/src/daemon/children.ts +142 -0
- package/src/daemon/client.ts +267 -0
- package/src/daemon/control.ts +350 -0
- package/src/daemon/discovery.ts +234 -0
- package/src/daemon/feed.ts +155 -0
- package/src/daemon/framing.ts +94 -0
- package/src/daemon/host.ts +316 -0
- package/src/daemon/protocol.ts +217 -0
- package/src/daemon/resource.ts +170 -0
- package/src/daemon/sanitize.ts +57 -0
- package/src/daemon/server.ts +627 -0
- package/src/daemon/store.ts +317 -0
- package/src/daemon/subscription.ts +270 -0
- package/src/daemon/telemetry.ts +81 -0
- package/src/hooks/run.ts +12 -1
- package/src/lib/atomic-write.ts +29 -3
- package/src/lib/db-path.ts +26 -0
- package/src/lib/keystore.ts +544 -42
- package/src/lib/net.ts +43 -1
- package/src/lib/safe-open.ts +116 -1
- package/src/lib/session-id.ts +15 -0
- package/src/lsp/client.ts +123 -5
- package/src/mcp/client.ts +139 -17
- package/src/mcp/server.ts +177 -50
- package/src/mcp/transport.ts +16 -19
- package/src/memory/vector.ts +770 -55
- package/src/policy/allowlist.ts +14 -2
- package/src/policy/bash-guard.ts +105 -10
- package/src/policy/compaction.ts +82 -5
- package/src/policy/consequence-engine.ts +220 -0
- package/src/policy/context.ts +132 -0
- package/src/policy/executor.ts +187 -121
- package/src/policy/no-progress.ts +559 -0
- package/src/policy/permission.ts +4 -8
- package/src/policy/pricing.ts +17 -3
- package/src/policy/scrub.ts +69 -2
- package/src/policy/tool-contract.ts +515 -0
- package/src/policy/usage.ts +310 -24
- package/src/policy/verifier.ts +6 -3
- package/src/policy/work-mode.ts +182 -0
- package/src/presentation/adapter.ts +533 -26
- package/src/presentation/events.ts +151 -0
- package/src/presentation/model.ts +180 -27
- package/src/presentation/projection.ts +858 -0
- package/src/presentation/reducer.ts +645 -310
- package/src/presentation/store.ts +12 -7
- package/src/providers/anthropic.ts +18 -10
- package/src/providers/responses.ts +17 -7
- package/src/runtime/capability.ts +409 -0
- package/src/runtime/child-execution.ts +274 -0
- package/src/runtime/composition.ts +958 -0
- package/src/runtime/dispatch.ts +591 -0
- package/src/runtime/execution-backend.ts +632 -0
- package/src/runtime/execution-events.ts +272 -0
- package/src/runtime/execution-id.ts +230 -0
- package/src/runtime/execution-journal.ts +926 -0
- package/src/runtime/execution-kernel.ts +456 -0
- package/src/runtime/execution-registry.ts +379 -0
- package/src/runtime/process-supervision.ts +276 -0
- package/src/runtime/production-execution.ts +551 -0
- package/src/runtime/production-runtime.ts +217 -0
- package/src/runtime/recovery-safety.ts +70 -0
- package/src/runtime/recovery.ts +477 -0
- package/src/runtime/runtime-capabilities.ts +179 -0
- package/src/runtime/runtime-diagnostics.ts +141 -0
- package/src/runtime/runtime-host.ts +365 -0
- package/src/runtime/runtime-status-facts.ts +222 -0
- package/src/runtime/runtime-status-provider.ts +74 -0
- package/src/runtime/runtime-status.ts +459 -0
- package/src/runtime/runtime-task-admission.ts +101 -0
- package/src/runtime/supervisor.ts +444 -0
- package/src/session/authority.ts +168 -0
- package/src/session/checkpoint.ts +88 -1
- package/src/session/context-adapter.ts +251 -0
- package/src/session/context-assembly.ts +242 -0
- package/src/session/context-identity.ts +389 -0
- package/src/session/context-projection.ts +248 -0
- package/src/session/context-selector.ts +500 -0
- package/src/session/cross-session-adoption.ts +249 -0
- package/src/session/full-history-fold.ts +171 -0
- package/src/session/identity.ts +192 -0
- package/src/session/journal.ts +805 -102
- package/src/session/open-work-intent.ts +732 -0
- package/src/session/open-work.ts +767 -0
- package/src/session/pending-tools.ts +94 -0
- package/src/session/persistence.ts +3569 -127
- package/src/session/runtime-header.ts +208 -0
- package/src/session/shadow-git.ts +13 -5
- package/src/session/verification.ts +1623 -0
- package/src/task/assignment.ts +136 -0
- package/src/task/autonomous-adapter.ts +168 -0
- package/src/task/autonomous-context.ts +630 -0
- package/src/task/autonomous-policy.ts +490 -0
- package/src/task/continuity.ts +179 -0
- package/src/task/execution-cancellation.ts +165 -0
- package/src/task/graph-validate.ts +379 -0
- package/src/task/graph.ts +305 -0
- package/src/task/identity.ts +230 -0
- package/src/task/model.ts +417 -0
- package/src/task/production-scheduler.ts +293 -0
- package/src/task/readiness.ts +229 -0
- package/src/task/scheduler-observability.ts +387 -0
- package/src/task/scheduler.ts +1197 -0
- package/src/task/scheduling-policy.ts +165 -0
- package/src/task/session-authority.ts +148 -0
- package/src/task/session-ownership.ts +211 -0
- package/src/task/store.ts +2887 -0
- package/src/task/sync.ts +343 -0
- package/src/task/trigger.ts +260 -0
- package/src/tools/bash.ts +377 -211
- package/src/tools/code_run.ts +1 -1
- package/src/tools/delete_file.ts +1 -1
- package/src/tools/edit.ts +33 -6
- package/src/tools/evidence.ts +30 -0
- package/src/tools/git.ts +4 -4
- package/src/tools/glob.ts +12 -3
- package/src/tools/grep.ts +1 -1
- package/src/tools/lsp.ts +4 -4
- package/src/tools/mcp_call.ts +11 -4
- package/src/tools/memory.ts +153 -18
- package/src/tools/move_file.ts +1 -1
- package/src/tools/patch.ts +1 -1
- package/src/tools/read_file.ts +1 -1
- package/src/tools/read_image.ts +1 -1
- package/src/tools/submit_result.ts +1 -1
- package/src/tools/task.ts +437 -41
- package/src/tools/todo.ts +662 -35
- package/src/tools/web_fetch.ts +4 -2
- package/src/tools/web_search.ts +9 -3
- package/src/tools/write_file.ts +1 -1
- package/src/ui/assistant/simple.ts +123 -50
- package/src/ui/assistant/turn-status.ts +17 -3
- package/src/ui/contract.ts +184 -0
- package/src/ui/footer.ts +54 -18
- package/src/ui/i18n/en.ts +62 -6
- package/src/ui/i18n/id.ts +62 -6
- package/src/ui/input/prompt-engine.ts +120 -25
- package/src/ui/render/errors.ts +39 -1
- package/src/ui/render/markdown-table.ts +455 -0
- package/src/ui/render/markdown.ts +22 -1
- package/src/ui/render/table-grid.ts +184 -0
- package/src/ui/render/table.ts +15 -76
- package/src/ui/runtime/screen.ts +7 -2
- package/src/ui/runtime/statusline.ts +25 -6
- package/src/ui/tui/app.ts +598 -133
- package/src/ui/tui/transcript.ts +531 -89
- package/vendor/minicore/VENDOR.md +1 -1
- 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/AGENTS.md +0 -45
- package/PLAN.md +0 -491
- package/docs/ARCHITECTURE.html +0 -1248
- package/docs/CONTROL-PLANE-MAP.md +0 -139
- package/docs/HARNESS.md +0 -150
- package/docs/SUMMARY.md +0 -52
- package/docs/UI_RENDER_PIPELINE.md +0 -165
- package/docs/USAGE.md +0 -11
package/src/policy/context.ts
CHANGED
|
@@ -22,6 +22,138 @@ export function estimateImageTokens(bytes: number): number {
|
|
|
22
22
|
return Math.ceil(b64 / DEFAULT_CHARS_PER_TOKEN)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
// ── [Fase 3 / CTX-03] Kalibrasi estimasi token vs usage aktual provider ─────
|
|
26
|
+
//
|
|
27
|
+
// `minicodeEstimator` berbasis `chars / 4` bisa *under-estimate* pada teks CJK,
|
|
28
|
+
// JSON padat, atau kode simbolik (sering 1.5–2.5 karakter/token). Bila estimasi
|
|
29
|
+
// lokal terlalu rendah, ambang kompaksi terlambat terpicu dan request meledak
|
|
30
|
+
// `context_length_exceeded`. Kalibrator ini mencatat drift terhadap angka aktual
|
|
31
|
+
// terakhir yang dilaporkan provider (`inputTokens` / `totalTokens`) dan memakai
|
|
32
|
+
// *conservative max* (`factor >= 1`) agar estimasi kompaksi tak pernah lebih
|
|
33
|
+
// rendah daripada realitas provider.
|
|
34
|
+
|
|
35
|
+
export interface TokenCalibrationSnapshot {
|
|
36
|
+
readonly factor: number
|
|
37
|
+
readonly lastLocalEstimate: number | null
|
|
38
|
+
readonly lastProviderTokens: number | null
|
|
39
|
+
readonly driftTokens: number
|
|
40
|
+
readonly effectiveTokens: number | null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface CalibratedEstimator {
|
|
44
|
+
readonly estimator: TokenEstimator
|
|
45
|
+
observeProviderUsage(input: {
|
|
46
|
+
localEstimatedTokens: number
|
|
47
|
+
providerInputTokens?: number
|
|
48
|
+
providerTotalTokens?: number
|
|
49
|
+
}): TokenCalibrationSnapshot
|
|
50
|
+
effectiveContextTokens(localEstimatedTokens: number): number
|
|
51
|
+
snapshot(): TokenCalibrationSnapshot
|
|
52
|
+
/** [Fase 7 / CTX-03 / U2] Rekonsiliasi eksplisit drift ratio estimator vs
|
|
53
|
+
* provider `totalTokens`. Mencatat sampel dan menyesuaikan faktor kalibrasi
|
|
54
|
+
* deterministik; mengembalikan snapshot terbaru. Nama eksplisit agar test
|
|
55
|
+
* (dan telemetri) bisa memanggilnya tanpa bergantung pada event bus. */
|
|
56
|
+
recordTokenCalibrationSample(
|
|
57
|
+
estimatedTokens: number,
|
|
58
|
+
providerActualTokens: number,
|
|
59
|
+
): TokenCalibrationSnapshot
|
|
60
|
+
/** [Fase 7 / CTX-03 / U2] Rasio drift terakhir: actual / max(1, estimated). */
|
|
61
|
+
lastDriftRatio(): number
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const DEFAULT_MAX_CALIBRATION_FACTOR = 64
|
|
65
|
+
|
|
66
|
+
export function createCalibratedEstimator(opts: { maxFactor?: number } = {}): CalibratedEstimator {
|
|
67
|
+
const maxFactor =
|
|
68
|
+
typeof opts.maxFactor === "number" && Number.isFinite(opts.maxFactor) && opts.maxFactor >= 1
|
|
69
|
+
? opts.maxFactor
|
|
70
|
+
: DEFAULT_MAX_CALIBRATION_FACTOR
|
|
71
|
+
let factor = 1
|
|
72
|
+
let lastLocal: number | null = null
|
|
73
|
+
let lastActual: number | null = null
|
|
74
|
+
let drift = 0
|
|
75
|
+
|
|
76
|
+
const estimator: TokenEstimator = (text: string) => {
|
|
77
|
+
const base = minicodeEstimator(text)
|
|
78
|
+
return factor > 1 ? Math.ceil(base * factor) : base
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Satu-satunya titik yang menyesuaikan `factor` dari sampel
|
|
82
|
+
// (estimer vs provider aktual). Idempoten dan konservatif: factor tidak
|
|
83
|
+
// pernah < 1, sehingga estimasi lokal tidak pernah dilemahkan; hanya naik
|
|
84
|
+
// proporsional saat aktual > estimasi (mencegah under-budget kompaksi).
|
|
85
|
+
const applyCalibration = (local: number, actual: number): void => {
|
|
86
|
+
lastLocal = local > 0 ? Math.ceil(local) : null
|
|
87
|
+
lastActual = actual > 0 ? Math.ceil(actual) : null
|
|
88
|
+
drift = (lastActual ?? 0) - (lastLocal ?? 0)
|
|
89
|
+
if (local > 0 && actual > 0) {
|
|
90
|
+
const observedRatio = actual / local
|
|
91
|
+
factor = Math.min(maxFactor, Math.max(1, observedRatio))
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
estimator,
|
|
97
|
+
observeProviderUsage(input) {
|
|
98
|
+
const local =
|
|
99
|
+
Number.isFinite(input.localEstimatedTokens) && input.localEstimatedTokens > 0
|
|
100
|
+
? Math.ceil(input.localEstimatedTokens)
|
|
101
|
+
: 0
|
|
102
|
+
const pInput =
|
|
103
|
+
typeof input.providerInputTokens === "number" &&
|
|
104
|
+
Number.isFinite(input.providerInputTokens) &&
|
|
105
|
+
input.providerInputTokens > 0
|
|
106
|
+
? Math.ceil(input.providerInputTokens)
|
|
107
|
+
: 0
|
|
108
|
+
const pTotal =
|
|
109
|
+
typeof input.providerTotalTokens === "number" &&
|
|
110
|
+
Number.isFinite(input.providerTotalTokens) &&
|
|
111
|
+
input.providerTotalTokens > 0
|
|
112
|
+
? Math.ceil(input.providerTotalTokens)
|
|
113
|
+
: 0
|
|
114
|
+
// Pakai inputTokens bila tersedia (apel-ke-apel dengan prompt konteks);
|
|
115
|
+
// fallback ke totalTokens bila provider hanya melaporkan total.
|
|
116
|
+
const actual = pInput > 0 ? pInput : pTotal
|
|
117
|
+
if (actual > 0) applyCalibration(local, actual)
|
|
118
|
+
return this.snapshot()
|
|
119
|
+
},
|
|
120
|
+
recordTokenCalibrationSample(estimatedTokens, providerActualTokens) {
|
|
121
|
+
const local =
|
|
122
|
+
Number.isFinite(estimatedTokens) && estimatedTokens > 0 ? Math.ceil(estimatedTokens) : 0
|
|
123
|
+
const actual =
|
|
124
|
+
Number.isFinite(providerActualTokens) && providerActualTokens > 0
|
|
125
|
+
? Math.ceil(providerActualTokens)
|
|
126
|
+
: 0
|
|
127
|
+
if (actual > 0) applyCalibration(local, actual)
|
|
128
|
+
return this.snapshot()
|
|
129
|
+
},
|
|
130
|
+
lastDriftRatio(): number {
|
|
131
|
+
return lastLocal !== null && lastLocal > 0 ? (lastActual ?? 0) / lastLocal : 1
|
|
132
|
+
},
|
|
133
|
+
effectiveContextTokens(localEstimatedTokens: number): number {
|
|
134
|
+
const cleanLocal =
|
|
135
|
+
Number.isFinite(localEstimatedTokens) && localEstimatedTokens > 0
|
|
136
|
+
? Math.ceil(localEstimatedTokens)
|
|
137
|
+
: 0
|
|
138
|
+
const scaled = Math.ceil(cleanLocal * factor)
|
|
139
|
+
return lastActual !== null ? Math.max(scaled, lastActual) : scaled
|
|
140
|
+
},
|
|
141
|
+
snapshot(): TokenCalibrationSnapshot {
|
|
142
|
+
const effective =
|
|
143
|
+
lastLocal !== null && lastActual !== null
|
|
144
|
+
? Math.max(Math.ceil(lastLocal * factor), lastActual)
|
|
145
|
+
: null
|
|
146
|
+
return {
|
|
147
|
+
factor,
|
|
148
|
+
lastLocalEstimate: lastLocal,
|
|
149
|
+
lastProviderTokens: lastActual,
|
|
150
|
+
driftTokens: drift,
|
|
151
|
+
effectiveTokens: effective,
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
25
157
|
const MAX_SYSTEM_CHARS = LIMITS.SYSTEM_PROMPT_MAX_CHARS
|
|
26
158
|
|
|
27
159
|
// Penanda truncation (temuan audit #02): instruksi yang dipotong diam-diam
|
package/src/policy/executor.ts
CHANGED
|
@@ -4,21 +4,33 @@ import { runCall } from "#minicore/core/executor.ts"
|
|
|
4
4
|
import type { ExecutorDeps, ToolExecutor } from "#minicore/core/index.ts"
|
|
5
5
|
import type { ToolCall, ToolResult } from "#minicore/core/types.ts"
|
|
6
6
|
import { LIMITS } from "../constants.ts"
|
|
7
|
-
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// Tool yang harus eksklusif tapi tidak punya path tunggal untuk di-lock.
|
|
12
|
-
// `bash` bisa menulis apa pun, jadi tidak boleh paralel dengan write lain;
|
|
13
|
-
// tapi memasukkannya ke WRITE_TOOLS membuat dua bash read-only
|
|
14
|
-
// (`bun test` + `git log`) ikut terserialisasi tanpa alasan karena
|
|
15
|
-
// getFilePath() selalu null untuk bash. Dipisah supaya cap-nya sendiri.
|
|
16
|
-
const EXCLUSIVE_TOOLS = new Set(["bash", "write_memory", "forget_memory", "todo_write"])
|
|
7
|
+
// [Fase 2 / TOOL-01] Klasifikasi konkurensi diturunkan dari SATU registry
|
|
8
|
+
// kontrak tool — dulu dua Set manual di file ini yang bisa menyimpang.
|
|
9
|
+
import { EXCLUSIVE_TOOLS, WRITE_TOOLS } from "./tool-contract.ts"
|
|
17
10
|
|
|
18
11
|
function isWrite(call: ToolCall): boolean {
|
|
19
12
|
return WRITE_TOOLS.has(call.name) || EXCLUSIVE_TOOLS.has(call.name)
|
|
20
13
|
}
|
|
21
14
|
|
|
15
|
+
// Kunci file-lock: hanya WRITE_TOOLS dengan args.path string valid.
|
|
16
|
+
// EXCLUSIVE_TOOLS (tanpa path) dan move_file (memakai from/to) sengaja tidak
|
|
17
|
+
// punya kunci — inilah yang membuat writeConcurrency > 1 tidak aman untuk
|
|
18
|
+
// mereka (F-01). Klasifikasi batch memakai fungsi ini; jangan diganti dengan
|
|
19
|
+
// cek `isWrite` saja.
|
|
20
|
+
function getLockPath(call: ToolCall, cwd: string | undefined): string | null {
|
|
21
|
+
if (!WRITE_TOOLS.has(call.name)) return null
|
|
22
|
+
const p = (call.args as Record<string, unknown>)?.path
|
|
23
|
+
if (typeof p !== "string" || !p) return null
|
|
24
|
+
// normalisasi: resolve abs + lowerCase di Windows agar ./a.ts vs a.ts tidak miss lock
|
|
25
|
+
try {
|
|
26
|
+
const base = cwd ?? process.cwd()
|
|
27
|
+
const abs = resolve(base, p)
|
|
28
|
+
return process.platform === "win32" ? abs.toLowerCase() : abs
|
|
29
|
+
} catch {
|
|
30
|
+
return process.platform === "win32" ? p.toLowerCase() : p
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
22
34
|
// Antrean abort-aware: saat signal abort, entry dibuang dari antrean dan
|
|
23
35
|
// promise langsung reject — worker tidak menunggu tool in-flight selesai.
|
|
24
36
|
interface Waiter {
|
|
@@ -62,138 +74,192 @@ function makeWaiter(
|
|
|
62
74
|
}
|
|
63
75
|
|
|
64
76
|
export function parallelExecutor(
|
|
65
|
-
opts: { concurrency?: number; writeConcurrency?: number } = {},
|
|
77
|
+
opts: { concurrency?: number; writeConcurrency?: number; maxToolsPerBatch?: number } = {},
|
|
66
78
|
): ToolExecutor {
|
|
67
79
|
const concurrency = opts.concurrency ?? LIMITS.EXECUTOR_CONCURRENCY
|
|
68
80
|
const writeConcurrency = opts.writeConcurrency ?? LIMITS.EXECUTOR_WRITE_CONCURRENCY
|
|
81
|
+
// [Fase 8 / 8.3] Batas tool per batch. Bila disetel (>0), batch yang
|
|
82
|
+
// melebihi batas dipangkas: tool yang melampaui batas TIDAK dieksekusi dan
|
|
83
|
+
// diganti hasil tool-error dengan pesan `[batch limit reached: maxToolsPerBatch=...]`.
|
|
84
|
+
// Mencegah eksekusi berlebih dalam satu batch (Live Test: model mengirim
|
|
85
|
+
// puluhan tool call sekaligus yang membakar budget tanpa hasil berguna).
|
|
86
|
+
const maxToolsPerBatch = opts.maxToolsPerBatch ?? 0
|
|
69
87
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
88
|
+
async function runBatch(
|
|
89
|
+
calls: readonly ToolCall[],
|
|
90
|
+
deps: ExecutorDeps,
|
|
91
|
+
): Promise<readonly ToolResult[]> {
|
|
92
|
+
if (calls.length === 0) return []
|
|
93
|
+
const results: (ToolResult | undefined)[] = new Array(calls.length)
|
|
74
94
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return results as ToolResult[]
|
|
95
|
+
// write+baca campur → sekuensial urut input: read setelah write pada file
|
|
96
|
+
// sama harus melihat konten baru (paralel akan balapan). Perilaku P4 yang
|
|
97
|
+
// dibekukan.
|
|
98
|
+
const anyWrite = calls.some(isWrite)
|
|
99
|
+
const anyRead = calls.some((c) => !isWrite(c))
|
|
100
|
+
if (anyWrite && anyRead) {
|
|
101
|
+
for (let i = 0; i < calls.length; i++) {
|
|
102
|
+
if (deps.signal.aborted) throw abortError(deps.signal)
|
|
103
|
+
results[i] = await runCall(calls[i]!, deps)
|
|
85
104
|
}
|
|
105
|
+
return results as ToolResult[]
|
|
106
|
+
}
|
|
86
107
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
108
|
+
// F-01 (P5 §C1): slot tulis (writeConcurrency > 1) hanya didemonstrasikan
|
|
109
|
+
// aman untuk write tool ber-kunci args.path. Batch yang mengandung write
|
|
110
|
+
// tanpa kunci — EXCLUSIVE_TOOLS (bash dsb.), move_file (from/to), atau
|
|
111
|
+
// WRITE_TOOL tanpa path valid — dijalankan sekuensial penuh. Ini menutup
|
|
112
|
+
// overlap dua bash, bash vs write path, dan dua move_file yang dulu lolos
|
|
113
|
+
// pada wc > 1 tanpa bukti keselamatan; batch murni write ber-path tetap
|
|
114
|
+
// memakai semaphore + file-lock di bawah. Race terhadap signal
|
|
115
|
+
// mempertahankan penolakan prompt saat abort (setara antrean writeWaiter):
|
|
116
|
+
// tool in-flight boleh mengabaikan signal, tetapi execute() reject begitu
|
|
117
|
+
// abort datang, tanpa menunggu tool selesai.
|
|
118
|
+
const anyUnkeyedWrite = calls.some((c) => isWrite(c) && getLockPath(c, deps.cwd) === null)
|
|
119
|
+
if (anyWrite && anyUnkeyedWrite) {
|
|
93
120
|
const signal = deps.signal
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
121
|
+
let onAbort: (() => void) | undefined
|
|
122
|
+
const aborted = new Promise<never>((_, reject) => {
|
|
123
|
+
onAbort = () => reject(abortError(signal))
|
|
124
|
+
if (signal.aborted) onAbort()
|
|
125
|
+
else signal.addEventListener("abort", onAbort, { once: true })
|
|
126
|
+
})
|
|
127
|
+
try {
|
|
128
|
+
await Promise.race([
|
|
129
|
+
(async () => {
|
|
130
|
+
for (let i = 0; i < calls.length; i++) {
|
|
131
|
+
if (signal.aborted) throw abortError(signal)
|
|
132
|
+
results[i] = await runCall(calls[i]!, deps)
|
|
133
|
+
}
|
|
134
|
+
})(),
|
|
135
|
+
aborted,
|
|
136
|
+
])
|
|
137
|
+
} finally {
|
|
138
|
+
// lepas listener agar signal jangka panjang tidak menumpuk listener
|
|
139
|
+
if (onAbort) signal.removeEventListener("abort", onAbort)
|
|
107
140
|
}
|
|
141
|
+
return results as ToolResult[]
|
|
142
|
+
}
|
|
108
143
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
144
|
+
let cursor = 0
|
|
145
|
+
let activeWrites = 0
|
|
146
|
+
// per-file lock to prevent same-file write race (e.g. two edits to same path)
|
|
147
|
+
const fileLocks = new Map<string, number>()
|
|
148
|
+
const fileWaiters = new Map<string, Waiter[]>()
|
|
149
|
+
const writeWaiters: Waiter[] = []
|
|
150
|
+
const signal = deps.signal
|
|
151
|
+
|
|
152
|
+
function acquireWrite(): Promise<void> {
|
|
153
|
+
if (signal.aborted) return Promise.reject(abortError(signal))
|
|
154
|
+
if (activeWrites < writeConcurrency) {
|
|
155
|
+
activeWrites++
|
|
156
|
+
return Promise.resolve()
|
|
121
157
|
}
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
158
|
+
const { promise, waiter } = makeWaiter(signal, (w) => {
|
|
159
|
+
const i = writeWaiters.indexOf(w)
|
|
160
|
+
if (i !== -1) writeWaiters.splice(i, 1)
|
|
161
|
+
})
|
|
162
|
+
writeWaiters.push(waiter)
|
|
163
|
+
return promise
|
|
164
|
+
}
|
|
165
|
+
function releaseWrite(): void {
|
|
166
|
+
const next = writeWaiters.shift()
|
|
167
|
+
if (next) {
|
|
168
|
+
// ownership berpindah — counter tetap (setara decrement+increment lama)
|
|
169
|
+
next.resolve()
|
|
170
|
+
} else {
|
|
171
|
+
activeWrites--
|
|
130
172
|
}
|
|
173
|
+
}
|
|
131
174
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
const { promise, waiter } = makeWaiter(signal, (w) => {
|
|
140
|
-
const q = fileWaiters.get(path)
|
|
141
|
-
if (!q) return
|
|
142
|
-
const i = q.indexOf(w)
|
|
143
|
-
if (i !== -1) q.splice(i, 1)
|
|
144
|
-
if (q.length === 0) fileWaiters.delete(path)
|
|
145
|
-
})
|
|
146
|
-
const q = fileWaiters.get(path) ?? []
|
|
147
|
-
q.push(waiter)
|
|
148
|
-
fileWaiters.set(path, q)
|
|
149
|
-
return promise
|
|
175
|
+
function acquireFileLock(path: string | null): Promise<void> {
|
|
176
|
+
if (!path) return Promise.resolve()
|
|
177
|
+
if (signal.aborted) return Promise.reject(abortError(signal))
|
|
178
|
+
if ((fileLocks.get(path) ?? 0) === 0) {
|
|
179
|
+
fileLocks.set(path, 1)
|
|
180
|
+
return Promise.resolve()
|
|
150
181
|
}
|
|
151
|
-
|
|
152
|
-
if (!path) return
|
|
182
|
+
const { promise, waiter } = makeWaiter(signal, (w) => {
|
|
153
183
|
const q = fileWaiters.get(path)
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
184
|
+
if (!q) return
|
|
185
|
+
const i = q.indexOf(w)
|
|
186
|
+
if (i !== -1) q.splice(i, 1)
|
|
187
|
+
if (q.length === 0) fileWaiters.delete(path)
|
|
188
|
+
})
|
|
189
|
+
const q = fileWaiters.get(path) ?? []
|
|
190
|
+
q.push(waiter)
|
|
191
|
+
fileWaiters.set(path, q)
|
|
192
|
+
return promise
|
|
193
|
+
}
|
|
194
|
+
function releaseFileLock(path: string | null): void {
|
|
195
|
+
if (!path) return
|
|
196
|
+
const q = fileWaiters.get(path)
|
|
197
|
+
const next = q?.shift()
|
|
198
|
+
if (next) {
|
|
199
|
+
if (q && q.length === 0) fileWaiters.delete(path)
|
|
200
|
+
next.resolve() // ownership berpindah ke waiter berikutnya
|
|
201
|
+
} else {
|
|
202
|
+
fileLocks.delete(path)
|
|
161
203
|
}
|
|
204
|
+
}
|
|
162
205
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
releaseWrite()
|
|
182
|
-
throw e
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
const res = await runCall(call, deps)
|
|
186
|
-
results[idx] = res
|
|
187
|
-
} finally {
|
|
188
|
-
if (held) {
|
|
189
|
-
releaseFileLock(filePath)
|
|
206
|
+
const workers = Array(Math.min(concurrency, calls.length))
|
|
207
|
+
.fill(0)
|
|
208
|
+
.map(async () => {
|
|
209
|
+
while (true) {
|
|
210
|
+
if (deps.signal.aborted) throw abortError(deps.signal)
|
|
211
|
+
const idx = cursor++
|
|
212
|
+
if (idx >= calls.length) break
|
|
213
|
+
const call = calls[idx]!
|
|
214
|
+
const needWrite = isWrite(call)
|
|
215
|
+
const filePath = needWrite ? getLockPath(call, deps.cwd) : null
|
|
216
|
+
let held = false
|
|
217
|
+
try {
|
|
218
|
+
if (needWrite) {
|
|
219
|
+
await acquireWrite() // nothing held yet — safe to throw
|
|
220
|
+
try {
|
|
221
|
+
await acquireFileLock(filePath)
|
|
222
|
+
held = true
|
|
223
|
+
} catch (e) {
|
|
190
224
|
releaseWrite()
|
|
225
|
+
throw e
|
|
191
226
|
}
|
|
192
227
|
}
|
|
228
|
+
const res = await runCall(call, deps)
|
|
229
|
+
results[idx] = res
|
|
230
|
+
} finally {
|
|
231
|
+
if (held) {
|
|
232
|
+
releaseFileLock(filePath)
|
|
233
|
+
releaseWrite()
|
|
234
|
+
}
|
|
193
235
|
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
await Promise.all(workers)
|
|
239
|
+
return results as ToolResult[]
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
async execute(calls: readonly ToolCall[], deps: ExecutorDeps): Promise<readonly ToolResult[]> {
|
|
244
|
+
if (calls.length === 0) return []
|
|
245
|
+
// [Fase 8 / 8.3] Pangkas batch bila maxToolsPerBatch aktif.
|
|
246
|
+
if (maxToolsPerBatch > 0 && calls.length > maxToolsPerBatch) {
|
|
247
|
+
const kept = calls.slice(0, maxToolsPerBatch)
|
|
248
|
+
const results: (ToolResult | undefined)[] = new Array(calls.length)
|
|
249
|
+
const trimmed = await runBatch(kept, deps)
|
|
250
|
+
for (let i = 0; i < trimmed.length; i++) results[i] = trimmed[i]
|
|
251
|
+
for (let i = maxToolsPerBatch; i < calls.length; i++) {
|
|
252
|
+
results[i] = {
|
|
253
|
+
role: "tool",
|
|
254
|
+
toolCallId: calls[i]!.id,
|
|
255
|
+
name: calls[i]!.name,
|
|
256
|
+
content: `[batch limit reached: maxToolsPerBatch=${maxToolsPerBatch}]`,
|
|
257
|
+
isError: true,
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return results as ToolResult[]
|
|
261
|
+
}
|
|
262
|
+
return runBatch(calls, deps)
|
|
197
263
|
},
|
|
198
264
|
}
|
|
199
265
|
}
|