minicode-ai 0.10.0 → 0.11.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/AGENTS.md +45 -0
- package/CHANGELOG.md +59 -4
- package/PLAN.md +491 -0
- package/README.md +2 -2
- package/cli/commands/acp.ts +485 -0
- package/cli/commands/auth.ts +7 -4
- package/cli/commands/config.ts +41 -27
- package/cli/commands/exec.ts +33 -12
- package/cli/commands/mcp.ts +3 -2
- package/cli/commands/memory.ts +1 -1
- package/cli/commands/pricing.ts +6 -4
- package/cli/commands/providers.ts +18 -8
- package/cli/commands/sessions.ts +11 -4
- package/cli/commands/skills.ts +3 -3
- package/cli/commands/stats.ts +3 -1
- package/cli/commands.ts +6 -0
- package/cli/index.ts +82 -57
- package/cli/model-manager.ts +6 -1
- package/cli/router.ts +7 -0
- package/cli/setup.ts +341 -5
- package/cli/tui.ts +54 -18
- package/docs/ARCHITECTURE.html +29 -22
- package/docs/CONTROL-PLANE-MAP.md +139 -0
- package/docs/HARNESS.md +150 -0
- package/docs/PLAN_UIUX_V6.md +209 -0
- package/docs/README.md +1 -1
- package/docs/SUMMARY.md +52 -0
- package/docs/TERMINAL_CONTRACT.md +367 -0
- package/docs/UI_RENDER_PIPELINE.md +165 -0
- package/docs/USAGE.md +11 -0
- package/docs/agents.md +31 -0
- package/docs/architecture.md +3 -3
- package/docs/choosing-mode.md +34 -0
- package/docs/cli.md +130 -0
- package/docs/concepts.md +53 -0
- package/docs/config-providers.md +87 -0
- package/docs/config.md +63 -0
- package/docs/contributing.md +79 -0
- package/docs/environment.md +95 -0
- package/docs/exec.md +73 -0
- package/docs/getting-started.md +102 -0
- package/docs/glossary.md +37 -0
- package/docs/mcp-lsp.md +54 -0
- package/docs/memory-sessions.md +63 -0
- package/docs/policy-sandbox.md +72 -0
- package/docs/pricing-budget.md +49 -0
- package/docs/quickstart.md +60 -0
- package/docs/repl.md +67 -0
- package/docs/security-model.md +95 -0
- package/docs/security.md +91 -0
- package/docs/skills.md +48 -0
- package/docs/terminal.md +79 -0
- package/docs/tools.md +123 -0
- package/docs/troubleshooting.md +40 -0
- package/docs/verify-benchmark.md +58 -0
- package/package.json +8 -5
- package/src/app/provider-layer.ts +12 -4
- package/src/app/session.ts +6 -0
- package/src/app/tool-layer.ts +2 -1
- package/src/constants.ts +35 -0
- package/src/lib/net.ts +6 -1
- package/src/lsp/client.ts +41 -8
- package/src/mcp/http-transport.ts +52 -15
- package/src/mcp/transport.ts +32 -1
- package/src/memory/files.ts +6 -5
- package/src/policy/allowlist.ts +1 -1
- package/src/policy/bash-guard.ts +154 -14
- package/src/policy/compaction.ts +57 -3
- package/src/policy/context.ts +4 -0
- package/src/policy/jail.ts +49 -4
- package/src/policy/permission.ts +152 -12
- package/src/policy/pricing.ts +12 -9
- package/src/presentation/adapter.ts +721 -0
- package/src/presentation/events.ts +324 -0
- package/src/presentation/label.ts +140 -0
- package/src/presentation/model.ts +172 -0
- package/src/presentation/reducer.ts +486 -0
- package/src/presentation/store.ts +256 -0
- package/src/providers/anthropic.ts +22 -4
- package/src/providers/detect.ts +2 -1
- package/src/providers/router.ts +13 -0
- package/src/repo/repomap.ts +2 -29
- package/src/session/checkpoint.ts +27 -9
- package/src/session/journal.ts +27 -5
- package/src/skills/loader.ts +88 -5
- package/src/telemetry/trace.ts +36 -15
- package/src/tools/ask_user.ts +73 -4
- package/src/tools/bash.ts +15 -17
- package/src/tools/glob.ts +10 -11
- package/src/tools/lsp.ts +3 -56
- package/src/tools/memory.ts +13 -12
- package/src/tools/task.ts +1 -0
- package/src/ui/approval/prompt.ts +14 -6
- package/src/ui/assistant/simple.ts +137 -26
- package/src/ui/assistant/turn-status.ts +16 -3
- package/src/ui/contract.ts +119 -0
- package/src/ui/footer.ts +21 -2
- package/src/ui/i18n/en.ts +34 -6
- package/src/ui/i18n/id.ts +34 -6
- package/src/ui/i18n/locale.ts +3 -1
- package/src/ui/input/input.ts +51 -4
- package/src/ui/input/prompt-engine.ts +46 -4
- package/src/ui/render/collapse.ts +13 -13
- package/src/ui/render/diff.ts +7 -2
- package/src/ui/render/format.ts +16 -8
- package/src/ui/render/highlight.ts +7 -3
- package/src/ui/render/markdown.ts +5 -1
- package/src/ui/render/sanitize.ts +29 -2
- package/src/ui/render/table.ts +8 -5
- package/src/ui/render/theme.ts +18 -4
- package/src/ui/render/width.ts +89 -10
- package/src/ui/runtime/motion.ts +16 -0
- package/src/ui/runtime/screen.ts +45 -0
- package/src/ui/runtime/spinner.ts +50 -4
- package/src/ui/runtime/statusline.ts +50 -1
- package/src/ui/screens/form.ts +42 -15
- package/src/ui/screens/model-manager.ts +26 -18
- package/src/ui/screens/picker.ts +47 -23
- package/src/ui/screens/provider-manager.ts +18 -12
- package/src/ui/screens/wizard.ts +24 -8
- package/src/ui/tui/app.ts +233 -27
- package/src/ui/tui/transcript.ts +269 -65
- package/vendor/minicore/LICENSE +21 -0
- package/vendor/minicore/VENDOR.md +6 -3
- package/vendor/minicore/package.json +6 -3
- package/vendor/minicore/src/core/loop.ts +12 -2
- package/vendor/minicore/src/core/snapshot.ts +4 -0
- package/vendor/minicore/src/providers/openai-compat.ts +76 -25
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
// Reducer presentasi murni (§15 plan) — satu-satunya penulis PresentationState.
|
|
2
|
+
//
|
|
3
|
+
// DILANGGAN: jam acak / berkas / jaringan / env / terminal. Semua waktu
|
|
4
|
+
// tiba sebagai data event (ts, durationMs) dari adapter. Counter diagnostik
|
|
5
|
+
// di LUAR state semantik (objek terpisah) — §30.
|
|
6
|
+
//
|
|
7
|
+
// Semantik kunci:
|
|
8
|
+
// · first-terminal-wins — terminal kedua toolCallId sama diabaikan + counter
|
|
9
|
+
// · force-close I-A08 — turn settle → approval terbuka → cancelled(parent-ended)
|
|
10
|
+
// · deriveSupersedes — retry-link deterministik dari eventSeq (§10)
|
|
11
|
+
// · rebuildFromDurable — crash/restart: open → interrupted, approval → force-close
|
|
12
|
+
|
|
13
|
+
import { type DomainEvent, DURABILITY } from "./events.ts"
|
|
14
|
+
import { labelTool, targetOf } from "./label.ts"
|
|
15
|
+
import {
|
|
16
|
+
type ActivityEntry,
|
|
17
|
+
type ApprovalEntry,
|
|
18
|
+
activityKey,
|
|
19
|
+
type PresentationState,
|
|
20
|
+
type ToolStatus,
|
|
21
|
+
type TurnEntry,
|
|
22
|
+
type TurnStatus,
|
|
23
|
+
turnKey,
|
|
24
|
+
} from "./model.ts"
|
|
25
|
+
|
|
26
|
+
export interface ReducerDiagnostics {
|
|
27
|
+
eventsIn: number
|
|
28
|
+
/** Terminal kedua untuk id sama (first-terminal-wins). */
|
|
29
|
+
duplicateTerminal: number
|
|
30
|
+
/** Event setelah entry terminal (progress/late settle). */
|
|
31
|
+
lateEvent: number
|
|
32
|
+
/** Terminal/progress tanpa started. */
|
|
33
|
+
orphanTool: number
|
|
34
|
+
/** Settle tanpa requested. */
|
|
35
|
+
orphanApproval: number
|
|
36
|
+
/** Turn settle ganda. */
|
|
37
|
+
duplicateTurn: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createReducerDiagnostics(): ReducerDiagnostics {
|
|
41
|
+
return {
|
|
42
|
+
eventsIn: 0,
|
|
43
|
+
duplicateTerminal: 0,
|
|
44
|
+
lateEvent: 0,
|
|
45
|
+
orphanTool: 0,
|
|
46
|
+
orphanApproval: 0,
|
|
47
|
+
duplicateTurn: 0,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const TOOL_TERMINAL = new Set<ToolStatus>([
|
|
52
|
+
"completed",
|
|
53
|
+
"failed",
|
|
54
|
+
"denied",
|
|
55
|
+
"cancelled",
|
|
56
|
+
"interrupted",
|
|
57
|
+
])
|
|
58
|
+
|
|
59
|
+
const TURN_TERMINAL = new Set<TurnStatus>(["completed", "failed", "cancelled", "interrupted"])
|
|
60
|
+
|
|
61
|
+
function isToolTerminal(status: ToolStatus): boolean {
|
|
62
|
+
return TOOL_TERMINAL.has(status)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isTurnTerminal(status: TurnStatus): boolean {
|
|
66
|
+
return TURN_TERMINAL.has(status)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function ensureTurn(
|
|
70
|
+
state: PresentationState,
|
|
71
|
+
sessionId: string,
|
|
72
|
+
turnId: number,
|
|
73
|
+
seq: number,
|
|
74
|
+
ts: number,
|
|
75
|
+
): TurnEntry {
|
|
76
|
+
const tKey = turnKey(sessionId, turnId)
|
|
77
|
+
const existing = state.turns.get(tKey)
|
|
78
|
+
if (existing) return existing
|
|
79
|
+
const entry: TurnEntry = {
|
|
80
|
+
kind: "turn",
|
|
81
|
+
seq,
|
|
82
|
+
turnId,
|
|
83
|
+
sessionId,
|
|
84
|
+
status: "running",
|
|
85
|
+
tsStart: ts,
|
|
86
|
+
}
|
|
87
|
+
state.turns.set(tKey, entry)
|
|
88
|
+
if (!state.order.some((o) => o.kind === "turn" && o.id === tKey)) {
|
|
89
|
+
state.order.push({ kind: "turn", id: tKey, seq })
|
|
90
|
+
}
|
|
91
|
+
return entry
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Predikat retry (§10): same turn + same qualified+target + old ∈ {failed,denied}
|
|
96
|
+
* + old.endSeq < new.startSeq. Paralel tumpang-tindih TIDAK ditautkan.
|
|
97
|
+
* Pilih kandidat terminal-seq terbesar bila >1.
|
|
98
|
+
*/
|
|
99
|
+
export function deriveSupersedes(
|
|
100
|
+
state: PresentationState,
|
|
101
|
+
probe: {
|
|
102
|
+
sessionId: string
|
|
103
|
+
turnId: number
|
|
104
|
+
qualified: string
|
|
105
|
+
target?: string
|
|
106
|
+
startSeq: number
|
|
107
|
+
excludeId?: string
|
|
108
|
+
},
|
|
109
|
+
): string | undefined {
|
|
110
|
+
let best: { id: string; endSeq: number } | undefined
|
|
111
|
+
for (const a of state.activities.values()) {
|
|
112
|
+
if (a.sessionId !== probe.sessionId) continue
|
|
113
|
+
if (a.turnId !== probe.turnId) continue
|
|
114
|
+
if (a.toolCallId === probe.excludeId) continue
|
|
115
|
+
if (a.identity.qualified !== probe.qualified) continue
|
|
116
|
+
if ((a.target ?? undefined) !== (probe.target ?? undefined)) continue
|
|
117
|
+
if (a.status !== "failed" && a.status !== "denied") continue
|
|
118
|
+
if (a.endSeq === undefined || a.endSeq >= probe.startSeq) continue
|
|
119
|
+
if (!best || a.endSeq > best.endSeq) best = { id: a.toolCallId, endSeq: a.endSeq }
|
|
120
|
+
}
|
|
121
|
+
return best?.id
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function forceCloseTurn(
|
|
125
|
+
state: PresentationState,
|
|
126
|
+
sessionId: string,
|
|
127
|
+
turnId: number,
|
|
128
|
+
seq: number,
|
|
129
|
+
): void {
|
|
130
|
+
// I-A08: approval terbuka turn ini → settled{cancelled(parent-ended)}.
|
|
131
|
+
for (const ap of state.approvals.values()) {
|
|
132
|
+
if (ap.turnId !== turnId || ap.state !== "requested") continue
|
|
133
|
+
// approvalId milik turn; cocokkan sessionId via tool bila ada — turnId
|
|
134
|
+
// cukup karena approvalId unik per sesi runtime.
|
|
135
|
+
ap.state = "settled"
|
|
136
|
+
ap.outcome = { decision: "cancelled", by: "system", reason: "parent-ended" }
|
|
137
|
+
ap.seq = seq
|
|
138
|
+
}
|
|
139
|
+
// Running tools turn itu → cancelled(parent-ended) — bukan failed (kegagalan
|
|
140
|
+
// milik turn, bukan tiap tool). interrupted TIDAK di sini (itu rebuild).
|
|
141
|
+
for (const a of state.activities.values()) {
|
|
142
|
+
if (a.sessionId !== sessionId || a.turnId !== turnId) continue
|
|
143
|
+
if (a.status !== "running") continue
|
|
144
|
+
a.status = "cancelled"
|
|
145
|
+
a.endSeq = seq
|
|
146
|
+
a.tsEnd = a.tsEnd ?? seq
|
|
147
|
+
a.error = undefined
|
|
148
|
+
a.summary = a.summary ?? "cancelled (parent-ended)"
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Terapkan SATU event. Mutable-ke-dalam (state milik caller) — deterministik
|
|
154
|
+
* selama eventSeq urut; tanpa side-effect selain mutasi state + diag.
|
|
155
|
+
*/
|
|
156
|
+
export function reduce(
|
|
157
|
+
state: PresentationState,
|
|
158
|
+
event: DomainEvent,
|
|
159
|
+
diag: ReducerDiagnostics = createReducerDiagnostics(),
|
|
160
|
+
): PresentationState {
|
|
161
|
+
diag.eventsIn++
|
|
162
|
+
const seq = event.eventSeq
|
|
163
|
+
// Live-only (delta/progress) TIDAK menggeser state.seq — replay durable
|
|
164
|
+
// harus menghasilkan seq identik (progress memang tak pernah direkam).
|
|
165
|
+
if (DURABILITY[event.type]?.durable && seq > state.seq) state.seq = seq
|
|
166
|
+
|
|
167
|
+
switch (event.type) {
|
|
168
|
+
case "turn.started": {
|
|
169
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
170
|
+
return state
|
|
171
|
+
}
|
|
172
|
+
case "tool.started": {
|
|
173
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
174
|
+
const key = activityKey(event.sessionId, event.toolCallId)
|
|
175
|
+
const existing = state.activities.get(key)
|
|
176
|
+
if (existing) {
|
|
177
|
+
if (isToolTerminal(existing.status)) diag.duplicateTerminal++
|
|
178
|
+
else diag.lateEvent++
|
|
179
|
+
return state
|
|
180
|
+
}
|
|
181
|
+
const target = event.argsSummary.target ?? targetOf(undefined)
|
|
182
|
+
const entry: ActivityEntry = {
|
|
183
|
+
kind: "tool",
|
|
184
|
+
seq,
|
|
185
|
+
turnId: event.turnId,
|
|
186
|
+
stepId: event.stepId,
|
|
187
|
+
toolCallId: event.toolCallId,
|
|
188
|
+
sessionId: event.sessionId,
|
|
189
|
+
...(event.parentLink ? { parentToolCallId: event.parentLink.parentToolCallId } : {}),
|
|
190
|
+
identity: event.identity,
|
|
191
|
+
...(target ? { target } : {}),
|
|
192
|
+
status: "running",
|
|
193
|
+
tsStart: event.ts,
|
|
194
|
+
summary: labelTool(event.identity, event.argsSummary).summary,
|
|
195
|
+
}
|
|
196
|
+
const sup = deriveSupersedes(state, {
|
|
197
|
+
sessionId: event.sessionId,
|
|
198
|
+
turnId: event.turnId,
|
|
199
|
+
qualified: event.identity.qualified,
|
|
200
|
+
target,
|
|
201
|
+
startSeq: seq,
|
|
202
|
+
excludeId: event.toolCallId,
|
|
203
|
+
})
|
|
204
|
+
if (sup) entry.supersedes = sup
|
|
205
|
+
state.activities.set(key, entry)
|
|
206
|
+
state.order.push({ kind: "tool", id: event.toolCallId, seq })
|
|
207
|
+
return state
|
|
208
|
+
}
|
|
209
|
+
case "tool.progress": {
|
|
210
|
+
const key = activityKey(event.sessionId, event.toolCallId)
|
|
211
|
+
const a = state.activities.get(key)
|
|
212
|
+
if (!a) {
|
|
213
|
+
diag.orphanTool++
|
|
214
|
+
return state
|
|
215
|
+
}
|
|
216
|
+
if (isToolTerminal(a.status)) {
|
|
217
|
+
diag.lateEvent++
|
|
218
|
+
return state
|
|
219
|
+
}
|
|
220
|
+
a.progress = event.message
|
|
221
|
+
return state
|
|
222
|
+
}
|
|
223
|
+
case "tool.completed":
|
|
224
|
+
case "tool.failed":
|
|
225
|
+
case "tool.denied":
|
|
226
|
+
case "tool.cancelled": {
|
|
227
|
+
const key = activityKey(event.sessionId, event.toolCallId)
|
|
228
|
+
let a = state.activities.get(key)
|
|
229
|
+
if (!a) {
|
|
230
|
+
// Terminal tanpa started (forward race / crash-rebuild) — entry minimal.
|
|
231
|
+
diag.orphanTool++
|
|
232
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
233
|
+
a = {
|
|
234
|
+
kind: "tool",
|
|
235
|
+
seq,
|
|
236
|
+
turnId: event.turnId,
|
|
237
|
+
stepId: 0,
|
|
238
|
+
toolCallId: event.toolCallId,
|
|
239
|
+
sessionId: event.sessionId,
|
|
240
|
+
identity: { origin: "builtin", name: event.toolCallId, qualified: event.toolCallId },
|
|
241
|
+
status: "running",
|
|
242
|
+
tsStart: event.ts,
|
|
243
|
+
incomplete: true,
|
|
244
|
+
parentToolCallId: event.parentLink?.parentToolCallId,
|
|
245
|
+
}
|
|
246
|
+
state.activities.set(key, a)
|
|
247
|
+
state.order.push({ kind: "tool", id: event.toolCallId, seq })
|
|
248
|
+
}
|
|
249
|
+
if (isToolTerminal(a.status)) {
|
|
250
|
+
diag.duplicateTerminal++
|
|
251
|
+
return state
|
|
252
|
+
}
|
|
253
|
+
a.endSeq = seq
|
|
254
|
+
a.tsEnd = event.ts
|
|
255
|
+
if (event.type === "tool.completed") {
|
|
256
|
+
a.status = "completed"
|
|
257
|
+
a.durationMs = event.durationMs
|
|
258
|
+
a.summary = event.summary
|
|
259
|
+
a.expandRef = event.expandRef
|
|
260
|
+
if (event.receipt) a.receipt = event.receipt
|
|
261
|
+
} else if (event.type === "tool.failed") {
|
|
262
|
+
a.status = "failed"
|
|
263
|
+
a.durationMs = event.durationMs
|
|
264
|
+
a.error = {
|
|
265
|
+
cause: event.cause,
|
|
266
|
+
message: event.message,
|
|
267
|
+
...(event.hint ? { hint: event.hint } : {}),
|
|
268
|
+
}
|
|
269
|
+
a.expandRef = event.expandRef
|
|
270
|
+
} else if (event.type === "tool.denied") {
|
|
271
|
+
a.status = "denied"
|
|
272
|
+
a.denyReason = event.reason
|
|
273
|
+
a.summary = a.summary ?? `denied: ${event.reason}`
|
|
274
|
+
} else {
|
|
275
|
+
a.status = "cancelled"
|
|
276
|
+
a.summary = a.summary ?? `cancelled (${event.reason})`
|
|
277
|
+
}
|
|
278
|
+
return state
|
|
279
|
+
}
|
|
280
|
+
case "approval.requested": {
|
|
281
|
+
const existing = state.approvals.get(event.approvalId)
|
|
282
|
+
if (existing) {
|
|
283
|
+
if (existing.state === "settled") diag.duplicateTerminal++
|
|
284
|
+
else diag.lateEvent++
|
|
285
|
+
return state
|
|
286
|
+
}
|
|
287
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
288
|
+
const entry: ApprovalEntry = {
|
|
289
|
+
kind: "approval",
|
|
290
|
+
seq,
|
|
291
|
+
turnId: event.turnId,
|
|
292
|
+
approvalId: event.approvalId,
|
|
293
|
+
toolCallId: event.toolCallId ?? "",
|
|
294
|
+
identity: event.identity,
|
|
295
|
+
state: "requested",
|
|
296
|
+
}
|
|
297
|
+
state.approvals.set(event.approvalId, entry)
|
|
298
|
+
state.order.push({ kind: "approval", id: event.approvalId, seq })
|
|
299
|
+
if (event.toolCallId) {
|
|
300
|
+
const a = state.activities.get(activityKey(event.sessionId, event.toolCallId))
|
|
301
|
+
if (a && !a.approvalId) a.approvalId = event.approvalId
|
|
302
|
+
}
|
|
303
|
+
return state
|
|
304
|
+
}
|
|
305
|
+
case "approval.settled": {
|
|
306
|
+
let ap = state.approvals.get(event.approvalId)
|
|
307
|
+
if (!ap) {
|
|
308
|
+
diag.orphanApproval++
|
|
309
|
+
ap = {
|
|
310
|
+
kind: "approval",
|
|
311
|
+
seq,
|
|
312
|
+
turnId: event.turnId,
|
|
313
|
+
approvalId: event.approvalId,
|
|
314
|
+
toolCallId: event.toolCallId ?? "",
|
|
315
|
+
identity: { origin: "builtin", name: event.approvalId, qualified: event.approvalId },
|
|
316
|
+
state: "settled",
|
|
317
|
+
outcome: event.outcome,
|
|
318
|
+
}
|
|
319
|
+
state.approvals.set(event.approvalId, ap)
|
|
320
|
+
state.order.push({ kind: "approval", id: event.approvalId, seq })
|
|
321
|
+
return state
|
|
322
|
+
}
|
|
323
|
+
if (ap.state === "settled") {
|
|
324
|
+
diag.duplicateTerminal++
|
|
325
|
+
return state
|
|
326
|
+
}
|
|
327
|
+
ap.state = "settled"
|
|
328
|
+
ap.outcome = event.outcome
|
|
329
|
+
ap.seq = seq
|
|
330
|
+
return state
|
|
331
|
+
}
|
|
332
|
+
case "file.changed": {
|
|
333
|
+
const a = state.activities.get(activityKey(event.sessionId, event.toolCallId))
|
|
334
|
+
if (!a) {
|
|
335
|
+
diag.orphanTool++
|
|
336
|
+
return state
|
|
337
|
+
}
|
|
338
|
+
const prev = a.receipt
|
|
339
|
+
// dedup: paths sama + journalSeq sama = idempoten
|
|
340
|
+
if (
|
|
341
|
+
prev &&
|
|
342
|
+
prev.journalSeq === event.journalSeq &&
|
|
343
|
+
JSON.stringify(prev.paths ?? []) === JSON.stringify(event.paths)
|
|
344
|
+
) {
|
|
345
|
+
return state
|
|
346
|
+
}
|
|
347
|
+
a.receipt = {
|
|
348
|
+
toolCallId: event.toolCallId,
|
|
349
|
+
...(event.journalSeq !== undefined ? { journalSeq: event.journalSeq } : {}),
|
|
350
|
+
...(event.checkpointId !== undefined ? { checkpointId: event.checkpointId } : {}),
|
|
351
|
+
paths: event.paths,
|
|
352
|
+
...(prev?.stats ? { stats: prev.stats } : {}),
|
|
353
|
+
...(prev?.test ? { test: prev.test } : {}),
|
|
354
|
+
...(prev?.cmd ? { cmd: prev.cmd } : {}),
|
|
355
|
+
}
|
|
356
|
+
return state
|
|
357
|
+
}
|
|
358
|
+
case "test.completed": {
|
|
359
|
+
const a = state.activities.get(activityKey(event.sessionId, event.toolCallId))
|
|
360
|
+
if (!a) {
|
|
361
|
+
diag.orphanTool++
|
|
362
|
+
return state
|
|
363
|
+
}
|
|
364
|
+
const receipt = a.receipt ?? { toolCallId: event.toolCallId }
|
|
365
|
+
receipt.test = { passed: event.passed, failed: event.failed, summary: event.summary }
|
|
366
|
+
a.receipt = receipt
|
|
367
|
+
return state
|
|
368
|
+
}
|
|
369
|
+
case "model.completed": {
|
|
370
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
371
|
+
// last-wins per (turnId, role=assistant) bila replay kirim ulang
|
|
372
|
+
const idx = state.conversation.findIndex(
|
|
373
|
+
(m) => m.turnId === event.turnId && m.role === "assistant",
|
|
374
|
+
)
|
|
375
|
+
const entry = {
|
|
376
|
+
kind: "message" as const,
|
|
377
|
+
seq,
|
|
378
|
+
turnId: event.turnId,
|
|
379
|
+
role: "assistant" as const,
|
|
380
|
+
text: event.text,
|
|
381
|
+
truncated: event.truncated,
|
|
382
|
+
...(event.expandRef ? { expandRef: event.expandRef } : {}),
|
|
383
|
+
}
|
|
384
|
+
if (idx >= 0) state.conversation[idx] = entry
|
|
385
|
+
else {
|
|
386
|
+
state.conversation.push(entry)
|
|
387
|
+
state.order.push({ kind: "message", id: `msg:${seq}`, seq })
|
|
388
|
+
}
|
|
389
|
+
return state
|
|
390
|
+
}
|
|
391
|
+
case "reasoning.completed": {
|
|
392
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
393
|
+
state.order.push({ kind: "reasoning", id: `rsn:${seq}`, seq })
|
|
394
|
+
return state
|
|
395
|
+
}
|
|
396
|
+
case "context.compacted": {
|
|
397
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
398
|
+
state.order.push({ kind: "system", id: `sys:${seq}`, seq })
|
|
399
|
+
return state
|
|
400
|
+
}
|
|
401
|
+
case "turn.completed":
|
|
402
|
+
case "turn.failed":
|
|
403
|
+
case "turn.cancelled": {
|
|
404
|
+
const turn = ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
405
|
+
if (isTurnTerminal(turn.status)) {
|
|
406
|
+
diag.duplicateTurn++
|
|
407
|
+
return state
|
|
408
|
+
}
|
|
409
|
+
turn.tsEnd = event.ts
|
|
410
|
+
if (event.type === "turn.completed") {
|
|
411
|
+
turn.status = "completed"
|
|
412
|
+
turn.summary = event.summary
|
|
413
|
+
} else if (event.type === "turn.failed") {
|
|
414
|
+
turn.status = "failed"
|
|
415
|
+
turn.error = event.error.message
|
|
416
|
+
} else {
|
|
417
|
+
turn.status = "cancelled"
|
|
418
|
+
turn.error = event.reason
|
|
419
|
+
}
|
|
420
|
+
forceCloseTurn(state, event.sessionId, event.turnId, seq)
|
|
421
|
+
return state
|
|
422
|
+
}
|
|
423
|
+
case "model.delta":
|
|
424
|
+
case "reasoning.delta":
|
|
425
|
+
// live-only — reducer tidak menyimpan stream (§17).
|
|
426
|
+
return state
|
|
427
|
+
default: {
|
|
428
|
+
// Event tak dikenal (evolusi kontrak) — jangan throw; abaikan.
|
|
429
|
+
const _exhaustive: never = event
|
|
430
|
+
void _exhaustive
|
|
431
|
+
return state
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Rekonstruksi dari event durable (§12): terapkan DURABILITY.durable saja,
|
|
438
|
+
* lalu tutup yang terbuka — activities → interrupted, approvals → force-close,
|
|
439
|
+
* turn tanpa settle → interrupted.
|
|
440
|
+
*/
|
|
441
|
+
export function rebuildFromDurable(
|
|
442
|
+
durable: readonly DomainEvent[],
|
|
443
|
+
diag: ReducerDiagnostics = createReducerDiagnostics(),
|
|
444
|
+
): { state: PresentationState; sessionId: string } {
|
|
445
|
+
const first = durable.find((e) => DURABILITY[e.type]?.durable)
|
|
446
|
+
const sessionId = first?.sessionId ?? ""
|
|
447
|
+
const state: PresentationState = {
|
|
448
|
+
sessionId,
|
|
449
|
+
seq: 0,
|
|
450
|
+
turns: new Map(),
|
|
451
|
+
activities: new Map(),
|
|
452
|
+
approvals: new Map(),
|
|
453
|
+
conversation: [],
|
|
454
|
+
order: [],
|
|
455
|
+
}
|
|
456
|
+
for (const e of durable) {
|
|
457
|
+
if (!DURABILITY[e.type]?.durable) continue
|
|
458
|
+
reduce(state, e, diag)
|
|
459
|
+
}
|
|
460
|
+
// Sesi berakhir tanpa terminal → interrupted (satu-satunya status disimpulkan).
|
|
461
|
+
const endSeq = state.seq + 1
|
|
462
|
+
for (const a of state.activities.values()) {
|
|
463
|
+
if (a.status === "running") {
|
|
464
|
+
a.status = "interrupted"
|
|
465
|
+
a.endSeq = endSeq
|
|
466
|
+
a.summary = a.summary ?? "interrupted — verify before retry"
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
for (const ap of state.approvals.values()) {
|
|
470
|
+
if (ap.state === "requested") {
|
|
471
|
+
ap.state = "settled"
|
|
472
|
+
ap.outcome = { decision: "cancelled", by: "system", reason: "parent-ended" }
|
|
473
|
+
ap.seq = endSeq
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
for (const t of state.turns.values()) {
|
|
477
|
+
if (t.status === "running") {
|
|
478
|
+
t.status = "interrupted"
|
|
479
|
+
t.error = t.error ?? "process ended before turn settle"
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return { state, sessionId }
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
export type { ActivityEntry, ApprovalEntry, PresentationState, TurnEntry } from "./model.ts"
|
|
486
|
+
export { cloneState, createInitialState } from "./model.ts"
|