minicode-ai 0.11.1 → 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 +89 -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 -71
- 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 +2 -2
- 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 +52 -19
- package/src/ui/i18n/en.ts +58 -6
- package/src/ui/i18n/id.ts +58 -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 +507 -169
- 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 -169
- package/docs/USAGE.md +0 -11
package/cli/commands/exec.ts
CHANGED
|
@@ -1,45 +1,68 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto"
|
|
2
1
|
import { resolve as resolvePath } from "node:path"
|
|
3
2
|
import { NoProviderError } from "../../src/app/provider-layer.ts"
|
|
4
3
|
import { createRateLimiter } from "../../src/policy/ratelimit.ts"
|
|
5
4
|
import { resolveSandbox, sandboxRefusalReason } from "../../src/policy/sandbox-policy.ts"
|
|
6
5
|
import { scrubSecrets } from "../../src/policy/scrub.ts"
|
|
7
6
|
import { budgetStatus } from "../../src/policy/usage.ts"
|
|
7
|
+
import {
|
|
8
|
+
MACHINE_SCHEMA,
|
|
9
|
+
machineError,
|
|
10
|
+
toMachineEnvelope,
|
|
11
|
+
} from "../../src/presentation/projection.ts"
|
|
12
|
+
import { resolveRuntimeMode } from "../../src/runtime/production-execution.ts"
|
|
8
13
|
import { clearSubmittedResult, getSubmittedResult } from "../../src/tools/submit_result.ts"
|
|
9
14
|
import { formatError } from "../../src/ui/assistant/simple.ts"
|
|
10
15
|
import { formatUsd } from "../../src/ui/render/money.ts"
|
|
11
|
-
import {
|
|
16
|
+
import { cleanUntrusted } from "../../src/ui/render/sanitize.ts"
|
|
17
|
+
import {
|
|
18
|
+
allowLocalConfig,
|
|
19
|
+
hasFlag,
|
|
20
|
+
promptFromArgs,
|
|
21
|
+
getArg as rawGetArg,
|
|
22
|
+
resolveWorkModeFlag,
|
|
23
|
+
} from "../args.ts"
|
|
12
24
|
import { createCliSession } from "../setup.ts"
|
|
13
25
|
|
|
26
|
+
/** Satu baris stdout mesin: JSON yang sudah di-scrub + tanpa ANSI apa pun. */
|
|
27
|
+
function writeMachineLine(line: string): void {
|
|
28
|
+
process.stdout.write(`${cleanUntrusted(scrubSecrets(line), false)}\n`)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function machineSummary(ok: boolean, fields: Record<string, unknown>): Record<string, unknown> {
|
|
32
|
+
return { schema: MACHINE_SCHEMA, type: "summary", ok, ...fields }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function machineFailure(prompt: string, e: unknown): Record<string, unknown> {
|
|
36
|
+
return machineSummary(false, { error: machineError(e), prompt })
|
|
37
|
+
}
|
|
38
|
+
|
|
14
39
|
export async function handleExec(
|
|
15
|
-
|
|
40
|
+
// P11.2 — router mengirim argv scoped TANPA token subcommand ("exec"):
|
|
41
|
+
// flag global yang dikenal (sebelum subcommand) + semua token sesudahnya.
|
|
42
|
+
// Irisan lokal `args[0] === "exec"` lama hanya benar bila exec token
|
|
43
|
+
// pertama; dengan flag global mendahului ("--json exec ..."), token
|
|
44
|
+
// routing bocor ke prompt + summary mesin. Handler mempercayai argv yang
|
|
45
|
+
// sudah scoped — jangan iris lagi di sini.
|
|
46
|
+
subArgs: string[],
|
|
16
47
|
getArg: (name: string) => string | undefined,
|
|
17
48
|
): Promise<never> {
|
|
18
49
|
// Bersihkan state submit_result dari run sebelumnya — tanpa ini, exec
|
|
19
50
|
// berturut-turut dalam proses sama bisa membaca hasil run lama.
|
|
20
51
|
clearSubmittedResult()
|
|
21
|
-
//
|
|
22
52
|
// Prompt diambil lewat promptFromArgs() — satu implementasi yang sama dengan
|
|
23
53
|
// jalur non-exec. Versi sebelumnya menyaring dengan `a !== getArg("--model")`,
|
|
24
54
|
// yang hanya membuang NILAI dari dua flag (`--model`, `--cwd`); nilai flag lain
|
|
25
55
|
// ikut masuk ke prompt. `exec "tes" --provider gorouter --timeout 60000` benar-
|
|
26
56
|
// benar mengirim "tes gorouter 60000" ke model, dan model membalas dengan
|
|
27
57
|
// menebak-nebak soal "gorouter" dan "60000".
|
|
28
|
-
// Router memanggil dengan argv penuh (["exec", ...]) sementara hasFlag/
|
|
29
|
-
// getArg berhenti di token subcommand (boundary anti-injeksi) — tanpa
|
|
30
|
-
// slice, SEMUA flag boolean exec (--json/--verify/--ask/...) mati diam-diam
|
|
31
|
-
// di CLI nyata. Pola yang sama dengan promptFromArgs di bawah.
|
|
32
|
-
const subArgs = args[0] === "exec" ? args.slice(1) : args
|
|
33
58
|
const prompt = promptFromArgs(subArgs) || (rawGetArg(subArgs, "--prompt") ?? "")
|
|
34
59
|
const jsonMode = hasFlag(subArgs, "--json") || rawGetArg(subArgs, "--output-format") === "json"
|
|
35
60
|
const cwdRaw = getArg("--cwd")
|
|
36
61
|
const cwd = cwdRaw ? resolvePath(cwdRaw) : undefined
|
|
37
62
|
const modelOverride = getArg("--model")
|
|
38
63
|
const providerOverride = getArg("--provider")
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
?.replace(/[^A-Za-z0-9._-]/g, "-")
|
|
42
|
-
.slice(0, 64) || randomUUID().slice(0, 8)
|
|
64
|
+
// P2.1: flag mentah; identitas kanonik diputus di resolver (satu sanitizer).
|
|
65
|
+
const sessionFlag = getArg("--session") ?? ""
|
|
43
66
|
const allowAll = hasFlag(subArgs, "--allow-all")
|
|
44
67
|
const ask = hasFlag(subArgs, "--ask")
|
|
45
68
|
const plan = hasFlag(subArgs, "--plan")
|
|
@@ -53,6 +76,8 @@ export async function handleExec(
|
|
|
53
76
|
const sandboxRefusal = sandboxRefusalReason(requestedSandbox)
|
|
54
77
|
if (sandboxRefusal) {
|
|
55
78
|
console.error(sandboxRefusal)
|
|
79
|
+
if (jsonMode)
|
|
80
|
+
writeMachineLine(JSON.stringify(machineFailure(prompt.trim(), new Error(sandboxRefusal))))
|
|
56
81
|
process.exit(1)
|
|
57
82
|
}
|
|
58
83
|
const sandbox = resolveSandbox(requestedSandbox, allowAll || ask || plan || allowlistFlag)
|
|
@@ -62,11 +87,13 @@ export async function handleExec(
|
|
|
62
87
|
// Audit #07 P0: local config repo tak dipercaya kecuali operator opt-in.
|
|
63
88
|
const allowLocal = allowLocalConfig(subArgs)
|
|
64
89
|
const budgetRaw = getArg("--budget")
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
90
|
+
let budget = budgetRaw ? Number(budgetRaw) : undefined
|
|
91
|
+
// [Audit #2 A.1/B.4] Selaras dengan cli/index.ts: --budget non-angka/negatif
|
|
92
|
+
// diingatkan eksplisit, bukan dibiarkan berubah jadi undefined secara diam.
|
|
93
|
+
if (budgetRaw && (!Number.isFinite(budget) || (budget as number) < 0)) {
|
|
94
|
+
process.stderr.write(`[warn] --budget requires a USD number, ignoring "${budgetRaw}"\n`)
|
|
95
|
+
budget = undefined
|
|
96
|
+
}
|
|
70
97
|
// Harness-P1: sama seperti one-shot — strict fail-closed bila cost tak dikenal.
|
|
71
98
|
const budgetStrict =
|
|
72
99
|
hasFlag(subArgs, "--budget-strict") || process.env.MINICODE_BUDGET_STRICT === "1"
|
|
@@ -77,34 +104,62 @@ export async function handleExec(
|
|
|
77
104
|
""
|
|
78
105
|
).toLowerCase()
|
|
79
106
|
const toolScope = toolScopeRaw === "explore" ? ("explore" as const) : ("full" as const)
|
|
107
|
+
// P12 S2.2 — WorkMode (flag > env, default EXECUTE; invalid → EXECUTE+warn).
|
|
108
|
+
const workModeResolved = resolveWorkModeFlag(getArg)
|
|
109
|
+
const workMode = workModeResolved.workMode
|
|
110
|
+
if (workModeResolved.warning) console.error(`[warn] ${workModeResolved.warning}`)
|
|
80
111
|
const ratelimitRaw = getArg("--ratelimit")
|
|
81
|
-
|
|
112
|
+
let rateLimiter: ReturnType<typeof createRateLimiter> | undefined
|
|
113
|
+
if (ratelimitRaw) {
|
|
114
|
+
const rpm = Number(ratelimitRaw)
|
|
115
|
+
if (!Number.isFinite(rpm) || rpm <= 0) {
|
|
116
|
+
process.stderr.write(
|
|
117
|
+
`[warn] --ratelimit requires a positive number, ignoring "${ratelimitRaw}"\n`,
|
|
118
|
+
)
|
|
119
|
+
} else {
|
|
120
|
+
rateLimiter = createRateLimiter(rpm)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
82
123
|
|
|
83
124
|
const effectivePrompt = prompt.trim()
|
|
84
125
|
if (!effectivePrompt) {
|
|
85
126
|
console.error(
|
|
86
127
|
'usage: minicode exec "prompt" [--json] [--cwd <dir>] [--model <m>] [--sandbox docker|os]',
|
|
87
128
|
)
|
|
129
|
+
if (jsonMode)
|
|
130
|
+
writeMachineLine(
|
|
131
|
+
JSON.stringify(
|
|
132
|
+
machineSummary(false, {
|
|
133
|
+
error: { category: "USER_ERROR", message: "prompt is required" },
|
|
134
|
+
}),
|
|
135
|
+
),
|
|
136
|
+
)
|
|
88
137
|
process.exit(2)
|
|
89
138
|
}
|
|
90
139
|
|
|
91
140
|
const ctx = await createCliSession({
|
|
92
141
|
cwd,
|
|
93
|
-
sessionId,
|
|
142
|
+
sessionId: sessionFlag,
|
|
94
143
|
modelOverride,
|
|
95
144
|
providerOverride,
|
|
96
145
|
prompt: effectivePrompt,
|
|
97
146
|
enterRepl: false,
|
|
147
|
+
machineOutput: jsonMode,
|
|
98
148
|
verbose: false,
|
|
99
149
|
allowAll,
|
|
100
150
|
ask,
|
|
101
151
|
plan,
|
|
102
152
|
allowlist,
|
|
103
153
|
verify: hasFlag(subArgs, "--verify"),
|
|
154
|
+
// [P1 M15] Mode runtime produksi, deterministik per invokasi (sama seperti
|
|
155
|
+
// entry utama). Nilai tak dikenal = off + warning (fail-closed).
|
|
156
|
+
runtimeMode: resolveRuntimeMode(subArgs).mode,
|
|
104
157
|
allowLocalConfig: allowLocal,
|
|
105
158
|
budget,
|
|
106
159
|
budgetStrict,
|
|
107
160
|
toolScope,
|
|
161
|
+
// P12 S2.2 — WorkMode efektif (sudah di-resolve, default EXECUTE).
|
|
162
|
+
workMode,
|
|
108
163
|
rateLimiter,
|
|
109
164
|
// Notice hanya bila user eksplisit meminta mode (daemon mati / tak dikenal).
|
|
110
165
|
sandboxNotice: requestedSandbox ? sandbox.notice : undefined,
|
|
@@ -113,28 +168,42 @@ export async function handleExec(
|
|
|
113
168
|
// mesin tetap pulang membawa satu baris summary agar pipeline CI tak
|
|
114
169
|
// menerima stream kosong — stderr manusia + exit 1 tidak berubah.
|
|
115
170
|
if (e instanceof NoProviderError) {
|
|
116
|
-
if (jsonMode)
|
|
117
|
-
process.stdout.write(
|
|
118
|
-
`${scrubSecrets(JSON.stringify({ type: "summary", ok: false, error: formatError(e), prompt: effectivePrompt }))}\n`,
|
|
119
|
-
)
|
|
171
|
+
if (jsonMode) writeMachineLine(JSON.stringify(machineFailure(effectivePrompt, e)))
|
|
120
172
|
console.error(e.message)
|
|
121
173
|
process.exit(1)
|
|
122
174
|
}
|
|
175
|
+
// Setup lain yang gagal (config rusak, dsb.): mesin tetap pulang membawa
|
|
176
|
+
// envelope terminal agar stream tidak kosong, lalu error asli dilempar
|
|
177
|
+
// agar exit code + stderr manusia tidak berubah.
|
|
178
|
+
if (jsonMode) writeMachineLine(JSON.stringify(machineFailure(effectivePrompt, e)))
|
|
123
179
|
throw e
|
|
124
180
|
})
|
|
125
181
|
const t0 = Date.now()
|
|
126
|
-
|
|
127
|
-
//
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
182
|
+
let streamed = 0
|
|
183
|
+
// Lifecycle kanonik (bukan raw kernel shape): tiap UiPresentationEvent
|
|
184
|
+
// dipetakan ke envelope minicode.output.v1. Teks model tetap record
|
|
185
|
+
// {type:"text",delta} terpisah — delta bukan lifecycle.
|
|
186
|
+
const unsubMachine = ctx.onPresentationEvent
|
|
187
|
+
? ctx.onPresentationEvent((event) => {
|
|
188
|
+
const envelope = toMachineEnvelope(event, {
|
|
189
|
+
sessionId: ctx.sessionId,
|
|
190
|
+
timestamp: Date.now(),
|
|
191
|
+
})
|
|
192
|
+
if (!envelope) return
|
|
193
|
+
streamed++
|
|
194
|
+
if (jsonMode) writeMachineLine(JSON.stringify(envelope))
|
|
195
|
+
})
|
|
196
|
+
: () => {}
|
|
197
|
+
const unsubText = ctx.session.events.on("provider:text", (ev) => {
|
|
198
|
+
const text = (ev as { text?: unknown }).text
|
|
199
|
+
if (typeof text !== "string" || !text) return
|
|
200
|
+
if (!jsonMode) return
|
|
201
|
+
const max = 100_000
|
|
202
|
+
const delta = text.length > max ? text.slice(0, max) : text
|
|
203
|
+
streamed++
|
|
204
|
+
writeMachineLine(
|
|
205
|
+
JSON.stringify({ type: "text", delta, ...(text.length > max ? { truncated: true } : {}) }),
|
|
206
|
+
)
|
|
138
207
|
})
|
|
139
208
|
try {
|
|
140
209
|
await ctx.runPromptWithVerify(effectivePrompt)
|
|
@@ -147,12 +216,12 @@ export async function handleExec(
|
|
|
147
216
|
bStatus === "over" && ue.cost != null && budget != null
|
|
148
217
|
? `[budget] ${formatUsd(ue.cost)} > ${formatUsd(budget)} - over budget, stopping.`
|
|
149
218
|
: `[budget] cost unknown (model without pricing) with ${ue.totalTokens} tokens spent - over budget, stopping.`
|
|
150
|
-
|
|
219
|
+
unsubMachine()
|
|
220
|
+
unsubText()
|
|
221
|
+
await ctx.persistCurrent(ctx.usage.getSession(ctx.modelRef.current))
|
|
151
222
|
await ctx.close()
|
|
152
223
|
if (jsonMode)
|
|
153
|
-
|
|
154
|
-
`${scrubSecrets(JSON.stringify({ type: "summary", ok: false, error: msg, prompt: effectivePrompt }))}\n`,
|
|
155
|
-
)
|
|
224
|
+
writeMachineLine(JSON.stringify(machineFailure(effectivePrompt, new Error(msg))))
|
|
156
225
|
else process.stderr.write(`${msg}\n`)
|
|
157
226
|
process.exit(1)
|
|
158
227
|
}
|
|
@@ -162,38 +231,61 @@ export async function handleExec(
|
|
|
162
231
|
// submit_result dari model (bila dipanggil) ikut verbatim — pipeline CI
|
|
163
232
|
// tak perlu menebak batas JSON dari prosa turn terakhir.
|
|
164
233
|
const submitted = getSubmittedResult()
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
234
|
+
// P11.1 — hasil verify TERAKHIR (final pasca-self-heal) untuk summary.
|
|
235
|
+
// Aditif & absen bila verify tak jalan: cermin lastVerify sesi, bukan
|
|
236
|
+
// otoritas baru. ok top-level dan exit code SENGAJA tak berubah.
|
|
237
|
+
const lastVerify = ctx.getLastVerifyResult()
|
|
238
|
+
const result = machineSummary(true, {
|
|
239
|
+
sessionId: ctx.sessionId,
|
|
169
240
|
model: ctx.modelRef.current,
|
|
170
241
|
prompt: effectivePrompt,
|
|
171
242
|
durationMs: Date.now() - t0,
|
|
172
243
|
steps: ctx.session.state.stepCount,
|
|
173
244
|
turns: ctx.session.state.turnCount,
|
|
174
245
|
usage: ue,
|
|
175
|
-
eventCount:
|
|
246
|
+
eventCount: streamed,
|
|
176
247
|
...(submitted ? { submitted: submitted.result } : {}),
|
|
177
|
-
|
|
248
|
+
...(lastVerify
|
|
249
|
+
? {
|
|
250
|
+
verify: {
|
|
251
|
+
active: true,
|
|
252
|
+
ok: lastVerify.ok,
|
|
253
|
+
command: lastVerify.command,
|
|
254
|
+
},
|
|
255
|
+
}
|
|
256
|
+
: {}),
|
|
257
|
+
})
|
|
178
258
|
// Event sudah di-stream sebagai JSONL di stdout; summary jadi baris
|
|
179
259
|
// terakhir di stdout juga (bukan stderr) supaya pipeline CI bisa
|
|
180
260
|
// membaca satu stream saja.
|
|
181
|
-
|
|
261
|
+
writeMachineLine(JSON.stringify(result))
|
|
182
262
|
} else {
|
|
183
263
|
process.stdout.write(
|
|
184
264
|
`\n[exec] done model=${ctx.modelRef.current} steps=${ctx.session.state.stepCount} tokens=${ue.totalTokens} ${Date.now() - t0}ms\n`,
|
|
185
265
|
)
|
|
186
266
|
}
|
|
187
|
-
|
|
267
|
+
unsubMachine()
|
|
268
|
+
unsubText()
|
|
269
|
+
await ctx.persistCurrent(ue)
|
|
270
|
+
// P2.2: histori yang tak durable = kegagalan jujur (bukan exit 0).
|
|
271
|
+
if (ctx.isWriterStale()) {
|
|
272
|
+
const msg = `[writer] stale writer, history NOT durable: ${ctx.writerStaleNote()}`
|
|
273
|
+
if (jsonMode)
|
|
274
|
+
writeMachineLine(JSON.stringify(machineFailure(effectivePrompt, new Error(msg))))
|
|
275
|
+
else process.stderr.write(`${msg}\n`)
|
|
276
|
+
await ctx.close()
|
|
277
|
+
process.exit(1)
|
|
278
|
+
}
|
|
188
279
|
await ctx.close()
|
|
189
280
|
process.exit(0)
|
|
190
281
|
} catch (e) {
|
|
191
|
-
if (jsonMode)
|
|
192
|
-
process.stdout.write(
|
|
193
|
-
`${scrubSecrets(JSON.stringify({ type: "summary", ok: false, error: formatError(e), prompt: effectivePrompt }))}\n`,
|
|
194
|
-
)
|
|
282
|
+
if (jsonMode) writeMachineLine(JSON.stringify(machineFailure(effectivePrompt, e)))
|
|
195
283
|
else process.stderr.write(`\n${formatError(e)}\n`)
|
|
196
|
-
|
|
284
|
+
unsubMachine()
|
|
285
|
+
unsubText()
|
|
286
|
+
try {
|
|
287
|
+
await ctx.persistCurrent(ctx.usage.getSession(ctx.modelRef.current))
|
|
288
|
+
} catch {}
|
|
197
289
|
await ctx.close()
|
|
198
290
|
process.exit(1)
|
|
199
291
|
}
|