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
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
1
|
+
import type { ChildSessionLink, TurnSummary } from "./events.ts"
|
|
13
2
|
import { type DomainEvent, DURABILITY } from "./events.ts"
|
|
14
|
-
import { labelTool
|
|
3
|
+
import { labelTool } from "./label.ts"
|
|
15
4
|
import {
|
|
16
5
|
type ActivityEntry,
|
|
17
6
|
type ApprovalEntry,
|
|
18
7
|
activityKey,
|
|
8
|
+
approvalKey,
|
|
9
|
+
type ConversationEntry,
|
|
10
|
+
conversationKey,
|
|
11
|
+
createInitialState,
|
|
12
|
+
type DiagnosticEntry,
|
|
13
|
+
type FindingEntry,
|
|
14
|
+
MAX_STATE_ENTRIES,
|
|
15
|
+
type PlanEntry,
|
|
19
16
|
type PresentationState,
|
|
17
|
+
type ReasoningEntry,
|
|
18
|
+
type ResultEntry,
|
|
19
|
+
type SystemEntry,
|
|
20
20
|
type ToolStatus,
|
|
21
21
|
type TurnEntry,
|
|
22
22
|
type TurnStatus,
|
|
@@ -25,16 +25,13 @@ import {
|
|
|
25
25
|
|
|
26
26
|
export interface ReducerDiagnostics {
|
|
27
27
|
eventsIn: number
|
|
28
|
-
/** Terminal kedua untuk id sama (first-terminal-wins). */
|
|
29
28
|
duplicateTerminal: number
|
|
30
|
-
/** Event setelah entry terminal (progress/late settle). */
|
|
31
29
|
lateEvent: number
|
|
32
|
-
/** Terminal/progress tanpa started. */
|
|
33
30
|
orphanTool: number
|
|
34
|
-
/** Settle tanpa requested. */
|
|
35
31
|
orphanApproval: number
|
|
36
|
-
/** Turn settle ganda. */
|
|
37
32
|
duplicateTurn: number
|
|
33
|
+
unknownEvent: number
|
|
34
|
+
orphanEvidence: number
|
|
38
35
|
}
|
|
39
36
|
|
|
40
37
|
export function createReducerDiagnostics(): ReducerDiagnostics {
|
|
@@ -45,6 +42,8 @@ export function createReducerDiagnostics(): ReducerDiagnostics {
|
|
|
45
42
|
orphanTool: 0,
|
|
46
43
|
orphanApproval: 0,
|
|
47
44
|
duplicateTurn: 0,
|
|
45
|
+
unknownEvent: 0,
|
|
46
|
+
orphanEvidence: 0,
|
|
48
47
|
}
|
|
49
48
|
}
|
|
50
49
|
|
|
@@ -55,8 +54,15 @@ const TOOL_TERMINAL = new Set<ToolStatus>([
|
|
|
55
54
|
"cancelled",
|
|
56
55
|
"interrupted",
|
|
57
56
|
])
|
|
58
|
-
|
|
59
57
|
const TURN_TERMINAL = new Set<TurnStatus>(["completed", "failed", "cancelled", "interrupted"])
|
|
58
|
+
const MUTATION_TOOLS = new Set([
|
|
59
|
+
"bash",
|
|
60
|
+
"edit",
|
|
61
|
+
"write_file",
|
|
62
|
+
"apply_patch",
|
|
63
|
+
"delete_file",
|
|
64
|
+
"git_commit",
|
|
65
|
+
])
|
|
60
66
|
|
|
61
67
|
function isToolTerminal(status: ToolStatus): boolean {
|
|
62
68
|
return TOOL_TERMINAL.has(status)
|
|
@@ -66,36 +72,101 @@ function isTurnTerminal(status: TurnStatus): boolean {
|
|
|
66
72
|
return TURN_TERMINAL.has(status)
|
|
67
73
|
}
|
|
68
74
|
|
|
75
|
+
function addOrder(
|
|
76
|
+
state: PresentationState,
|
|
77
|
+
kind: PresentationState["order"][number]["kind"],
|
|
78
|
+
id: string,
|
|
79
|
+
seq: number,
|
|
80
|
+
): void {
|
|
81
|
+
if (!state.order.some((o) => o.kind === kind && o.id === id)) state.order.push({ kind, id, seq })
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function removeOrder(
|
|
85
|
+
state: PresentationState,
|
|
86
|
+
kind: PresentationState["order"][number]["kind"],
|
|
87
|
+
id: string,
|
|
88
|
+
): void {
|
|
89
|
+
for (let i = state.order.length - 1; i >= 0; i--) {
|
|
90
|
+
if (state.order[i]!.kind === kind && state.order[i]!.id === id) state.order.splice(i, 1)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
69
94
|
function ensureTurn(
|
|
70
95
|
state: PresentationState,
|
|
71
96
|
sessionId: string,
|
|
72
97
|
turnId: number,
|
|
73
98
|
seq: number,
|
|
74
99
|
ts: number,
|
|
100
|
+
promptRef?: string,
|
|
75
101
|
): TurnEntry {
|
|
76
|
-
const
|
|
77
|
-
const existing = state.turns.get(
|
|
78
|
-
if (existing)
|
|
102
|
+
const key = turnKey(sessionId, turnId)
|
|
103
|
+
const existing = state.turns.get(key)
|
|
104
|
+
if (existing) {
|
|
105
|
+
if (promptRef && !existing.promptRef) existing.promptRef = promptRef
|
|
106
|
+
return existing
|
|
107
|
+
}
|
|
79
108
|
const entry: TurnEntry = {
|
|
80
109
|
kind: "turn",
|
|
81
110
|
seq,
|
|
82
111
|
turnId,
|
|
83
112
|
sessionId,
|
|
113
|
+
...(promptRef ? { promptRef } : {}),
|
|
84
114
|
status: "running",
|
|
85
115
|
tsStart: ts,
|
|
116
|
+
evidenceComplete: true,
|
|
86
117
|
}
|
|
87
|
-
state.turns.set(
|
|
88
|
-
|
|
89
|
-
|
|
118
|
+
state.turns.set(key, entry)
|
|
119
|
+
addOrder(state, "turn", key, seq)
|
|
120
|
+
return entry
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function approvalMapKey(state: PresentationState, sessionId: string, approvalId: string): string {
|
|
124
|
+
return sessionId === state.sessionId ? approvalId : approvalKey(sessionId, approvalId)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function evidenceActivity(
|
|
128
|
+
state: PresentationState,
|
|
129
|
+
event: Extract<
|
|
130
|
+
DomainEvent,
|
|
131
|
+
{
|
|
132
|
+
type:
|
|
133
|
+
| "file.changed"
|
|
134
|
+
| "test.completed"
|
|
135
|
+
| "tool.completed"
|
|
136
|
+
| "tool.failed"
|
|
137
|
+
| "tool.denied"
|
|
138
|
+
| "tool.cancelled"
|
|
139
|
+
| "verification.observed"
|
|
140
|
+
}
|
|
141
|
+
> & { parentLink?: ChildSessionLink },
|
|
142
|
+
diag: ReducerDiagnostics,
|
|
143
|
+
): ActivityEntry {
|
|
144
|
+
const key = activityKey(event.sessionId, event.toolCallId)
|
|
145
|
+
const existing = state.activities.get(key)
|
|
146
|
+
if (existing) return existing
|
|
147
|
+
if (event.type.startsWith("tool.")) diag.orphanTool++
|
|
148
|
+
diag.orphanEvidence++
|
|
149
|
+
ensureTurn(state, event.sessionId, event.turnId, event.eventSeq, event.ts)
|
|
150
|
+
const entry: ActivityEntry = {
|
|
151
|
+
kind: "tool",
|
|
152
|
+
seq: event.eventSeq,
|
|
153
|
+
turnId: event.turnId,
|
|
154
|
+
stepId: 0,
|
|
155
|
+
toolCallId: event.toolCallId,
|
|
156
|
+
sessionId: event.sessionId,
|
|
157
|
+
identity: { origin: "builtin", name: event.toolCallId, qualified: event.toolCallId },
|
|
158
|
+
...(event.parentLink ? { parentToolCallId: event.parentLink.parentToolCallId } : {}),
|
|
159
|
+
status: "completed",
|
|
160
|
+
tsStart: event.ts,
|
|
161
|
+
tsEnd: event.ts,
|
|
162
|
+
durationMs: 0,
|
|
163
|
+
incomplete: true,
|
|
90
164
|
}
|
|
165
|
+
state.activities.set(key, entry)
|
|
166
|
+
addOrder(state, "tool", key, event.eventSeq)
|
|
91
167
|
return entry
|
|
92
168
|
}
|
|
93
169
|
|
|
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
170
|
export function deriveSupersedes(
|
|
100
171
|
state: PresentationState,
|
|
101
172
|
probe: {
|
|
@@ -109,8 +180,7 @@ export function deriveSupersedes(
|
|
|
109
180
|
): string | undefined {
|
|
110
181
|
let best: { id: string; endSeq: number } | undefined
|
|
111
182
|
for (const a of state.activities.values()) {
|
|
112
|
-
if (a.sessionId !== probe.sessionId) continue
|
|
113
|
-
if (a.turnId !== probe.turnId) continue
|
|
183
|
+
if (a.sessionId !== probe.sessionId || a.turnId !== probe.turnId) continue
|
|
114
184
|
if (a.toolCallId === probe.excludeId) continue
|
|
115
185
|
if (a.identity.qualified !== probe.qualified) continue
|
|
116
186
|
if ((a.target ?? undefined) !== (probe.target ?? undefined)) continue
|
|
@@ -121,38 +191,149 @@ export function deriveSupersedes(
|
|
|
121
191
|
return best?.id
|
|
122
192
|
}
|
|
123
193
|
|
|
194
|
+
function normalizedPath(path: string): string {
|
|
195
|
+
return path.replace(/\\/g, "/").replace(/^\.\/+/, "")
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function deriveTurnSummary(
|
|
199
|
+
state: PresentationState,
|
|
200
|
+
sessionId: string,
|
|
201
|
+
turnId: number,
|
|
202
|
+
fallback?: TurnSummary,
|
|
203
|
+
): TurnSummary {
|
|
204
|
+
const turn = state.turns.get(turnKey(sessionId, turnId))
|
|
205
|
+
const activities = [...state.activities.values()].filter(
|
|
206
|
+
(a) => a.sessionId === sessionId && a.turnId === turnId,
|
|
207
|
+
)
|
|
208
|
+
let toolsOk = 0
|
|
209
|
+
let toolsFailed = 0
|
|
210
|
+
let toolsDenied = 0
|
|
211
|
+
let toolsCancelled = 0
|
|
212
|
+
let toolsInterrupted = 0
|
|
213
|
+
const paths = new Set<string>()
|
|
214
|
+
let passed = 0
|
|
215
|
+
let failed = 0
|
|
216
|
+
const testSummaries: string[] = []
|
|
217
|
+
let checkpointId: string | undefined
|
|
218
|
+
let evidenceComplete = turn?.evidenceComplete ?? true
|
|
219
|
+
for (const activity of activities) {
|
|
220
|
+
if (activity.status === "completed") toolsOk++
|
|
221
|
+
else if (activity.status === "failed") toolsFailed++
|
|
222
|
+
else if (activity.status === "denied") toolsDenied++
|
|
223
|
+
else if (activity.status === "cancelled") toolsCancelled++
|
|
224
|
+
else if (activity.status === "interrupted") toolsInterrupted++
|
|
225
|
+
const receipt = activity.receipt
|
|
226
|
+
if (!receipt) {
|
|
227
|
+
if (MUTATION_TOOLS.has(activity.identity.qualified)) evidenceComplete = false
|
|
228
|
+
continue
|
|
229
|
+
}
|
|
230
|
+
for (const path of receipt.paths ?? []) paths.add(normalizedPath(path))
|
|
231
|
+
if (receipt.checkpointId) checkpointId = receipt.checkpointId
|
|
232
|
+
if (receipt.test) {
|
|
233
|
+
passed += receipt.test.passed
|
|
234
|
+
failed += receipt.test.failed
|
|
235
|
+
if (receipt.test.summary) testSummaries.push(receipt.test.summary)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (turn?.checkpointId) checkpointId = turn.checkpointId
|
|
239
|
+
const durationMs =
|
|
240
|
+
turn?.tsEnd !== undefined ? Math.max(0, turn.tsEnd - turn.tsStart) : (fallback?.durationMs ?? 0)
|
|
241
|
+
return {
|
|
242
|
+
toolsOk,
|
|
243
|
+
toolsFailed,
|
|
244
|
+
toolsDenied,
|
|
245
|
+
toolsCancelled,
|
|
246
|
+
toolsInterrupted,
|
|
247
|
+
filesChanged: paths.size,
|
|
248
|
+
...(checkpointId ? { checkpointId } : {}),
|
|
249
|
+
...(testSummaries.length > 0
|
|
250
|
+
? { testSummary: { passed, failed, summary: testSummaries.join("; ") } }
|
|
251
|
+
: {}),
|
|
252
|
+
evidenceComplete,
|
|
253
|
+
durationMs,
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function refreshTurnSummary(
|
|
258
|
+
state: PresentationState,
|
|
259
|
+
sessionId: string,
|
|
260
|
+
turnId: number,
|
|
261
|
+
fallback?: TurnSummary,
|
|
262
|
+
): void {
|
|
263
|
+
const turn = state.turns.get(turnKey(sessionId, turnId))
|
|
264
|
+
if (turn) turn.summary = deriveTurnSummary(state, sessionId, turnId, fallback)
|
|
265
|
+
}
|
|
266
|
+
|
|
124
267
|
function forceCloseTurn(
|
|
125
268
|
state: PresentationState,
|
|
126
269
|
sessionId: string,
|
|
127
270
|
turnId: number,
|
|
128
271
|
seq: number,
|
|
272
|
+
tsEnd: number,
|
|
129
273
|
): void {
|
|
130
|
-
// I-A08: approval terbuka turn ini → settled{cancelled(parent-ended)}.
|
|
131
274
|
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.
|
|
275
|
+
if (ap.sessionId !== sessionId || ap.turnId !== turnId || ap.state !== "requested") continue
|
|
135
276
|
ap.state = "settled"
|
|
136
277
|
ap.outcome = { decision: "cancelled", by: "system", reason: "parent-ended" }
|
|
137
278
|
ap.seq = seq
|
|
138
279
|
}
|
|
139
|
-
// Running tools turn itu → cancelled(parent-ended) — bukan failed (kegagalan
|
|
140
|
-
// milik turn, bukan tiap tool). interrupted TIDAK di sini (itu rebuild).
|
|
141
280
|
for (const a of state.activities.values()) {
|
|
142
|
-
if (a.sessionId !== sessionId || a.turnId !== turnId) continue
|
|
143
|
-
if (a.status !== "running") continue
|
|
281
|
+
if (a.sessionId !== sessionId || a.turnId !== turnId || a.status !== "running") continue
|
|
144
282
|
a.status = "cancelled"
|
|
145
283
|
a.endSeq = seq
|
|
146
|
-
a.tsEnd = a.tsEnd ??
|
|
284
|
+
a.tsEnd = a.tsEnd ?? tsEnd
|
|
285
|
+
a.durationMs = a.durationMs ?? Math.max(0, a.tsEnd - a.tsStart)
|
|
147
286
|
a.error = undefined
|
|
148
287
|
a.summary = a.summary ?? "cancelled (parent-ended)"
|
|
149
288
|
}
|
|
150
289
|
}
|
|
151
290
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
291
|
+
function trimState(state: PresentationState): void {
|
|
292
|
+
const trimArray = <T extends { seq: number }>(
|
|
293
|
+
values: T[],
|
|
294
|
+
kind: PresentationState["order"][number]["kind"],
|
|
295
|
+
id: (value: T) => string,
|
|
296
|
+
removable: (value: T) => boolean = () => true,
|
|
297
|
+
): void => {
|
|
298
|
+
while (values.length > MAX_STATE_ENTRIES) {
|
|
299
|
+
const index = values.findIndex(removable)
|
|
300
|
+
if (index < 0) return
|
|
301
|
+
const [removed] = values.splice(index, 1)
|
|
302
|
+
if (!removed) return
|
|
303
|
+
const key = id(removed)
|
|
304
|
+
state.evicted.push({ kind, id: key, seq: removed.seq, reason: "bounded" })
|
|
305
|
+
removeOrder(state, kind, key)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
trimArray(state.conversation, "message", (v) => v.id)
|
|
309
|
+
trimArray(state.reasoning, "reasoning", (v) => v.id)
|
|
310
|
+
trimArray(state.system, "system", (v) => v.id)
|
|
311
|
+
trimArray(state.diagnostics, "diagnostic", (v) => v.id)
|
|
312
|
+
const trimMap = <T extends { seq: number }>(
|
|
313
|
+
values: Map<string, T>,
|
|
314
|
+
kind: PresentationState["order"][number]["kind"],
|
|
315
|
+
removable: (value: T) => boolean,
|
|
316
|
+
): void => {
|
|
317
|
+
while (values.size > MAX_STATE_ENTRIES) {
|
|
318
|
+
const entry = [...values.entries()].find(([, value]) => removable(value))
|
|
319
|
+
if (!entry) return
|
|
320
|
+
values.delete(entry[0])
|
|
321
|
+
state.evicted.push({ kind, id: entry[0], seq: entry[1].seq, reason: "bounded" })
|
|
322
|
+
removeOrder(state, kind, entry[0])
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
trimMap(state.turns, "turn", (v) => isTurnTerminal(v.status))
|
|
326
|
+
trimMap(state.activities, "tool", (v) => isToolTerminal(v.status))
|
|
327
|
+
trimMap(state.approvals, "approval", (v) => v.state === "settled")
|
|
328
|
+
trimMap(state.plans, "plan", () => true)
|
|
329
|
+
trimMap(state.findings, "finding", () => true)
|
|
330
|
+
trimMap(state.results, "result", () => true)
|
|
331
|
+
if (state.evicted.length > MAX_STATE_ENTRIES)
|
|
332
|
+
state.evicted.splice(0, state.evicted.length - MAX_STATE_ENTRIES)
|
|
333
|
+
if (state.order.length > MAX_STATE_ENTRIES * 2)
|
|
334
|
+
state.order.splice(0, state.order.length - MAX_STATE_ENTRIES * 2)
|
|
335
|
+
}
|
|
336
|
+
|
|
156
337
|
export function reduce(
|
|
157
338
|
state: PresentationState,
|
|
158
339
|
event: DomainEvent,
|
|
@@ -160,327 +341,481 @@ export function reduce(
|
|
|
160
341
|
): PresentationState {
|
|
161
342
|
diag.eventsIn++
|
|
162
343
|
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
344
|
if (DURABILITY[event.type]?.durable && seq > state.seq) state.seq = seq
|
|
166
345
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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++
|
|
346
|
+
try {
|
|
347
|
+
switch (event.type) {
|
|
348
|
+
case "user.message": {
|
|
349
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts, event.promptRef)
|
|
350
|
+
const id = conversationKey(event.sessionId, event.turnId, "user")
|
|
351
|
+
const entry: ConversationEntry = {
|
|
352
|
+
kind: "message",
|
|
353
|
+
id,
|
|
354
|
+
seq,
|
|
355
|
+
sessionId: event.sessionId,
|
|
356
|
+
turnId: event.turnId,
|
|
357
|
+
role: "user",
|
|
358
|
+
text: event.text,
|
|
359
|
+
truncated: false,
|
|
360
|
+
promptRef: event.promptRef,
|
|
361
|
+
}
|
|
362
|
+
const index = state.conversation.findIndex((m) => m.id === id)
|
|
363
|
+
if (index >= 0) state.conversation[index] = entry
|
|
364
|
+
else {
|
|
365
|
+
state.conversation.push(entry)
|
|
366
|
+
addOrder(state, "message", id, seq)
|
|
367
|
+
}
|
|
214
368
|
return state
|
|
215
369
|
}
|
|
216
|
-
|
|
217
|
-
|
|
370
|
+
case "turn.started":
|
|
371
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts, event.promptRef)
|
|
218
372
|
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++
|
|
373
|
+
case "tool.started": {
|
|
232
374
|
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
233
|
-
|
|
375
|
+
const key = activityKey(event.sessionId, event.toolCallId)
|
|
376
|
+
const existing = state.activities.get(key)
|
|
377
|
+
if (existing) {
|
|
378
|
+
if (isToolTerminal(existing.status)) diag.duplicateTerminal++
|
|
379
|
+
else diag.lateEvent++
|
|
380
|
+
return state
|
|
381
|
+
}
|
|
382
|
+
const target = event.argsSummary.target
|
|
383
|
+
const entry: ActivityEntry = {
|
|
234
384
|
kind: "tool",
|
|
235
385
|
seq,
|
|
236
386
|
turnId: event.turnId,
|
|
237
|
-
stepId:
|
|
387
|
+
stepId: event.stepId,
|
|
238
388
|
toolCallId: event.toolCallId,
|
|
239
389
|
sessionId: event.sessionId,
|
|
240
|
-
|
|
390
|
+
...(event.parentLink ? { parentToolCallId: event.parentLink.parentToolCallId } : {}),
|
|
391
|
+
identity: event.identity,
|
|
392
|
+
...(target ? { target } : {}),
|
|
241
393
|
status: "running",
|
|
242
394
|
tsStart: event.ts,
|
|
243
|
-
|
|
244
|
-
parentToolCallId: event.parentLink?.parentToolCallId,
|
|
395
|
+
summary: labelTool(event.identity, event.argsSummary).summary,
|
|
245
396
|
}
|
|
246
|
-
state
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
397
|
+
const supersedes = deriveSupersedes(state, {
|
|
398
|
+
sessionId: event.sessionId,
|
|
399
|
+
turnId: event.turnId,
|
|
400
|
+
qualified: event.identity.qualified,
|
|
401
|
+
target,
|
|
402
|
+
startSeq: seq,
|
|
403
|
+
excludeId: event.toolCallId,
|
|
404
|
+
})
|
|
405
|
+
if (supersedes) entry.supersedes = supersedes
|
|
406
|
+
state.activities.set(key, entry)
|
|
407
|
+
addOrder(state, "tool", key, seq)
|
|
251
408
|
return state
|
|
252
409
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
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 } : {}),
|
|
410
|
+
case "tool.progress": {
|
|
411
|
+
const a = state.activities.get(activityKey(event.sessionId, event.toolCallId))
|
|
412
|
+
if (!a) {
|
|
413
|
+
diag.orphanTool++
|
|
414
|
+
return state
|
|
268
415
|
}
|
|
269
|
-
a.
|
|
270
|
-
|
|
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++
|
|
416
|
+
if (isToolTerminal(a.status)) diag.lateEvent++
|
|
417
|
+
else a.progress = event.message
|
|
285
418
|
return state
|
|
286
419
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
420
|
+
case "tool.completed":
|
|
421
|
+
case "tool.failed":
|
|
422
|
+
case "tool.denied":
|
|
423
|
+
case "tool.cancelled": {
|
|
424
|
+
const a = evidenceActivity(state, event, diag)
|
|
425
|
+
if (isToolTerminal(a.status)) {
|
|
426
|
+
diag.duplicateTerminal++
|
|
427
|
+
return state
|
|
428
|
+
}
|
|
429
|
+
a.endSeq = seq
|
|
430
|
+
a.tsEnd = event.ts
|
|
431
|
+
if (event.type === "tool.completed") {
|
|
432
|
+
a.status = "completed"
|
|
433
|
+
a.durationMs = event.durationMs
|
|
434
|
+
a.summary = event.summary
|
|
435
|
+
a.expandRef = { ...event.expandRef }
|
|
436
|
+
if (event.receipt)
|
|
437
|
+
a.receipt = { ...event.receipt, sessionId: event.sessionId, turnId: event.turnId }
|
|
438
|
+
} else if (event.type === "tool.failed") {
|
|
439
|
+
a.status = "failed"
|
|
440
|
+
a.durationMs = event.durationMs
|
|
441
|
+
a.error = {
|
|
442
|
+
cause: event.cause,
|
|
443
|
+
message: event.message,
|
|
444
|
+
...(event.hint ? { hint: event.hint } : {}),
|
|
445
|
+
}
|
|
446
|
+
a.expandRef = { ...event.expandRef }
|
|
447
|
+
} else if (event.type === "tool.denied") {
|
|
448
|
+
a.status = "denied"
|
|
449
|
+
a.denyReason = event.reason
|
|
450
|
+
a.summary = a.summary ?? `denied: ${event.reason}`
|
|
451
|
+
} else {
|
|
452
|
+
a.status = "cancelled"
|
|
453
|
+
a.summary = a.summary ?? `cancelled (${event.reason})`
|
|
454
|
+
}
|
|
455
|
+
refreshTurnSummary(state, event.sessionId, event.turnId)
|
|
456
|
+
return state
|
|
296
457
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
458
|
+
case "verification.observed": {
|
|
459
|
+
// Observasi saja: tempelkan verdict ke activity (last-wins agar
|
|
460
|
+
// verifikasi susulan memperbarui tampilan), tanpa mengubah status,
|
|
461
|
+
// summary, atau turn. Ketiadaan entry ini = UNKNOWN (diturunkan di
|
|
462
|
+
// proyeksi, bukan di sini).
|
|
463
|
+
const a = evidenceActivity(state, event, diag)
|
|
464
|
+
a.verification = {
|
|
465
|
+
invocationId: event.invocationId,
|
|
466
|
+
verdict: event.verdict,
|
|
467
|
+
method: event.method,
|
|
468
|
+
observedAt: event.observedAt,
|
|
469
|
+
}
|
|
470
|
+
return state
|
|
302
471
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
472
|
+
case "approval.requested": {
|
|
473
|
+
const key = approvalMapKey(state, event.sessionId, event.approvalId)
|
|
474
|
+
const existing = state.approvals.get(key)
|
|
475
|
+
if (existing) {
|
|
476
|
+
if (existing.state === "settled") diag.duplicateTerminal++
|
|
477
|
+
else diag.lateEvent++
|
|
478
|
+
return state
|
|
479
|
+
}
|
|
480
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
481
|
+
const entry: ApprovalEntry = {
|
|
310
482
|
kind: "approval",
|
|
311
483
|
seq,
|
|
312
484
|
turnId: event.turnId,
|
|
485
|
+
sessionId: event.sessionId,
|
|
313
486
|
approvalId: event.approvalId,
|
|
314
487
|
toolCallId: event.toolCallId ?? "",
|
|
315
|
-
identity:
|
|
316
|
-
state: "
|
|
317
|
-
|
|
488
|
+
identity: event.identity,
|
|
489
|
+
state: "requested",
|
|
490
|
+
}
|
|
491
|
+
state.approvals.set(key, entry)
|
|
492
|
+
addOrder(state, "approval", key, seq)
|
|
493
|
+
if (event.toolCallId) {
|
|
494
|
+
const activity = state.activities.get(activityKey(event.sessionId, event.toolCallId))
|
|
495
|
+
if (activity && !activity.approvalId) activity.approvalId = event.approvalId
|
|
318
496
|
}
|
|
319
|
-
state.approvals.set(event.approvalId, ap)
|
|
320
|
-
state.order.push({ kind: "approval", id: event.approvalId, seq })
|
|
321
497
|
return state
|
|
322
498
|
}
|
|
323
|
-
|
|
324
|
-
|
|
499
|
+
case "approval.settled": {
|
|
500
|
+
const key = approvalMapKey(state, event.sessionId, event.approvalId)
|
|
501
|
+
let approval = state.approvals.get(key)
|
|
502
|
+
if (!approval) {
|
|
503
|
+
diag.orphanApproval++
|
|
504
|
+
approval = {
|
|
505
|
+
kind: "approval",
|
|
506
|
+
seq,
|
|
507
|
+
turnId: event.turnId,
|
|
508
|
+
sessionId: event.sessionId,
|
|
509
|
+
approvalId: event.approvalId,
|
|
510
|
+
toolCallId: event.toolCallId ?? "",
|
|
511
|
+
identity: { origin: "builtin", name: event.approvalId, qualified: event.approvalId },
|
|
512
|
+
state: "settled",
|
|
513
|
+
outcome: event.outcome,
|
|
514
|
+
}
|
|
515
|
+
state.approvals.set(key, approval)
|
|
516
|
+
addOrder(state, "approval", key, seq)
|
|
517
|
+
return state
|
|
518
|
+
}
|
|
519
|
+
if (approval.state === "settled") {
|
|
520
|
+
diag.duplicateTerminal++
|
|
521
|
+
return state
|
|
522
|
+
}
|
|
523
|
+
approval.state = "settled"
|
|
524
|
+
approval.outcome = event.outcome
|
|
525
|
+
approval.seq = seq
|
|
325
526
|
return state
|
|
326
527
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
528
|
+
case "file.changed": {
|
|
529
|
+
const activity = evidenceActivity(state, event, diag)
|
|
530
|
+
const previous = activity.receipt
|
|
531
|
+
const paths = [...new Set(event.paths.map(normalizedPath))].sort()
|
|
532
|
+
if (
|
|
533
|
+
previous &&
|
|
534
|
+
previous.journalSeq === event.journalSeq &&
|
|
535
|
+
JSON.stringify(previous.paths ?? []) === JSON.stringify(paths)
|
|
536
|
+
)
|
|
537
|
+
return state
|
|
538
|
+
activity.receipt = {
|
|
539
|
+
toolCallId: event.toolCallId,
|
|
540
|
+
sessionId: event.sessionId,
|
|
541
|
+
turnId: event.turnId,
|
|
542
|
+
...(event.journalSeq !== undefined ? { journalSeq: event.journalSeq } : {}),
|
|
543
|
+
...(event.checkpointId !== undefined ? { checkpointId: event.checkpointId } : {}),
|
|
544
|
+
paths,
|
|
545
|
+
...(previous?.stats ? { stats: { ...previous.stats } } : {}),
|
|
546
|
+
...(previous?.test ? { test: { ...previous.test } } : {}),
|
|
547
|
+
...(previous?.cmd ? { cmd: { ...previous.cmd } } : {}),
|
|
548
|
+
}
|
|
549
|
+
const turn = ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
550
|
+
if (event.checkpointId) turn.checkpointId = event.checkpointId
|
|
551
|
+
refreshTurnSummary(state, event.sessionId, event.turnId)
|
|
336
552
|
return state
|
|
337
553
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
554
|
+
case "test.completed": {
|
|
555
|
+
const activity = evidenceActivity(state, event, diag)
|
|
556
|
+
const receipt = activity.receipt ?? {
|
|
557
|
+
toolCallId: event.toolCallId,
|
|
558
|
+
sessionId: event.sessionId,
|
|
559
|
+
turnId: event.turnId,
|
|
560
|
+
}
|
|
561
|
+
receipt.test = { passed: event.passed, failed: event.failed, summary: event.summary }
|
|
562
|
+
activity.receipt = receipt
|
|
563
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
564
|
+
refreshTurnSummary(state, event.sessionId, event.turnId)
|
|
345
565
|
return state
|
|
346
566
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
567
|
+
case "model.completed": {
|
|
568
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
569
|
+
const id = conversationKey(event.sessionId, event.turnId, "assistant")
|
|
570
|
+
const entry: ConversationEntry = {
|
|
571
|
+
kind: "message",
|
|
572
|
+
id,
|
|
573
|
+
seq,
|
|
574
|
+
sessionId: event.sessionId,
|
|
575
|
+
turnId: event.turnId,
|
|
576
|
+
role: "assistant",
|
|
577
|
+
text: event.text,
|
|
578
|
+
truncated: event.truncated,
|
|
579
|
+
...(event.expandRef ? { expandRef: { ...event.expandRef } } : {}),
|
|
580
|
+
}
|
|
581
|
+
const index = state.conversation.findIndex((m) => m.id === id)
|
|
582
|
+
if (index >= 0) state.conversation[index] = entry
|
|
583
|
+
else {
|
|
584
|
+
state.conversation.push(entry)
|
|
585
|
+
addOrder(state, "message", id, seq)
|
|
586
|
+
}
|
|
587
|
+
const resultId = `${event.sessionId}:${event.turnId}:result`
|
|
588
|
+
const result: ResultEntry = {
|
|
589
|
+
kind: "result",
|
|
590
|
+
resultId,
|
|
591
|
+
seq,
|
|
592
|
+
sessionId: event.sessionId,
|
|
593
|
+
turnId: event.turnId,
|
|
594
|
+
status: "completed",
|
|
595
|
+
summary: event.text,
|
|
596
|
+
...(event.expandRef ? { expandRef: { ...event.expandRef } } : {}),
|
|
597
|
+
}
|
|
598
|
+
state.results.set(resultId, result)
|
|
599
|
+
addOrder(state, "result", resultId, seq)
|
|
600
|
+
return state
|
|
355
601
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
602
|
+
case "reasoning.completed": {
|
|
603
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
604
|
+
const id = `${event.sessionId}:${event.turnId}:reasoning:${seq}`
|
|
605
|
+
const entry: ReasoningEntry = {
|
|
606
|
+
kind: "reasoning",
|
|
607
|
+
id,
|
|
608
|
+
seq,
|
|
609
|
+
sessionId: event.sessionId,
|
|
610
|
+
turnId: event.turnId,
|
|
611
|
+
truncated: event.truncated,
|
|
612
|
+
expandRef: { ...event.expandRef, kind: "reasoning" },
|
|
613
|
+
}
|
|
614
|
+
const index = state.reasoning.findIndex((r) => r.id === id)
|
|
615
|
+
if (index >= 0) state.reasoning[index] = entry
|
|
616
|
+
else {
|
|
617
|
+
state.reasoning.push(entry)
|
|
618
|
+
addOrder(state, "reasoning", id, seq)
|
|
619
|
+
}
|
|
362
620
|
return state
|
|
363
621
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
truncated: event.truncated,
|
|
382
|
-
...(event.expandRef ? { expandRef: event.expandRef } : {}),
|
|
622
|
+
case "context.compacted": {
|
|
623
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
624
|
+
const id = `${event.sessionId}:${event.turnId}:system:${seq}`
|
|
625
|
+
const entry: SystemEntry = {
|
|
626
|
+
kind: "system",
|
|
627
|
+
id,
|
|
628
|
+
seq,
|
|
629
|
+
sessionId: event.sessionId,
|
|
630
|
+
turnId: event.turnId,
|
|
631
|
+
systemKind: "context_compacted",
|
|
632
|
+
text: event.reason,
|
|
633
|
+
reason: event.reason,
|
|
634
|
+
severity: "info",
|
|
635
|
+
}
|
|
636
|
+
state.system.push(entry)
|
|
637
|
+
addOrder(state, "system", id, seq)
|
|
638
|
+
return state
|
|
383
639
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
640
|
+
case "plan.updated": {
|
|
641
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
642
|
+
const entry: PlanEntry = {
|
|
643
|
+
kind: "plan",
|
|
644
|
+
planId: event.planId,
|
|
645
|
+
seq,
|
|
646
|
+
sessionId: event.sessionId,
|
|
647
|
+
turnId: event.turnId,
|
|
648
|
+
status: event.status,
|
|
649
|
+
steps: event.steps.map((step) => ({ ...step })),
|
|
650
|
+
...(event.expandRef ? { expandRef: { ...event.expandRef } } : {}),
|
|
651
|
+
}
|
|
652
|
+
state.plans.set(event.planId, entry)
|
|
653
|
+
addOrder(state, "plan", event.planId, seq)
|
|
654
|
+
return state
|
|
388
655
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
const turn = ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
405
|
-
if (isTurnTerminal(turn.status)) {
|
|
406
|
-
diag.duplicateTurn++
|
|
656
|
+
case "finding.detected": {
|
|
657
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
658
|
+
const entry: FindingEntry = {
|
|
659
|
+
kind: "finding",
|
|
660
|
+
findingId: event.findingId,
|
|
661
|
+
seq,
|
|
662
|
+
sessionId: event.sessionId,
|
|
663
|
+
turnId: event.turnId,
|
|
664
|
+
category: event.category,
|
|
665
|
+
severity: event.severity,
|
|
666
|
+
summary: event.summary,
|
|
667
|
+
evidence: [...(event.evidence ?? [])],
|
|
668
|
+
}
|
|
669
|
+
state.findings.set(event.findingId, entry)
|
|
670
|
+
addOrder(state, "finding", event.findingId, seq)
|
|
407
671
|
return state
|
|
408
672
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
673
|
+
case "result.produced": {
|
|
674
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
675
|
+
const entry: ResultEntry = {
|
|
676
|
+
kind: "result",
|
|
677
|
+
resultId: event.resultId,
|
|
678
|
+
seq,
|
|
679
|
+
sessionId: event.sessionId,
|
|
680
|
+
turnId: event.turnId,
|
|
681
|
+
status: event.status,
|
|
682
|
+
summary: event.summary,
|
|
683
|
+
...(event.action ? { action: event.action } : {}),
|
|
684
|
+
...(event.expandRef ? { expandRef: { ...event.expandRef } } : {}),
|
|
685
|
+
}
|
|
686
|
+
state.results.set(event.resultId, entry)
|
|
687
|
+
addOrder(state, "result", event.resultId, seq)
|
|
688
|
+
return state
|
|
689
|
+
}
|
|
690
|
+
case "diagnostic.raised": {
|
|
691
|
+
ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
692
|
+
const id = `${event.sessionId}:${event.turnId}:diagnostic:${seq}`
|
|
693
|
+
const entry: DiagnosticEntry = {
|
|
694
|
+
kind: "diagnostic",
|
|
695
|
+
id,
|
|
696
|
+
seq,
|
|
697
|
+
sessionId: event.sessionId,
|
|
698
|
+
turnId: event.turnId,
|
|
699
|
+
category: event.category,
|
|
700
|
+
severity: event.severity,
|
|
701
|
+
message: event.message,
|
|
702
|
+
...(event.cause ? { cause: event.cause } : {}),
|
|
703
|
+
...(event.action ? { action: event.action } : {}),
|
|
704
|
+
}
|
|
705
|
+
state.diagnostics.push(entry)
|
|
706
|
+
addOrder(state, "diagnostic", id, seq)
|
|
707
|
+
return state
|
|
708
|
+
}
|
|
709
|
+
case "checkpoint.created": {
|
|
710
|
+
const turn = ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
711
|
+
turn.checkpointId = event.checkpointId
|
|
712
|
+
const id = `${event.sessionId}:${event.turnId}:checkpoint:${seq}`
|
|
713
|
+
const entry: SystemEntry = {
|
|
714
|
+
kind: "system",
|
|
715
|
+
id,
|
|
716
|
+
seq,
|
|
717
|
+
sessionId: event.sessionId,
|
|
718
|
+
turnId: event.turnId,
|
|
719
|
+
systemKind: "checkpoint",
|
|
720
|
+
text: event.checkpointId,
|
|
721
|
+
reason: event.paths?.join(", "),
|
|
722
|
+
severity: "info",
|
|
723
|
+
}
|
|
724
|
+
state.system.push(entry)
|
|
725
|
+
addOrder(state, "system", id, seq)
|
|
726
|
+
refreshTurnSummary(state, event.sessionId, event.turnId)
|
|
727
|
+
return state
|
|
728
|
+
}
|
|
729
|
+
case "turn.completed":
|
|
730
|
+
case "turn.failed":
|
|
731
|
+
case "turn.cancelled": {
|
|
732
|
+
const turn = ensureTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
733
|
+
if (isTurnTerminal(turn.status)) {
|
|
734
|
+
diag.duplicateTurn++
|
|
735
|
+
return state
|
|
736
|
+
}
|
|
737
|
+
turn.tsEnd = event.ts
|
|
738
|
+
if (event.type === "turn.completed") turn.status = "completed"
|
|
739
|
+
else if (event.type === "turn.failed") {
|
|
740
|
+
turn.status = "failed"
|
|
741
|
+
turn.error = event.error.message
|
|
742
|
+
} else {
|
|
743
|
+
turn.status = "cancelled"
|
|
744
|
+
turn.error = event.reason
|
|
745
|
+
}
|
|
746
|
+
forceCloseTurn(state, event.sessionId, event.turnId, seq, event.ts)
|
|
747
|
+
refreshTurnSummary(
|
|
748
|
+
state,
|
|
749
|
+
event.sessionId,
|
|
750
|
+
event.turnId,
|
|
751
|
+
event.type === "turn.completed" ? event.summary : undefined,
|
|
752
|
+
)
|
|
753
|
+
return state
|
|
754
|
+
}
|
|
755
|
+
case "model.delta":
|
|
756
|
+
case "reasoning.delta":
|
|
757
|
+
return state
|
|
758
|
+
default: {
|
|
759
|
+
diag.unknownEvent++
|
|
760
|
+
return state
|
|
419
761
|
}
|
|
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
762
|
}
|
|
763
|
+
} finally {
|
|
764
|
+
trimState(state)
|
|
433
765
|
}
|
|
434
766
|
}
|
|
435
767
|
|
|
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
768
|
export function rebuildFromDurable(
|
|
442
769
|
durable: readonly DomainEvent[],
|
|
443
770
|
diag: ReducerDiagnostics = createReducerDiagnostics(),
|
|
771
|
+
fallbackSessionId = "",
|
|
444
772
|
): { state: PresentationState; sessionId: string } {
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
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).
|
|
773
|
+
const ordered = durable
|
|
774
|
+
.map((event, index) => ({ event, index }))
|
|
775
|
+
.filter(({ event }) => DURABILITY[event.type]?.durable)
|
|
776
|
+
.sort(
|
|
777
|
+
(a, b) => a.event.eventSeq - b.event.eventSeq || a.event.ts - b.event.ts || a.index - b.index,
|
|
778
|
+
)
|
|
779
|
+
const first = ordered[0]?.event
|
|
780
|
+
const sessionId = first?.sessionId ?? fallbackSessionId
|
|
781
|
+
const state = createInitialState(sessionId)
|
|
782
|
+
for (const { event } of ordered) reduce(state, event, diag)
|
|
783
|
+
const lastTs = ordered.reduce((max, { event }) => Math.max(max, event.ts), 0)
|
|
461
784
|
const endSeq = state.seq + 1
|
|
462
|
-
for (const
|
|
463
|
-
if (
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
785
|
+
for (const activity of state.activities.values()) {
|
|
786
|
+
if (activity.status !== "running") continue
|
|
787
|
+
activity.status = "interrupted"
|
|
788
|
+
activity.endSeq = endSeq
|
|
789
|
+
activity.tsEnd = lastTs || activity.tsStart
|
|
790
|
+
activity.durationMs = Math.max(0, activity.tsEnd - activity.tsStart)
|
|
791
|
+
activity.summary = activity.summary ?? "interrupted — verify before retry"
|
|
468
792
|
}
|
|
469
|
-
for (const
|
|
470
|
-
if (
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
}
|
|
793
|
+
for (const approval of state.approvals.values()) {
|
|
794
|
+
if (approval.state !== "requested") continue
|
|
795
|
+
approval.state = "settled"
|
|
796
|
+
approval.outcome = { decision: "cancelled", by: "system", reason: "parent-ended" }
|
|
797
|
+
approval.seq = endSeq
|
|
475
798
|
}
|
|
476
|
-
for (const
|
|
477
|
-
if (
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
799
|
+
for (const turn of state.turns.values()) {
|
|
800
|
+
if (turn.status !== "running") continue
|
|
801
|
+
turn.status = "interrupted"
|
|
802
|
+
turn.tsEnd = lastTs || turn.tsStart
|
|
803
|
+
turn.error = turn.error ?? "process ended before turn settle"
|
|
481
804
|
}
|
|
805
|
+
for (const turn of state.turns.values()) refreshTurnSummary(state, turn.sessionId, turn.turnId)
|
|
482
806
|
return { state, sessionId }
|
|
483
807
|
}
|
|
484
808
|
|
|
485
|
-
export type { ActivityEntry, ApprovalEntry, PresentationState, TurnEntry } from "./model.ts"
|
|
486
809
|
export { cloneState, createInitialState } from "./model.ts"
|
|
810
|
+
export type {
|
|
811
|
+
ActivityEntry,
|
|
812
|
+
ApprovalEntry,
|
|
813
|
+
DiagnosticEntry,
|
|
814
|
+
FindingEntry,
|
|
815
|
+
PlanEntry,
|
|
816
|
+
PresentationState,
|
|
817
|
+
ReasoningEntry,
|
|
818
|
+
ResultEntry,
|
|
819
|
+
SystemEntry,
|
|
820
|
+
TurnEntry,
|
|
821
|
+
}
|