minicode-ai 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +98 -0
- package/LICENSE +21 -21
- package/README.md +4 -4
- package/cli/args.ts +46 -0
- package/cli/commands/acp.ts +163 -10
- package/cli/commands/config.ts +33 -18
- package/cli/commands/daemon.ts +515 -0
- package/cli/commands/exec.ts +145 -53
- package/cli/commands/openwork.ts +986 -0
- package/cli/commands/sessions.ts +4 -1
- package/cli/commands/stats.ts +34 -4
- package/cli/commands.ts +217 -2
- package/cli/index.ts +146 -14
- package/cli/recovery-warning.ts +115 -0
- package/cli/router.ts +32 -1
- package/cli/setup.ts +2332 -171
- package/cli/tui.ts +113 -7
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +489 -0
- package/docs/OUTPUT_EVENT_MODEL.md +289 -0
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +391 -0
- package/docs/OUTPUT_PROTOCOL_SPEC.md +285 -0
- package/docs/OUTPUT_RENDERING_SPEC.md +248 -0
- package/docs/OUTPUT_UX_RULES.md +449 -0
- package/docs/PLAN_UIUX_V6.md +2 -2
- package/docs/README.md +4 -3
- package/docs/TERMINAL_CONTRACT.md +278 -66
- package/docs/architecture.md +89 -7
- package/docs/changelog.md +1 -1
- package/docs/choosing-mode.md +2 -2
- package/docs/cli.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +9 -2
- package/docs/environment.md +6 -2
- package/docs/exec.md +46 -5
- package/docs/memory-sessions.md +2 -0
- package/docs/policy-sandbox.md +1 -1
- package/docs/pricing-budget.md +1 -1
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +6 -2
- package/docs/security-model.md +1 -1
- package/docs/security.md +1 -1
- package/docs/skills.md +2 -2
- package/docs/terminal.md +9 -5
- package/docs/tools.md +1 -1
- package/package.json +35 -5
- package/src/app/session.ts +261 -15
- package/src/app/tool-layer.ts +94 -7
- package/src/daemon/approvals.ts +361 -0
- package/src/daemon/capability.ts +145 -0
- package/src/daemon/children.ts +142 -0
- package/src/daemon/client.ts +267 -0
- package/src/daemon/control.ts +350 -0
- package/src/daemon/discovery.ts +234 -0
- package/src/daemon/feed.ts +155 -0
- package/src/daemon/framing.ts +94 -0
- package/src/daemon/host.ts +316 -0
- package/src/daemon/protocol.ts +217 -0
- package/src/daemon/resource.ts +170 -0
- package/src/daemon/sanitize.ts +57 -0
- package/src/daemon/server.ts +627 -0
- package/src/daemon/store.ts +317 -0
- package/src/daemon/subscription.ts +270 -0
- package/src/daemon/telemetry.ts +81 -0
- package/src/hooks/run.ts +12 -1
- package/src/lib/atomic-write.ts +29 -3
- package/src/lib/db-path.ts +26 -0
- package/src/lib/keystore.ts +544 -42
- package/src/lib/net.ts +43 -1
- package/src/lib/safe-open.ts +116 -1
- package/src/lib/session-id.ts +15 -0
- package/src/lsp/client.ts +123 -5
- package/src/mcp/client.ts +139 -17
- package/src/mcp/server.ts +177 -50
- package/src/mcp/transport.ts +16 -19
- package/src/memory/vector.ts +770 -55
- package/src/policy/allowlist.ts +14 -2
- package/src/policy/bash-guard.ts +105 -10
- package/src/policy/compaction.ts +82 -5
- package/src/policy/consequence-engine.ts +220 -0
- package/src/policy/context.ts +132 -0
- package/src/policy/executor.ts +187 -121
- package/src/policy/no-progress.ts +559 -0
- package/src/policy/permission.ts +4 -8
- package/src/policy/pricing.ts +17 -3
- package/src/policy/scrub.ts +69 -2
- package/src/policy/tool-contract.ts +515 -0
- package/src/policy/usage.ts +310 -24
- package/src/policy/verifier.ts +6 -3
- package/src/policy/work-mode.ts +182 -0
- package/src/presentation/adapter.ts +533 -26
- package/src/presentation/events.ts +151 -0
- package/src/presentation/model.ts +180 -27
- package/src/presentation/projection.ts +858 -0
- package/src/presentation/reducer.ts +645 -310
- package/src/presentation/store.ts +12 -7
- package/src/providers/anthropic.ts +18 -10
- package/src/providers/responses.ts +17 -7
- package/src/runtime/capability.ts +409 -0
- package/src/runtime/child-execution.ts +274 -0
- package/src/runtime/composition.ts +958 -0
- package/src/runtime/dispatch.ts +591 -0
- package/src/runtime/execution-backend.ts +632 -0
- package/src/runtime/execution-events.ts +272 -0
- package/src/runtime/execution-id.ts +230 -0
- package/src/runtime/execution-journal.ts +926 -0
- package/src/runtime/execution-kernel.ts +456 -0
- package/src/runtime/execution-registry.ts +379 -0
- package/src/runtime/process-supervision.ts +276 -0
- package/src/runtime/production-execution.ts +551 -0
- package/src/runtime/production-runtime.ts +217 -0
- package/src/runtime/recovery-safety.ts +70 -0
- package/src/runtime/recovery.ts +477 -0
- package/src/runtime/runtime-capabilities.ts +179 -0
- package/src/runtime/runtime-diagnostics.ts +141 -0
- package/src/runtime/runtime-host.ts +365 -0
- package/src/runtime/runtime-status-facts.ts +222 -0
- package/src/runtime/runtime-status-provider.ts +74 -0
- package/src/runtime/runtime-status.ts +459 -0
- package/src/runtime/runtime-task-admission.ts +101 -0
- package/src/runtime/supervisor.ts +444 -0
- package/src/session/authority.ts +168 -0
- package/src/session/checkpoint.ts +88 -1
- package/src/session/context-adapter.ts +251 -0
- package/src/session/context-assembly.ts +242 -0
- package/src/session/context-identity.ts +389 -0
- package/src/session/context-projection.ts +248 -0
- package/src/session/context-selector.ts +500 -0
- package/src/session/cross-session-adoption.ts +249 -0
- package/src/session/full-history-fold.ts +171 -0
- package/src/session/identity.ts +192 -0
- package/src/session/journal.ts +805 -102
- package/src/session/open-work-intent.ts +732 -0
- package/src/session/open-work.ts +767 -0
- package/src/session/pending-tools.ts +94 -0
- package/src/session/persistence.ts +3569 -127
- package/src/session/runtime-header.ts +208 -0
- package/src/session/shadow-git.ts +13 -5
- package/src/session/verification.ts +1623 -0
- package/src/task/assignment.ts +136 -0
- package/src/task/autonomous-adapter.ts +168 -0
- package/src/task/autonomous-context.ts +630 -0
- package/src/task/autonomous-policy.ts +490 -0
- package/src/task/continuity.ts +179 -0
- package/src/task/execution-cancellation.ts +165 -0
- package/src/task/graph-validate.ts +379 -0
- package/src/task/graph.ts +305 -0
- package/src/task/identity.ts +230 -0
- package/src/task/model.ts +417 -0
- package/src/task/production-scheduler.ts +293 -0
- package/src/task/readiness.ts +229 -0
- package/src/task/scheduler-observability.ts +387 -0
- package/src/task/scheduler.ts +1197 -0
- package/src/task/scheduling-policy.ts +165 -0
- package/src/task/session-authority.ts +148 -0
- package/src/task/session-ownership.ts +211 -0
- package/src/task/store.ts +2887 -0
- package/src/task/sync.ts +343 -0
- package/src/task/trigger.ts +260 -0
- package/src/tools/bash.ts +377 -211
- package/src/tools/code_run.ts +1 -1
- package/src/tools/delete_file.ts +1 -1
- package/src/tools/edit.ts +33 -6
- package/src/tools/evidence.ts +30 -0
- package/src/tools/git.ts +4 -4
- package/src/tools/glob.ts +12 -3
- package/src/tools/grep.ts +1 -1
- package/src/tools/lsp.ts +4 -4
- package/src/tools/mcp_call.ts +11 -4
- package/src/tools/memory.ts +153 -18
- package/src/tools/move_file.ts +1 -1
- package/src/tools/patch.ts +1 -1
- package/src/tools/read_file.ts +1 -1
- package/src/tools/read_image.ts +1 -1
- package/src/tools/submit_result.ts +1 -1
- package/src/tools/task.ts +437 -41
- package/src/tools/todo.ts +662 -35
- package/src/tools/web_fetch.ts +4 -2
- package/src/tools/web_search.ts +9 -3
- package/src/tools/write_file.ts +1 -1
- package/src/ui/assistant/simple.ts +123 -50
- package/src/ui/assistant/turn-status.ts +17 -3
- package/src/ui/contract.ts +184 -0
- package/src/ui/footer.ts +54 -18
- package/src/ui/i18n/en.ts +62 -6
- package/src/ui/i18n/id.ts +62 -6
- package/src/ui/input/prompt-engine.ts +120 -25
- package/src/ui/render/errors.ts +39 -1
- package/src/ui/render/markdown-table.ts +455 -0
- package/src/ui/render/markdown.ts +22 -1
- package/src/ui/render/table-grid.ts +184 -0
- package/src/ui/render/table.ts +15 -76
- package/src/ui/runtime/screen.ts +7 -2
- package/src/ui/runtime/statusline.ts +25 -6
- package/src/ui/tui/app.ts +598 -133
- package/src/ui/tui/transcript.ts +531 -89
- package/vendor/minicore/VENDOR.md +1 -1
- package/vendor/minicore/src/core/budget.ts +25 -25
- package/vendor/minicore/src/core/compact.ts +75 -75
- package/vendor/minicore/src/core/errors.ts +59 -59
- package/vendor/minicore/src/core/events.ts +73 -73
- package/vendor/minicore/src/core/executor.ts +139 -139
- package/vendor/minicore/src/core/history.ts +31 -31
- package/vendor/minicore/src/core/index.ts +41 -41
- package/vendor/minicore/src/core/loop.ts +355 -355
- package/vendor/minicore/src/core/permission.ts +22 -22
- package/vendor/minicore/src/core/provider.ts +39 -39
- package/vendor/minicore/src/core/recovery.ts +35 -35
- package/vendor/minicore/src/core/session.ts +327 -327
- package/vendor/minicore/src/core/snapshot.ts +146 -146
- package/vendor/minicore/src/core/tokens.ts +107 -107
- package/vendor/minicore/src/core/tool.ts +115 -115
- package/vendor/minicore/src/core/types.ts +50 -50
- package/vendor/minicore/src/providers/openai-compat.ts +417 -417
- package/AGENTS.md +0 -45
- package/PLAN.md +0 -491
- package/docs/ARCHITECTURE.html +0 -1248
- package/docs/CONTROL-PLANE-MAP.md +0 -139
- package/docs/HARNESS.md +0 -150
- package/docs/SUMMARY.md +0 -52
- package/docs/UI_RENDER_PIPELINE.md +0 -165
- package/docs/USAGE.md +0 -11
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// P2.12 — toko durable daemon: offset konsumen + mesin persetujuan.
|
|
2
|
+
//
|
|
3
|
+
// Kenapa di sini, bukan di persistence.ts: persistence.ts adalah pemilik
|
|
4
|
+
// SKEMA (DDL ada di `open()`), sedangkan kebijakan transisi/monotonisitas adalah
|
|
5
|
+
// perilaku daemon. Memisahkan keduanya membuat aturan "apa yang boleh berubah"
|
|
6
|
+
// bisa diuji tanpa membuka SQLite, dan DDL tetap satu-satunya di `open()`.
|
|
7
|
+
|
|
8
|
+
import { openWorkspaceSessionDb } from "../session/persistence.ts"
|
|
9
|
+
import type { ApprovalState } from "./protocol.ts"
|
|
10
|
+
import { sanitizeLabel } from "./sanitize.ts"
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Offset konsumen
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Baca offset ack terakhir. `-1` = belum pernah ack (konsumen mulai dari
|
|
18
|
+
* awal / tail sesuai permintaannya sendiri, bukan dari sini).
|
|
19
|
+
*/
|
|
20
|
+
export function readConsumerOffset(consumerId: string, sid: string, cwd?: string): number {
|
|
21
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
22
|
+
try {
|
|
23
|
+
const row = db
|
|
24
|
+
.prepare(
|
|
25
|
+
"SELECT last_acked_seq AS seq FROM consumer_offsets WHERE consumer_id = ? AND session_id = ?",
|
|
26
|
+
)
|
|
27
|
+
.get(consumerId, sid) as { seq: number | null } | null
|
|
28
|
+
return row?.seq ?? -1
|
|
29
|
+
} finally {
|
|
30
|
+
db.close()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Ack konsumen. MONOTONIK: ack mundur (bawaan koneksi lambat yang tiba
|
|
36
|
+
* setelah ack lebih baru) diabaikan — kalau tidak, seekbolak-ke-depan akan
|
|
37
|
+
* memutar konsumen mundur lalu memainkan ulang event yang sudah diproses.
|
|
38
|
+
* Konsumen yang memang mau mulai ulang memakai `subscribe from=N` secara
|
|
39
|
+
* eksplisit (reset terpisah di bawah), bukan lewat ack terselip.
|
|
40
|
+
*
|
|
41
|
+
* Mengembalikan offset tersimpan setelah tulis.
|
|
42
|
+
*/
|
|
43
|
+
export function writeConsumerOffset(
|
|
44
|
+
consumerId: string,
|
|
45
|
+
sid: string,
|
|
46
|
+
seq: number,
|
|
47
|
+
cwd?: string,
|
|
48
|
+
): number {
|
|
49
|
+
if (!Number.isInteger(seq) || seq < -1) throw new Error("writeConsumerOffset: seq invalid")
|
|
50
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
51
|
+
try {
|
|
52
|
+
const now = Date.now()
|
|
53
|
+
return db.transaction(() => {
|
|
54
|
+
const cur = db
|
|
55
|
+
.prepare(
|
|
56
|
+
"SELECT last_acked_seq AS seq FROM consumer_offsets WHERE consumer_id = ? AND session_id = ?",
|
|
57
|
+
)
|
|
58
|
+
.get(consumerId, sid) as { seq: number | null } | null
|
|
59
|
+
const prev = cur?.seq ?? -1
|
|
60
|
+
const next = Math.max(prev, seq)
|
|
61
|
+
if (cur === null) {
|
|
62
|
+
db.prepare(
|
|
63
|
+
"INSERT INTO consumer_offsets(consumer_id, session_id, last_acked_seq, updated_at) VALUES(?,?,?,?)",
|
|
64
|
+
).run(consumerId, sid, next, now)
|
|
65
|
+
} else if (next !== prev) {
|
|
66
|
+
db.prepare(
|
|
67
|
+
"UPDATE consumer_offsets SET last_acked_seq = ?, updated_at = ? WHERE consumer_id = ? AND session_id = ?",
|
|
68
|
+
).run(next, now, consumerId, sid)
|
|
69
|
+
}
|
|
70
|
+
return next
|
|
71
|
+
})()
|
|
72
|
+
} finally {
|
|
73
|
+
db.close()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Reset eksplisit (konsumen meminta mulai dari awal/tail) — bukan hasil ack. */
|
|
78
|
+
export function resetConsumerOffset(consumerId: string, sid: string, to = -1, cwd?: string): void {
|
|
79
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
80
|
+
try {
|
|
81
|
+
db.prepare(
|
|
82
|
+
"UPDATE consumer_offsets SET last_acked_seq = ?, updated_at = ? WHERE consumer_id = ? AND session_id = ?",
|
|
83
|
+
).run(to, Date.now(), consumerId, sid)
|
|
84
|
+
} finally {
|
|
85
|
+
db.close()
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface ConsumerOffsetRow {
|
|
90
|
+
consumerId: string
|
|
91
|
+
sid: string
|
|
92
|
+
lastAckedSeq: number
|
|
93
|
+
updatedAt: number
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function listConsumerOffsets(sid: string, cwd?: string): ConsumerOffsetRow[] {
|
|
97
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
98
|
+
try {
|
|
99
|
+
const rows = db
|
|
100
|
+
.prepare(
|
|
101
|
+
"SELECT consumer_id, session_id, last_acked_seq, updated_at FROM consumer_offsets WHERE session_id = ? ORDER BY consumer_id",
|
|
102
|
+
)
|
|
103
|
+
.all(sid) as {
|
|
104
|
+
consumer_id: string
|
|
105
|
+
session_id: string
|
|
106
|
+
last_acked_seq: number
|
|
107
|
+
updated_at: number
|
|
108
|
+
}[]
|
|
109
|
+
return rows.map((r) => ({
|
|
110
|
+
consumerId: r.consumer_id,
|
|
111
|
+
sid: r.session_id,
|
|
112
|
+
lastAckedSeq: r.last_acked_seq,
|
|
113
|
+
updatedAt: r.updated_at,
|
|
114
|
+
}))
|
|
115
|
+
} finally {
|
|
116
|
+
db.close()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
// Persetujuan durable
|
|
122
|
+
// ---------------------------------------------------------------------------
|
|
123
|
+
|
|
124
|
+
export interface ApprovalRow {
|
|
125
|
+
approvalId: string
|
|
126
|
+
sid: string
|
|
127
|
+
runId: string | null
|
|
128
|
+
tool: string
|
|
129
|
+
summary: string
|
|
130
|
+
state: ApprovalState
|
|
131
|
+
requestedAt: number
|
|
132
|
+
expiresAt: number | null
|
|
133
|
+
decidedAt: number | null
|
|
134
|
+
decision: string | null
|
|
135
|
+
incarnation: string
|
|
136
|
+
/**
|
|
137
|
+
* [Fase 2 / LOOP-INV-08] SHA-256 argumen kanonis saat approval DIMINTA.
|
|
138
|
+
* `null` = baris legacy/pemanggil tanpa argumen; validasi memperlakukannya
|
|
139
|
+
* sebagai tidak cocok bila eksekusi membawa hash (fail-closed).
|
|
140
|
+
*/
|
|
141
|
+
argsHash?: string | null
|
|
142
|
+
/** Identitas pemanggilan (tool call id) yang di-approve. */
|
|
143
|
+
invocationId?: string | null
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Transisi yang sah. UNKNOWN = terminal (hasil tak pernah diketahui). */
|
|
147
|
+
const ALLOWED: Record<ApprovalState, readonly ApprovalState[]> = {
|
|
148
|
+
requested: ["pending", "accepted", "denied", "expired", "cancelled", "UNKNOWN"],
|
|
149
|
+
pending: ["accepted", "denied", "expired", "cancelled", "UNKNOWN"],
|
|
150
|
+
accepted: [],
|
|
151
|
+
denied: [],
|
|
152
|
+
expired: [],
|
|
153
|
+
cancelled: [],
|
|
154
|
+
UNKNOWN: [],
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function canTransition(from: ApprovalState, to: ApprovalState): boolean {
|
|
158
|
+
return ALLOWED[from]?.includes(to) ?? false
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export class ApprovalTransitionError extends Error {
|
|
162
|
+
readonly code = "APPROVAL_INVALID_STATE"
|
|
163
|
+
constructor(
|
|
164
|
+
readonly id: string,
|
|
165
|
+
readonly from: ApprovalState,
|
|
166
|
+
readonly to: ApprovalState,
|
|
167
|
+
) {
|
|
168
|
+
super(`approval ${id}: ${from} -> ${to} tidak sah`)
|
|
169
|
+
this.name = "ApprovalTransitionError"
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function toRow(r: Record<string, unknown>): ApprovalRow {
|
|
174
|
+
return {
|
|
175
|
+
approvalId: String(r.approval_id),
|
|
176
|
+
sid: String(r.session_id),
|
|
177
|
+
runId: r.run_id === null || r.run_id === undefined ? null : String(r.run_id),
|
|
178
|
+
tool: String(r.tool),
|
|
179
|
+
summary: String(r.summary ?? ""),
|
|
180
|
+
state: String(r.state) as ApprovalState,
|
|
181
|
+
requestedAt: Number(r.requested_at),
|
|
182
|
+
expiresAt: r.expires_at === null || r.expires_at === undefined ? null : Number(r.expires_at),
|
|
183
|
+
decidedAt: r.decided_at === null || r.decided_at === undefined ? null : Number(r.decided_at),
|
|
184
|
+
decision: r.decision === null || r.decision === undefined ? null : String(r.decision),
|
|
185
|
+
incarnation: String(r.incarnation),
|
|
186
|
+
argsHash: r.args_hash === null || r.args_hash === undefined ? null : String(r.args_hash),
|
|
187
|
+
invocationId:
|
|
188
|
+
r.invocation_id === null || r.invocation_id === undefined ? null : String(r.invocation_id),
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function createApproval(
|
|
193
|
+
row: Omit<ApprovalRow, "state" | "decidedAt" | "decision"> & { state?: ApprovalState },
|
|
194
|
+
cwd?: string,
|
|
195
|
+
): ApprovalRow {
|
|
196
|
+
const state: ApprovalState = row.state ?? "requested"
|
|
197
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
198
|
+
try {
|
|
199
|
+
db.prepare(
|
|
200
|
+
`INSERT INTO daemon_approvals
|
|
201
|
+
(approval_id, session_id, run_id, tool, summary, state, requested_at, expires_at, decided_at, decision, incarnation, args_hash, invocation_id)
|
|
202
|
+
VALUES (?,?,?,?,?,?,?,?,NULL,NULL,?,?,?)`,
|
|
203
|
+
).run(
|
|
204
|
+
row.approvalId,
|
|
205
|
+
row.sid,
|
|
206
|
+
row.runId,
|
|
207
|
+
sanitizeLabel(row.tool, 64),
|
|
208
|
+
sanitizeLabel(row.summary, 500),
|
|
209
|
+
state,
|
|
210
|
+
row.requestedAt,
|
|
211
|
+
row.expiresAt,
|
|
212
|
+
row.incarnation,
|
|
213
|
+
row.argsHash ?? null,
|
|
214
|
+
row.invocationId ?? null,
|
|
215
|
+
)
|
|
216
|
+
return {
|
|
217
|
+
...row,
|
|
218
|
+
state,
|
|
219
|
+
decidedAt: null,
|
|
220
|
+
decision: null,
|
|
221
|
+
argsHash: row.argsHash ?? null,
|
|
222
|
+
invocationId: row.invocationId ?? null,
|
|
223
|
+
}
|
|
224
|
+
} finally {
|
|
225
|
+
db.close()
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function getApproval(id: string, cwd?: string): ApprovalRow | null {
|
|
230
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
231
|
+
try {
|
|
232
|
+
const r = db.prepare("SELECT * FROM daemon_approvals WHERE approval_id = ?").get(id) as Record<
|
|
233
|
+
string,
|
|
234
|
+
unknown
|
|
235
|
+
> | null
|
|
236
|
+
return r ? toRow(r) : null
|
|
237
|
+
} finally {
|
|
238
|
+
db.close()
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function listApprovals(sid: string, cwd?: string): ApprovalRow[] {
|
|
243
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
244
|
+
try {
|
|
245
|
+
const rows = db
|
|
246
|
+
.prepare("SELECT * FROM daemon_approvals WHERE session_id = ? ORDER BY requested_at DESC")
|
|
247
|
+
.all(sid) as Record<string, unknown>[]
|
|
248
|
+
return rows.map(toRow)
|
|
249
|
+
} finally {
|
|
250
|
+
db.close()
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Pindahkan state. Melempar `ApprovalTransitionError` bila tak sah —
|
|
256
|
+
* kegagalan transisi = bug pemanggil, bukan kondisi dunia, sehingga jangan
|
|
257
|
+
* ditelan diam-diam.
|
|
258
|
+
*/
|
|
259
|
+
export function transitionApproval(
|
|
260
|
+
id: string,
|
|
261
|
+
to: ApprovalState,
|
|
262
|
+
opts: { decision?: string | null; decidedAt?: number | null },
|
|
263
|
+
cwd?: string,
|
|
264
|
+
): ApprovalRow {
|
|
265
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
266
|
+
try {
|
|
267
|
+
return db.transaction(() => {
|
|
268
|
+
const r = db
|
|
269
|
+
.prepare("SELECT * FROM daemon_approvals WHERE approval_id = ?")
|
|
270
|
+
.get(id) as Record<string, unknown> | null
|
|
271
|
+
if (!r) throw new Error(`approval ${id} tidak ditemukan`)
|
|
272
|
+
const cur = toRow(r)
|
|
273
|
+
if (!canTransition(cur.state, to)) throw new ApprovalTransitionError(id, cur.state, to)
|
|
274
|
+
const decidedAt = opts.decidedAt ?? (to === "pending" ? null : Date.now())
|
|
275
|
+
const decision = to === "pending" ? null : (opts.decision ?? null)
|
|
276
|
+
db.prepare(
|
|
277
|
+
"UPDATE daemon_approvals SET state = ?, decided_at = ?, decision = ? WHERE approval_id = ?",
|
|
278
|
+
).run(to, decidedAt, decision, id)
|
|
279
|
+
return { ...cur, state: to, decidedAt, decision }
|
|
280
|
+
})()
|
|
281
|
+
} finally {
|
|
282
|
+
db.close()
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Ketika daemon START: semua persetujuan yang belum pernah memperoleh
|
|
288
|
+
* keputusan (requested/pending) berpindah ke UNKNOWN.
|
|
289
|
+
*
|
|
290
|
+
* Kenapa UNKNOWN, bukan `denied`: `denied` adalah KEPUTUSAN — mencatat
|
|
291
|
+
* "ditolak" padahal tidak ada siapa pun yang menolak = mengarang hasil.
|
|
292
|
+
* UNKNOWN jujur: tak ada yang bisa melanjutkan keputusan lama setelah
|
|
293
|
+
* prosesnya mati, dan konsumen yang melihatnya harus memperlakukan ulang
|
|
294
|
+
* sebagai pertanyaan baru, bukan jawaban.
|
|
295
|
+
*
|
|
296
|
+
* Mengembalikan jumlah baris yang dipindahkan.
|
|
297
|
+
*/
|
|
298
|
+
export function markPendingApprovalsUnknown(incarnation: string, cwd?: string): number {
|
|
299
|
+
const db = openWorkspaceSessionDb(cwd)
|
|
300
|
+
try {
|
|
301
|
+
const info = db
|
|
302
|
+
.prepare(
|
|
303
|
+
`UPDATE daemon_approvals
|
|
304
|
+
SET state = 'UNKNOWN', decided_at = ?, decision = 'daemon-restart', incarnation = ?
|
|
305
|
+
WHERE state IN ('requested','pending')`,
|
|
306
|
+
)
|
|
307
|
+
.run(Date.now(), incarnation)
|
|
308
|
+
return Number(info.changes ?? 0)
|
|
309
|
+
} finally {
|
|
310
|
+
db.close()
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Persetujuan yang masih menunggu keputusan (dipakai saat replay attach). */
|
|
315
|
+
export function listOpenApprovals(sid: string, cwd?: string): ApprovalRow[] {
|
|
316
|
+
return listApprovals(sid, cwd).filter((a) => a.state === "requested" || a.state === "pending")
|
|
317
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// P2.12 — langganan konsumen: kursor + antrean TERBATAS.
|
|
2
|
+
//
|
|
3
|
+
// Kenapa antrean terbatas adalah keputusan keamanan, bukan detail: kontrak
|
|
4
|
+
// melarang daemon menahan memori tak terbatas atas nama konsumen yang lambat.
|
|
5
|
+
// Satu konsumen Desktop yang jeda (window minimized, suspend, lag) akan
|
|
6
|
+
// menumpuk delta stream — kalau antrean bebas, daemon OOM karena klien yang
|
|
7
|
+
// tak bisa dipegang. Batas + overflow eksplisit menjadikannya penolakan yang
|
|
8
|
+
// terlihat (QUEUE_OVERFLOW) alih-alih memori yang meledak.
|
|
9
|
+
|
|
10
|
+
import { MAX_QUEUE_PER_CONN } from "./protocol.ts"
|
|
11
|
+
import { readConsumerOffset, resetConsumerOffset, writeConsumerOffset } from "./store.ts"
|
|
12
|
+
|
|
13
|
+
export interface PendingEvent {
|
|
14
|
+
seq: number
|
|
15
|
+
provenance: "LIVE" | "REPLAY" | "RECONSTRUCTED" | "UNKNOWN"
|
|
16
|
+
event: unknown
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Antrean berkapasitas tetap. push mengembalikan false saat penuh — pemanggil
|
|
21
|
+
* yang memutuskan (kirim halte), bukan antrean yang membuang diam-diam.
|
|
22
|
+
*/
|
|
23
|
+
export class BoundedQueue<T> {
|
|
24
|
+
#items: T[] = []
|
|
25
|
+
readonly max: number
|
|
26
|
+
|
|
27
|
+
constructor(max: number = MAX_QUEUE_PER_CONN) {
|
|
28
|
+
// `0` sengaja dipertahankan: hanya seam uji yang memakainya untuk meniru
|
|
29
|
+
// socket yang sudah tertahan sejak awal, sehingga overflow deterministik
|
|
30
|
+
// tanpa bergantung pada buffer kernel. Produksi selalu >= 1.
|
|
31
|
+
this.max = Math.max(0, max)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
push(item: T): boolean {
|
|
35
|
+
if (this.#items.length >= this.max) return false
|
|
36
|
+
this.#items.push(item)
|
|
37
|
+
return true
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Ambil semua isi (urut sisipan) dan kosongkan. */
|
|
41
|
+
drain(): T[] {
|
|
42
|
+
const out = this.#items
|
|
43
|
+
this.#items = []
|
|
44
|
+
return out
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get size(): number {
|
|
48
|
+
return this.#items.length
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get full(): boolean {
|
|
52
|
+
return this.#items.length >= this.max
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SubscriptionInit {
|
|
57
|
+
/**
|
|
58
|
+
* Identitas LIVE langganan. Server memakai identitas ber-scope koneksi agar
|
|
59
|
+
* dua koneksi dengan identitas logis yang sama tidak saling menutup
|
|
60
|
+
* langganan. Untuk pemakai langsung, nilai ini juga menjadi identitas offset
|
|
61
|
+
* bila `offsetConsumerId` tidak diberikan.
|
|
62
|
+
*/
|
|
63
|
+
consumerId: string
|
|
64
|
+
/**
|
|
65
|
+
* Identitas LOGIS yang dipakai untuk offset durable. Dipisah dari
|
|
66
|
+
* `consumerId` supaya reconnect tetap bisa `resume` dari offset konsumen yang
|
|
67
|
+
* sama, sementara dua koneksi bersamaan tetap punya stream live sendiri.
|
|
68
|
+
*/
|
|
69
|
+
offsetConsumerId?: string
|
|
70
|
+
sid: string
|
|
71
|
+
/** Seq pertama yang AKAN dikirim. Lebih besar dari head = tak ada yang dikirim. */
|
|
72
|
+
fromSeq: number
|
|
73
|
+
maxQueue?: number
|
|
74
|
+
/**
|
|
75
|
+
* Workspace pemilik offset. TANPA ini ack jatuh ke `process.cwd()` daemon —
|
|
76
|
+
* padahal offset adalah milik SATU workspace; menulisnya ke tempat lain
|
|
77
|
+
* membuat reconnect "berhasil" tapi melanjutkan dari nol.
|
|
78
|
+
*/
|
|
79
|
+
cwd?: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Satu langganan (konsumen x sesi).
|
|
84
|
+
*
|
|
85
|
+
* cursor = seq TERAKHIR yang sudah DIKIRIM — bukan yang di-ack, bukan yang ada
|
|
86
|
+
* di disk. Tiga angka itu sengaja terpisah: mencampurnya membuat klaim
|
|
87
|
+
* "sudah terkirim" tak lagi bisa diverifikasi oleh pihak manapun.
|
|
88
|
+
*/
|
|
89
|
+
export class Subscription {
|
|
90
|
+
/** Identitas live (bisa ber-scope koneksi) — bukan kunci offset durable. */
|
|
91
|
+
readonly consumerId: string
|
|
92
|
+
/** Identitas logis untuk offset durable; default = consumerId. */
|
|
93
|
+
readonly offsetConsumerId: string
|
|
94
|
+
readonly sid: string
|
|
95
|
+
readonly startSeq: number
|
|
96
|
+
cursor: number
|
|
97
|
+
/** true saat antrean pernah penuh — konsumen wajib reconnect + mulai ulang. */
|
|
98
|
+
overflowed = false
|
|
99
|
+
closed = false
|
|
100
|
+
readonly queue: BoundedQueue<PendingEvent>
|
|
101
|
+
readonly cwd: string | undefined
|
|
102
|
+
|
|
103
|
+
constructor(init: SubscriptionInit) {
|
|
104
|
+
this.consumerId = init.consumerId
|
|
105
|
+
this.offsetConsumerId = init.offsetConsumerId ?? init.consumerId
|
|
106
|
+
this.sid = init.sid
|
|
107
|
+
this.startSeq = init.fromSeq
|
|
108
|
+
this.cwd = init.cwd
|
|
109
|
+
// Cursor awal = satu sebelum seq pertama yang akan dikirim.
|
|
110
|
+
this.cursor = init.fromSeq - 1
|
|
111
|
+
this.queue = new BoundedQueue<PendingEvent>(init.maxQueue ?? MAX_QUEUE_PER_CONN)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Antre event. false = overflow (pemanggil mengirim halte + memutus). */
|
|
115
|
+
enqueue(p: PendingEvent): boolean {
|
|
116
|
+
if (this.closed) return false
|
|
117
|
+
if (!this.queue.push(p)) {
|
|
118
|
+
this.overflowed = true
|
|
119
|
+
return false
|
|
120
|
+
}
|
|
121
|
+
return true
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
drain(): PendingEvent[] {
|
|
125
|
+
return this.queue.drain()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Ambil SATU item terdepan tanpa mengorbankan sisanya.
|
|
130
|
+
*
|
|
131
|
+
* Ini penting: `drain()` mengosongkan seluruh antrean, sehingga bila tulisan
|
|
132
|
+
* socket tertahan di tengah loop, sisa item ikut hilang — event yang sudah
|
|
133
|
+
* "diproses" dari sisi daemon tapi tak pernah sampai. Dengan mengambil
|
|
134
|
+
* satu-satu, hanya yang benar-benar terkirim yang dikeluarkan dari antrean.
|
|
135
|
+
*/
|
|
136
|
+
takeFirst(): PendingEvent | undefined {
|
|
137
|
+
const items = this.queue.drain()
|
|
138
|
+
if (items.length === 0) return undefined
|
|
139
|
+
const first = items[0]!
|
|
140
|
+
for (let i = 1; i < items.length; i++) this.queue.push(items[i]!)
|
|
141
|
+
return first
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Konsumen mengonfirmasi pemrosesan sampai seq.
|
|
146
|
+
*
|
|
147
|
+
* Menolak ack di atas cursor: konsumen tak bisa memproses sesuatu yang belum
|
|
148
|
+
* pernah dikirim — ack begitu berarti kursor salah baca, dan mencatatnya
|
|
149
|
+
* akan melompatkan offset ke depan sehingga event yang BELUM diproses
|
|
150
|
+
* dianggap selesai selamanya. Itu kehilangan data yang tak bisa diperbaiki.
|
|
151
|
+
*/
|
|
152
|
+
ack(
|
|
153
|
+
seq: number,
|
|
154
|
+
): { ok: true; persisted: number } | { ok: false; reason: "OUT_OF_RANGE" | "CLOSED" } {
|
|
155
|
+
if (this.closed) return { ok: false, reason: "CLOSED" }
|
|
156
|
+
if (!Number.isInteger(seq) || seq < 0 || seq > this.cursor)
|
|
157
|
+
return { ok: false, reason: "OUT_OF_RANGE" }
|
|
158
|
+
const persisted = writeConsumerOffset(this.offsetConsumerId, this.sid, seq, this.cwd)
|
|
159
|
+
return { ok: true, persisted }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
close(): void {
|
|
163
|
+
this.closed = true
|
|
164
|
+
this.queue.drain()
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Pemisah yang mustahil muncul di identitas manusiawi: dibangkitkan lewat
|
|
169
|
+
// charCode, bukan literal, supaya berkas ini tak pernah memuat byte kontrol
|
|
170
|
+
// mentah (yang merusak formatter dan tooling teks). Tanpa pemisah yang tak
|
|
171
|
+
// mungkin, consumerId "a b" + sid "c" menghasilkan kunci sama dengan
|
|
172
|
+
// consumerId "a" + sid "b c" — satu konsumen membaca sesi milik lain.
|
|
173
|
+
const KEY_SEP = String.fromCharCode(1)
|
|
174
|
+
|
|
175
|
+
export class SubscriptionRegistry {
|
|
176
|
+
readonly #byKey = new Map<string, Subscription>()
|
|
177
|
+
|
|
178
|
+
static key(consumerId: string, sid: string): string {
|
|
179
|
+
return consumerId + KEY_SEP + sid
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Buat langganan. from:
|
|
184
|
+
* - "tail" -> hanya event BARU (head+1), tanpa replay
|
|
185
|
+
* - "resume" -> lanjut dari offset ack terakhir; belum pernah = tail
|
|
186
|
+
* - angka -> mulai dari sana (replay eksplisit; 0 = dari awal)
|
|
187
|
+
*
|
|
188
|
+
* Bila `offsetConsumerId` diberikan, offset durable memakai identitas logis itu
|
|
189
|
+
* (reconnect melanjutkan dari ack konsumen yang sama), sementara kunci
|
|
190
|
+
* langganan live tetap `consumerId` — bisa ber-scope koneksi agar dua koneksi
|
|
191
|
+
* konsumen yang sama tidak saling menutup.
|
|
192
|
+
*
|
|
193
|
+
* Mengganti langganan lama untuk kunci yang sama: dua pump pada sesi yang
|
|
194
|
+
* sama mengirim ganda dan membuat kursor saling melompat.
|
|
195
|
+
*/
|
|
196
|
+
attach(
|
|
197
|
+
init: SubscriptionInit & { from: number | "tail" | "resume"; head: number },
|
|
198
|
+
resolveResume: (consumerId: string, sid: string) => number,
|
|
199
|
+
): { sub: Subscription; fromSeq: number } {
|
|
200
|
+
const key = SubscriptionRegistry.key(init.consumerId, init.sid)
|
|
201
|
+
this.#byKey.get(key)?.close()
|
|
202
|
+
const offsetId = init.offsetConsumerId ?? init.consumerId
|
|
203
|
+
let fromSeq: number
|
|
204
|
+
if (init.from === "tail") {
|
|
205
|
+
fromSeq = init.head + 1
|
|
206
|
+
} else if (init.from === "resume") {
|
|
207
|
+
const offset = resolveResume(offsetId, init.sid)
|
|
208
|
+
fromSeq = offset < 0 ? init.head + 1 : offset + 1
|
|
209
|
+
} else {
|
|
210
|
+
// Angka = permintaan replay eksplisit. `0` berarti "dari awal", bukan
|
|
211
|
+
// "resume" — menggabungkan dua makna pada satu nilai membuat konsumen
|
|
212
|
+
// yang minta replay penuh diam-diam mendapat potongan offset-nya sendiri.
|
|
213
|
+
fromSeq = Number.isFinite(init.from) && init.from > 0 ? Math.floor(init.from) : 0
|
|
214
|
+
}
|
|
215
|
+
const sub = new Subscription({
|
|
216
|
+
consumerId: init.consumerId,
|
|
217
|
+
...(init.offsetConsumerId !== undefined ? { offsetConsumerId: init.offsetConsumerId } : {}),
|
|
218
|
+
sid: init.sid,
|
|
219
|
+
fromSeq,
|
|
220
|
+
...(init.maxQueue !== undefined ? { maxQueue: init.maxQueue } : {}),
|
|
221
|
+
...(init.cwd !== undefined ? { cwd: init.cwd } : {}),
|
|
222
|
+
})
|
|
223
|
+
this.#byKey.set(key, sub)
|
|
224
|
+
return { sub, fromSeq }
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
get(consumerId: string, sid: string): Subscription | undefined {
|
|
228
|
+
return this.#byKey.get(SubscriptionRegistry.key(consumerId, sid))
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
list(consumerId?: string): Subscription[] {
|
|
232
|
+
const all = [...this.#byKey.values()]
|
|
233
|
+
return consumerId ? all.filter((s) => s.consumerId === consumerId) : all
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Tutup semua langganan milik satu konsumen (putusnya socket). */
|
|
237
|
+
dropConsumer(consumerId: string): number {
|
|
238
|
+
let n = 0
|
|
239
|
+
for (const [k, s] of [...this.#byKey]) {
|
|
240
|
+
if (s.consumerId === consumerId) {
|
|
241
|
+
s.close()
|
|
242
|
+
this.#byKey.delete(k)
|
|
243
|
+
n++
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return n
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
drop(sid: string, consumerId: string): void {
|
|
250
|
+
this.#byKey.delete(SubscriptionRegistry.key(consumerId, sid))
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
clear(): void {
|
|
254
|
+
for (const s of this.#byKey.values()) s.close()
|
|
255
|
+
this.#byKey.clear()
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
get size(): number {
|
|
259
|
+
return this.#byKey.size
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Offset durable konsumen — TERPISAH dari cursor langganan. */
|
|
264
|
+
export function persistedOffset(consumerId: string, sid: string, cwd?: string): number {
|
|
265
|
+
return readConsumerOffset(consumerId, sid, cwd)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export function resetOffset(consumerId: string, sid: string, cwd?: string): void {
|
|
269
|
+
resetConsumerOffset(consumerId, sid, -1, cwd)
|
|
270
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// P2.12 — telemetri daemon.
|
|
2
|
+
//
|
|
3
|
+
// Kenapa file sendiri dan bukan variabel acak: kontrak menyatakan telemetri
|
|
4
|
+
// NON-AUTORITATIF — angka di sini tidak pernah dipakai mengambil keputusan
|
|
5
|
+
// (tidak ada "karena counter ini, boleh lanjut"). Memisahkannya membuat
|
|
6
|
+
// penjaga bisa MEMERIKSA bahwa tak ada kode keputusan yang membaca counter
|
|
7
|
+
// ini, bukan sekadar percaya komentar.
|
|
8
|
+
|
|
9
|
+
export type CounterName =
|
|
10
|
+
| "connections.opened"
|
|
11
|
+
| "connections.closed"
|
|
12
|
+
| "connections.rejected"
|
|
13
|
+
| "subscribe.attached"
|
|
14
|
+
| "events.delivered"
|
|
15
|
+
| "events.replayed"
|
|
16
|
+
| "events.dropped.overflow"
|
|
17
|
+
| "ack.accepted"
|
|
18
|
+
| "ack.rejected"
|
|
19
|
+
| "control.executed"
|
|
20
|
+
| "control.refused"
|
|
21
|
+
| "idempotent.replay"
|
|
22
|
+
| "approval.requested"
|
|
23
|
+
| "approval.decided"
|
|
24
|
+
| "approval.unknown.onRestart"
|
|
25
|
+
| "storage.pressure"
|
|
26
|
+
| "child.registered"
|
|
27
|
+
| "child.finished"
|
|
28
|
+
| "shutdown.ordered"
|
|
29
|
+
|
|
30
|
+
const COUNTERS: readonly CounterName[] = [
|
|
31
|
+
"connections.opened",
|
|
32
|
+
"connections.closed",
|
|
33
|
+
"connections.rejected",
|
|
34
|
+
"subscribe.attached",
|
|
35
|
+
"events.delivered",
|
|
36
|
+
"events.replayed",
|
|
37
|
+
"events.dropped.overflow",
|
|
38
|
+
"ack.accepted",
|
|
39
|
+
"ack.rejected",
|
|
40
|
+
"control.executed",
|
|
41
|
+
"control.refused",
|
|
42
|
+
"idempotent.replay",
|
|
43
|
+
"approval.requested",
|
|
44
|
+
"approval.decided",
|
|
45
|
+
"approval.unknown.onRestart",
|
|
46
|
+
"storage.pressure",
|
|
47
|
+
"child.registered",
|
|
48
|
+
"child.finished",
|
|
49
|
+
"shutdown.ordered",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
export class Telemetry {
|
|
53
|
+
readonly #counts = new Map<CounterName, number>()
|
|
54
|
+
readonly startedAt = Date.now()
|
|
55
|
+
|
|
56
|
+
constructor() {
|
|
57
|
+
for (const n of COUNTERS) this.#counts.set(n, 0)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
inc(name: CounterName, by = 1): void {
|
|
61
|
+
// Nama di luar daftar ditolak keras: typo diam-diam menjadi counter yang
|
|
62
|
+
// tak pernah terlihat di snapshot, dan dashboard akan melaporkan nol yang
|
|
63
|
+
// seolah berarti "tidak pernah terjadi".
|
|
64
|
+
if (!this.#counts.has(name)) throw new Error(`telemetry: counter tak dikenal ${name}`)
|
|
65
|
+
this.#counts.set(name, (this.#counts.get(name) ?? 0) + by)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get(name: CounterName): number {
|
|
69
|
+
return this.#counts.get(name) ?? 0
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
snapshot(): Record<string, number> {
|
|
73
|
+
const out: Record<string, number> = { uptimeMs: Date.now() - this.startedAt }
|
|
74
|
+
for (const [k, v] of [...this.#counts].sort((a, b) => (a[0] < b[0] ? -1 : 1))) out[k] = v
|
|
75
|
+
return out
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
reset(): void {
|
|
79
|
+
for (const n of COUNTERS) this.#counts.set(n, 0)
|
|
80
|
+
}
|
|
81
|
+
}
|
package/src/hooks/run.ts
CHANGED
|
@@ -16,8 +16,19 @@ export interface RunHookCtx {
|
|
|
16
16
|
* system — di mode tanpa-eksekusi (plan/readonly) hook TETAP mengeksekusi
|
|
17
17
|
* tanpa gate bila tidak dijaga, melanggar kontrak read-only. Diekspor agar
|
|
18
18
|
* composition root + test memakai predikat yang sama.
|
|
19
|
+
*
|
|
20
|
+
* P12 S2.2 — WorkMode-aware. WorkMode DISCUSS adalah posture read-only penuh
|
|
21
|
+
* (ceiling handler menolak semua tool tulis/eksekusi, termasuk delegate), jadi
|
|
22
|
+
* hooks (eksekusi Node di luar permission system) juga TIDAK boleh jalan —
|
|
23
|
+
* walau permissionMode="auto". PLAN/EXECUTE mempertahankan perilaku lama:
|
|
24
|
+
* keputusan murni dari permission mode. Param workMode opsional agar pemanggil
|
|
25
|
+
* lama tanpa WorkMode tetap kompatibel (default undefined = perilaku lama).
|
|
19
26
|
*/
|
|
20
|
-
export function shouldRunHooks(
|
|
27
|
+
export function shouldRunHooks(
|
|
28
|
+
permissionMode: string,
|
|
29
|
+
workMode?: "DISCUSS" | "PLAN" | "EXECUTE",
|
|
30
|
+
): boolean {
|
|
31
|
+
if (workMode === "DISCUSS") return false
|
|
21
32
|
return permissionMode !== "plan" && permissionMode !== "readonly"
|
|
22
33
|
}
|
|
23
34
|
|