opencode-extended-sidebar 0.2.90 → 0.2.91
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/.oesignore +17 -17
- package/CHANGELOG.md +96 -95
- package/LICENSE +21 -21
- package/README.md +150 -235
- package/oes.json +13 -11
- package/package.json +100 -100
- package/scripts/install-git-hooks.mjs +29 -29
- package/src/pware.oc.core/constants/index.ts +18 -18
- package/src/pware.oc.core/constants/pware.oc.core.constants.eventKind.ts +29 -29
- package/src/pware.oc.core/constants/pware.oc.core.constants.eventName.ts +15 -13
- package/src/pware.oc.core/constants/pware.oc.core.constants.eventType.ts +241 -241
- package/src/pware.oc.core/constants/pware.oc.core.constants.myWork.ts +55 -55
- package/src/pware.oc.core/constants/pware.oc.core.constants.partType.ts +65 -65
- package/src/pware.oc.core/constants/pware.oc.core.constants.phase.ts +58 -58
- package/src/pware.oc.core/constants/pware.oc.core.constants.pulse.ts +63 -63
- package/src/pware.oc.core/constants/pware.oc.core.constants.rowKind.ts +34 -34
- package/src/pware.oc.core/constants/pware.oc.core.constants.status.ts +74 -74
- package/src/pware.oc.core/constants/pware.oc.core.constants.toolName.ts +69 -69
- package/src/pware.oc.core/git/index.ts +7 -7
- package/src/pware.oc.core/git/pware.oc.core.git.ts +237 -237
- package/src/pware.oc.core/git/pware.oc.core.gitignore.ts +87 -87
- package/src/pware.oc.core/index.ts +19 -18
- package/src/pware.oc.core/pware.oc.core.bus.ts +42 -42
- package/src/pware.oc.core/pware.oc.core.cache.ts +48 -48
- package/src/pware.oc.core/pware.oc.core.clipboard.ts +97 -97
- package/src/pware.oc.core/pware.oc.core.debug.ts +280 -280
- package/src/pware.oc.core/pware.oc.core.events.ts +79 -79
- package/src/pware.oc.core/pware.oc.core.glyph.ts +135 -135
- package/src/pware.oc.core/pware.oc.core.layout.ts +234 -234
- package/src/pware.oc.core/pware.oc.core.oes.ts +99 -89
- package/src/pware.oc.core/pware.oc.core.paths.ts +240 -240
- package/src/pware.oc.core/pware.oc.core.preview.ts +74 -74
- package/src/pware.oc.core/pware.oc.core.pulse.ts +726 -726
- package/src/pware.oc.core/pware.oc.core.runtimeConfig.ts +41 -0
- package/src/pware.oc.core/pware.oc.core.sqlite.ts +164 -175
- package/src/pware.oc.core/pware.oc.core.sqliteGateway.ts +89 -0
- package/src/pware.oc.core/pware.oc.core.status.ts +214 -214
- package/src/pware.oc.core/pware.oc.core.timing.ts +63 -63
- package/src/pware.oc.core/pware.oc.core.width.ts +215 -215
- package/src/pware.oc.omo/constants/index.ts +15 -15
- package/src/pware.oc.omo/constants/pware.oc.omo.constants.backgroundTask.ts +18 -18
- package/src/pware.oc.omo/constants/pware.oc.omo.constants.boulderStatus.ts +49 -49
- package/src/pware.oc.omo/constants/pware.oc.omo.constants.docKind.ts +29 -29
- package/src/pware.oc.omo/constants/pware.oc.omo.constants.eventName.ts +11 -11
- package/src/pware.oc.omo/constants/pware.oc.omo.constants.planStatus.ts +60 -60
- package/src/pware.oc.omo/constants/pware.oc.omo.constants.reviewStatus.ts +49 -49
- package/src/pware.oc.omo/constants/pware.oc.omo.constants.startWork.ts +25 -25
- package/src/pware.oc.omo/constants/pware.oc.omo.constants.verdict.ts +38 -38
- package/src/pware.oc.omo/index.ts +7 -7
- package/src/pware.oc.omo/resolver/index.ts +19 -19
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.approval.ts +179 -179
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.approvalGroup.ts +116 -116
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.approvalState.ts +34 -34
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.boulder.ts +503 -503
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.config.ts +40 -40
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.doc.ts +269 -269
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.draftFile.ts +18 -18
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.notepadsFile.ts +18 -18
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.plan.ts +155 -155
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.planFile.ts +363 -361
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.proofFile.ts +18 -18
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.rulesFile.ts +11 -11
- package/src/pware.oc.omo/resolver/pware.oc.omo.resolver.runContinuationFile.ts +14 -14
- package/src/pware.oc.opencode/constants/index.ts +10 -10
- package/src/pware.oc.opencode/constants/pware.oc.opencode.constants.eventName.ts +15 -15
- package/src/pware.oc.opencode/constants/pware.oc.opencode.constants.fileTouch.ts +18 -18
- package/src/pware.oc.opencode/constants/pware.oc.opencode.constants.questionKind.ts +45 -45
- package/src/pware.oc.opencode/constants/pware.oc.opencode.constants.sessionStatus.ts +58 -58
- package/src/pware.oc.opencode/index.ts +8 -8
- package/src/pware.oc.opencode/pware.oc.opencode.events.ts +182 -182
- package/src/pware.oc.opencode/pware.oc.opencode.files.ts +399 -399
- package/src/pware.oc.opencode/resolver/index.ts +223 -170
- package/src/pware.oc.opencode/resolver/pware.oc.opencode.resolver.file.ts +150 -162
- package/src/pware.oc.opencode/resolver/pware.oc.opencode.resolver.question.ts +149 -185
- package/src/pware.oc.opencode/resolver/pware.oc.opencode.resolver.session.ts +248 -211
- package/src/pware.oc.opencode/resolver/pware.oc.opencode.resolver.todo.ts +22 -26
- package/src/pware.oc.opencode/resolver/pware.oc.opencode.resolver.tool.ts +224 -236
- package/src/pware.oc.perf/index.ts +14 -14
- package/src/pware.oc.perf/pware.oc.perf.asciichart.d.ts +15 -15
- package/src/pware.oc.perf/pware.oc.perf.charts.ts +284 -284
- package/src/pware.oc.perf/pware.oc.perf.reader.ts +962 -927
- package/src/pware.oc.perf/pware.oc.perf.realtime.ts +88 -88
- package/src/pware.oc.perf/pware.oc.perf.realtimeBlock.ts +118 -118
- package/src/pware.oc.perf/pware.oc.perf.realtimeCpuRam.ts +79 -79
- package/src/pware.oc.perf/pware.oc.perf.realtimeSampler.ts +71 -71
- package/src/pware.oc.perf/pware.oc.perf.realtimeTimeline.ts +214 -214
- package/src/pware.oc.perf/pware.oc.perf.self.ts +179 -179
- package/src/pware.oc.perf/pware.oc.perf.view.tsx +463 -462
- package/src/pware.oc.runtime/index.ts +13 -13
- package/src/pware.oc.runtime/pware.oc.runtime.monitor.ts +142 -142
- package/src/pware.oc.runtime/pware.oc.runtime.mywork-enrich.ts +87 -79
- package/src/pware.oc.runtime/pware.oc.runtime.mywork.ts +282 -253
- package/src/pware.oc.runtime/pware.oc.runtime.omoRead.ts +37 -0
- package/src/pware.oc.runtime/pware.oc.runtime.questions.ts +83 -95
- package/src/pware.oc.runtime/pware.oc.runtime.snapshotClient.ts +130 -129
- package/src/pware.oc.runtime/pware.oc.runtime.source.ts +105 -105
- package/src/pware.oc.runtime/pware.oc.runtime.worker.ts +44 -43
- package/src/pware.oc.runtime/resolver/index.ts +268 -224
- package/src/pware.oc.runtime/resolver/pware.oc.runtime.resolver.delegate.ts +146 -146
- package/src/pware.oc.ui/index.ts +12 -12
- package/src/pware.oc.ui/pware.oc.ui.chrome.tsx +381 -339
- package/src/pware.oc.ui/pware.oc.ui.glyphs.tsx +144 -144
- package/src/pware.oc.ui/pware.oc.ui.host.tsx +111 -111
- package/src/pware.oc.ui/pware.oc.ui.live.tsx +82 -82
- package/src/pware.oc.ui/pware.oc.ui.menudialogs.tsx +843 -843
- package/src/pware.oc.ui/pware.oc.ui.sections.tsx +552 -526
- package/src/pware.oc.ui/pware.oc.ui.sidebar.tsx +1718 -1629
- package/src/pware.oc.ui.tsx +98 -98
|
@@ -1,927 +1,962 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Turn timing from opencode.db: wait / think / recv / tool split per model.
|
|
3
|
-
* Reads through json_extract, so only numbers and statuses leave SQLite —
|
|
4
|
-
* message text, reasoning text and tool I/O never enter the process.
|
|
5
|
-
*/
|
|
6
|
-
import fs from "node:fs"
|
|
7
|
-
import os from "node:os"
|
|
8
|
-
import path from "node:path"
|
|
9
|
-
import { dbg, profile } from "../pware.oc.core/pware.oc.core.debug.js"
|
|
10
|
-
import { createStampCache } from "../pware.oc.core/pware.oc.core.cache.js"
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
export type
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
json_extract(data,'$.
|
|
147
|
-
json_extract(data,'$.
|
|
148
|
-
json_extract(data,'$.
|
|
149
|
-
json_extract(data,'$.
|
|
150
|
-
json_extract(data,'$.
|
|
151
|
-
json_extract(data,'$.
|
|
152
|
-
json_extract(data,'$.tokens.
|
|
153
|
-
json_extract(data,'$.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
json_extract(data,'$.
|
|
166
|
-
json_extract(data,'$.
|
|
167
|
-
json_extract(data,'$.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
json_extract(data,'$.
|
|
181
|
-
json_extract(data,'$.
|
|
182
|
-
json_extract(data,'$.
|
|
183
|
-
json_extract(data,'$.
|
|
184
|
-
json_extract(data,'$.state.
|
|
185
|
-
json_extract(data,'$.
|
|
186
|
-
json_extract(data,'$.state.
|
|
187
|
-
json_extract(data,'$.
|
|
188
|
-
json_extract(data,'$.state.input.
|
|
189
|
-
json_extract(data,'$.input.
|
|
190
|
-
json_extract(data,'$.state.input.
|
|
191
|
-
json_extract(data,'$.input.
|
|
192
|
-
json_extract(data,'$.state.input.
|
|
193
|
-
json_extract(data,'$.
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
return
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
return
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
export function
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
const
|
|
384
|
-
const
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
by.
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
.
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
}
|
|
515
|
-
if (
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
extra[
|
|
527
|
-
extra[
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
r.
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
r.
|
|
556
|
-
r.
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
const
|
|
619
|
-
const
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
const
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
tools
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
const
|
|
705
|
-
|
|
706
|
-
const
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
const
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
snap.totals.
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
snap.totals.
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
return
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Turn timing from opencode.db: wait / think / recv / tool split per model.
|
|
3
|
+
* Reads through json_extract, so only numbers and statuses leave SQLite —
|
|
4
|
+
* message text, reasoning text and tool I/O never enter the process.
|
|
5
|
+
*/
|
|
6
|
+
import fs from "node:fs"
|
|
7
|
+
import os from "node:os"
|
|
8
|
+
import path from "node:path"
|
|
9
|
+
import { dbg, profile, profileAsync } from "../pware.oc.core/pware.oc.core.debug.js"
|
|
10
|
+
import { createStampCache } from "../pware.oc.core/pware.oc.core.cache.js"
|
|
11
|
+
import {
|
|
12
|
+
ReadonlyDbUnavailableError,
|
|
13
|
+
readonlyReadGateway,
|
|
14
|
+
} from "../pware.oc.core/pware.oc.core.sqliteGateway.js"
|
|
15
|
+
import { finiteNum } from "../pware.oc.core/pware.oc.core.paths.js"
|
|
16
|
+
import { formatDuration, formatWhen, shortToolLabel, toEpochMs } from "../pware.oc.core/pware.oc.core.pulse.js"
|
|
17
|
+
import type { SqlDb } from "../pware.oc.core/pware.oc.core.sqlite.js"
|
|
18
|
+
import { PART_TYPE_REASONING, PART_TYPE_TEXT, PART_TYPE_TOOL } from "../pware.oc.core/constants/pware.oc.core.constants.partType.js"
|
|
19
|
+
import { TOOL_QUESTION } from "../pware.oc.core/constants/pware.oc.core.constants.toolName.js"
|
|
20
|
+
import { STATUS_ERROR } from "../pware.oc.core/constants/pware.oc.core.constants.status.js"
|
|
21
|
+
import {
|
|
22
|
+
PERF_LOG_KIND_MODELS,
|
|
23
|
+
PERF_LOG_KIND_TIME,
|
|
24
|
+
PERF_PHASE_IDLE,
|
|
25
|
+
PERF_PHASE_RECV,
|
|
26
|
+
PERF_PHASE_THINK,
|
|
27
|
+
PERF_PHASE_TOOL,
|
|
28
|
+
PERF_PHASE_WAIT,
|
|
29
|
+
type PerfLogKind,
|
|
30
|
+
type PerfPhase,
|
|
31
|
+
} from "../pware.oc.core/constants/pware.oc.core.constants.phase.js"
|
|
32
|
+
|
|
33
|
+
export type { PerfLogKind, PerfPhase }
|
|
34
|
+
|
|
35
|
+
export type PhaseSplit = Record<PerfPhase, number>
|
|
36
|
+
|
|
37
|
+
export type ModelPerf = {
|
|
38
|
+
key: string
|
|
39
|
+
model: string
|
|
40
|
+
provider: string
|
|
41
|
+
turns: number
|
|
42
|
+
/** Averages per turn, null when nothing was measurable. */
|
|
43
|
+
waitMs: number | null
|
|
44
|
+
thinkMs: number | null
|
|
45
|
+
recvMs: number | null
|
|
46
|
+
turnMs: number | null
|
|
47
|
+
tokensPerSec: number | null
|
|
48
|
+
toolMs: number
|
|
49
|
+
tools: number
|
|
50
|
+
errors: number
|
|
51
|
+
tokensIn: number
|
|
52
|
+
tokensOut: number
|
|
53
|
+
tokensReasoning: number
|
|
54
|
+
cacheRead: number
|
|
55
|
+
cacheWrite: number
|
|
56
|
+
cost: number
|
|
57
|
+
lastAt: number
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type ToolPerf = {
|
|
61
|
+
name: string
|
|
62
|
+
count: number
|
|
63
|
+
errors: number
|
|
64
|
+
totalMs: number
|
|
65
|
+
avgMs: number
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type PerfTotals = {
|
|
69
|
+
turns: number
|
|
70
|
+
errors: number
|
|
71
|
+
aborts: number
|
|
72
|
+
wallMs: number
|
|
73
|
+
activeMs: number
|
|
74
|
+
phases: PhaseSplit
|
|
75
|
+
tokensIn: number
|
|
76
|
+
tokensOut: number
|
|
77
|
+
tokensReasoning: number
|
|
78
|
+
cacheRead: number
|
|
79
|
+
cacheWrite: number
|
|
80
|
+
cost: number
|
|
81
|
+
/** cacheRead / (input + cacheRead), null when nothing was sent yet. */
|
|
82
|
+
cacheHit: number | null
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** One assistant turn, oldest first, for the trend charts. */
|
|
86
|
+
export type TrendPoint = {
|
|
87
|
+
at: number
|
|
88
|
+
waitMs: number | null
|
|
89
|
+
tokensPerSec: number | null
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export type SessionPerf = {
|
|
93
|
+
id: string
|
|
94
|
+
title: string
|
|
95
|
+
model: string
|
|
96
|
+
turns: number
|
|
97
|
+
waitMs: number | null
|
|
98
|
+
tokensPerSec: number | null
|
|
99
|
+
toolShare: number | null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type PerfSnapshot = {
|
|
103
|
+
present: boolean
|
|
104
|
+
sessionId: string
|
|
105
|
+
totals: PerfTotals
|
|
106
|
+
models: ModelPerf[]
|
|
107
|
+
tools: ToolPerf[]
|
|
108
|
+
trend: TrendPoint[]
|
|
109
|
+
history: SessionPerf[]
|
|
110
|
+
error: string | null
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function emptyPhases(): PhaseSplit {
|
|
114
|
+
return { wait: 0, think: 0, recv: 0, tool: 0, idle: 0 }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function emptyPerf(sessionId: string, error: string | null = null): PerfSnapshot {
|
|
118
|
+
return {
|
|
119
|
+
present: false,
|
|
120
|
+
sessionId,
|
|
121
|
+
totals: {
|
|
122
|
+
turns: 0,
|
|
123
|
+
errors: 0,
|
|
124
|
+
aborts: 0,
|
|
125
|
+
wallMs: 0,
|
|
126
|
+
activeMs: 0,
|
|
127
|
+
phases: emptyPhases(),
|
|
128
|
+
tokensIn: 0,
|
|
129
|
+
tokensOut: 0,
|
|
130
|
+
tokensReasoning: 0,
|
|
131
|
+
cacheRead: 0,
|
|
132
|
+
cacheWrite: 0,
|
|
133
|
+
cost: 0,
|
|
134
|
+
cacheHit: null,
|
|
135
|
+
},
|
|
136
|
+
models: [],
|
|
137
|
+
tools: [],
|
|
138
|
+
trend: [],
|
|
139
|
+
history: [],
|
|
140
|
+
error,
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const MSG_SQL = `
|
|
145
|
+
SELECT id,
|
|
146
|
+
json_extract(data,'$.role') AS role,
|
|
147
|
+
json_extract(data,'$.modelID') AS model,
|
|
148
|
+
json_extract(data,'$.providerID') AS provider,
|
|
149
|
+
json_extract(data,'$.time.created') AS created,
|
|
150
|
+
json_extract(data,'$.time.completed') AS completed,
|
|
151
|
+
json_extract(data,'$.error.name') AS err,
|
|
152
|
+
json_extract(data,'$.tokens.input') AS tin,
|
|
153
|
+
json_extract(data,'$.tokens.output') AS tout,
|
|
154
|
+
json_extract(data,'$.tokens.reasoning') AS treason,
|
|
155
|
+
json_extract(data,'$.tokens.cache.read') AS cread,
|
|
156
|
+
json_extract(data,'$.tokens.cache.write') AS cwrite,
|
|
157
|
+
json_extract(data,'$.cost') AS cost
|
|
158
|
+
FROM message
|
|
159
|
+
WHERE session_id = ?
|
|
160
|
+
ORDER BY time_created DESC
|
|
161
|
+
LIMIT ?`
|
|
162
|
+
|
|
163
|
+
const PART_SQL = `
|
|
164
|
+
SELECT message_id AS mid,
|
|
165
|
+
json_extract(data,'$.type') AS kind,
|
|
166
|
+
json_extract(data,'$.time.start') AS pstart,
|
|
167
|
+
json_extract(data,'$.time.end') AS pend,
|
|
168
|
+
json_extract(data,'$.tool') AS tool,
|
|
169
|
+
json_extract(data,'$.state.status') AS status,
|
|
170
|
+
json_extract(data,'$.state.time.start') AS tstart,
|
|
171
|
+
json_extract(data,'$.state.time.end') AS tend
|
|
172
|
+
FROM part
|
|
173
|
+
WHERE session_id = ?
|
|
174
|
+
ORDER BY time_created DESC
|
|
175
|
+
LIMIT ?`
|
|
176
|
+
|
|
177
|
+
/** On-click log only — same hint columns as Current tools, never the blob. */
|
|
178
|
+
const LOG_PART_SQL = `
|
|
179
|
+
SELECT message_id AS mid,
|
|
180
|
+
json_extract(data,'$.type') AS kind,
|
|
181
|
+
json_extract(data,'$.time.start') AS pstart,
|
|
182
|
+
json_extract(data,'$.time.end') AS pend,
|
|
183
|
+
json_extract(data,'$.tool') AS tool,
|
|
184
|
+
json_extract(data,'$.state.status') AS status,
|
|
185
|
+
json_extract(data,'$.state.time.start') AS tstart,
|
|
186
|
+
json_extract(data,'$.state.time.end') AS tend,
|
|
187
|
+
json_extract(data,'$.state.title') AS title,
|
|
188
|
+
json_extract(data,'$.state.input.command') AS command,
|
|
189
|
+
json_extract(data,'$.input.command') AS command2,
|
|
190
|
+
json_extract(data,'$.state.input.filePath') AS filePath,
|
|
191
|
+
json_extract(data,'$.input.filePath') AS filePath2,
|
|
192
|
+
json_extract(data,'$.state.input.pattern') AS pattern,
|
|
193
|
+
json_extract(data,'$.input.pattern') AS pattern2,
|
|
194
|
+
json_extract(data,'$.state.input.description') AS description,
|
|
195
|
+
json_extract(data,'$.input.description') AS description2,
|
|
196
|
+
json_extract(data,'$.state.input.subagent_type') AS subagent,
|
|
197
|
+
json_extract(data,'$.state.input.category') AS category
|
|
198
|
+
FROM part
|
|
199
|
+
WHERE session_id = ?
|
|
200
|
+
ORDER BY time_created DESC
|
|
201
|
+
LIMIT ?`
|
|
202
|
+
|
|
203
|
+
export type MsgRow = {
|
|
204
|
+
id: string
|
|
205
|
+
role: string | null
|
|
206
|
+
model: string | null
|
|
207
|
+
provider: string | null
|
|
208
|
+
created: number | null
|
|
209
|
+
completed: number | null
|
|
210
|
+
err: string | null
|
|
211
|
+
tin: number | null
|
|
212
|
+
tout: number | null
|
|
213
|
+
treason: number | null
|
|
214
|
+
cread: number | null
|
|
215
|
+
cwrite: number | null
|
|
216
|
+
cost: number | null
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export type PartRow = {
|
|
220
|
+
mid: string
|
|
221
|
+
kind: string | null
|
|
222
|
+
pstart: number | null
|
|
223
|
+
pend: number | null
|
|
224
|
+
tool: string | null
|
|
225
|
+
status: string | null
|
|
226
|
+
tstart: number | null
|
|
227
|
+
tend: number | null
|
|
228
|
+
/** Log-only hints — absent on the live PART_SQL scan. */
|
|
229
|
+
title?: string | null
|
|
230
|
+
command?: string | null
|
|
231
|
+
filePath?: string | null
|
|
232
|
+
pattern?: string | null
|
|
233
|
+
description?: string | null
|
|
234
|
+
subagent?: string | null
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Per-message timing collected from its parts. */
|
|
238
|
+
type Bucket = {
|
|
239
|
+
firstOut: number | null
|
|
240
|
+
textStart: number | null
|
|
241
|
+
textEnd: number | null
|
|
242
|
+
thinkMs: number
|
|
243
|
+
toolMs: number
|
|
244
|
+
tools: number
|
|
245
|
+
toolErrors: number
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function num(v: unknown): number {
|
|
249
|
+
return finiteNum(v)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
function span(from: number | null, to: number | null): number {
|
|
254
|
+
if (from == null || to == null || to < from) return 0
|
|
255
|
+
return to - from
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function newBucket(): Bucket {
|
|
259
|
+
return {
|
|
260
|
+
firstOut: null,
|
|
261
|
+
textStart: null,
|
|
262
|
+
textEnd: null,
|
|
263
|
+
thinkMs: 0,
|
|
264
|
+
toolMs: 0,
|
|
265
|
+
tools: 0,
|
|
266
|
+
toolErrors: 0,
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function avg(total: number, n: number): number | null {
|
|
271
|
+
return n > 0 ? Math.round(total / n) : null
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Tools that pause for a human rather than do work — never tool timing. */
|
|
275
|
+
const NON_TOOLS = new Set<string>([TOOL_QUESTION])
|
|
276
|
+
|
|
277
|
+
function countsAsTool(tool: string | null | undefined): boolean {
|
|
278
|
+
return !NON_TOOLS.has((tool || "").trim().toLowerCase())
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Drop the provider prefix; keep the tail, which carries the variant. */
|
|
282
|
+
function modelLabel(provider: string | null, model: string | null): string {
|
|
283
|
+
const m = (model || "").trim()
|
|
284
|
+
if (m) return m
|
|
285
|
+
return (provider || "model").trim() || "model"
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function logDur(ms: number): string {
|
|
289
|
+
return formatDuration(ms) || "0ms"
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function logStatus(err: string | null | undefined): string {
|
|
293
|
+
const raw = (err || "").trim()
|
|
294
|
+
if (!raw) return "ok"
|
|
295
|
+
return raw.toLowerCase().includes("abort") ? "abort" : "error"
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** Aligned columns for a Perf log. Last cell is not padded. */
|
|
299
|
+
export function formatColumns(headers: string[], rows: string[][]): string {
|
|
300
|
+
if (headers.length === 0) return ""
|
|
301
|
+
const widths = headers.map((h, i) => {
|
|
302
|
+
let w = h.length
|
|
303
|
+
for (const row of rows) {
|
|
304
|
+
const cell = row[i] ?? ""
|
|
305
|
+
if (cell.length > w) w = cell.length
|
|
306
|
+
}
|
|
307
|
+
return w
|
|
308
|
+
})
|
|
309
|
+
const line = (cells: string[]) =>
|
|
310
|
+
cells.map((c, i) => (i >= widths.length - 1 ? c : (c ?? "").padEnd(widths[i]!))).join(" ")
|
|
311
|
+
return [line(headers), ...rows.map((r) => line(headers.map((_, i) => r[i] ?? "")))].join("\n")
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export type PerfLogRow = {
|
|
315
|
+
at: number | null
|
|
316
|
+
end?: number | null
|
|
317
|
+
phase: string
|
|
318
|
+
/** Bare tool id (`bash`, `read`). Empty on wait/think/recv. */
|
|
319
|
+
tool?: string
|
|
320
|
+
/** Exact call hint (`db.ts`, `bun test …`) or the model name. */
|
|
321
|
+
name: string
|
|
322
|
+
status: string
|
|
323
|
+
ms: number
|
|
324
|
+
extra?: string
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function hintStr(v: unknown): string | null {
|
|
328
|
+
return typeof v === "string" && v.trim() ? v.trim() : null
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Bare tool + the specific call (file / command / pattern). Never I/O. */
|
|
332
|
+
export function toolLogCall(part: PartRow): { tool: string; call: string } {
|
|
333
|
+
const tool = hintStr(part.tool) || "tool"
|
|
334
|
+
const label = shortToolLabel({
|
|
335
|
+
tool,
|
|
336
|
+
title: part.title,
|
|
337
|
+
command: part.command,
|
|
338
|
+
filePath: part.filePath,
|
|
339
|
+
pattern: part.pattern,
|
|
340
|
+
description: part.description,
|
|
341
|
+
subagent: part.subagent,
|
|
342
|
+
maxHint: 48,
|
|
343
|
+
})
|
|
344
|
+
const prefix = `${tool} `
|
|
345
|
+
const call = label.startsWith(prefix) ? label.slice(prefix.length) : label === tool ? "—" : label
|
|
346
|
+
return { tool, call }
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function perfLogKindLabel(kind: PerfLogKind): string {
|
|
350
|
+
return kind === PERF_PHASE_TOOL ? "tools" : kind
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function perfLogFileName(kind: PerfLogKind, generatedAt: number, toolFilter?: string): string {
|
|
354
|
+
const stamp = formatWhen(generatedAt).replace(/[: ]/g, "-")
|
|
355
|
+
const ms = String(generatedAt % 1000).padStart(3, "0")
|
|
356
|
+
const toolPart = toolFilter ? `-${toolFilter.replace(/[^a-z0-9_-]/gi, "_")}` : ""
|
|
357
|
+
return `perf-${perfLogKindLabel(kind)}${toolPart}-${stamp}-${ms}.log`
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** One dated event per turn / tool / idle gap. Exported for tests. */
|
|
361
|
+
export function collectPerfLogRows(
|
|
362
|
+
kind: PerfLogKind,
|
|
363
|
+
msgs: MsgRow[],
|
|
364
|
+
parts: PartRow[],
|
|
365
|
+
): PerfLogRow[] {
|
|
366
|
+
const { byMsg } = collectParts(parts)
|
|
367
|
+
const rows: PerfLogRow[] = []
|
|
368
|
+
const assistant: Array<{
|
|
369
|
+
start: number | null
|
|
370
|
+
end: number | null
|
|
371
|
+
model: string
|
|
372
|
+
status: string
|
|
373
|
+
waitMs: number
|
|
374
|
+
thinkMs: number
|
|
375
|
+
recvMs: number
|
|
376
|
+
toolMs: number
|
|
377
|
+
tin: number
|
|
378
|
+
tout: number
|
|
379
|
+
}> = []
|
|
380
|
+
|
|
381
|
+
for (const row of msgs) {
|
|
382
|
+
if ((row.role || "") !== "assistant") continue
|
|
383
|
+
const start = toEpochMs(row.created)
|
|
384
|
+
const end = toEpochMs(row.completed)
|
|
385
|
+
const b = byMsg.get(String(row.id)) ?? newBucket()
|
|
386
|
+
const model = modelLabel(row.provider, row.model)
|
|
387
|
+
const status = logStatus(row.err)
|
|
388
|
+
const waitMs = span(start, b.firstOut)
|
|
389
|
+
const recvMs = span(b.textStart, b.textEnd)
|
|
390
|
+
assistant.push({
|
|
391
|
+
start,
|
|
392
|
+
end,
|
|
393
|
+
model,
|
|
394
|
+
status,
|
|
395
|
+
waitMs,
|
|
396
|
+
thinkMs: b.thinkMs,
|
|
397
|
+
recvMs,
|
|
398
|
+
toolMs: b.toolMs,
|
|
399
|
+
tin: num(row.tin),
|
|
400
|
+
tout: num(row.tout),
|
|
401
|
+
})
|
|
402
|
+
if (kind === PERF_PHASE_WAIT || kind === PERF_LOG_KIND_TIME) {
|
|
403
|
+
rows.push({ at: start, end, phase: PERF_PHASE_WAIT, name: model, status, ms: waitMs })
|
|
404
|
+
}
|
|
405
|
+
if ((kind === PERF_PHASE_THINK || kind === PERF_LOG_KIND_TIME) && b.thinkMs > 0) {
|
|
406
|
+
rows.push({ at: start, end, phase: PERF_PHASE_THINK, name: model, status, ms: b.thinkMs })
|
|
407
|
+
}
|
|
408
|
+
if (kind === PERF_PHASE_RECV || kind === PERF_LOG_KIND_TIME) {
|
|
409
|
+
rows.push({ at: start, end, phase: PERF_PHASE_RECV, name: model, status, ms: recvMs })
|
|
410
|
+
}
|
|
411
|
+
if (kind === PERF_LOG_KIND_MODELS) {
|
|
412
|
+
rows.push({
|
|
413
|
+
at: start,
|
|
414
|
+
end,
|
|
415
|
+
phase: "turn",
|
|
416
|
+
name: model,
|
|
417
|
+
status,
|
|
418
|
+
ms: span(start, end),
|
|
419
|
+
extra: [logDur(waitMs), logDur(b.thinkMs), logDur(recvMs), logDur(b.toolMs), String(num(row.tin)), String(num(row.tout))].join("\t"),
|
|
420
|
+
})
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (kind === PERF_PHASE_TOOL || kind === PERF_LOG_KIND_TIME) {
|
|
425
|
+
for (const part of parts) {
|
|
426
|
+
if ((part.kind || "") !== PERF_PHASE_TOOL) continue
|
|
427
|
+
if (!countsAsTool(part.tool)) continue
|
|
428
|
+
const start = toEpochMs(part.tstart)
|
|
429
|
+
const end = toEpochMs(part.tend)
|
|
430
|
+
const ms = span(start, end)
|
|
431
|
+
const failed = (part.status || "").toLowerCase() === STATUS_ERROR
|
|
432
|
+
const { tool, call } = toolLogCall(part)
|
|
433
|
+
rows.push({
|
|
434
|
+
at: start,
|
|
435
|
+
end,
|
|
436
|
+
phase: PERF_PHASE_TOOL,
|
|
437
|
+
tool,
|
|
438
|
+
name: call,
|
|
439
|
+
status: failed ? "error" : hintStr(part.status) || "ok",
|
|
440
|
+
ms,
|
|
441
|
+
})
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (kind === PERF_PHASE_IDLE || kind === PERF_LOG_KIND_TIME) {
|
|
446
|
+
const ordered = [...assistant].sort((a, b) => (a.start ?? 0) - (b.start ?? 0))
|
|
447
|
+
for (let i = 0; i < ordered.length - 1; i += 1) {
|
|
448
|
+
const cur = ordered[i]
|
|
449
|
+
const next = ordered[i + 1]
|
|
450
|
+
if (!cur || !next) continue
|
|
451
|
+
const from = cur.end ?? cur.start
|
|
452
|
+
const gap = span(from, next.start)
|
|
453
|
+
if (gap > 0) {
|
|
454
|
+
rows.push({ at: from, end: next.start, phase: PERF_PHASE_IDLE, name: "gap", status: "ok", ms: gap })
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
rows.sort((a, b) => (a.at ?? 0) - (b.at ?? 0) || a.phase.localeCompare(b.phase))
|
|
460
|
+
return rows
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export type PerfLogDoc = {
|
|
464
|
+
title: string
|
|
465
|
+
fileName: string
|
|
466
|
+
text: string
|
|
467
|
+
written: string | null
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function toolSummary(rows: PerfLogRow[]): { headers: string[]; rows: string[][] } {
|
|
471
|
+
const by = new Map<string, { tool: string; call: string; count: number; errors: number; totalMs: number }>()
|
|
472
|
+
for (const row of rows) {
|
|
473
|
+
if (row.phase !== PERF_PHASE_TOOL) continue
|
|
474
|
+
const tool = row.tool || row.name
|
|
475
|
+
const call = row.tool ? row.name : "—"
|
|
476
|
+
const key = `${tool}\t${call}`
|
|
477
|
+
const agg = by.get(key) ?? { tool, call, count: 0, errors: 0, totalMs: 0 }
|
|
478
|
+
agg.count += 1
|
|
479
|
+
agg.totalMs += row.ms
|
|
480
|
+
if (row.status === STATUS_ERROR) agg.errors += 1
|
|
481
|
+
by.set(key, agg)
|
|
482
|
+
}
|
|
483
|
+
const list = [...by.values()]
|
|
484
|
+
.map((a) => ({ ...a, avgMs: a.count > 0 ? Math.round(a.totalMs / a.count) : 0 }))
|
|
485
|
+
.sort((a, b) => b.totalMs - a.totalMs || b.count - a.count)
|
|
486
|
+
return {
|
|
487
|
+
headers: ["tool", "call", "count", "errors", "total", "avg"],
|
|
488
|
+
rows: list.map((t) => [t.tool, t.call, String(t.count), String(t.errors), logDur(t.totalMs), logDur(t.avgMs)]),
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Build the dated column log. `now` is the generation stamp. */
|
|
493
|
+
export function formatPerfLog(
|
|
494
|
+
kind: PerfLogKind,
|
|
495
|
+
sessionId: string,
|
|
496
|
+
now: number,
|
|
497
|
+
rows: PerfLogRow[],
|
|
498
|
+
titleOverride?: string,
|
|
499
|
+
): string {
|
|
500
|
+
const title = titleOverride ?? perfLogKindLabel(kind)
|
|
501
|
+
const head = [
|
|
502
|
+
`# Perf ${title} log`,
|
|
503
|
+
`# generated ${formatWhen(now)}`,
|
|
504
|
+
`# session ${sessionId}`,
|
|
505
|
+
`# rows ${rows.length}`,
|
|
506
|
+
"",
|
|
507
|
+
]
|
|
508
|
+
const parts = [...head]
|
|
509
|
+
if (kind === PERF_PHASE_TOOL || kind === PERF_LOG_KIND_TIME) {
|
|
510
|
+
const sum = toolSummary(rows)
|
|
511
|
+
if (sum.rows.length > 0) {
|
|
512
|
+
parts.push(formatColumns(sum.headers, sum.rows), "")
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
if (rows.length === 0) {
|
|
516
|
+
parts.push("(no rows)")
|
|
517
|
+
return `${parts.join("\n")}\n`
|
|
518
|
+
}
|
|
519
|
+
if (kind === PERF_LOG_KIND_MODELS) {
|
|
520
|
+
const headers = ["when", "model", "wait", "think", "recv", "tools", "in", "out", "status"]
|
|
521
|
+
const body = rows.map((r) => {
|
|
522
|
+
const extra = (r.extra ?? "").split("\t")
|
|
523
|
+
return [
|
|
524
|
+
formatWhen(r.at),
|
|
525
|
+
r.name,
|
|
526
|
+
extra[0] || "—",
|
|
527
|
+
extra[1] || "—",
|
|
528
|
+
extra[2] || "—",
|
|
529
|
+
extra[3] || "—",
|
|
530
|
+
extra[4] || "0",
|
|
531
|
+
extra[5] || "0",
|
|
532
|
+
r.status,
|
|
533
|
+
]
|
|
534
|
+
})
|
|
535
|
+
parts.push(formatColumns(headers, body))
|
|
536
|
+
} else if (kind === PERF_PHASE_TOOL) {
|
|
537
|
+
parts.push(
|
|
538
|
+
formatColumns(
|
|
539
|
+
["when", "ended", "tool", "call", "status", "duration"],
|
|
540
|
+
rows.map((r) => [
|
|
541
|
+
formatWhen(r.at),
|
|
542
|
+
formatWhen(r.end),
|
|
543
|
+
r.tool || r.name,
|
|
544
|
+
r.tool ? r.name : "—",
|
|
545
|
+
r.status,
|
|
546
|
+
logDur(r.ms),
|
|
547
|
+
]),
|
|
548
|
+
),
|
|
549
|
+
)
|
|
550
|
+
} else if (kind === PERF_LOG_KIND_TIME) {
|
|
551
|
+
parts.push(
|
|
552
|
+
formatColumns(
|
|
553
|
+
["when", "ended", "phase", "tool", "call", "status", "duration"],
|
|
554
|
+
rows.map((r) => [
|
|
555
|
+
formatWhen(r.at),
|
|
556
|
+
formatWhen(r.end),
|
|
557
|
+
r.phase,
|
|
558
|
+
r.tool || "—",
|
|
559
|
+
r.name,
|
|
560
|
+
r.status,
|
|
561
|
+
logDur(r.ms),
|
|
562
|
+
]),
|
|
563
|
+
),
|
|
564
|
+
)
|
|
565
|
+
} else {
|
|
566
|
+
parts.push(
|
|
567
|
+
formatColumns(
|
|
568
|
+
["when", "ended", "name", "status", "duration"],
|
|
569
|
+
rows.map((r) => [formatWhen(r.at), formatWhen(r.end), r.name, r.status, logDur(r.ms)]),
|
|
570
|
+
),
|
|
571
|
+
)
|
|
572
|
+
}
|
|
573
|
+
return `${parts.join("\n")}\n`
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export function writePerfLog(text: string, fileName: string, dir?: string): string | null {
|
|
577
|
+
try {
|
|
578
|
+
const root = dir ?? path.join(os.tmpdir(), "oes-perf")
|
|
579
|
+
fs.mkdirSync(root, { recursive: true })
|
|
580
|
+
const abs = path.join(root, fileName)
|
|
581
|
+
fs.writeFileSync(abs, text, "utf8")
|
|
582
|
+
return abs
|
|
583
|
+
} catch {
|
|
584
|
+
return null
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export async function readPerfLog(opts: {
|
|
589
|
+
dbPath: string
|
|
590
|
+
sessionId: string
|
|
591
|
+
turns: number
|
|
592
|
+
kind: PerfLogKind
|
|
593
|
+
now: number
|
|
594
|
+
logDir?: string
|
|
595
|
+
/** When set, filter tool-phase rows to this bare tool name (e.g. "bash"). */
|
|
596
|
+
toolFilter?: string
|
|
597
|
+
}): Promise<PerfLogDoc | null> {
|
|
598
|
+
if (!opts.dbPath || !fs.existsSync(opts.dbPath)) return null
|
|
599
|
+
const partLimit = Math.min(6000, Math.max(200, opts.turns * 12))
|
|
600
|
+
const data = await readonlyReadGateway.run<{ readonly msgs: MsgRow[]; readonly parts: PartRow[] } | null>({
|
|
601
|
+
dbPath: opts.dbPath,
|
|
602
|
+
initial: () => ({ msgs: [], parts: [] }),
|
|
603
|
+
stages: [
|
|
604
|
+
(db, state) => state && { ...state, msgs: db.all<MsgRow>(MSG_SQL, opts.sessionId, opts.turns) },
|
|
605
|
+
(db, state) => state && {
|
|
606
|
+
...state,
|
|
607
|
+
parts: db.all<LogPartSql>(LOG_PART_SQL, opts.sessionId, partLimit).map(foldLogPart),
|
|
608
|
+
},
|
|
609
|
+
],
|
|
610
|
+
fallback: () => null,
|
|
611
|
+
})
|
|
612
|
+
if (!data) return null
|
|
613
|
+
const allRows = collectPerfLogRows(opts.kind, data.msgs, data.parts)
|
|
614
|
+
const rows = opts.toolFilter ? allRows.filter((row) => row.tool === opts.toolFilter) : allRows
|
|
615
|
+
const kindLabel = opts.toolFilter
|
|
616
|
+
? `${perfLogKindLabel(opts.kind)} · ${opts.toolFilter}`
|
|
617
|
+
: perfLogKindLabel(opts.kind)
|
|
618
|
+
const text = formatPerfLog(opts.kind, opts.sessionId, opts.now, rows, kindLabel)
|
|
619
|
+
const fileName = perfLogFileName(opts.kind, opts.now, opts.toolFilter)
|
|
620
|
+
const written = writePerfLog(text, fileName, opts.logDir)
|
|
621
|
+
return { title: `${kindLabel} log`, fileName, text, written }
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
function collectParts(rows: PartRow[]): {
|
|
625
|
+
byMsg: Map<string, Bucket>
|
|
626
|
+
tools: Map<string, ToolPerf>
|
|
627
|
+
} {
|
|
628
|
+
const byMsg = new Map<string, Bucket>()
|
|
629
|
+
const tools = new Map<string, ToolPerf>()
|
|
630
|
+
for (const row of rows) {
|
|
631
|
+
const kind = row.kind || ""
|
|
632
|
+
if (kind !== PART_TYPE_TEXT && kind !== PART_TYPE_REASONING && kind !== PART_TYPE_TOOL) continue
|
|
633
|
+
const mid = String(row.mid || "")
|
|
634
|
+
if (!mid) continue
|
|
635
|
+
const b = byMsg.get(mid) ?? newBucket()
|
|
636
|
+
|
|
637
|
+
if (kind === PART_TYPE_TEXT || kind === PART_TYPE_REASONING) {
|
|
638
|
+
const start = toEpochMs(row.pstart)
|
|
639
|
+
const end = toEpochMs(row.pend)
|
|
640
|
+
if (start != null && (b.firstOut == null || start < b.firstOut)) b.firstOut = start
|
|
641
|
+
if (kind === PART_TYPE_REASONING) {
|
|
642
|
+
b.thinkMs += span(start, end)
|
|
643
|
+
} else {
|
|
644
|
+
if (start != null && (b.textStart == null || start < b.textStart)) b.textStart = start
|
|
645
|
+
if (end != null && (b.textEnd == null || end > b.textEnd)) b.textEnd = end
|
|
646
|
+
}
|
|
647
|
+
byMsg.set(mid, b)
|
|
648
|
+
continue
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (!countsAsTool(row.tool)) continue
|
|
652
|
+
const start = toEpochMs(row.tstart)
|
|
653
|
+
const end = toEpochMs(row.tend)
|
|
654
|
+
const ms = span(start, end)
|
|
655
|
+
const failed = (row.status || "").toLowerCase() === STATUS_ERROR
|
|
656
|
+
b.tools += 1
|
|
657
|
+
b.toolMs += ms
|
|
658
|
+
if (failed) b.toolErrors += 1
|
|
659
|
+
byMsg.set(mid, b)
|
|
660
|
+
|
|
661
|
+
const name = (row.tool || "tool").trim() || "tool"
|
|
662
|
+
const agg = tools.get(name) ?? { name, count: 0, errors: 0, totalMs: 0, avgMs: 0 }
|
|
663
|
+
agg.count += 1
|
|
664
|
+
agg.totalMs += ms
|
|
665
|
+
if (failed) agg.errors += 1
|
|
666
|
+
tools.set(name, agg)
|
|
667
|
+
}
|
|
668
|
+
return { byMsg, tools }
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
type LogPartSql = PartRow & {
|
|
672
|
+
command2?: string | null
|
|
673
|
+
filePath2?: string | null
|
|
674
|
+
pattern2?: string | null
|
|
675
|
+
description2?: string | null
|
|
676
|
+
category?: string | null
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function foldLogPart(row: LogPartSql): PartRow {
|
|
680
|
+
return {
|
|
681
|
+
mid: row.mid,
|
|
682
|
+
kind: row.kind,
|
|
683
|
+
pstart: row.pstart,
|
|
684
|
+
pend: row.pend,
|
|
685
|
+
tool: row.tool,
|
|
686
|
+
status: row.status,
|
|
687
|
+
tstart: row.tstart,
|
|
688
|
+
tend: row.tend,
|
|
689
|
+
title: hintStr(row.title),
|
|
690
|
+
command: hintStr(row.command) || hintStr(row.command2),
|
|
691
|
+
filePath: hintStr(row.filePath) || hintStr(row.filePath2),
|
|
692
|
+
pattern: hintStr(row.pattern) || hintStr(row.pattern2),
|
|
693
|
+
description: hintStr(row.description) || hintStr(row.description2),
|
|
694
|
+
subagent: hintStr(row.subagent) || hintStr(row.category),
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
function readRows(
|
|
699
|
+
db: SqlDb,
|
|
700
|
+
sessionId: string,
|
|
701
|
+
turns: number,
|
|
702
|
+
detailed = false,
|
|
703
|
+
): { msgs: MsgRow[]; parts: PartRow[] } {
|
|
704
|
+
const partLimit = Math.min(6000, Math.max(200, turns * 12))
|
|
705
|
+
const msgs = db.all<MsgRow>(MSG_SQL, sessionId, turns)
|
|
706
|
+
const parts = detailed
|
|
707
|
+
? db.all<LogPartSql>(LOG_PART_SQL, sessionId, partLimit).map(foldLogPart)
|
|
708
|
+
: db.all<PartRow>(PART_SQL, sessionId, partLimit)
|
|
709
|
+
return { msgs, parts }
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/** Fold message + part rows into the Perf snapshot. Exported for tests. */
|
|
713
|
+
export function aggregate(sessionId: string, msgs: MsgRow[], parts: PartRow[]): PerfSnapshot {
|
|
714
|
+
const snap = emptyPerf(sessionId)
|
|
715
|
+
snap.present = true
|
|
716
|
+
const { byMsg, tools } = collectParts(parts)
|
|
717
|
+
|
|
718
|
+
const models = new Map<string, ModelPerf>()
|
|
719
|
+
const waitCount = new Map<string, number>()
|
|
720
|
+
const thinkCount = new Map<string, number>()
|
|
721
|
+
const recvCount = new Map<string, number>()
|
|
722
|
+
const turnCount = new Map<string, number>()
|
|
723
|
+
const sums = new Map<string, { wait: number; think: number; recv: number; turn: number }>()
|
|
724
|
+
const trend: TrendPoint[] = []
|
|
725
|
+
|
|
726
|
+
let firstStart = 0
|
|
727
|
+
let lastEnd = 0
|
|
728
|
+
|
|
729
|
+
for (const row of msgs) {
|
|
730
|
+
if ((row.role || "") !== "assistant") continue
|
|
731
|
+
const start = toEpochMs(row.created)
|
|
732
|
+
const end = toEpochMs(row.completed)
|
|
733
|
+
const b = byMsg.get(String(row.id)) ?? newBucket()
|
|
734
|
+
|
|
735
|
+
const waitMs = span(start, b.firstOut)
|
|
736
|
+
const recvMs = span(b.textStart, b.textEnd)
|
|
737
|
+
const turnMs = span(start, end)
|
|
738
|
+
const out = num(row.tout)
|
|
739
|
+
|
|
740
|
+
snap.totals.turns += 1
|
|
741
|
+
if (row.err) {
|
|
742
|
+
if (String(row.err).toLowerCase().includes("abort")) snap.totals.aborts += 1
|
|
743
|
+
else snap.totals.errors += 1
|
|
744
|
+
}
|
|
745
|
+
snap.totals.phases.wait += waitMs
|
|
746
|
+
snap.totals.phases.think += b.thinkMs
|
|
747
|
+
snap.totals.phases.recv += recvMs
|
|
748
|
+
snap.totals.phases.tool += b.toolMs
|
|
749
|
+
snap.totals.activeMs += turnMs
|
|
750
|
+
snap.totals.tokensIn += num(row.tin)
|
|
751
|
+
snap.totals.tokensOut += out
|
|
752
|
+
snap.totals.tokensReasoning += num(row.treason)
|
|
753
|
+
snap.totals.cacheRead += num(row.cread)
|
|
754
|
+
snap.totals.cacheWrite += num(row.cwrite)
|
|
755
|
+
snap.totals.cost += typeof row.cost === "number" && row.cost > 0 ? row.cost : 0
|
|
756
|
+
if (start != null && (firstStart === 0 || start < firstStart)) firstStart = start
|
|
757
|
+
const tail = end ?? b.textEnd ?? start
|
|
758
|
+
if (tail != null && tail > lastEnd) lastEnd = tail
|
|
759
|
+
|
|
760
|
+
const key = `${row.provider || "?"}/${row.model || "?"}`
|
|
761
|
+
const m = models.get(key) ?? {
|
|
762
|
+
key,
|
|
763
|
+
model: modelLabel(row.provider, row.model),
|
|
764
|
+
provider: (row.provider || "").trim(),
|
|
765
|
+
turns: 0,
|
|
766
|
+
waitMs: null,
|
|
767
|
+
thinkMs: null,
|
|
768
|
+
recvMs: null,
|
|
769
|
+
turnMs: null,
|
|
770
|
+
tokensPerSec: null,
|
|
771
|
+
toolMs: 0,
|
|
772
|
+
tools: 0,
|
|
773
|
+
errors: 0,
|
|
774
|
+
tokensIn: 0,
|
|
775
|
+
tokensOut: 0,
|
|
776
|
+
tokensReasoning: 0,
|
|
777
|
+
cacheRead: 0,
|
|
778
|
+
cacheWrite: 0,
|
|
779
|
+
cost: 0,
|
|
780
|
+
lastAt: 0,
|
|
781
|
+
}
|
|
782
|
+
m.turns += 1
|
|
783
|
+
m.toolMs += b.toolMs
|
|
784
|
+
m.tools += b.tools
|
|
785
|
+
m.errors += b.toolErrors + (row.err ? 1 : 0)
|
|
786
|
+
m.tokensIn += num(row.tin)
|
|
787
|
+
m.tokensOut += out
|
|
788
|
+
m.tokensReasoning += num(row.treason)
|
|
789
|
+
m.cacheRead += num(row.cread)
|
|
790
|
+
m.cacheWrite += num(row.cwrite)
|
|
791
|
+
m.cost += typeof row.cost === "number" && row.cost > 0 ? row.cost : 0
|
|
792
|
+
if (start != null && start > m.lastAt) m.lastAt = start
|
|
793
|
+
models.set(key, m)
|
|
794
|
+
|
|
795
|
+
const s = sums.get(key) ?? { wait: 0, think: 0, recv: 0, turn: 0 }
|
|
796
|
+
if (waitMs > 0) {
|
|
797
|
+
s.wait += waitMs
|
|
798
|
+
waitCount.set(key, (waitCount.get(key) ?? 0) + 1)
|
|
799
|
+
}
|
|
800
|
+
if (b.thinkMs > 0) {
|
|
801
|
+
s.think += b.thinkMs
|
|
802
|
+
thinkCount.set(key, (thinkCount.get(key) ?? 0) + 1)
|
|
803
|
+
}
|
|
804
|
+
if (recvMs > 0) {
|
|
805
|
+
s.recv += recvMs
|
|
806
|
+
recvCount.set(key, (recvCount.get(key) ?? 0) + 1)
|
|
807
|
+
}
|
|
808
|
+
if (turnMs > 0) {
|
|
809
|
+
s.turn += turnMs
|
|
810
|
+
turnCount.set(key, (turnCount.get(key) ?? 0) + 1)
|
|
811
|
+
}
|
|
812
|
+
sums.set(key, s)
|
|
813
|
+
|
|
814
|
+
trend.push({
|
|
815
|
+
at: start ?? 0,
|
|
816
|
+
waitMs: waitMs > 0 ? waitMs : null,
|
|
817
|
+
tokensPerSec: recvMs > 0 && out > 0 ? (out / recvMs) * 1000 : null,
|
|
818
|
+
})
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
for (const [key, m] of models) {
|
|
822
|
+
const s = sums.get(key) ?? { wait: 0, think: 0, recv: 0, turn: 0 }
|
|
823
|
+
m.waitMs = avg(s.wait, waitCount.get(key) ?? 0)
|
|
824
|
+
m.thinkMs = avg(s.think, thinkCount.get(key) ?? 0)
|
|
825
|
+
m.recvMs = avg(s.recv, recvCount.get(key) ?? 0)
|
|
826
|
+
m.turnMs = avg(s.turn, turnCount.get(key) ?? 0)
|
|
827
|
+
m.tokensPerSec = s.recv > 0 ? (m.tokensOut / s.recv) * 1000 : null
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// Tool calls can straddle turn boundaries, so phases are measured against the
|
|
831
|
+
// whole window rather than the summed turn durations.
|
|
832
|
+
const p = snap.totals.phases
|
|
833
|
+
snap.totals.wallMs = span(firstStart, lastEnd)
|
|
834
|
+
p.idle = Math.max(0, snap.totals.wallMs - (p.wait + p.think + p.recv + p.tool))
|
|
835
|
+
const sent = snap.totals.tokensIn + snap.totals.cacheRead
|
|
836
|
+
snap.totals.cacheHit = sent > 0 ? snap.totals.cacheRead / sent : null
|
|
837
|
+
|
|
838
|
+
snap.models = [...models.values()].sort((a, b) => b.turns - a.turns || b.lastAt - a.lastAt)
|
|
839
|
+
snap.tools = [...tools.values()]
|
|
840
|
+
.map((t) => ({ ...t, avgMs: t.count > 0 ? Math.round(t.totalMs / t.count) : 0 }))
|
|
841
|
+
.sort((a, b) => b.totalMs - a.totalMs || b.count - a.count)
|
|
842
|
+
snap.trend = trend.reverse()
|
|
843
|
+
return snap
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/** Compact per-session row for the History section. */
|
|
847
|
+
function sessionPerf(db: SqlDb, id: string, title: string, turns: number): SessionPerf | null {
|
|
848
|
+
const { msgs, parts } = readRows(db, id, turns)
|
|
849
|
+
const agg = aggregate(id, msgs, parts)
|
|
850
|
+
if (agg.totals.turns === 0) return null
|
|
851
|
+
const top = agg.models[0]
|
|
852
|
+
const active = agg.totals.activeMs
|
|
853
|
+
return {
|
|
854
|
+
id,
|
|
855
|
+
title,
|
|
856
|
+
model: top?.model ?? "",
|
|
857
|
+
turns: agg.totals.turns,
|
|
858
|
+
waitMs: top?.waitMs ?? null,
|
|
859
|
+
tokensPerSec: top?.tokensPerSec ?? null,
|
|
860
|
+
toolShare: active > 0 ? agg.totals.phases.tool / active : null,
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
export type PerfOptions = {
|
|
865
|
+
dbPath: string
|
|
866
|
+
sessionId: string
|
|
867
|
+
/** How many recent messages to scan. */
|
|
868
|
+
turns: number
|
|
869
|
+
/** Recent sessions for the History section, current one excluded by the caller. */
|
|
870
|
+
history?: Array<{ id: string; title: string }>
|
|
871
|
+
historyTurns?: number
|
|
872
|
+
/** Snapshot fingerprint — identical input returns the cached result. */
|
|
873
|
+
cacheKey?: string
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
const perfCache = createStampCache<PerfSnapshot>()
|
|
877
|
+
let histKey = ""
|
|
878
|
+
let histAt = 0
|
|
879
|
+
let histCached: SessionPerf[] = []
|
|
880
|
+
const HIST_TTL_MS = 10_000
|
|
881
|
+
|
|
882
|
+
export function resetPerfCache(): void {
|
|
883
|
+
perfCache.reset()
|
|
884
|
+
histKey = ""
|
|
885
|
+
histAt = 0
|
|
886
|
+
histCached = []
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
type PerfReadState = {
|
|
890
|
+
readonly ok: boolean
|
|
891
|
+
readonly msgs: MsgRow[]
|
|
892
|
+
readonly parts: PartRow[]
|
|
893
|
+
readonly snapshot: PerfSnapshot
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
export async function readPerfSnapshot(opts: PerfOptions): Promise<PerfSnapshot> {
|
|
897
|
+
const key = opts.cacheKey
|
|
898
|
+
? `${opts.cacheKey}::${opts.turns}::${(opts.history ?? []).map((h) => h.id).join(",")}`
|
|
899
|
+
: ""
|
|
900
|
+
const hit = key ? perfCache.peek(key) : undefined
|
|
901
|
+
if (hit) return hit
|
|
902
|
+
if (!opts.dbPath || !fs.existsSync(opts.dbPath)) {
|
|
903
|
+
dbg("perf", "db missing", { dbPath: opts.dbPath, sessionId: opts.sessionId })
|
|
904
|
+
return emptyPerf(opts.sessionId, "db missing")
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
const partLimit = Math.min(6000, Math.max(200, opts.turns * 12))
|
|
908
|
+
const hKey = (opts.history ?? []).map((history) => history.id).join(",")
|
|
909
|
+
const historyHit = hKey === histKey && Date.now() - histAt < HIST_TTL_MS
|
|
910
|
+
const histTurns = Math.max(20, Math.min(opts.historyTurns ?? 60, opts.turns))
|
|
911
|
+
const historyStages = historyHit
|
|
912
|
+
? []
|
|
913
|
+
: (opts.history ?? [])
|
|
914
|
+
.filter((history) => history.id !== opts.sessionId)
|
|
915
|
+
.map((history) => (db: SqlDb, state: PerfReadState): PerfReadState => {
|
|
916
|
+
const row = sessionPerf(db, history.id, history.title, histTurns)
|
|
917
|
+
if (!row) return state
|
|
918
|
+
return {
|
|
919
|
+
...state,
|
|
920
|
+
snapshot: { ...state.snapshot, history: [...state.snapshot.history, row] },
|
|
921
|
+
}
|
|
922
|
+
})
|
|
923
|
+
const result = await profileAsync("perf.read", () =>
|
|
924
|
+
readonlyReadGateway.run<PerfReadState>({
|
|
925
|
+
dbPath: opts.dbPath,
|
|
926
|
+
initial: () => ({ ok: true, msgs: [], parts: [], snapshot: emptyPerf(opts.sessionId) }),
|
|
927
|
+
stages: [
|
|
928
|
+
(db, state) => ({ ...state, msgs: db.all<MsgRow>(MSG_SQL, opts.sessionId, opts.turns) }),
|
|
929
|
+
(db, state) => ({ ...state, parts: db.all<PartRow>(PART_SQL, opts.sessionId, partLimit) }),
|
|
930
|
+
(_db, state) => ({ ...state, snapshot: aggregate(opts.sessionId, state.msgs, state.parts) }),
|
|
931
|
+
...historyStages,
|
|
932
|
+
],
|
|
933
|
+
fallback: (error) => ({
|
|
934
|
+
ok: false,
|
|
935
|
+
msgs: [],
|
|
936
|
+
parts: [],
|
|
937
|
+
snapshot: emptyPerf(
|
|
938
|
+
opts.sessionId,
|
|
939
|
+
error instanceof ReadonlyDbUnavailableError
|
|
940
|
+
? "sqlite unavailable"
|
|
941
|
+
: error instanceof Error
|
|
942
|
+
? error.message
|
|
943
|
+
: "perf read failed",
|
|
944
|
+
),
|
|
945
|
+
}),
|
|
946
|
+
}),
|
|
947
|
+
)
|
|
948
|
+
if (result.ok && historyHit) result.snapshot.history = histCached
|
|
949
|
+
else if (result.ok) {
|
|
950
|
+
histKey = hKey
|
|
951
|
+
histAt = Date.now()
|
|
952
|
+
histCached = result.snapshot.history
|
|
953
|
+
}
|
|
954
|
+
dbg("perf", "loaded", {
|
|
955
|
+
dbPath: opts.dbPath,
|
|
956
|
+
sessionId: opts.sessionId,
|
|
957
|
+
msgs: result.msgs.length,
|
|
958
|
+
parts: result.parts.length,
|
|
959
|
+
})
|
|
960
|
+
if (key && result.ok) perfCache.set(key, result.snapshot)
|
|
961
|
+
return result.snapshot
|
|
962
|
+
}
|