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
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
// M15 — Production reroute: SATU jalur eksekusi turn, tanpa dual execution.
|
|
2
|
+
//
|
|
3
|
+
// Kenapa berkas ini ada (P1 M15): sampai M14, Runtime (M8–M14) terpasang rapi
|
|
4
|
+
// tapi TIDAK menjadi jalur produksi — `session.run()` dipanggil langsung oleh
|
|
5
|
+
// composition root, sub-agen, dan scheduler, sehingga lifecycle turn tidak punya
|
|
6
|
+
// authority yang jelas dan tidak pernah masuk jurnal durable M11. M15 tidak
|
|
7
|
+
// menulis lifecycle semantics baru: ia MEMINDAHKAN pencatatan lifecycle turn ke
|
|
8
|
+
// Kernel yang sudah ada (M8) melalui admission M13, sementaraSI eksekusi fisiknya
|
|
9
|
+
// tetap milik lapisan yang sudah terbukti benar (vendor MiniCore + tool executor).
|
|
10
|
+
//
|
|
11
|
+
// Aturan yang dikunci (jangan dilonggarkan tanpa ADR baru):
|
|
12
|
+
// - Mode `owned` = SATU jalur. Legacy `session.run()` dipanggil INSIDE runner
|
|
13
|
+
// yang sama, bukan dua kali. Tidak ada shadow path yang menjalankan efek luar
|
|
14
|
+
// dua kali; kalau admission ditolak, pekerjaan TIDAK dijalankan sama sekali
|
|
15
|
+
// (fail-closed), bukan "jalankan legacy saja".
|
|
16
|
+
// - Mode `constructed` = runtime dibangun (journal terbuka, evidence siap) tapi
|
|
17
|
+
// eksekusi tetap legacy. Itu Stage A/B, BUKAN shadow execution: nol efek
|
|
18
|
+
// tambahan, nol eksekusi ganda.
|
|
19
|
+
// - Mode `off` = tak ada runtime; jalur legacy persis seperti sebelum M15.
|
|
20
|
+
// - Pemetaan hasil (COMPLETED/FAILED/CANCELLED/TIMED_OUT/BUDGET_EXCEEDED) memakai
|
|
21
|
+
// taksonomi yang SUDAH ADA (AgentError.kind). Tidak ada taksonomi baru; error
|
|
22
|
+
// asli selalu dilempar ulang apa adanya sehinggaatribusi error tak berubah.
|
|
23
|
+
// - Setiap pekerjaan yang runtime pedati didaftarkan ke `track()` sehingga
|
|
24
|
+
// drain M14 benar-benar menutup eksekusi milik runtime (utang M14 resolved).
|
|
25
|
+
// - Tanpa efek samping di modul ini: tidak ada spawn, tidak ada network, tidak
|
|
26
|
+
// ada akses langsung ke TaskStore/scheduler/UI/CLI.
|
|
27
|
+
|
|
28
|
+
import { AgentError } from "#minicore/core/errors.ts"
|
|
29
|
+
import type { RuntimeComposition } from "./composition.ts"
|
|
30
|
+
import { dispatchIntentId } from "./composition.ts"
|
|
31
|
+
import { createDispatchId, type DispatchRecord } from "./dispatch.ts"
|
|
32
|
+
import {
|
|
33
|
+
type CancelReason,
|
|
34
|
+
type ExecutionTerminalState,
|
|
35
|
+
isExecutionTerminalState,
|
|
36
|
+
} from "./execution-kernel.ts"
|
|
37
|
+
import { deriveRuntimeView } from "./runtime-status.ts"
|
|
38
|
+
import { admitTaskWork } from "./runtime-task-admission.ts"
|
|
39
|
+
|
|
40
|
+
/** Token CLI untuk mode produksi runtime (deterministik per invokasi). */
|
|
41
|
+
export const RUNTIME_MODE_FLAG = "--runtime"
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Tiga tahap, bukan boolean. Alasannya: M14 membuktikan "dapat dibangun" dan M15
|
|
45
|
+
* membuktikan "dapat menjadi pemilik" — dua klaim berbeda, dan mencampurkannya
|
|
46
|
+
* dalam satu flag_boolean membuat rollback dan audit kabur.
|
|
47
|
+
* - `off` : Stage A — tak ada runtime (default, jalur legacy utuh).
|
|
48
|
+
* - `constructed` : Stage B — runtime + journal dibangun, eksekusi masih legacy.
|
|
49
|
+
* - `owned` : Stage C/D — admission wajib lewat M13 + Kernel.
|
|
50
|
+
*/
|
|
51
|
+
export const RUNTIME_MODES = ["off", "constructed", "owned"] as const
|
|
52
|
+
export type RuntimeProductionMode = (typeof RUNTIME_MODES)[number]
|
|
53
|
+
|
|
54
|
+
export interface RuntimeModeResolution {
|
|
55
|
+
readonly mode: RuntimeProductionMode
|
|
56
|
+
/** Pesan untuk operator bila input tak dikenal (fail-closed ke `off`). */
|
|
57
|
+
readonly warning?: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Parse mode dari argv. Token tak dikenal / tak lengkap = `off` + warning:
|
|
62
|
+
* gate yang gagal parse tak boleh jatuh ke mode yang lebih agresif.
|
|
63
|
+
*/
|
|
64
|
+
export function resolveRuntimeMode(argv: readonly string[]): RuntimeModeResolution {
|
|
65
|
+
for (let i = 0; i < argv.length; i++) {
|
|
66
|
+
const token = argv[i]
|
|
67
|
+
if (token === "--") break
|
|
68
|
+
if (token !== RUNTIME_MODE_FLAG) continue
|
|
69
|
+
const value = argv[i + 1]
|
|
70
|
+
if (value !== undefined && (RUNTIME_MODES as readonly string[]).includes(value))
|
|
71
|
+
return { mode: value as RuntimeProductionMode }
|
|
72
|
+
return {
|
|
73
|
+
mode: "off",
|
|
74
|
+
warning: `invalid ${RUNTIME_MODE_FLAG} value "${value ?? ""}" — expected one of ${RUNTIME_MODES.join(
|
|
75
|
+
"|",
|
|
76
|
+
)}; continuing with ${"off"}`,
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { mode: "off" }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Bentuk nilai untuk option composition root (dipakai CLI + test). */
|
|
83
|
+
export function runtimeModeFor(value: RuntimeProductionMode | undefined): RuntimeProductionMode {
|
|
84
|
+
return (RUNTIME_MODES as readonly string[]).includes(value ?? "")
|
|
85
|
+
? (value as RuntimeProductionMode)
|
|
86
|
+
: "off"
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface TurnExecutionRequest {
|
|
90
|
+
readonly kind: "turn" | "task" | "child" | "background"
|
|
91
|
+
/** Siapa yang meminta (scheduler / user-session / delegate …) — provenance. */
|
|
92
|
+
readonly schedulerSource: string
|
|
93
|
+
/** Authority pemanggil atas eksekusi ini (dinyatakan, bukan ditebak). */
|
|
94
|
+
readonly authorityHeld: boolean
|
|
95
|
+
readonly provenance: { requestedBy: string; reason: string }
|
|
96
|
+
readonly taskId?: string
|
|
97
|
+
/**
|
|
98
|
+
* Owner lifecycle eksekusi ini. Default: `taskId` → `provenance.requestedBy`
|
|
99
|
+
* (rantai fallback M13). Dipakai setup.ts untuk turn user (sessionId), supaya
|
|
100
|
+
* "siapa pemilik turn ini" menjawab dari identitas, bukan dari nama sumber.
|
|
101
|
+
*/
|
|
102
|
+
readonly ownerId?: string
|
|
103
|
+
readonly parentExecutionId?: string
|
|
104
|
+
readonly lineageRootId?: string
|
|
105
|
+
readonly budget?: number | null
|
|
106
|
+
readonly deadlineAt?: number | null
|
|
107
|
+
readonly attempt?: number
|
|
108
|
+
readonly generation?: number
|
|
109
|
+
/** Dispatch id eksplisit (idempotensi lintas retry pemanggil). */
|
|
110
|
+
readonly dispatchId?: string
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Klasifikasi HASIL ke terminal Kernel memakai taksonomi yang sudah ada.
|
|
115
|
+
*
|
|
116
|
+
* [Stage D decision] `max_steps_exceeded` → `RESOURCE_EXCEEDED` (bukan FAILED,
|
|
117
|
+
* bukan BUDGET_EXCEEDED). Alasannya domain, bukan estetika taksonomi: cap langkah
|
|
118
|
+
* adalah kuota kerja komputasional (resource bound), bukan kegagalan turn dan
|
|
119
|
+
* bukan pagu uang — kernelling sudah punya terminal persis untuk itu dan tiga
|
|
120
|
+
* transisi legalnya (ADMITTED/RUNNING/CANCELLING → RESOURCE_EXCEEDED) menutupi
|
|
121
|
+
* semua keadaan turn saat cap tercapai. Kompatibilitas: keputusan ini hanya
|
|
122
|
+
* memengaruhi record kernel di mode `owned`; jalur legacy tak punya kernel state,
|
|
123
|
+
* dan klasifikasi presentasi (`noteRunSettled`) tak berubah. Reason asli tetap
|
|
124
|
+
* membawa kind vendor (`turn-max-steps-exceeded`) supaya tak ada info hilang.
|
|
125
|
+
*/
|
|
126
|
+
export function classifyTurnFailure(error: unknown): {
|
|
127
|
+
terminal: ExecutionTerminalState
|
|
128
|
+
reason: string
|
|
129
|
+
/**
|
|
130
|
+
* Cancel-kind M9 untuk dua langkah settle (RUNNING→CANCELLING→terminal).
|
|
131
|
+
* `null` = terminal langsung legal (COMPLETED/FAILED/RESOURCE_EXCEEDED).
|
|
132
|
+
*/
|
|
133
|
+
cancelReason: CancelReason | null
|
|
134
|
+
} {
|
|
135
|
+
const kind =
|
|
136
|
+
error instanceof AgentError
|
|
137
|
+
? error.kind
|
|
138
|
+
: typeof (error as { kind?: unknown } | null)?.kind === "string"
|
|
139
|
+
? String((error as { kind: string }).kind)
|
|
140
|
+
: null
|
|
141
|
+
switch (kind) {
|
|
142
|
+
case "aborted":
|
|
143
|
+
return { terminal: "CANCELLED", reason: "turn-aborted", cancelReason: "user" }
|
|
144
|
+
case "timeout":
|
|
145
|
+
return { terminal: "TIMED_OUT", reason: "turn-timeout", cancelReason: "timeout" }
|
|
146
|
+
case "budget_exceeded":
|
|
147
|
+
return { terminal: "BUDGET_EXCEEDED", reason: "turn-budget-exceeded", cancelReason: "budget" }
|
|
148
|
+
case "max_steps_exceeded":
|
|
149
|
+
return {
|
|
150
|
+
terminal: "RESOURCE_EXCEEDED",
|
|
151
|
+
reason: "turn-max-steps-exceeded",
|
|
152
|
+
cancelReason: null,
|
|
153
|
+
}
|
|
154
|
+
default:
|
|
155
|
+
return {
|
|
156
|
+
terminal: "FAILED",
|
|
157
|
+
reason: `turn-failed${kind ? `:${kind}` : ""}`,
|
|
158
|
+
cancelReason: null,
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export interface TurnExecutionOutcome<T> {
|
|
164
|
+
/** `legacy` = jalur lama (mode off/constructed); `runtime` = M15 reroute. */
|
|
165
|
+
readonly via: "legacy" | "runtime"
|
|
166
|
+
readonly executionId: string | null
|
|
167
|
+
readonly dispatchId: string
|
|
168
|
+
/** State Kernel terakhir (terminal untuk mode runtime). */
|
|
169
|
+
readonly state: string
|
|
170
|
+
readonly result?: T
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export interface RuntimeNotAdmittedError extends Error {
|
|
174
|
+
readonly code: "RUNTIME_NOT_OWNED" | "RUNTIME_NOT_ADMITTED"
|
|
175
|
+
readonly dispatch: DispatchRecord | null
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function notAdmitted(
|
|
179
|
+
code: RuntimeNotAdmittedError["code"],
|
|
180
|
+
message: string,
|
|
181
|
+
dispatch: DispatchRecord | null = null,
|
|
182
|
+
): RuntimeNotAdmittedError {
|
|
183
|
+
const err = new Error(message)
|
|
184
|
+
return Object.assign(err, { code, dispatch }) satisfies RuntimeNotAdmittedError
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// [P1 M16] `isRuntimeNotAdmitted` (type guard) DIHAPUS: nol pemakai di repo ini,
|
|
188
|
+
// dan setiap pemanggil dapat memeriksa `error.code` secara langsung. Satu
|
|
189
|
+
// predikat yang tak dipakai adalah dua definisi "apa itu RuntimeNotAdmitted"
|
|
190
|
+
// menunggu berbeda — lebih murah menghapus sekarang daripada mewariskannya.
|
|
191
|
+
|
|
192
|
+
export interface ProductionExecutionRunnerMetrics {
|
|
193
|
+
readonly legacyRuns: number
|
|
194
|
+
readonly runtimeRuns: number
|
|
195
|
+
readonly admitted: number
|
|
196
|
+
readonly rejected: number
|
|
197
|
+
/** Bukti dispatch gagal ditulis ke jurnal → dedupe lintas-restart konservatif. */
|
|
198
|
+
readonly intentWriteFailures: number
|
|
199
|
+
readonly terminals: Readonly<Record<string, number>>
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface ProductionExecutionRunner {
|
|
203
|
+
readonly mode: RuntimeProductionMode
|
|
204
|
+
readonly owns: boolean
|
|
205
|
+
/**
|
|
206
|
+
* Jalankan SATU pekerjaan turn. Dalam mode `owned`, pekerjaan WAJIB lolos
|
|
207
|
+
* admission M13 lebih dulu; kalau tidak, kerja TIDAK dijalankan (fail-closed).
|
|
208
|
+
*/
|
|
209
|
+
run<T>(
|
|
210
|
+
request: TurnExecutionRequest,
|
|
211
|
+
work: () => Promise<T>,
|
|
212
|
+
opts?: { signal?: AbortSignal },
|
|
213
|
+
): Promise<TurnExecutionOutcome<T>>
|
|
214
|
+
/**
|
|
215
|
+
* Execution parent yang sedang berjalan di runner ini (kind `turn`).
|
|
216
|
+
* Dipakai untuk lineage child (M7): anak HARUS punya satu parent, bukan
|
|
217
|
+
* Akar sendiri. `null` = tak ada turn parent (mis. Autonomous langsung).
|
|
218
|
+
*/
|
|
219
|
+
parentExecutionId(): string | null
|
|
220
|
+
metrics(): ProductionExecutionRunnerMetrics
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function createProductionExecutionRunner(deps: {
|
|
224
|
+
readonly mode: RuntimeProductionMode
|
|
225
|
+
/** Null sah untuk mode `off`/`constructed`; WAJIB ada untuk mode `owned`. */
|
|
226
|
+
readonly runtime: RuntimeComposition | null
|
|
227
|
+
}): ProductionExecutionRunner {
|
|
228
|
+
const mode = runtimeModeFor(deps.mode)
|
|
229
|
+
const owns = mode === "owned"
|
|
230
|
+
const m = {
|
|
231
|
+
legacyRuns: 0,
|
|
232
|
+
runtimeRuns: 0,
|
|
233
|
+
admitted: 0,
|
|
234
|
+
rejected: 0,
|
|
235
|
+
intentWriteFailures: 0,
|
|
236
|
+
terminals: {} as Record<string, number>,
|
|
237
|
+
}
|
|
238
|
+
const currentParentExecutionId: { value: string | null } = { value: null }
|
|
239
|
+
const countTerminal = (terminal: string) => {
|
|
240
|
+
m.terminals[terminal] = (m.terminals[terminal] ?? 0) + 1
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
mode,
|
|
245
|
+
owns,
|
|
246
|
+
|
|
247
|
+
async run<T>(
|
|
248
|
+
request: TurnExecutionRequest,
|
|
249
|
+
work: () => Promise<T>,
|
|
250
|
+
opts?: { signal?: AbortSignal },
|
|
251
|
+
): Promise<TurnExecutionOutcome<T>> {
|
|
252
|
+
const dispatchId = request.dispatchId ?? createDispatchId()
|
|
253
|
+
|
|
254
|
+
if (!owns) {
|
|
255
|
+
// Stage A/B: jalur legacy, tanpa menyentuh runtime. Nol efek tambahan.
|
|
256
|
+
m.legacyRuns++
|
|
257
|
+
const result = await work()
|
|
258
|
+
return { via: "legacy", executionId: null, dispatchId, state: "LEGACY", result }
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const runtime = deps.runtime
|
|
262
|
+
if (!runtime)
|
|
263
|
+
throw notAdmitted(
|
|
264
|
+
"RUNTIME_NOT_OWNED",
|
|
265
|
+
"runtime mode is 'owned' but no Runtime composition is available (fail-closed)",
|
|
266
|
+
)
|
|
267
|
+
if (!runtime.isAdmissionOpen())
|
|
268
|
+
throw notAdmitted(
|
|
269
|
+
"RUNTIME_NOT_OWNED",
|
|
270
|
+
"runtime admission is closed (shutdown in progress) — refusing to execute",
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
// [P13.3] GERBANG ADMISSION PER JENIS KERJA (P13.1 `admitDurableWork`).
|
|
274
|
+
//
|
|
275
|
+
// M13 (dispatch di bawah) menjawab "apakah INI sah?" — authority, budget,
|
|
276
|
+
// deadline, lineage. Gerbang ini menjawab pertanyaan YANG BERBEDA:
|
|
277
|
+
// "apakah runtime ini BOLEH menerima kerja jenis INI sekarang?" — kapabilitas
|
|
278
|
+
// runtime (jurnal durable, TaskStore, scheduler) dan bukti readiness-nya.
|
|
279
|
+
//
|
|
280
|
+
// Keduanya wajib dan tak saling menggantikan: M13 tak tahu kapabilitas
|
|
281
|
+
// runtime, dan kapabilitas tak tahu authority. Gerbang ini TIDAK
|
|
282
|
+
// menggantikan M13 — ia menolak lebih awal, sebelum intent dispatch ditulis
|
|
283
|
+
// ke jurnal, jadi penolakan tak meninggalkan jejak eksekusi.
|
|
284
|
+
const admission = admitTaskWork(deriveRuntimeView(runtime.statusFacts()), request.kind)
|
|
285
|
+
if (admission.admission === "REFUSE") {
|
|
286
|
+
m.rejected++
|
|
287
|
+
throw notAdmitted(
|
|
288
|
+
"RUNTIME_NOT_ADMITTED",
|
|
289
|
+
`work kind ${request.kind} ditolak admission runtime: ${admission.reason}`,
|
|
290
|
+
)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Admission: SATU-SATUNYA jalan masuk (M13 → kernel). Dispatch ditolak ⇒
|
|
294
|
+
// TIDAK ADA legacy fallback: dua jalur untuk satu operasi = dual execution.
|
|
295
|
+
//
|
|
296
|
+
// Lineage (M7): anak mewarisi root parent — root korelasi IMMUTABLE, jadi
|
|
297
|
+
// anak tak pernah menjadi akar baru. Owner: eksplisit → pemanggil →
|
|
298
|
+
// provenance (rantai fallback yang sudah ada di M13, bukan aturan baru).
|
|
299
|
+
const parent = request.parentExecutionId
|
|
300
|
+
const lineageRootId = request.lineageRootId ?? parent
|
|
301
|
+
const ownerId = request.ownerId ?? request.taskId ?? request.provenance.requestedBy
|
|
302
|
+
const record = runtime.dispatch({
|
|
303
|
+
dispatchId,
|
|
304
|
+
schedulerSource: request.schedulerSource,
|
|
305
|
+
authorityHeld: request.authorityHeld,
|
|
306
|
+
ownerId,
|
|
307
|
+
...(request.taskId ? { taskId: request.taskId } : {}),
|
|
308
|
+
...(parent ? { parentExecutionId: parent } : {}),
|
|
309
|
+
...(lineageRootId ? { lineageRootId } : {}),
|
|
310
|
+
kind: request.kind,
|
|
311
|
+
...(request.budget !== undefined ? { budget: request.budget } : {}),
|
|
312
|
+
...(request.deadlineAt !== undefined ? { deadlineAt: request.deadlineAt } : {}),
|
|
313
|
+
...(request.attempt !== undefined ? { attempt: request.attempt } : {}),
|
|
314
|
+
...(request.generation !== undefined ? { generation: request.generation } : {}),
|
|
315
|
+
provenance: request.provenance,
|
|
316
|
+
})
|
|
317
|
+
if (record.state !== "ADMITTED" || !record.executionId) {
|
|
318
|
+
m.rejected++
|
|
319
|
+
throw notAdmitted(
|
|
320
|
+
"RUNTIME_NOT_ADMITTED",
|
|
321
|
+
`dispatch ${dispatchId} not admitted (${record.state}: ${record.reason})`,
|
|
322
|
+
record,
|
|
323
|
+
)
|
|
324
|
+
}
|
|
325
|
+
const executionId = record.executionId
|
|
326
|
+
m.admitted++
|
|
327
|
+
// Parent lineage (M7): turn anak WAJIB menunjuk turn ini sebagai
|
|
328
|
+
// parentExecutionId, bukan menjadi akar sendiri.
|
|
329
|
+
if (request.kind === "turn") currentParentExecutionId.value = executionId
|
|
330
|
+
|
|
331
|
+
// Bukti durable: intent dispatch ditulis SETELAH admission sukses dan
|
|
332
|
+
// DITUNGGU, jadi redelivery lintas proses bisa dibuktikan (bukan
|
|
333
|
+
// ditebak). Kegagalan menulis TIDAK membatalkan turn yang sudah admitted:
|
|
334
|
+
// eksekusi tak boleh berhenti karena jurnal — angkanya dicatat supaya
|
|
335
|
+
// operator tahu dedupe lintas-restart menjadi konservatif.
|
|
336
|
+
if (runtime.journal) {
|
|
337
|
+
try {
|
|
338
|
+
const intent = await runtime.journal.noteIntent({
|
|
339
|
+
intentId: dispatchIntentId(dispatchId),
|
|
340
|
+
executionId,
|
|
341
|
+
...(parent ? { parentExecutionId: parent } : {}),
|
|
342
|
+
lineageRootId: runtime.kernel.get(executionId)?.rootExecutionId ?? executionId,
|
|
343
|
+
executionVersion: runtime.kernel.get(executionId)?.version ?? 0,
|
|
344
|
+
reason: `dispatch-admission:${request.schedulerSource}`,
|
|
345
|
+
source: "host",
|
|
346
|
+
})
|
|
347
|
+
if (intent.status === "error") m.intentWriteFailures++
|
|
348
|
+
} catch {
|
|
349
|
+
m.intentWriteFailures++
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// RUNNING commit: lifecycle authority tetap Kernel (M8).
|
|
354
|
+
const running = runtime.kernel.requestTransition({
|
|
355
|
+
executionId,
|
|
356
|
+
to: "RUNNING",
|
|
357
|
+
reason: "turn-start",
|
|
358
|
+
source: "host",
|
|
359
|
+
})
|
|
360
|
+
if (!running.committed) {
|
|
361
|
+
m.rejected++
|
|
362
|
+
throw notAdmitted(
|
|
363
|
+
"RUNTIME_NOT_ADMITTED",
|
|
364
|
+
`execution ${executionId} refused RUNNING (${running.outcome})`,
|
|
365
|
+
record,
|
|
366
|
+
)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// track() menutup utang M14: pekerjaan yang masih berjalan saat shutdown
|
|
370
|
+
// HARUS terdaftar, supaya flush jurnal tak mendahului event terakhirnya.
|
|
371
|
+
let untrack: (() => void) | null = null
|
|
372
|
+
let settled: TurnExecutionOutcome<T> | null = null
|
|
373
|
+
try {
|
|
374
|
+
const promise = (async () => {
|
|
375
|
+
try {
|
|
376
|
+
const result = await work()
|
|
377
|
+
countTerminal("COMPLETED")
|
|
378
|
+
const done = runtime.kernel.requestTransition({
|
|
379
|
+
executionId,
|
|
380
|
+
to: "COMPLETED",
|
|
381
|
+
reason: "turn-complete",
|
|
382
|
+
source: "host",
|
|
383
|
+
})
|
|
384
|
+
settled = {
|
|
385
|
+
via: "runtime",
|
|
386
|
+
executionId,
|
|
387
|
+
dispatchId,
|
|
388
|
+
state: done.committed ? "COMPLETED" : String(done.outcome),
|
|
389
|
+
result,
|
|
390
|
+
}
|
|
391
|
+
return result
|
|
392
|
+
} catch (error) {
|
|
393
|
+
const { terminal, reason, cancelReason } = classifyTurnFailure(error)
|
|
394
|
+
countTerminal(terminal)
|
|
395
|
+
// Cancel-family MENUNTUT dua langkah M8 (RUNNING→CANCELLING→terminal):
|
|
396
|
+
// commit langsung RUNNING→TIMED_OUT/CANCELLED adalah ilegal dan akan
|
|
397
|
+
// ditolak kernel. Mekaniknya milik Supervisor (M9) — requestCancel
|
|
398
|
+
// lalu settleCancellation dengan tabel pemetaan yang SAMA, jadi tak
|
|
399
|
+
// ada mapping kedua di sini.
|
|
400
|
+
let finalState: string
|
|
401
|
+
if (cancelReason) {
|
|
402
|
+
const cancel = runtime.supervisor.requestCancel(executionId, cancelReason)
|
|
403
|
+
if (cancel.requested) {
|
|
404
|
+
const done = runtime.supervisor.settleCancellation(executionId, cancelReason)
|
|
405
|
+
finalState = done.requested
|
|
406
|
+
? done.terminal
|
|
407
|
+
: String(runtime.kernel.get(executionId)?.state ?? terminal)
|
|
408
|
+
} else {
|
|
409
|
+
// Sudah terminal lewat jalur lain (race jujur): baca state aktual.
|
|
410
|
+
finalState = String(runtime.kernel.get(executionId)?.state ?? terminal)
|
|
411
|
+
}
|
|
412
|
+
} else {
|
|
413
|
+
const direct = runtime.kernel.requestTransition({
|
|
414
|
+
executionId,
|
|
415
|
+
to: terminal,
|
|
416
|
+
reason,
|
|
417
|
+
source: "host",
|
|
418
|
+
...(opts?.signal?.aborted
|
|
419
|
+
? {
|
|
420
|
+
causality:
|
|
421
|
+
opts.signal.reason instanceof Error ? opts.signal.reason.name : undefined,
|
|
422
|
+
}
|
|
423
|
+
: {}),
|
|
424
|
+
})
|
|
425
|
+
finalState = direct.committed
|
|
426
|
+
? terminal
|
|
427
|
+
: String(runtime.kernel.get(executionId)?.state ?? terminal)
|
|
428
|
+
}
|
|
429
|
+
settled = {
|
|
430
|
+
via: "runtime",
|
|
431
|
+
executionId,
|
|
432
|
+
dispatchId,
|
|
433
|
+
state: finalState,
|
|
434
|
+
}
|
|
435
|
+
// Error asli diteruskan apa adanya (atribusi error tak berubah).
|
|
436
|
+
throw error
|
|
437
|
+
}
|
|
438
|
+
})()
|
|
439
|
+
untrack = runtime.track(executionId, promise)
|
|
440
|
+
m.runtimeRuns++
|
|
441
|
+
const result = await promise
|
|
442
|
+
return settled ?? { via: "runtime", executionId, dispatchId, state: "COMPLETED", result }
|
|
443
|
+
} finally {
|
|
444
|
+
untrack?.()
|
|
445
|
+
// Bila promise ditolak di luar jalur `work` (mis. track/append gagal),
|
|
446
|
+
// eksekusi TIDAK boleh terminal palsu: biarkan non-terminal dan
|
|
447
|
+
// biarkan M12 yang menafsirkan dari bukti durable.
|
|
448
|
+
if (settled === null && !runtime.isClosed()) {
|
|
449
|
+
const state = runtime.kernel.get(executionId)?.state
|
|
450
|
+
if (!isExecutionTerminalState(state)) {
|
|
451
|
+
countTerminal("UNSETTLED")
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
},
|
|
456
|
+
|
|
457
|
+
parentExecutionId: () => currentParentExecutionId.value,
|
|
458
|
+
|
|
459
|
+
metrics: () => ({
|
|
460
|
+
legacyRuns: m.legacyRuns,
|
|
461
|
+
runtimeRuns: m.runtimeRuns,
|
|
462
|
+
admitted: m.admitted,
|
|
463
|
+
rejected: m.rejected,
|
|
464
|
+
intentWriteFailures: m.intentWriteFailures,
|
|
465
|
+
terminals: Object.freeze({ ...m.terminals }),
|
|
466
|
+
}),
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Pemeriksaan startup untuk runtime yang punya jurnal (M15 §19): validasi
|
|
472
|
+
* integritas + tafsir M12 untuk eksekusi yang berakhir non-terminal.
|
|
473
|
+
*
|
|
474
|
+
* Sengaja TIDAK mendispatch apa pun: M12 hanya merencanakan, dan redispatch tetap
|
|
475
|
+
* tunduk pada authority/budget/deadline/idempotensi. Fungsi ini jadi bukti,
|
|
476
|
+
* bukan aksi.
|
|
477
|
+
*/
|
|
478
|
+
export interface StartupRecoveryReport {
|
|
479
|
+
readonly integrity: { checked: number; mismatched: readonly number[] }
|
|
480
|
+
readonly frontier: number
|
|
481
|
+
readonly recoverable: readonly {
|
|
482
|
+
readonly executionId: string
|
|
483
|
+
readonly lastState: string
|
|
484
|
+
readonly interpretation: string
|
|
485
|
+
readonly nextAction: string
|
|
486
|
+
readonly requiresVerifier: boolean
|
|
487
|
+
readonly redispatchPlan: unknown
|
|
488
|
+
}[]
|
|
489
|
+
readonly corrupt: boolean
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
export function inspectStartupRecovery(
|
|
493
|
+
runtime: RuntimeComposition,
|
|
494
|
+
ctx: {
|
|
495
|
+
readonly authorityHeld?: boolean
|
|
496
|
+
readonly budgetRemaining?: number | null
|
|
497
|
+
readonly deadlineRemainingMs?: number | null
|
|
498
|
+
} = {},
|
|
499
|
+
): StartupRecoveryReport {
|
|
500
|
+
const journal = runtime.journal
|
|
501
|
+
if (!journal)
|
|
502
|
+
return {
|
|
503
|
+
integrity: { checked: 0, mismatched: [] },
|
|
504
|
+
frontier: 0,
|
|
505
|
+
recoverable: [],
|
|
506
|
+
corrupt: false,
|
|
507
|
+
}
|
|
508
|
+
const integrity = journal.integrityCheck()
|
|
509
|
+
const records = journal.readAll()
|
|
510
|
+
const frontier = records.reduce((max, r) => Math.max(max, r.journalSequence), 0)
|
|
511
|
+
const byExecution = new Map<string, string>()
|
|
512
|
+
for (const record of records) {
|
|
513
|
+
if (record.kind !== "event") continue
|
|
514
|
+
if (record.state) byExecution.set(record.executionId, record.state)
|
|
515
|
+
}
|
|
516
|
+
const recoverable: {
|
|
517
|
+
executionId: string
|
|
518
|
+
lastState: string
|
|
519
|
+
interpretation: string
|
|
520
|
+
nextAction: string
|
|
521
|
+
requiresVerifier: boolean
|
|
522
|
+
redispatchPlan: unknown
|
|
523
|
+
}[] = []
|
|
524
|
+
for (const [executionId, lastState] of byExecution) {
|
|
525
|
+
if (isExecutionTerminalState(lastState)) continue
|
|
526
|
+
const result = runtime.recover(executionId, {
|
|
527
|
+
authorityHeld: ctx.authorityHeld ?? false,
|
|
528
|
+
budgetRemaining: ctx.budgetRemaining ?? null,
|
|
529
|
+
deadlineRemainingMs: ctx.deadlineRemainingMs ?? null,
|
|
530
|
+
idempotent: true,
|
|
531
|
+
dedupeKeyPresent: false,
|
|
532
|
+
dedupeCheckPass: false,
|
|
533
|
+
verifierAvailable: false,
|
|
534
|
+
journalCorrupt: integrity.mismatched.length > 0,
|
|
535
|
+
})
|
|
536
|
+
recoverable.push({
|
|
537
|
+
executionId,
|
|
538
|
+
lastState,
|
|
539
|
+
interpretation: result.interpretation,
|
|
540
|
+
nextAction: result.nextAction,
|
|
541
|
+
requiresVerifier: result.requiresVerifier,
|
|
542
|
+
redispatchPlan: result.redispatchPlan,
|
|
543
|
+
})
|
|
544
|
+
}
|
|
545
|
+
return {
|
|
546
|
+
integrity,
|
|
547
|
+
frontier,
|
|
548
|
+
recoverable,
|
|
549
|
+
corrupt: integrity.mismatched.length > 0,
|
|
550
|
+
}
|
|
551
|
+
}
|