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.
Files changed (221) hide show
  1. package/CHANGELOG.md +98 -0
  2. package/LICENSE +21 -21
  3. package/README.md +4 -4
  4. package/cli/args.ts +46 -0
  5. package/cli/commands/acp.ts +163 -10
  6. package/cli/commands/config.ts +33 -18
  7. package/cli/commands/daemon.ts +515 -0
  8. package/cli/commands/exec.ts +145 -53
  9. package/cli/commands/openwork.ts +986 -0
  10. package/cli/commands/sessions.ts +4 -1
  11. package/cli/commands/stats.ts +34 -4
  12. package/cli/commands.ts +217 -2
  13. package/cli/index.ts +146 -14
  14. package/cli/recovery-warning.ts +115 -0
  15. package/cli/router.ts +32 -1
  16. package/cli/setup.ts +2332 -171
  17. package/cli/tui.ts +113 -7
  18. package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +489 -0
  19. package/docs/OUTPUT_EVENT_MODEL.md +289 -0
  20. package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +391 -0
  21. package/docs/OUTPUT_PROTOCOL_SPEC.md +285 -0
  22. package/docs/OUTPUT_RENDERING_SPEC.md +248 -0
  23. package/docs/OUTPUT_UX_RULES.md +449 -0
  24. package/docs/PLAN_UIUX_V6.md +2 -2
  25. package/docs/README.md +4 -3
  26. package/docs/TERMINAL_CONTRACT.md +278 -66
  27. package/docs/architecture.md +89 -7
  28. package/docs/changelog.md +1 -1
  29. package/docs/choosing-mode.md +2 -2
  30. package/docs/cli.md +9 -5
  31. package/docs/concepts.md +1 -1
  32. package/docs/contributing.md +9 -2
  33. package/docs/environment.md +6 -2
  34. package/docs/exec.md +46 -5
  35. package/docs/memory-sessions.md +2 -0
  36. package/docs/policy-sandbox.md +1 -1
  37. package/docs/pricing-budget.md +1 -1
  38. package/docs/quickstart.md +2 -2
  39. package/docs/repl.md +6 -2
  40. package/docs/security-model.md +1 -1
  41. package/docs/security.md +1 -1
  42. package/docs/skills.md +2 -2
  43. package/docs/terminal.md +9 -5
  44. package/docs/tools.md +1 -1
  45. package/package.json +35 -5
  46. package/src/app/session.ts +261 -15
  47. package/src/app/tool-layer.ts +94 -7
  48. package/src/daemon/approvals.ts +361 -0
  49. package/src/daemon/capability.ts +145 -0
  50. package/src/daemon/children.ts +142 -0
  51. package/src/daemon/client.ts +267 -0
  52. package/src/daemon/control.ts +350 -0
  53. package/src/daemon/discovery.ts +234 -0
  54. package/src/daemon/feed.ts +155 -0
  55. package/src/daemon/framing.ts +94 -0
  56. package/src/daemon/host.ts +316 -0
  57. package/src/daemon/protocol.ts +217 -0
  58. package/src/daemon/resource.ts +170 -0
  59. package/src/daemon/sanitize.ts +57 -0
  60. package/src/daemon/server.ts +627 -0
  61. package/src/daemon/store.ts +317 -0
  62. package/src/daemon/subscription.ts +270 -0
  63. package/src/daemon/telemetry.ts +81 -0
  64. package/src/hooks/run.ts +12 -1
  65. package/src/lib/atomic-write.ts +29 -3
  66. package/src/lib/db-path.ts +26 -0
  67. package/src/lib/keystore.ts +544 -42
  68. package/src/lib/net.ts +43 -1
  69. package/src/lib/safe-open.ts +116 -1
  70. package/src/lib/session-id.ts +15 -0
  71. package/src/lsp/client.ts +123 -5
  72. package/src/mcp/client.ts +139 -17
  73. package/src/mcp/server.ts +177 -50
  74. package/src/mcp/transport.ts +16 -19
  75. package/src/memory/vector.ts +770 -55
  76. package/src/policy/allowlist.ts +14 -2
  77. package/src/policy/bash-guard.ts +105 -10
  78. package/src/policy/compaction.ts +82 -5
  79. package/src/policy/consequence-engine.ts +220 -0
  80. package/src/policy/context.ts +132 -0
  81. package/src/policy/executor.ts +187 -121
  82. package/src/policy/no-progress.ts +559 -0
  83. package/src/policy/permission.ts +4 -8
  84. package/src/policy/pricing.ts +17 -3
  85. package/src/policy/scrub.ts +69 -2
  86. package/src/policy/tool-contract.ts +515 -0
  87. package/src/policy/usage.ts +310 -24
  88. package/src/policy/verifier.ts +6 -3
  89. package/src/policy/work-mode.ts +182 -0
  90. package/src/presentation/adapter.ts +533 -26
  91. package/src/presentation/events.ts +151 -0
  92. package/src/presentation/model.ts +180 -27
  93. package/src/presentation/projection.ts +858 -0
  94. package/src/presentation/reducer.ts +645 -310
  95. package/src/presentation/store.ts +12 -7
  96. package/src/providers/anthropic.ts +18 -10
  97. package/src/providers/responses.ts +17 -7
  98. package/src/runtime/capability.ts +409 -0
  99. package/src/runtime/child-execution.ts +274 -0
  100. package/src/runtime/composition.ts +958 -0
  101. package/src/runtime/dispatch.ts +591 -0
  102. package/src/runtime/execution-backend.ts +632 -0
  103. package/src/runtime/execution-events.ts +272 -0
  104. package/src/runtime/execution-id.ts +230 -0
  105. package/src/runtime/execution-journal.ts +926 -0
  106. package/src/runtime/execution-kernel.ts +456 -0
  107. package/src/runtime/execution-registry.ts +379 -0
  108. package/src/runtime/process-supervision.ts +276 -0
  109. package/src/runtime/production-execution.ts +551 -0
  110. package/src/runtime/production-runtime.ts +217 -0
  111. package/src/runtime/recovery-safety.ts +70 -0
  112. package/src/runtime/recovery.ts +477 -0
  113. package/src/runtime/runtime-capabilities.ts +179 -0
  114. package/src/runtime/runtime-diagnostics.ts +141 -0
  115. package/src/runtime/runtime-host.ts +365 -0
  116. package/src/runtime/runtime-status-facts.ts +222 -0
  117. package/src/runtime/runtime-status-provider.ts +74 -0
  118. package/src/runtime/runtime-status.ts +459 -0
  119. package/src/runtime/runtime-task-admission.ts +101 -0
  120. package/src/runtime/supervisor.ts +444 -0
  121. package/src/session/authority.ts +168 -0
  122. package/src/session/checkpoint.ts +88 -1
  123. package/src/session/context-adapter.ts +251 -0
  124. package/src/session/context-assembly.ts +242 -0
  125. package/src/session/context-identity.ts +389 -0
  126. package/src/session/context-projection.ts +248 -0
  127. package/src/session/context-selector.ts +500 -0
  128. package/src/session/cross-session-adoption.ts +249 -0
  129. package/src/session/full-history-fold.ts +171 -0
  130. package/src/session/identity.ts +192 -0
  131. package/src/session/journal.ts +805 -102
  132. package/src/session/open-work-intent.ts +732 -0
  133. package/src/session/open-work.ts +767 -0
  134. package/src/session/pending-tools.ts +94 -0
  135. package/src/session/persistence.ts +3569 -127
  136. package/src/session/runtime-header.ts +208 -0
  137. package/src/session/shadow-git.ts +13 -5
  138. package/src/session/verification.ts +1623 -0
  139. package/src/task/assignment.ts +136 -0
  140. package/src/task/autonomous-adapter.ts +168 -0
  141. package/src/task/autonomous-context.ts +630 -0
  142. package/src/task/autonomous-policy.ts +490 -0
  143. package/src/task/continuity.ts +179 -0
  144. package/src/task/execution-cancellation.ts +165 -0
  145. package/src/task/graph-validate.ts +379 -0
  146. package/src/task/graph.ts +305 -0
  147. package/src/task/identity.ts +230 -0
  148. package/src/task/model.ts +417 -0
  149. package/src/task/production-scheduler.ts +293 -0
  150. package/src/task/readiness.ts +229 -0
  151. package/src/task/scheduler-observability.ts +387 -0
  152. package/src/task/scheduler.ts +1197 -0
  153. package/src/task/scheduling-policy.ts +165 -0
  154. package/src/task/session-authority.ts +148 -0
  155. package/src/task/session-ownership.ts +211 -0
  156. package/src/task/store.ts +2887 -0
  157. package/src/task/sync.ts +343 -0
  158. package/src/task/trigger.ts +260 -0
  159. package/src/tools/bash.ts +377 -211
  160. package/src/tools/code_run.ts +1 -1
  161. package/src/tools/delete_file.ts +1 -1
  162. package/src/tools/edit.ts +33 -6
  163. package/src/tools/evidence.ts +30 -0
  164. package/src/tools/git.ts +4 -4
  165. package/src/tools/glob.ts +12 -3
  166. package/src/tools/grep.ts +1 -1
  167. package/src/tools/lsp.ts +4 -4
  168. package/src/tools/mcp_call.ts +11 -4
  169. package/src/tools/memory.ts +153 -18
  170. package/src/tools/move_file.ts +1 -1
  171. package/src/tools/patch.ts +1 -1
  172. package/src/tools/read_file.ts +1 -1
  173. package/src/tools/read_image.ts +1 -1
  174. package/src/tools/submit_result.ts +1 -1
  175. package/src/tools/task.ts +437 -41
  176. package/src/tools/todo.ts +662 -35
  177. package/src/tools/web_fetch.ts +4 -2
  178. package/src/tools/web_search.ts +9 -3
  179. package/src/tools/write_file.ts +1 -1
  180. package/src/ui/assistant/simple.ts +123 -50
  181. package/src/ui/assistant/turn-status.ts +17 -3
  182. package/src/ui/contract.ts +184 -0
  183. package/src/ui/footer.ts +54 -18
  184. package/src/ui/i18n/en.ts +62 -6
  185. package/src/ui/i18n/id.ts +62 -6
  186. package/src/ui/input/prompt-engine.ts +120 -25
  187. package/src/ui/render/errors.ts +39 -1
  188. package/src/ui/render/markdown-table.ts +455 -0
  189. package/src/ui/render/markdown.ts +22 -1
  190. package/src/ui/render/table-grid.ts +184 -0
  191. package/src/ui/render/table.ts +15 -76
  192. package/src/ui/runtime/screen.ts +7 -2
  193. package/src/ui/runtime/statusline.ts +25 -6
  194. package/src/ui/tui/app.ts +598 -133
  195. package/src/ui/tui/transcript.ts +531 -89
  196. package/vendor/minicore/VENDOR.md +1 -1
  197. package/vendor/minicore/src/core/budget.ts +25 -25
  198. package/vendor/minicore/src/core/compact.ts +75 -75
  199. package/vendor/minicore/src/core/errors.ts +59 -59
  200. package/vendor/minicore/src/core/events.ts +73 -73
  201. package/vendor/minicore/src/core/executor.ts +139 -139
  202. package/vendor/minicore/src/core/history.ts +31 -31
  203. package/vendor/minicore/src/core/index.ts +41 -41
  204. package/vendor/minicore/src/core/loop.ts +355 -355
  205. package/vendor/minicore/src/core/permission.ts +22 -22
  206. package/vendor/minicore/src/core/provider.ts +39 -39
  207. package/vendor/minicore/src/core/recovery.ts +35 -35
  208. package/vendor/minicore/src/core/session.ts +327 -327
  209. package/vendor/minicore/src/core/snapshot.ts +146 -146
  210. package/vendor/minicore/src/core/tokens.ts +107 -107
  211. package/vendor/minicore/src/core/tool.ts +115 -115
  212. package/vendor/minicore/src/core/types.ts +50 -50
  213. package/vendor/minicore/src/providers/openai-compat.ts +417 -417
  214. package/AGENTS.md +0 -45
  215. package/PLAN.md +0 -491
  216. package/docs/ARCHITECTURE.html +0 -1248
  217. package/docs/CONTROL-PLANE-MAP.md +0 -139
  218. package/docs/HARNESS.md +0 -150
  219. package/docs/SUMMARY.md +0 -52
  220. package/docs/UI_RENDER_PIPELINE.md +0 -165
  221. package/docs/USAGE.md +0 -11
@@ -0,0 +1,926 @@
1
+ // M11 — Durable execution journal: HISTORY/EVIDENCE, bukan truth, bukan recovery.
2
+ //
3
+ // Kenapa berkas ini ada (P1 M11): Kernel (truth) + Event Plane (observasi)
4
+ // bersifat RAM-only; crash menghapusnya. Modul ini mencatat history durable
5
+ // (event + intent) ke SQLite sehingga M12 nanti dapat merekonstruksi bukti
6
+ // tanpa menebak. Arah SELALU: Kernel commit → event → journal append. TAK
7
+ // PERNAH sebaliknya.
8
+ //
9
+ // BUKAN: lifecycle authority (Kernel), recovery/replay/UNKNOWN-resolution
10
+ // (M12), scheduler bridge (M13), TaskStore/session claims, redispatch policy.
11
+ // Aturan yang dikunci (jangan dilonggarkan tanpa ADR baru):
12
+ // - Tiga sequence TERPISAH: executionVersion (Kernel) ≠ eventSequence (plane)
13
+ // ≠ journalSequence (JURNAL INI, AUTOINCREMENT). Tak ada yang diturunkan
14
+ // dari yang lain; monotonic ≠ gap-free (rollback dapat meninggalkan gap).
15
+ // - eventId UNIQUE → append ganda payload SAMA = satu record logis (atomic
16
+ // dedupe); eventId SAMA + payload BEDA = IDENTITY_CONFLICT (perbandingan
17
+ // kanonis; tak pernah overwrite; konflik tak alokasi sequence).
18
+ // - Durability jujur tiga-status: committed (transaksi SQLite) → checkpoint
19
+ // FULL → durable-confirmed | durability-uncertain | failed. PASSIVE tak
20
+ // dipakai (berhenti dini saat reader). confirmed = frame ter-checkpoint +
21
+ // fsync-boundary flush (synchronous=FULL sementara); cakupan klaim = crash
22
+ // proses + OS-crash pada storage yang menghormati fsync — BUKAN garansi
23
+ // hardware/power-loss universal (dinyatakan eksplisit, bukan diklaim).
24
+ // - Record immutable: TANPA UPDATE/DELETE di modul ini (tak ada statementnya);
25
+ // koreksi = event observasi baru. Hash integritas deterministik per record.
26
+ // - Intent-before-effect seam: noteIntent() untuk bukti intent durable SEBELUM
27
+ // efek; terminal/intent/uncertain-flush TETAP DIBEDAKAN (UNKNOWN milik M12).
28
+ // - Synchronous append (caller block sampai commit/bounded-busy-fail); tanpa
29
+ // queue tak terbatas; tanpa retry tak terbatas (3× bounded, busy-only).
30
+ // - Redaction di batas tulis (key-based, cermin scrub; jurnal lebih sensitif
31
+ // dari RAM — enforcement penyamaran tetap scrub, ini lapis kedua).
32
+ // - Storage: bun:sqlite (fondasi existing — tanpa engine baru), WAL + NORMAL +
33
+ // busy_timeout DULU (konvensi persistence.ts), schema_version=1 eksplisit.
34
+
35
+ import { Database, type Statement } from "bun:sqlite"
36
+ import { createHash } from "node:crypto"
37
+ import { mkdirSync } from "node:fs"
38
+ import { dirname } from "node:path"
39
+ import { LIMITS } from "../constants.ts"
40
+ import type { ExecutionEvent } from "./execution-events.ts"
41
+
42
+ /** Versi skema jurnal (bentuk/encoding) — BUKAN executionVersion. */
43
+ export const JOURNAL_SCHEMA_VERSION = 1
44
+
45
+ /**
46
+ * [Fase 10 / 10.1] Otoritas penyimpanan jurnal eksekusi.
47
+ * `runtime/execution-journal.ts` adalah Diagnostic Audit Projection: history
48
+ * audit durable yang MEREKAM event+intent untuk forensik, BUKAN pemutus
49
+ * recovery. Canonical Recovery Authority adalah `session/journal.ts`
50
+ * (`SESSION_JOURNAL_AUTHORITY = "canonical_recovery_ledger"`). Konstanta ini
51
+ * menandai peran lokal agar batas source-of-truth tidak pernah tertukar.
52
+ */
53
+ export const EXECUTION_JOURNAL_AUTHORITY = "diagnostic_audit_projection" as const
54
+
55
+ /**
56
+ * [Fase 10 / 10.1] Matriks otoritas penyimpanan MiniCode — klasifikasi eksplisit
57
+ * peran + source-of-truth 7 penyimpanan lokal. Dipakai audit/uji untuk
58
+ * memastikan tidak ada source of truth kedua (invariant "derived ≠ canonical").
59
+ *
60
+ * - "canonical" : sumber kebenaran; state di sini bersifat otoritatif.
61
+ * - "derived_projection" : view yang DITURUNKAN dari canonical; bila tidak
62
+ * sinkron, canonical yang menang (reconciliation).
63
+ * - "derived_index" : indeks percepat look-up; bisa di-rebuild dari canonical.
64
+ */
65
+ export const STORAGE_AUTHORITY_MATRIX = {
66
+ /** Session store: sesi/run/thread/approval durable + writer epoch. */
67
+ "sessions.db": { role: "canonical", domain: "session_runs_threads_approvals" },
68
+ /** Task store: state task + lease otoritas sesi (single authority sinkron todo). */
69
+ "tasks.db": { role: "canonical", domain: "task_state_and_session_authority_leases" },
70
+ /** Vector store: memori semantik + episodik (provenance supersession). */
71
+ "vector.db": { role: "canonical", domain: "semantic_and_episodic_memory" },
72
+ /** Session journal (session/journal.ts): intent/receipt/verification recovery. */
73
+ session_journal: { role: "canonical", domain: "crash_recovery_intent_receipt_verification" },
74
+ /** Execution journal (modul ini): history audit, diturunkan dari session journal. */
75
+ execution_journal: { role: "derived_projection", sourceOfTruth: "session_journal" },
76
+ /** MEMORY.md: teks datar baca-manusia, projection dari vector.db. */
77
+ "MEMORY.md": { role: "derived_projection", sourceOfTruth: "vector.db" },
78
+ /** memory_fts: indeks FTS5 percepat keyword recall, rebuild-able dari vector.db. */
79
+ memory_fts: { role: "derived_index", sourceOfTruth: "vector.db" },
80
+ } as const
81
+
82
+ export type JournalErrorCode =
83
+ | "not-open"
84
+ | "already-closed"
85
+ | "constraint-conflict"
86
+ | "identity-conflict"
87
+ | "serialization-failure"
88
+ | "storage-failure"
89
+ | "busy-timeout"
90
+ | "flush-uncertain"
91
+ | "corruption"
92
+
93
+ export interface JournalError {
94
+ readonly code: JournalErrorCode
95
+ readonly detail: string
96
+ }
97
+
98
+ interface JournalBase {
99
+ readonly journalSequence: number
100
+ readonly schemaVersion: number
101
+ readonly kind: "event" | "intent"
102
+ /** SHA-256 atas konten kanonis (cakupan §integrity, di bawah). */
103
+ readonly recordHash: string
104
+ }
105
+
106
+ export interface JournalEventRecord extends JournalBase {
107
+ readonly kind: "event"
108
+ readonly eventId: string
109
+ readonly eventType: string
110
+ readonly executionId: string
111
+ readonly parentExecutionId?: string
112
+ readonly lineageRootId: string
113
+ readonly timestamp: number
114
+ readonly eventSequence: number | null
115
+ readonly executionVersion: number
116
+ readonly source: string
117
+ readonly causality?: string
118
+ readonly reason?: string
119
+ readonly state?: string
120
+ readonly previousState?: string
121
+ readonly attempt?: number
122
+ readonly generation?: number
123
+ readonly supersedes?: string
124
+ readonly metadataJson: string
125
+ }
126
+
127
+ export interface JournalIntentRecord extends JournalBase {
128
+ readonly kind: "intent"
129
+ /** Kunci idempotency intent (mis. `intent:<exec>:<ver>:<hash>`); UNIQUE. */
130
+ readonly intentId: string
131
+ readonly executionId: string
132
+ readonly parentExecutionId?: string
133
+ readonly lineageRootId: string
134
+ readonly executionVersion: number
135
+ readonly timestamp: number
136
+ readonly reason: string
137
+ readonly source: string
138
+ readonly metadataJson: string
139
+ }
140
+
141
+ export type JournalRecord = JournalEventRecord | JournalIntentRecord
142
+
143
+ export interface IntentInput {
144
+ readonly intentId: string
145
+ readonly executionId: string
146
+ readonly parentExecutionId?: string
147
+ readonly lineageRootId: string
148
+ readonly executionVersion: number
149
+ readonly reason: string
150
+ readonly source: string
151
+ readonly metadata?: Record<string, string>
152
+ readonly timestamp?: number
153
+ }
154
+
155
+ export type AppendResult =
156
+ | { readonly status: "appended"; readonly record: JournalRecord }
157
+ | { readonly status: "duplicate"; readonly record: JournalRecord }
158
+ | { readonly status: "error"; readonly error: JournalError }
159
+
160
+ export type FlushResult =
161
+ | { readonly status: "durable-confirmed" }
162
+ | { readonly status: "durability-uncertain"; readonly error: JournalError }
163
+ | { readonly status: "failed"; readonly error: JournalError }
164
+
165
+ export interface CheckpointResult {
166
+ readonly busy: boolean
167
+ readonly mode: string
168
+ }
169
+
170
+ export interface JournalMetrics {
171
+ readonly appendCount: number
172
+ readonly duplicateCount: number
173
+ readonly appendFailureCount: number
174
+ readonly flushFailureCount: number
175
+ readonly corruptionCount: number
176
+ readonly bytesWritten: number
177
+ }
178
+
179
+ // ── Redaction batas-tulis (lapis kedua; enforcement milik scrub) ──
180
+
181
+ const SECRET_KEY_RE =
182
+ /(?:API[_-]?KEYS?|SECRET|TOKEN|PASSWORD|PASSWD|PRIVATE[_-]?KEY|CREDENTIALS?|AUTH|BEARER|SESSION[_-]?KEY)/i
183
+
184
+ function redactMetadata(input: Record<string, string> | undefined): Record<string, string> {
185
+ if (!input) return {}
186
+ const out: Record<string, string> = {}
187
+ for (const [k, v] of Object.entries(input)) {
188
+ out[k] = SECRET_KEY_RE.test(k) ? "[REDACTED]" : v
189
+ }
190
+ return out
191
+ }
192
+
193
+ // ── Hash integritas: SHA-256 atas JSON kanonis field immutable (tanpa seq,
194
+ // tanpa hash itu sendiri). Cakupan: identitas + isi + provenance + versi.
195
+ // Dikecualikan: journalSequence (diberikan storage), recordHash (output).
196
+ // BUKAN klaim anti-tamper absolut — bukti deteksi perubahan, terdokumentasi. ──
197
+
198
+ function canonicalJson(value: Record<string, unknown>): string {
199
+ const sorted: Record<string, unknown> = {}
200
+ for (const k of Object.keys(value).sort()) sorted[k] = value[k]
201
+ return JSON.stringify(sorted)
202
+ }
203
+
204
+ function hashRecord(content: Record<string, unknown>): string {
205
+ return createHash("sha256").update(canonicalJson(content)).digest("hex")
206
+ }
207
+
208
+ // ── Konten kanonis event/intent: SATU representasi untuk hash + equality.
209
+ // Dibandingkan: seluruh payload identitas M10 (eventId..metadata), TANPA
210
+ // metadata persistence (journalSequence, recordHash, schemaVersion — lokal
211
+ // storage dan boleh berbeda secara sah). Deterministik via canonicalJson. ──
212
+
213
+ export interface CanonicalEventContent {
214
+ readonly [key: string]: unknown
215
+ }
216
+
217
+ export function canonicalEventContent(event: ExecutionEvent): Record<string, unknown> {
218
+ const redacted = redactMetadata(event.metadata as Record<string, string> | undefined)
219
+ return {
220
+ eventId: event.eventId,
221
+ eventType: event.eventType,
222
+ executionId: event.executionId,
223
+ parentExecutionId: event.parentExecutionId ?? null,
224
+ lineageRootId: event.lineageRootId,
225
+ timestamp: event.timestamp,
226
+ eventSequence: event.eventSequence,
227
+ executionVersion: event.executionVersion,
228
+ source: event.source,
229
+ causality: event.causality ?? null,
230
+ reason: event.reason ?? null,
231
+ state: event.state ?? null,
232
+ previousState: event.previousState ?? null,
233
+ attempt: event.attempt ?? null,
234
+ generation: event.generation ?? null,
235
+ supersedes: event.supersedes ?? null,
236
+ metadata: redacted,
237
+ }
238
+ }
239
+
240
+ export function canonicalIntentContent(intent: IntentInput): Record<string, unknown> {
241
+ const redacted = redactMetadata(intent.metadata)
242
+ return {
243
+ intentId: intent.intentId,
244
+ executionId: intent.executionId,
245
+ parentExecutionId: intent.parentExecutionId ?? null,
246
+ lineageRootId: intent.lineageRootId,
247
+ executionVersion: intent.executionVersion,
248
+ reason: intent.reason,
249
+ source: intent.source,
250
+ metadata: redacted,
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Bandingkan konten kanonis tersimpan (dari record hash-able) vs incoming.
256
+ * Mengembalikan nama field yang berbeda (diagnostik TANPA nilai — tanpa dump
257
+ * payload sensitif ke error). Sama = duplicate aman; beda = IDENTITY_CONFLICT.
258
+ */
259
+ export function diffCanonicalContent(
260
+ stored: Record<string, unknown>,
261
+ incoming: Record<string, unknown>,
262
+ ): string[] {
263
+ const keys = new Set([...Object.keys(stored), ...Object.keys(incoming)])
264
+ const diff: string[] = []
265
+ for (const k of [...keys].sort()) {
266
+ if (canonicalJson({ v: stored[k] }) !== canonicalJson({ v: incoming[k] })) diff.push(k)
267
+ }
268
+ return diff
269
+ }
270
+
271
+ /** [ST-6] Parse metadata JSON aman: baris korup → `null`, bukan uncaught throw
272
+ * (integritas record tak boleh membuat `integrityCheck`/`readAll` melempar). */
273
+ function parseMetadata(metadataJson: string | null | undefined): Record<string, unknown> | null {
274
+ if (metadataJson == null) return null
275
+ try {
276
+ return JSON.parse(metadataJson) as Record<string, unknown>
277
+ } catch {
278
+ return null
279
+ }
280
+ }
281
+
282
+ function storedEventContent(rec: JournalEventRecord): Record<string, unknown> {
283
+ return {
284
+ eventId: rec.eventId,
285
+ eventType: rec.eventType,
286
+ executionId: rec.executionId,
287
+ parentExecutionId: rec.parentExecutionId ?? null,
288
+ lineageRootId: rec.lineageRootId,
289
+ timestamp: rec.timestamp,
290
+ eventSequence: rec.eventSequence,
291
+ executionVersion: rec.executionVersion,
292
+ source: rec.source,
293
+ causality: rec.causality ?? null,
294
+ reason: rec.reason ?? null,
295
+ state: rec.state ?? null,
296
+ previousState: rec.previousState ?? null,
297
+ attempt: rec.attempt ?? null,
298
+ generation: rec.generation ?? null,
299
+ supersedes: rec.supersedes ?? null,
300
+ metadata: parseMetadata(rec.metadataJson),
301
+ }
302
+ }
303
+
304
+ function storedIntentContent(rec: JournalIntentRecord): Record<string, unknown> {
305
+ return {
306
+ intentId: rec.intentId,
307
+ executionId: rec.executionId,
308
+ parentExecutionId: rec.parentExecutionId ?? null,
309
+ lineageRootId: rec.lineageRootId,
310
+ executionVersion: rec.executionVersion,
311
+ reason: rec.reason,
312
+ source: rec.source,
313
+ metadata: parseMetadata(rec.metadataJson),
314
+ }
315
+ }
316
+
317
+ // ── Busy retry bounded (konvensi persistence.ts: 3×, busy-only, rethrow lain) ──
318
+
319
+ /** Seam uji: retry bounded generik agar dapat diuji deterministik tanpa SQLite sibuk nyata. */
320
+ export async function withJournalBusyRetry<T>(
321
+ fn: () => T,
322
+ opts?: { attempts?: number; sleepMs?: (attempt: number) => Promise<void> },
323
+ ): Promise<T> {
324
+ const attempts = opts?.attempts ?? 3
325
+ const sleep = opts?.sleepMs ?? ((n: number) => Bun.sleep(Math.min(25 * 2 ** n, 200)))
326
+ let last: unknown = null
327
+ for (let i = 0; i < attempts; i++) {
328
+ try {
329
+ return fn()
330
+ } catch (e) {
331
+ if (!isSqliteContention(e)) throw e
332
+ last = e
333
+ if (i + 1 < attempts) await sleep(i)
334
+ }
335
+ }
336
+ throw last instanceof Error ? last : new Error(`SQLITE_BUSY after ${attempts} attempts`)
337
+ }
338
+
339
+ // [Phase 12.3] Predikat kontensi SQLite transien lintas-proses — SELARAS dengan
340
+ // persistence.ts / store.ts / vector.ts (Fase 12.2). Bun menaruh kode simbolik di
341
+ // `error.code` (mis. "SQLITE_IOERR_TRUNCATE") dan teks manusiawi di `error.message`
342
+ // ("disk I/O error"), jadi keduanya diperiksa. SENGAJA TIDAK di-retry:
343
+ // SQLITE_CORRUPT / SQLITE_NOTADB / constraint error.
344
+ const SQLITE_CONTENTION_RE =
345
+ /SQLITE_BUSY|database is locked|database table is locked|SQLITE_LOCKED|SQLITE_PROTOCOL|SQLITE_IOERR_TRUNCATE|SQLITE_IOERR_SHMOPEN|SQLITE_IOERR_LOCK|disk I\/O error/i
346
+
347
+ function isSqliteContention(e: unknown): boolean {
348
+ const code = (e as { code?: unknown } | null)?.code
349
+ if (typeof code === "string" && SQLITE_CONTENTION_RE.test(code)) return true
350
+ const msg = e instanceof Error ? String(e.message ?? e) : String(e)
351
+ return SQLITE_CONTENTION_RE.test(msg)
352
+ }
353
+
354
+ const DB_INIT_ATTEMPTS = 8
355
+ const DB_INIT_BACKOFF_MS = 15
356
+
357
+ /** Retry sinkron untuk setup PRAGMA/WAL saat open (jalur sinkron openExecutionJournal). */
358
+ function withBusyRetrySync<T>(fn: () => T, attempts = DB_INIT_ATTEMPTS): T | null {
359
+ for (let i = 0; i < attempts; i++) {
360
+ try {
361
+ return fn()
362
+ } catch (e) {
363
+ if (!isSqliteContention(e)) throw e
364
+ Bun.sleepSync(DB_INIT_BACKOFF_MS * 2 ** i)
365
+ }
366
+ }
367
+ return null
368
+ }
369
+
370
+ function isCorruptionError(e: unknown): boolean {
371
+ const msg = (e instanceof Error ? e.message : String(e)).toLowerCase()
372
+ return (
373
+ msg.includes("malformed") ||
374
+ msg.includes("not a database") ||
375
+ msg.includes("file is not a database")
376
+ )
377
+ }
378
+
379
+ export interface ExecutionJournal {
380
+ appendEvent(event: ExecutionEvent): Promise<AppendResult>
381
+ noteIntent(intent: IntentInput): Promise<AppendResult>
382
+ readAll(): JournalRecord[]
383
+ readAfter(journalSequence: number): JournalRecord[]
384
+ /** History execution (BUKAN current state — konsumen menafsirkan). */
385
+ readExecutionHistory(executionId: string): JournalRecord[]
386
+ /** Durability boundary: checkpoint WAL; gagal = uncertain (jujur). */
387
+ flush(): FlushResult
388
+ /** Verifikasi hash ulang; mismatch dilaporkan, tak pernah dibuang diam-diam. */
389
+ integrityCheck(): { checked: number; mismatched: number[] }
390
+ metrics(): JournalMetrics
391
+ close(): void
392
+ isOpen(): boolean
393
+ }
394
+
395
+ export function openExecutionJournal(
396
+ path: string,
397
+ opts?: {
398
+ readonly?: boolean
399
+ /**
400
+ * Seam injeksi checkpoint (uji deterministik boundary confirmed/uncertain).
401
+ * Default = checkpoint FULL nyata + fsync-boundary sementara (di bawah).
402
+ * Produksi tak mengeset ini; test memakainya untuk memaksa busy/gagal.
403
+ */
404
+ checkpoint?: () => CheckpointResult
405
+ },
406
+ ): ExecutionJournal {
407
+ const checkpointFn = opts?.checkpoint
408
+ let db: Database | null = null
409
+ let openError: JournalError | null = null
410
+ // Statement registry di scope fungsi (bukan blok try): semua pemakai
411
+ // (insert/fetch/read/flush/close) berbagi; close() finalize semua agar
412
+ // Windows file lock lepas (tanpa ini rm tmp macet EBUSY).
413
+ const statements: Statement[] = []
414
+ const prep = (sql: string): Statement => {
415
+ if (!db) throw new Error("journal closed")
416
+ const stmt = db.prepare(sql)
417
+ statements.push(stmt)
418
+ return stmt
419
+ }
420
+ try {
421
+ if (!opts?.readonly) mkdirSync(dirname(path), { recursive: true })
422
+ db = new Database(path, opts?.readonly ? { readonly: true } : undefined)
423
+ // busy_timeout DULU (konvensi persistence.ts — sebelum statement lock apa pun).
424
+ // [Phase 12.3] Setup busy_timeout + WAL di-retry kontensi transien
425
+ // (SQLITE_IOERR_TRUNCATE dkk.); non-kontensi langsung lempar, dan bila semua
426
+ // percobaan habis LEMPAR (bukan defer) agar catch di bawah menutup db.
427
+ const dbHandle = db
428
+ const setupOk = withBusyRetrySync(() => {
429
+ dbHandle.exec(`PRAGMA busy_timeout=${LIMITS.SQLITE_BUSY_TIMEOUT_MS}`)
430
+ if (!opts?.readonly) {
431
+ dbHandle.exec(
432
+ `PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL; PRAGMA journal_size_limit=${LIMITS.SQLITE_WAL_SIZE_LIMIT_BYTES}; PRAGMA wal_autocheckpoint=${LIMITS.SQLITE_WAL_AUTOCHECKPOINT_PAGES};`,
433
+ )
434
+ }
435
+ return true
436
+ })
437
+ if (setupOk === null) {
438
+ throw new Error(
439
+ `execution journal: WAL/busy_timeout setup gave up after ${DB_INIT_ATTEMPTS} busy attempts`,
440
+ )
441
+ }
442
+ if (!opts?.readonly) {
443
+ db.exec(`CREATE TABLE IF NOT EXISTS journal_meta(key TEXT PRIMARY KEY, value TEXT)`)
444
+ db.exec(
445
+ `CREATE TABLE IF NOT EXISTS journal_events(
446
+ journal_sequence INTEGER PRIMARY KEY AUTOINCREMENT,
447
+ schema_version INTEGER NOT NULL,
448
+ kind TEXT NOT NULL,
449
+ event_id TEXT UNIQUE,
450
+ intent_id TEXT UNIQUE,
451
+ event_type TEXT,
452
+ execution_id TEXT NOT NULL,
453
+ parent_execution_id TEXT,
454
+ lineage_root_id TEXT NOT NULL,
455
+ ts INTEGER NOT NULL,
456
+ event_sequence INTEGER,
457
+ execution_version INTEGER NOT NULL,
458
+ source TEXT NOT NULL,
459
+ causality TEXT,
460
+ reason TEXT,
461
+ state TEXT,
462
+ previous_state TEXT,
463
+ attempt INTEGER,
464
+ generation INTEGER,
465
+ supersedes TEXT,
466
+ metadata_json TEXT NOT NULL,
467
+ record_hash TEXT NOT NULL
468
+ )`,
469
+ )
470
+ db.exec(
471
+ `CREATE INDEX IF NOT EXISTS idx_journal_execution ON journal_events(execution_id, journal_sequence)`,
472
+ )
473
+ const cur = prep(`SELECT value FROM journal_meta WHERE key='schema_version'`).get() as
474
+ | { value: string }
475
+ | undefined
476
+ if (!cur) {
477
+ prep(`INSERT INTO journal_meta(key, value) VALUES ('schema_version', ?)`).run(
478
+ String(JOURNAL_SCHEMA_VERSION),
479
+ )
480
+ }
481
+ }
482
+ } catch (e) {
483
+ // Open gagal (korupsi/storage): TUTUP handle yang sempat dibuat — tanpa
484
+ // ini lock file bocor dan cleanup macet EBUSY (defect nyata, bukan teori).
485
+ try {
486
+ db?.close()
487
+ } catch {}
488
+ db = null
489
+ if (isCorruptionError(e)) {
490
+ openError = {
491
+ code: "corruption",
492
+ detail: (e instanceof Error ? e.message : String(e)).slice(0, 300),
493
+ }
494
+ } else {
495
+ openError = {
496
+ code: "storage-failure",
497
+ detail: (e instanceof Error ? e.message : String(e)).slice(0, 300),
498
+ }
499
+ }
500
+ }
501
+
502
+ const m = {
503
+ appendCount: 0,
504
+ duplicateCount: 0,
505
+ appendFailureCount: 0,
506
+ flushFailureCount: 0,
507
+ corruptionCount: openError?.code === "corruption" ? 1 : 0,
508
+ bytesWritten: 0,
509
+ }
510
+
511
+ const fail = (code: JournalErrorCode, detail: string): AppendResult => {
512
+ m.appendFailureCount++
513
+ return { status: "error", error: { code, detail } }
514
+ }
515
+
516
+ /**
517
+ * Checkpoint default: FULL (menunggu selesai; PASSIVE boleh berhenti dini
518
+ * saat reader sehingga tak cocok untuk boundary) + synchronous=FULL SEMENTARA
519
+ * agar frame ter-checkpoint ter-sync (append path tetap NORMAL). Restore
520
+ * NORMAL di finally — penalti fsync hanya di boundary eksplisit, bukan tiap
521
+ * append. Klaim tetap scoped (lihat flush()).
522
+ */
523
+ const defaultCheckpoint = (): CheckpointResult => {
524
+ if (!db) throw new Error("journal closed")
525
+ db.exec(`PRAGMA synchronous=FULL`)
526
+ try {
527
+ const res = prep(`PRAGMA wal_checkpoint(FULL)`).get() as { busy?: number } | undefined
528
+ return { busy: (res?.busy ?? 0) !== 0, mode: "FULL+fsync" }
529
+ } finally {
530
+ try {
531
+ db.exec(`PRAGMA synchronous=NORMAL`)
532
+ } catch {}
533
+ }
534
+ }
535
+
536
+ const rowToRecord = (row: Record<string, unknown>): JournalRecord => {
537
+ const base = {
538
+ journalSequence: row.journal_sequence as number,
539
+ schemaVersion: row.schema_version as number,
540
+ recordHash: row.record_hash as string,
541
+ }
542
+ const meta = (row.metadata_json as string) ?? "{}"
543
+ if (row.kind === "intent") {
544
+ const rec: JournalIntentRecord = Object.freeze({
545
+ ...base,
546
+ kind: "intent" as const,
547
+ intentId: row.intent_id as string,
548
+ executionId: row.execution_id as string,
549
+ ...((row.parent_execution_id as string | null)
550
+ ? { parentExecutionId: row.parent_execution_id as string }
551
+ : {}),
552
+ lineageRootId: row.lineage_root_id as string,
553
+ executionVersion: row.execution_version as number,
554
+ timestamp: row.ts as number,
555
+ reason: (row.reason as string) ?? "",
556
+ source: (row.source as string) ?? "",
557
+ metadataJson: meta,
558
+ })
559
+ return rec
560
+ }
561
+ const rec: JournalEventRecord = Object.freeze({
562
+ ...base,
563
+ kind: "event" as const,
564
+ eventId: row.event_id as string,
565
+ eventType: (row.event_type as string) ?? "",
566
+ executionId: row.execution_id as string,
567
+ ...((row.parent_execution_id as string | null)
568
+ ? { parentExecutionId: row.parent_execution_id as string }
569
+ : {}),
570
+ lineageRootId: row.lineage_root_id as string,
571
+ timestamp: row.ts as number,
572
+ eventSequence: (row.event_sequence as number | null) ?? null,
573
+ executionVersion: row.execution_version as number,
574
+ source: (row.source as string) ?? "",
575
+ ...((row.causality as string | null) ? { causality: row.causality as string } : {}),
576
+ ...((row.reason as string | null) ? { reason: row.reason as string } : {}),
577
+ ...((row.state as string | null) ? { state: row.state as string } : {}),
578
+ ...((row.previous_state as string | null)
579
+ ? { previousState: row.previous_state as string }
580
+ : {}),
581
+ ...((row.attempt as number | null) !== null && row.attempt !== undefined
582
+ ? { attempt: row.attempt as number }
583
+ : {}),
584
+ ...((row.generation as number | null) !== null && row.generation !== undefined
585
+ ? { generation: row.generation as number }
586
+ : {}),
587
+ ...((row.supersedes as string | null) ? { supersedes: row.supersedes as string } : {}),
588
+ metadataJson: meta,
589
+ })
590
+ return rec
591
+ }
592
+
593
+ const fetchByEventId = (eventId: string): JournalRecord | null => {
594
+ if (!db) return null
595
+ const row = prep(`SELECT * FROM journal_events WHERE event_id = ?`).get(eventId) as
596
+ | Record<string, unknown>
597
+ | undefined
598
+ return row ? rowToRecord(row) : null
599
+ }
600
+
601
+ const fetchByIntentId = (intentId: string): JournalRecord | null => {
602
+ if (!db) return null
603
+ const row = prep(`SELECT * FROM journal_events WHERE intent_id = ?`).get(intentId) as
604
+ | Record<string, unknown>
605
+ | undefined
606
+ return row ? rowToRecord(row) : null
607
+ }
608
+
609
+ const insertRow = (cols: Record<string, unknown>): JournalRecord => {
610
+ if (!db) throw new Error("journal closed")
611
+ // Atomic: single INSERT dalam transaksi implisit; UNIQUE(event_id/intent_id)
612
+ // menjadikan retry konkuren aman (salah satu menang, lainnya konflik).
613
+ const keys = Object.keys(cols)
614
+ const stmt = prep(
615
+ `INSERT INTO journal_events(${keys.join(",")}) VALUES (${keys.map(() => "?").join(",")})`,
616
+ )
617
+ const info = stmt.run(...keys.map((k) => cols[k] as unknown)) as unknown as {
618
+ lastInsertRowid: number | bigint
619
+ }
620
+ const row = prep(`SELECT * FROM journal_events WHERE journal_sequence = ?`).get(
621
+ Number(info.lastInsertRowid),
622
+ ) as Record<string, unknown>
623
+ const rec = rowToRecord(row)
624
+ m.appendCount++
625
+ m.bytesWritten += canonicalJson(cols).length
626
+ return rec
627
+ }
628
+
629
+ const api: ExecutionJournal = {
630
+ async appendEvent(event: ExecutionEvent): Promise<AppendResult> {
631
+ if (!db) return fail(openError?.code ?? "not-open", openError?.detail ?? "journal not open")
632
+ // Validasi bentuk (serialization-failure, bukan throw generik).
633
+ if (
634
+ !event ||
635
+ typeof event.eventId !== "string" ||
636
+ typeof event.eventType !== "string" ||
637
+ typeof event.executionId !== "string" ||
638
+ typeof event.lineageRootId !== "string" ||
639
+ typeof event.source !== "string" ||
640
+ typeof event.executionVersion !== "number"
641
+ ) {
642
+ return fail("serialization-failure", "malformed ExecutionEvent (missing required fields)")
643
+ }
644
+ try {
645
+ const existing = await withJournalBusyRetry(() => fetchByEventId(event.eventId)).catch(
646
+ () => null,
647
+ )
648
+ const incoming = canonicalEventContent(event)
649
+ if (existing) {
650
+ // Sama eventId + payload SAMA = duplicate aman; BEDA = IDENTITY_CONFLICT
651
+ // (tak pernah overwrite; konflik tak alokasi sequence).
652
+ if (existing.kind !== "event") {
653
+ return fail("identity-conflict", `identity-conflict ${event.eventId} fields: kind`)
654
+ }
655
+ const diff = diffCanonicalContent(storedEventContent(existing), incoming)
656
+ if (diff.length === 0) {
657
+ m.duplicateCount++
658
+ return { status: "duplicate", record: existing }
659
+ }
660
+ return fail(
661
+ "identity-conflict",
662
+ `identity-conflict ${event.eventId} fields: ${diff.join(",")}`,
663
+ )
664
+ }
665
+ const redacted = incoming.metadata as Record<string, string>
666
+ const record = await withJournalBusyRetry(() =>
667
+ insertRow({
668
+ schema_version: JOURNAL_SCHEMA_VERSION,
669
+ kind: "event",
670
+ event_id: event.eventId,
671
+ intent_id: null,
672
+ event_type: event.eventType,
673
+ execution_id: event.executionId,
674
+ parent_execution_id: event.parentExecutionId ?? null,
675
+ lineage_root_id: event.lineageRootId,
676
+ ts: event.timestamp,
677
+ event_sequence: event.eventSequence,
678
+ execution_version: event.executionVersion,
679
+ source: event.source,
680
+ causality: event.causality ?? null,
681
+ reason: event.reason ?? null,
682
+ state: event.state ?? null,
683
+ previous_state: event.previousState ?? null,
684
+ attempt: event.attempt ?? null,
685
+ generation: event.generation ?? null,
686
+ supersedes: event.supersedes ?? null,
687
+ metadata_json: JSON.stringify(redacted),
688
+ record_hash: hashRecord(incoming),
689
+ }),
690
+ )
691
+ return { status: "appended", record }
692
+ } catch (e) {
693
+ const msg = e instanceof Error ? e.message : String(e)
694
+ // Race insert konkuren: UNIQUE conflict = baca pemenang lalu bandingkan
695
+ // kanonis (sama = duplicate; beda = IDENTITY_CONFLICT; pemenang duluan
696
+ // yang otoritatif — race tak menentukan ulang).
697
+ if (msg.includes("UNIQUE constraint failed")) {
698
+ const existing = fetchByEventId(event.eventId)
699
+ if (existing && existing.kind === "event") {
700
+ const diff = diffCanonicalContent(
701
+ storedEventContent(existing),
702
+ canonicalEventContent(event),
703
+ )
704
+ if (diff.length === 0) {
705
+ m.duplicateCount++
706
+ return { status: "duplicate", record: existing }
707
+ }
708
+ return fail(
709
+ "identity-conflict",
710
+ `identity-conflict ${event.eventId} fields: ${diff.join(",")}`,
711
+ )
712
+ }
713
+ if (existing) {
714
+ m.duplicateCount++
715
+ return { status: "duplicate", record: existing }
716
+ }
717
+ return fail("constraint-conflict", msg.slice(0, 300))
718
+ }
719
+ if (isSqliteContention(e)) return fail("busy-timeout", msg.slice(0, 300))
720
+ if (isCorruptionError(e)) {
721
+ m.corruptionCount++
722
+ return fail("corruption", msg.slice(0, 300))
723
+ }
724
+ return fail("storage-failure", msg.slice(0, 300))
725
+ }
726
+ },
727
+
728
+ async noteIntent(intent: IntentInput): Promise<AppendResult> {
729
+ if (!db) return fail(openError?.code ?? "not-open", openError?.detail ?? "journal not open")
730
+ if (
731
+ !intent ||
732
+ typeof intent.intentId !== "string" ||
733
+ typeof intent.executionId !== "string" ||
734
+ typeof intent.lineageRootId !== "string" ||
735
+ typeof intent.reason !== "string" ||
736
+ typeof intent.source !== "string" ||
737
+ typeof intent.executionVersion !== "number"
738
+ ) {
739
+ return fail("serialization-failure", "malformed intent (missing required fields)")
740
+ }
741
+ try {
742
+ const existing = await withJournalBusyRetry(() => fetchByIntentId(intent.intentId)).catch(
743
+ () => null,
744
+ )
745
+ const incoming = canonicalIntentContent(intent)
746
+ if (existing) {
747
+ if (existing.kind !== "intent") {
748
+ return fail("identity-conflict", `identity-conflict ${intent.intentId} fields: kind`)
749
+ }
750
+ const diff = diffCanonicalContent(storedIntentContent(existing), incoming)
751
+ if (diff.length === 0) {
752
+ m.duplicateCount++
753
+ return { status: "duplicate", record: existing }
754
+ }
755
+ return fail(
756
+ "identity-conflict",
757
+ `identity-conflict ${intent.intentId} fields: ${diff.join(",")}`,
758
+ )
759
+ }
760
+ const redacted = incoming.metadata as Record<string, string>
761
+ const ts = intent.timestamp ?? Date.now()
762
+ const record = await withJournalBusyRetry(() =>
763
+ insertRow({
764
+ schema_version: JOURNAL_SCHEMA_VERSION,
765
+ kind: "intent",
766
+ event_id: null,
767
+ intent_id: intent.intentId,
768
+ event_type: null,
769
+ execution_id: intent.executionId,
770
+ parent_execution_id: intent.parentExecutionId ?? null,
771
+ lineage_root_id: intent.lineageRootId,
772
+ ts,
773
+ event_sequence: null,
774
+ execution_version: intent.executionVersion,
775
+ source: intent.source,
776
+ causality: null,
777
+ reason: intent.reason,
778
+ state: null,
779
+ previous_state: null,
780
+ attempt: null,
781
+ generation: null,
782
+ supersedes: null,
783
+ metadata_json: JSON.stringify(redacted),
784
+ record_hash: hashRecord(incoming),
785
+ }),
786
+ )
787
+ return { status: "appended", record }
788
+ } catch (e) {
789
+ const msg = e instanceof Error ? e.message : String(e)
790
+ if (msg.includes("UNIQUE constraint failed")) {
791
+ const existing = fetchByIntentId(intent.intentId)
792
+ if (existing && existing.kind === "intent") {
793
+ const diff = diffCanonicalContent(
794
+ storedIntentContent(existing),
795
+ canonicalIntentContent(intent),
796
+ )
797
+ if (diff.length === 0) {
798
+ m.duplicateCount++
799
+ return { status: "duplicate", record: existing }
800
+ }
801
+ return fail(
802
+ "identity-conflict",
803
+ `identity-conflict ${intent.intentId} fields: ${diff.join(",")}`,
804
+ )
805
+ }
806
+ if (existing) {
807
+ m.duplicateCount++
808
+ return { status: "duplicate", record: existing }
809
+ }
810
+ return fail("constraint-conflict", msg.slice(0, 300))
811
+ }
812
+ if (isSqliteContention(e)) return fail("busy-timeout", msg.slice(0, 300))
813
+ if (isCorruptionError(e)) {
814
+ m.corruptionCount++
815
+ return fail("corruption", msg.slice(0, 300))
816
+ }
817
+ return fail("storage-failure", msg.slice(0, 300))
818
+ }
819
+ },
820
+
821
+ readAll(): JournalRecord[] {
822
+ if (!db) return []
823
+ const rows = prep(`SELECT * FROM journal_events ORDER BY journal_sequence`).all() as Record<
824
+ string,
825
+ unknown
826
+ >[]
827
+ return rows.map((r) => rowToRecord(r))
828
+ },
829
+
830
+ readAfter(journalSequence: number): JournalRecord[] {
831
+ if (!db) return []
832
+ const rows = prep(
833
+ `SELECT * FROM journal_events WHERE journal_sequence > ? ORDER BY journal_sequence`,
834
+ ).all(journalSequence) as Record<string, unknown>[]
835
+ return rows.map((r) => rowToRecord(r))
836
+ },
837
+
838
+ readExecutionHistory(executionId: string): JournalRecord[] {
839
+ // History, BUKAN current state: konsumen (M12) menafsirkan urutan.
840
+ if (!db) return []
841
+ const rows = prep(
842
+ `SELECT * FROM journal_events WHERE execution_id = ? ORDER BY journal_sequence`,
843
+ ).all(executionId) as Record<string, unknown>[]
844
+ return rows.map((r) => rowToRecord(r))
845
+ },
846
+
847
+ flush(): FlushResult {
848
+ if (!db) return { status: "failed", error: { code: "not-open", detail: "journal not open" } }
849
+ // Durability boundary eksplisit (FIX A): FULL menunggu selesai (PASSIVE
850
+ // boleh berhenti dini saat reader → tak cocok untuk boundary), dengan
851
+ // synchronous=FULL SEMENTARA agar frame ter-checkpoint ter-sync ke
852
+ // storage (append path tetap NORMAL — tanpa penalti global).
853
+ // Makna status (jujur, terdokumentasi):
854
+ // - durable-confirmed: FULL selesai busy=0 di bawah fsync-boundary → aman
855
+ // crash proses + OS-crash pada storage yang menghormati fsync. BUKAN
856
+ // garansi hardware/power-loss universal (SQLite sendiri tak klaim itu).
857
+ // - durability-uncertain: checkpoint sibuk/gagal → commit transaksi TETAP
858
+ // valid (record present), daya tahan tak terbukti.
859
+ // - failed: jurnal mati / error tulis.
860
+ try {
861
+ const checkpoint = checkpointFn ?? defaultCheckpoint
862
+ const res = checkpoint()
863
+ if (res.busy) {
864
+ m.flushFailureCount++
865
+ return {
866
+ status: "durability-uncertain",
867
+ error: { code: "flush-uncertain", detail: `checkpoint busy (${res.mode})` },
868
+ }
869
+ }
870
+ return { status: "durable-confirmed" }
871
+ } catch (e) {
872
+ m.flushFailureCount++
873
+ return {
874
+ status: "failed",
875
+ error: {
876
+ code: "flush-uncertain",
877
+ detail: (e instanceof Error ? e.message : String(e)).slice(0, 200),
878
+ },
879
+ }
880
+ }
881
+ },
882
+
883
+ integrityCheck(): { checked: number; mismatched: number[] } {
884
+ // Mismatch dilaporkan (seq list), TAK PERNAH dibuang diam-diam.
885
+ // Reuse storedEventContent agar definisi konten tunggal (anti-drift).
886
+ const records = api.readAll()
887
+ const mismatched: number[] = []
888
+ for (const rec of records) {
889
+ if (rec.kind === "event") {
890
+ if (hashRecord(storedEventContent(rec)) !== rec.recordHash)
891
+ mismatched.push(rec.journalSequence)
892
+ }
893
+ }
894
+ return { checked: records.length, mismatched }
895
+ },
896
+
897
+ metrics() {
898
+ return { ...m }
899
+ },
900
+
901
+ close(): void {
902
+ // Idempoten: ganda aman; post-close append = already-closed eksplisit.
903
+ // Finalize statements DULU (Windows file lock), baru db.close().
904
+ for (const stmt of statements.splice(0)) {
905
+ try {
906
+ stmt.finalize()
907
+ } catch {}
908
+ }
909
+ if (db) {
910
+ try {
911
+ db.close()
912
+ } catch {}
913
+ db = null
914
+ }
915
+ },
916
+
917
+ isOpen(): boolean {
918
+ return db !== null
919
+ },
920
+ }
921
+ return api
922
+ }
923
+
924
+ // [P1 M16] `randomEventIdForTest` dihapus: tanpa pemakai, dan kehadirannya berarti
925
+ // allocator `evt_` hidup di DUA tempat (plane emit + helper uji) — dua sumber
926
+ // untuk satu namespace. Event id tetap dicetak hanya di M10 `createEventPlane`.