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
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
// M8 — Execution Kernel: SATU-SATUNYA authoritative lifecycle owner.
|
|
2
|
+
//
|
|
3
|
+
// Kenapa berkas ini ada (P1 ADR-001/010, M8): sebelum M8, lifecycle execution
|
|
4
|
+
// implisit/tersebar — vendor Session memiliki turn loop, Host memiliki guard
|
|
5
|
+
// host-level, Registry menahan snapshot observasi — tanpa satu otoritas yang
|
|
6
|
+
// memvalidasi + meng-commit transisi lifecycle. Modul ini adalah otoritas itu.
|
|
7
|
+
//
|
|
8
|
+
// Prinsip yang dikunci (jangan dilonggarkan tanpa ADR baru):
|
|
9
|
+
// Kernel decides. Host requests. Registry observes. Backend reports. Agent interprets.
|
|
10
|
+
// - SATU stream transisi serial per Execution: semua mutasi melewati SATU fungsi
|
|
11
|
+
// commit() sinkron (JS run-to-completion = serialisasi; tanpa mutex/thread).
|
|
12
|
+
// Commit point = mutasi state atomik di sini — BUKAN network/callback/event/
|
|
13
|
+
// registry/backend/scheduler arrival. First committed terminal wins.
|
|
14
|
+
// - Terminal sticky absolut: sesudah terminal, SEMUA request terminal berikutnya
|
|
15
|
+
// = duplicate_terminal_ignored (sama) / late_result_ignored (beda); SEMUA
|
|
16
|
+
// request non-terminal = rejected. Tak ada COMPLETED→CANCELLED (atau sebaliknya).
|
|
17
|
+
// - Reason + provenance WAJIB (non-empty reason, source label, timestamp, order).
|
|
18
|
+
// Timeout/budget/authority/resource TAK PERNAH collapse ke FAILED generik.
|
|
19
|
+
// - WAITING→COMPLETED langsung DITOLAK (completion harus lewat RESUMED→RUNNING;
|
|
20
|
+
// RESUMED transient yang di-commit, bukan dilewat). RESUMED→CANCELLING
|
|
21
|
+
// diizinkan (cancel harus servable dari semua non-terminal).
|
|
22
|
+
// - BUKAN: supervisor/retry (M9), persistence (M11), recovery (M12), scheduler
|
|
23
|
+
// bridge (M13), TUI/CLI/SQLite/journal/policy/agent-reasoning. Tak ada import
|
|
24
|
+
// selain M1 identity (+ node:crypto untuk id bila caller tak memberi).
|
|
25
|
+
// - Vendor Session TAK DISENTUH (engine internal, bukan authority baru);
|
|
26
|
+
// Host/Registry/Backend TAK DIUBAH di sini (integrasi = requestTransition
|
|
27
|
+
// seam, wiring milik M9/M14/M15). Live process resume BUKAN P1. //
|
|
28
|
+
// [P1 M16] Id yang di-mint memakai allocator M1 (anti-tabrakan + metrik), bukan
|
|
29
|
+
// randomUUID inline: dua tempat men-*cetak* id eksekusi = dua authority. Jalur ini,
|
|
30
|
+
// yang paling sering dipakai produksi (M15 admission), kini ter-meterik dan
|
|
31
|
+
// collision-safe seperti yang lain.
|
|
32
|
+
|
|
33
|
+
import { allocateUniqueExecutionId, type ExecutionKind, isExecutionId } from "./execution-id.ts"
|
|
34
|
+
|
|
35
|
+
/** Non-terminal lifecycle states (otoritas Kernel). */
|
|
36
|
+
export type ExecutionLifecycleState =
|
|
37
|
+
| "CREATED"
|
|
38
|
+
| "ADMITTED"
|
|
39
|
+
| "RUNNING"
|
|
40
|
+
| "WAITING"
|
|
41
|
+
| "RESUMED"
|
|
42
|
+
| "CANCELLING"
|
|
43
|
+
| "TERMINATING"
|
|
44
|
+
|
|
45
|
+
/** Terminal lifecycle states — absorbing, immutable pasca-commit. */
|
|
46
|
+
export type ExecutionTerminalState =
|
|
47
|
+
| "COMPLETED"
|
|
48
|
+
| "FAILED"
|
|
49
|
+
| "CANCELLED"
|
|
50
|
+
| "TIMED_OUT"
|
|
51
|
+
| "BUDGET_EXCEEDED"
|
|
52
|
+
| "AUTHORITY_LOST"
|
|
53
|
+
| "RESOURCE_EXCEEDED"
|
|
54
|
+
|
|
55
|
+
export type ExecutionState = ExecutionLifecycleState | ExecutionTerminalState
|
|
56
|
+
|
|
57
|
+
const TERMINALS: ReadonlySet<string> = new Set([
|
|
58
|
+
"COMPLETED",
|
|
59
|
+
"FAILED",
|
|
60
|
+
"CANCELLED",
|
|
61
|
+
"TIMED_OUT",
|
|
62
|
+
"BUDGET_EXCEEDED",
|
|
63
|
+
"AUTHORITY_LOST",
|
|
64
|
+
"RESOURCE_EXCEEDED",
|
|
65
|
+
])
|
|
66
|
+
|
|
67
|
+
export function isExecutionTerminalState(state: unknown): state is ExecutionTerminalState {
|
|
68
|
+
return typeof state === "string" && TERMINALS.has(state)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Tabel transisi eksplisit (otoritatif). BUKAN dokumentasi — validator
|
|
73
|
+
* menggunakannya sebagai satu-satunya kebenaran legalitas.
|
|
74
|
+
* Penyimpangan sadar dari draft: WAITING→COMPLETED langsung DITOLAK (harus
|
|
75
|
+
* lewat RESUMED→RUNNING); RESUMED→CANCELLING DIIZINKAN (cancel servable dari
|
|
76
|
+
* semua non-terminal). Keduanya didokumentasikan + diuji.
|
|
77
|
+
*/
|
|
78
|
+
const TRANSITIONS: Record<ExecutionLifecycleState, ReadonlySet<ExecutionState>> = {
|
|
79
|
+
CREATED: new Set(["ADMITTED", "CANCELLING"]),
|
|
80
|
+
ADMITTED: new Set(["RUNNING", "CANCELLING", "RESOURCE_EXCEEDED"]),
|
|
81
|
+
RUNNING: new Set([
|
|
82
|
+
"WAITING",
|
|
83
|
+
"CANCELLING",
|
|
84
|
+
"TERMINATING",
|
|
85
|
+
"COMPLETED",
|
|
86
|
+
"FAILED",
|
|
87
|
+
"RESOURCE_EXCEEDED",
|
|
88
|
+
]),
|
|
89
|
+
WAITING: new Set(["RESUMED", "CANCELLING", "RESOURCE_EXCEEDED"]),
|
|
90
|
+
RESUMED: new Set(["RUNNING", "CANCELLING", "RESOURCE_EXCEEDED"]),
|
|
91
|
+
CANCELLING: new Set([
|
|
92
|
+
"CANCELLED",
|
|
93
|
+
"TIMED_OUT",
|
|
94
|
+
"BUDGET_EXCEEDED",
|
|
95
|
+
"AUTHORITY_LOST",
|
|
96
|
+
"RESOURCE_EXCEEDED",
|
|
97
|
+
]),
|
|
98
|
+
TERMINATING: new Set(["CANCELLED", "FAILED", "RESOURCE_EXCEEDED"]),
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function isLegalTransition(from: ExecutionState, to: ExecutionState): boolean {
|
|
102
|
+
if (isExecutionTerminalState(from)) return false
|
|
103
|
+
return TRANSITIONS[from as ExecutionLifecycleState]?.has(to) ?? false
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Sumber request yang diizinkan (ekstensibel M13; TANPA policy di sini). */
|
|
107
|
+
export type TransitionRequestSource =
|
|
108
|
+
| "host"
|
|
109
|
+
| "scheduler"
|
|
110
|
+
| "backend"
|
|
111
|
+
| "timeout"
|
|
112
|
+
| "budget"
|
|
113
|
+
| "parent"
|
|
114
|
+
| "verifier"
|
|
115
|
+
| "agent-loop"
|
|
116
|
+
| "supervisor"
|
|
117
|
+
| "test"
|
|
118
|
+
|
|
119
|
+
/** Alasan pembatalan kanonis → terminal mapping (tak pernah FAILED generik). */
|
|
120
|
+
export type CancelReason =
|
|
121
|
+
| "user"
|
|
122
|
+
| "timeout"
|
|
123
|
+
| "budget"
|
|
124
|
+
| "authority"
|
|
125
|
+
| "resource"
|
|
126
|
+
| "parent"
|
|
127
|
+
| "shutdown"
|
|
128
|
+
|
|
129
|
+
export function terminalForCancelReason(reason: CancelReason): ExecutionTerminalState {
|
|
130
|
+
switch (reason) {
|
|
131
|
+
case "timeout":
|
|
132
|
+
return "TIMED_OUT"
|
|
133
|
+
case "budget":
|
|
134
|
+
return "BUDGET_EXCEEDED"
|
|
135
|
+
case "authority":
|
|
136
|
+
return "AUTHORITY_LOST"
|
|
137
|
+
case "resource":
|
|
138
|
+
return "RESOURCE_EXCEEDED"
|
|
139
|
+
case "user":
|
|
140
|
+
case "parent":
|
|
141
|
+
case "shutdown":
|
|
142
|
+
return "CANCELLED"
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Request transisi: data inert (BUKAN commit). Commit hanya via kernel. */
|
|
147
|
+
export interface TransitionRequest {
|
|
148
|
+
readonly executionId: string
|
|
149
|
+
readonly to: ExecutionState
|
|
150
|
+
/** Wajib non-empty (provenance; penolakan tanpa alasan = invalid). */
|
|
151
|
+
readonly reason: string
|
|
152
|
+
readonly source: TransitionRequestSource
|
|
153
|
+
readonly causality?: string
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export interface TransitionProvenance {
|
|
157
|
+
readonly reason: string
|
|
158
|
+
readonly source: TransitionRequestSource
|
|
159
|
+
readonly causality?: string
|
|
160
|
+
/** Timestamp commit (Date.now; observasi, bukan ordering authority). */
|
|
161
|
+
readonly at: number
|
|
162
|
+
/** Counter monotonik per-execution (ordering authority dalam execution). */
|
|
163
|
+
readonly order: number
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface ExecutionRecord {
|
|
167
|
+
readonly executionId: string
|
|
168
|
+
readonly parentExecutionId?: string
|
|
169
|
+
readonly rootExecutionId: string
|
|
170
|
+
readonly kind: ExecutionKind
|
|
171
|
+
readonly ownerId: string
|
|
172
|
+
readonly state: ExecutionState
|
|
173
|
+
readonly reason: string | null
|
|
174
|
+
readonly provenance: TransitionProvenance | null
|
|
175
|
+
readonly createdAt: number
|
|
176
|
+
readonly updatedAt: number
|
|
177
|
+
/** Counter transisi monotonik (naik tiap commit; terminal tak mereset). */
|
|
178
|
+
readonly version: number
|
|
179
|
+
readonly terminalAt: number | null
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface KernelTransitionEvent {
|
|
183
|
+
readonly executionId: string
|
|
184
|
+
readonly from: ExecutionState
|
|
185
|
+
readonly to: ExecutionState
|
|
186
|
+
readonly reason: string
|
|
187
|
+
readonly source: TransitionRequestSource
|
|
188
|
+
readonly order: number
|
|
189
|
+
readonly at: number
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export type TransitionResult =
|
|
193
|
+
| { readonly committed: true; readonly record: ExecutionRecord }
|
|
194
|
+
| {
|
|
195
|
+
readonly committed: false
|
|
196
|
+
readonly outcome: "rejected-invalid" | "duplicate-terminal-ignored" | "late-result-ignored"
|
|
197
|
+
readonly current: ExecutionRecord
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface KernelMetrics {
|
|
201
|
+
readonly transitionAccepted: number
|
|
202
|
+
readonly transitionRejected: number
|
|
203
|
+
readonly terminalCommitted: number
|
|
204
|
+
readonly duplicateTerminalIgnored: number
|
|
205
|
+
readonly lateResultIgnored: number
|
|
206
|
+
readonly invalidTransition: number
|
|
207
|
+
readonly observerErrors: number
|
|
208
|
+
readonly executions: number
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function freezeRecord(r: ExecutionRecord): ExecutionRecord {
|
|
212
|
+
return Object.freeze({ ...r })
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface ExecutionKernel {
|
|
216
|
+
/** Buat execution CREATED (mint id M1 bila absent; validasi bentuk). */
|
|
217
|
+
create(spec: {
|
|
218
|
+
executionId?: string
|
|
219
|
+
parentExecutionId?: string
|
|
220
|
+
rootExecutionId?: string
|
|
221
|
+
kind: ExecutionKind
|
|
222
|
+
ownerId: string
|
|
223
|
+
}): ExecutionRecord
|
|
224
|
+
/**
|
|
225
|
+
* Minta transisi. SATU-SATUNYA penulis state. Sinkron-atomik: validasi +
|
|
226
|
+
* mutasi dalam satu section run-to-completion (serialisasi tanpa mutex).
|
|
227
|
+
*/
|
|
228
|
+
requestTransition(req: TransitionRequest): TransitionResult
|
|
229
|
+
/** Snapshot frozen copy atau undefined (observasi; bukan authority kedua). */
|
|
230
|
+
get(executionId: string): ExecutionRecord | undefined
|
|
231
|
+
/** Hook observasi transisi (M10/M11/registry future). Throw terisolasi. */
|
|
232
|
+
onTransition(handler: (event: KernelTransitionEvent) => void): () => void
|
|
233
|
+
metrics(): KernelMetrics
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function createExecutionKernel(): ExecutionKernel {
|
|
237
|
+
const store = new Map<string, ExecutionRecord>()
|
|
238
|
+
const handlers = new Set<(event: KernelTransitionEvent) => void>()
|
|
239
|
+
const m = {
|
|
240
|
+
transitionAccepted: 0,
|
|
241
|
+
transitionRejected: 0,
|
|
242
|
+
terminalCommitted: 0,
|
|
243
|
+
duplicateTerminalIgnored: 0,
|
|
244
|
+
lateResultIgnored: 0,
|
|
245
|
+
invalidTransition: 0,
|
|
246
|
+
observerErrors: 0,
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const emit = (event: KernelTransitionEvent): void => {
|
|
250
|
+
for (const h of [...handlers]) {
|
|
251
|
+
try {
|
|
252
|
+
h(event)
|
|
253
|
+
} catch {
|
|
254
|
+
// Observer throw terisolasi: commit tak terpengaruh (kontrak bus M2).
|
|
255
|
+
m.observerErrors++
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Commit point KANONIS: satu-satunya mutasi state. Sinkron penuh —
|
|
261
|
+
// tak ada await/yield antara baca-CAS-tulis (serialisasi struktural).
|
|
262
|
+
const commit = (
|
|
263
|
+
current: ExecutionRecord,
|
|
264
|
+
to: ExecutionState,
|
|
265
|
+
reason: string,
|
|
266
|
+
source: TransitionRequestSource,
|
|
267
|
+
causality: string | undefined,
|
|
268
|
+
now: number,
|
|
269
|
+
): ExecutionRecord => {
|
|
270
|
+
const version = current.version + 1
|
|
271
|
+
const terminal = isExecutionTerminalState(to)
|
|
272
|
+
const next: ExecutionRecord = freezeRecord({
|
|
273
|
+
...current,
|
|
274
|
+
state: to,
|
|
275
|
+
reason,
|
|
276
|
+
provenance: Object.freeze({
|
|
277
|
+
reason,
|
|
278
|
+
source,
|
|
279
|
+
...(causality !== undefined ? { causality } : {}),
|
|
280
|
+
at: now,
|
|
281
|
+
order: version,
|
|
282
|
+
}),
|
|
283
|
+
updatedAt: now,
|
|
284
|
+
version,
|
|
285
|
+
terminalAt: terminal ? now : current.terminalAt,
|
|
286
|
+
})
|
|
287
|
+
store.set(current.executionId, next)
|
|
288
|
+
m.transitionAccepted++
|
|
289
|
+
if (terminal) m.terminalCommitted++
|
|
290
|
+
emit({
|
|
291
|
+
executionId: current.executionId,
|
|
292
|
+
from: current.state,
|
|
293
|
+
to,
|
|
294
|
+
reason,
|
|
295
|
+
source,
|
|
296
|
+
order: version,
|
|
297
|
+
at: now,
|
|
298
|
+
})
|
|
299
|
+
return next
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
create(spec: {
|
|
304
|
+
executionId?: string
|
|
305
|
+
parentExecutionId?: string
|
|
306
|
+
rootExecutionId?: string
|
|
307
|
+
kind: ExecutionKind
|
|
308
|
+
ownerId: string
|
|
309
|
+
}): ExecutionRecord {
|
|
310
|
+
const executionId = spec.executionId ?? allocateUniqueExecutionId(store)
|
|
311
|
+
if (!isExecutionId(executionId)) throw new Error("kernel.create: invalid executionId")
|
|
312
|
+
if (store.has(executionId))
|
|
313
|
+
throw new Error("kernel.create: duplicate executionId (no overwrite)")
|
|
314
|
+
if (spec.parentExecutionId !== undefined && !isExecutionId(spec.parentExecutionId))
|
|
315
|
+
throw new Error("kernel.create: invalid parentExecutionId (no guessing)")
|
|
316
|
+
const rootExecutionId = spec.rootExecutionId ?? executionId
|
|
317
|
+
if (!isExecutionId(rootExecutionId)) throw new Error("kernel.create: invalid rootExecutionId")
|
|
318
|
+
if (!spec.kind || !spec.ownerId) throw new Error("kernel.create: kind + ownerId required")
|
|
319
|
+
const now = Date.now()
|
|
320
|
+
const rec = freezeRecord({
|
|
321
|
+
executionId,
|
|
322
|
+
...(spec.parentExecutionId ? { parentExecutionId: spec.parentExecutionId } : {}),
|
|
323
|
+
rootExecutionId,
|
|
324
|
+
kind: spec.kind,
|
|
325
|
+
ownerId: spec.ownerId,
|
|
326
|
+
state: "CREATED" as const,
|
|
327
|
+
reason: null,
|
|
328
|
+
provenance: null,
|
|
329
|
+
createdAt: now,
|
|
330
|
+
updatedAt: now,
|
|
331
|
+
version: 0,
|
|
332
|
+
terminalAt: null,
|
|
333
|
+
})
|
|
334
|
+
store.set(executionId, rec)
|
|
335
|
+
return freezeRecord({ ...rec })
|
|
336
|
+
},
|
|
337
|
+
|
|
338
|
+
requestTransition(req: TransitionRequest): TransitionResult {
|
|
339
|
+
const now = Date.now()
|
|
340
|
+
const current = typeof req?.executionId === "string" ? store.get(req.executionId) : undefined
|
|
341
|
+
// Bentuk tak-valid / execution tak dikenal / reason kosong = programmer
|
|
342
|
+
// error fail-closed (throw deterministik, TANPA mutasi). Invalid TABLE
|
|
343
|
+
// pada execution dikenal = return rejected (di bawah).
|
|
344
|
+
if (
|
|
345
|
+
!current ||
|
|
346
|
+
typeof req?.to !== "string" ||
|
|
347
|
+
typeof req?.reason !== "string" ||
|
|
348
|
+
req.reason.length === 0
|
|
349
|
+
) {
|
|
350
|
+
throw new Error(
|
|
351
|
+
"kernel.requestTransition: unknown execution or empty reason (no silent coerce)",
|
|
352
|
+
)
|
|
353
|
+
}
|
|
354
|
+
// Terminal state: SEMUA request berikutnya adalah observasi audit, bukan transisi.
|
|
355
|
+
if (isExecutionTerminalState(current.state)) {
|
|
356
|
+
if (current.state === req.to) {
|
|
357
|
+
m.duplicateTerminalIgnored++
|
|
358
|
+
return {
|
|
359
|
+
committed: false,
|
|
360
|
+
outcome: "duplicate-terminal-ignored",
|
|
361
|
+
current: freezeRecord({ ...current }),
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
m.lateResultIgnored++
|
|
365
|
+
return {
|
|
366
|
+
committed: false,
|
|
367
|
+
outcome: "late-result-ignored",
|
|
368
|
+
current: freezeRecord({ ...current }),
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (!isLegalTransition(current.state, req.to)) {
|
|
372
|
+
m.transitionRejected++
|
|
373
|
+
m.invalidTransition++
|
|
374
|
+
return {
|
|
375
|
+
committed: false,
|
|
376
|
+
outcome: "rejected-invalid",
|
|
377
|
+
current: freezeRecord({ ...current }),
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const record = commit(current, req.to, req.reason, req.source, req.causality, now)
|
|
381
|
+
return { committed: true, record: freezeRecord({ ...record }) }
|
|
382
|
+
},
|
|
383
|
+
|
|
384
|
+
get(executionId: string): ExecutionRecord | undefined {
|
|
385
|
+
const rec = store.get(executionId)
|
|
386
|
+
return rec ? freezeRecord({ ...rec }) : undefined
|
|
387
|
+
},
|
|
388
|
+
|
|
389
|
+
onTransition(handler: (event: KernelTransitionEvent) => void): () => void {
|
|
390
|
+
handlers.add(handler)
|
|
391
|
+
return () => {
|
|
392
|
+
handlers.delete(handler)
|
|
393
|
+
}
|
|
394
|
+
},
|
|
395
|
+
|
|
396
|
+
metrics(): KernelMetrics {
|
|
397
|
+
return {
|
|
398
|
+
transitionAccepted: m.transitionAccepted,
|
|
399
|
+
transitionRejected: m.transitionRejected,
|
|
400
|
+
terminalCommitted: m.terminalCommitted,
|
|
401
|
+
duplicateTerminalIgnored: m.duplicateTerminalIgnored,
|
|
402
|
+
lateResultIgnored: m.lateResultIgnored,
|
|
403
|
+
invalidTransition: m.invalidTransition,
|
|
404
|
+
observerErrors: m.observerErrors,
|
|
405
|
+
executions: store.size,
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// ── Translation boundary: observasi backend → request lifecycle (murni) ──
|
|
412
|
+
|
|
413
|
+
export type BackendObservationKind =
|
|
414
|
+
| "completed"
|
|
415
|
+
| "failed"
|
|
416
|
+
| "proven-dead"
|
|
417
|
+
| "timeout"
|
|
418
|
+
| "vanished"
|
|
419
|
+
| "unknown"
|
|
420
|
+
| "orphan"
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Terjemahkan hasil fisik backend menjadi request lifecycle — atau null
|
|
424
|
+
* (TANPA klaim lifecycle). Aturan jujur:
|
|
425
|
+
* - "completed" self-exit → COMPLETED hanya dari RUNNING/WAITING (WAITING lewat
|
|
426
|
+
* rantai RESUMED di lapisan pemanggil; helper ini tak melompati rantai).
|
|
427
|
+
* - "failed" → FAILED (RUNNING saja; WAITING butuh konteks approval — null).
|
|
428
|
+
* - "proven-dead" → CANCELLING dengan reason pemanggil (terminal datang dari
|
|
429
|
+
* reason, bukan dari fakta mati). Tanpa reason aktif → null.
|
|
430
|
+
* - "timeout"/"vanished"/"unknown"/"orphan" → null (dilaporkan ke M9/M12,
|
|
431
|
+
* bukan lifecycle). Timeout WAIT backend ≠ timeout deadline runtime.
|
|
432
|
+
*/
|
|
433
|
+
export function translateBackendObservation(
|
|
434
|
+
state: ExecutionState,
|
|
435
|
+
observation: BackendObservationKind,
|
|
436
|
+
activeCancelReason?: string,
|
|
437
|
+
): { to: ExecutionState; reason: string } | null {
|
|
438
|
+
if (isExecutionTerminalState(state)) return null
|
|
439
|
+
switch (observation) {
|
|
440
|
+
case "completed":
|
|
441
|
+
if (state === "RUNNING") return { to: "COMPLETED", reason: "backend reported completion" }
|
|
442
|
+
return null
|
|
443
|
+
case "failed":
|
|
444
|
+
if (state === "RUNNING") return { to: "FAILED", reason: "backend reported failure" }
|
|
445
|
+
return null
|
|
446
|
+
case "proven-dead":
|
|
447
|
+
if ((state === "RUNNING" || state === "WAITING" || state === "RESUMED") && activeCancelReason)
|
|
448
|
+
return { to: "CANCELLING", reason: activeCancelReason }
|
|
449
|
+
return null
|
|
450
|
+
case "timeout":
|
|
451
|
+
case "vanished":
|
|
452
|
+
case "unknown":
|
|
453
|
+
case "orphan":
|
|
454
|
+
return null
|
|
455
|
+
}
|
|
456
|
+
}
|