@ycodium-ai/plugin-trace-diagnostics 0.2.2167 → 0.2.2510

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +191 -164
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -44,14 +44,15 @@ function toRotatedTracePaths(traceFilePath, maxFiles) {
44
44
  );
45
45
  return [...backups, traceFilePath];
46
46
  }
47
- function insertBoundedSlowestSpan(slowestSpans, span) {
48
- if (slowestSpans.length >= TOP_LIMIT && span.durationMs <= slowestSpans[slowestSpans.length - 1].durationMs) {
49
- return;
50
- }
51
- slowestSpans.push(span);
52
- slowestSpans.sort((left, right) => right.durationMs - left.durationMs);
53
- if (slowestSpans.length > TOP_LIMIT) slowestSpans.length = TOP_LIMIT;
47
+ function insertBounded(items, item, limit, order) {
48
+ if (items.length >= limit && order(item, items[items.length - 1]) >= 0) return;
49
+ items.push(item);
50
+ items.sort(order);
51
+ if (items.length > limit) items.length = limit;
54
52
  }
53
+ var slowestFirst = (left, right) => right.durationMs - left.durationMs;
54
+ var latestEndedFirst = (left, right) => right.endedAtMs - left.endedAtMs;
55
+ var latestSeenFirst = (left, right) => right.seenAtMs - left.seenAtMs;
55
56
  function makeEmptyDiagnostics(input) {
56
57
  return {
57
58
  traceFilePath: input.traceFilePath,
@@ -75,18 +76,8 @@ function makeEmptyDiagnostics(input) {
75
76
  error: input.error ?? null
76
77
  };
77
78
  }
78
- function aggregateTraceDiagnostics(input) {
79
- const readAt = input.readAt;
79
+ function makeTraceDiagnosticsAggregator() {
80
80
  const slowSpanThresholdMs = DEFAULT_SLOW_SPAN_THRESHOLD_MS;
81
- if (input.files.length === 0) {
82
- return makeEmptyDiagnostics({
83
- ...input,
84
- error: input.error ?? {
85
- kind: "trace-file-not-found",
86
- message: "No local trace files were found."
87
- }
88
- });
89
- }
90
81
  let parseErrorCount = 0;
91
82
  let recordCount = 0;
92
83
  let failureCount = 0;
@@ -100,55 +91,60 @@ function aggregateTraceDiagnostics(input) {
100
91
  const slowestSpans = [];
101
92
  const latestWarningAndErrorLogs = [];
102
93
  const logLevelCounts = {};
103
- for (const file of input.files) {
104
- const lines = file.text.split(/\r?\n/);
105
- for (const line of lines) {
106
- if (line.trim().length === 0) continue;
107
- let parsed;
108
- try {
109
- parsed = JSON.parse(line);
110
- } catch {
111
- parseErrorCount += 1;
112
- continue;
113
- }
114
- if (!isRecordObject(parsed)) {
115
- parseErrorCount += 1;
116
- continue;
117
- }
118
- const name = toStringValue(parsed.name);
119
- const traceId = toStringValue(parsed.traceId);
120
- const spanId = toStringValue(parsed.spanId);
121
- const durationMs = toNumberValue(parsed.durationMs);
122
- const endedAtMs = unixNanoToMillis(parsed.endTimeUnixNano);
123
- const startedAtMs = unixNanoToMillis(parsed.startTimeUnixNano);
124
- if (!name || !traceId || !spanId || durationMs === null || endedAtMs === null) {
125
- parseErrorCount += 1;
126
- continue;
127
- }
128
- recordCount += 1;
129
- firstSpanAtMs = startedAtMs !== null && (firstSpanAtMs === null || startedAtMs < firstSpanAtMs) ? startedAtMs : firstSpanAtMs;
130
- lastSpanAtMs = lastSpanAtMs === null || endedAtMs > lastSpanAtMs ? endedAtMs : lastSpanAtMs;
131
- const exitTag = readExitTag(parsed.exit);
132
- const isFailure = exitTag === "Failure";
133
- const isInterrupted = exitTag === "Interrupted";
134
- if (isFailure) failureCount += 1;
135
- if (isInterrupted) interruptionCount += 1;
136
- const spanSummary = spansByName.get(name) ?? {
137
- count: 0,
138
- failureCount: 0,
139
- totalDurationMs: 0,
140
- maxDurationMs: 0
141
- };
142
- spanSummary.count += 1;
143
- spanSummary.totalDurationMs += durationMs;
144
- spanSummary.maxDurationMs = Math.max(spanSummary.maxDurationMs, durationMs);
145
- if (isFailure) spanSummary.failureCount += 1;
146
- spansByName.set(name, spanSummary);
147
- if (durationMs >= slowSpanThresholdMs) slowSpanCount += 1;
148
- insertBoundedSlowestSpan(slowestSpans, { name, durationMs, endedAtMs, traceId, spanId });
149
- if (isFailure) {
150
- const cause = readExitCause(parsed.exit);
151
- latestFailures.push({
94
+ const addLine = (line) => {
95
+ if (line.trim().length === 0) return;
96
+ let parsed;
97
+ try {
98
+ parsed = JSON.parse(line);
99
+ } catch {
100
+ parseErrorCount += 1;
101
+ return;
102
+ }
103
+ if (!isRecordObject(parsed)) {
104
+ parseErrorCount += 1;
105
+ return;
106
+ }
107
+ const name = toStringValue(parsed.name);
108
+ const traceId = toStringValue(parsed.traceId);
109
+ const spanId = toStringValue(parsed.spanId);
110
+ const durationMs = toNumberValue(parsed.durationMs);
111
+ const endedAtMs = unixNanoToMillis(parsed.endTimeUnixNano);
112
+ const startedAtMs = unixNanoToMillis(parsed.startTimeUnixNano);
113
+ if (!name || !traceId || !spanId || durationMs === null || endedAtMs === null) {
114
+ parseErrorCount += 1;
115
+ return;
116
+ }
117
+ recordCount += 1;
118
+ firstSpanAtMs = startedAtMs !== null && (firstSpanAtMs === null || startedAtMs < firstSpanAtMs) ? startedAtMs : firstSpanAtMs;
119
+ lastSpanAtMs = lastSpanAtMs === null || endedAtMs > lastSpanAtMs ? endedAtMs : lastSpanAtMs;
120
+ const exitTag = readExitTag(parsed.exit);
121
+ const isFailure = exitTag === "Failure";
122
+ const isInterrupted = exitTag === "Interrupted";
123
+ if (isFailure) failureCount += 1;
124
+ if (isInterrupted) interruptionCount += 1;
125
+ const spanSummary = spansByName.get(name) ?? {
126
+ count: 0,
127
+ failureCount: 0,
128
+ totalDurationMs: 0,
129
+ maxDurationMs: 0
130
+ };
131
+ spanSummary.count += 1;
132
+ spanSummary.totalDurationMs += durationMs;
133
+ spanSummary.maxDurationMs = Math.max(spanSummary.maxDurationMs, durationMs);
134
+ if (isFailure) spanSummary.failureCount += 1;
135
+ spansByName.set(name, spanSummary);
136
+ if (durationMs >= slowSpanThresholdMs) slowSpanCount += 1;
137
+ insertBounded(
138
+ slowestSpans,
139
+ { name, durationMs, endedAtMs, traceId, spanId },
140
+ TOP_LIMIT,
141
+ slowestFirst
142
+ );
143
+ if (isFailure) {
144
+ const cause = readExitCause(parsed.exit);
145
+ insertBounded(
146
+ latestFailures,
147
+ {
152
148
  name,
153
149
  cause,
154
150
  durationMs,
@@ -156,34 +152,39 @@ function aggregateTraceDiagnostics(input) {
156
152
  traceId,
157
153
  spanId,
158
154
  endedAtMs
159
- });
160
- const failureKey = `${name}\0${cause}`;
161
- const existing = failuresByKey.get(failureKey);
162
- const isLatest = existing === void 0 || endedAtMs > existing.lastSeenAtMs;
163
- failuresByKey.set(failureKey, {
164
- name,
165
- cause,
166
- count: (existing?.count ?? 0) + 1,
167
- lastSeenAtMs: isLatest ? endedAtMs : existing.lastSeenAtMs,
168
- lastSeenAt: new Date(isLatest ? endedAtMs : existing.lastSeenAtMs).toISOString(),
169
- traceId: isLatest ? traceId : existing.traceId,
170
- spanId: isLatest ? spanId : existing.spanId
171
- });
172
- }
173
- if (Array.isArray(parsed.events)) {
174
- for (const rawEvent of parsed.events) {
175
- if (!isTraceEvent(rawEvent)) continue;
176
- const attributes = readEventAttributes(rawEvent);
177
- const level = toStringValue(attributes["effect.logLevel"]);
178
- if (!level) continue;
179
- logLevelCounts[level] = (logLevelCounts[level] ?? 0) + 1;
180
- const normalizedLevel = level.toLowerCase();
181
- if (normalizedLevel !== "warning" && normalizedLevel !== "warn" && normalizedLevel !== "error" && normalizedLevel !== "fatal") {
182
- continue;
183
- }
184
- const seenAtMs = unixNanoToMillis(rawEvent.timeUnixNano) ?? endedAtMs;
185
- const message = toStringValue(rawEvent.name)?.trim() ?? "Log event";
186
- latestWarningAndErrorLogs.push({
155
+ },
156
+ RECENT_LIMIT,
157
+ latestEndedFirst
158
+ );
159
+ const failureKey = `${name}\0${cause}`;
160
+ const existing = failuresByKey.get(failureKey);
161
+ const isLatest = existing === void 0 || endedAtMs > existing.lastSeenAtMs;
162
+ failuresByKey.set(failureKey, {
163
+ name,
164
+ cause,
165
+ count: (existing?.count ?? 0) + 1,
166
+ lastSeenAtMs: isLatest ? endedAtMs : existing.lastSeenAtMs,
167
+ lastSeenAt: new Date(isLatest ? endedAtMs : existing.lastSeenAtMs).toISOString(),
168
+ traceId: isLatest ? traceId : existing.traceId,
169
+ spanId: isLatest ? spanId : existing.spanId
170
+ });
171
+ }
172
+ if (Array.isArray(parsed.events)) {
173
+ for (const rawEvent of parsed.events) {
174
+ if (!isTraceEvent(rawEvent)) continue;
175
+ const attributes = readEventAttributes(rawEvent);
176
+ const level = toStringValue(attributes["effect.logLevel"]);
177
+ if (!level) continue;
178
+ logLevelCounts[level] = (logLevelCounts[level] ?? 0) + 1;
179
+ const normalizedLevel = level.toLowerCase();
180
+ if (normalizedLevel !== "warning" && normalizedLevel !== "warn" && normalizedLevel !== "error" && normalizedLevel !== "fatal") {
181
+ continue;
182
+ }
183
+ const seenAtMs = unixNanoToMillis(rawEvent.timeUnixNano) ?? endedAtMs;
184
+ const message = toStringValue(rawEvent.name)?.trim() ?? "Log event";
185
+ insertBounded(
186
+ latestWarningAndErrorLogs,
187
+ {
187
188
  spanName: name,
188
189
  level,
189
190
  message,
@@ -191,73 +192,86 @@ function aggregateTraceDiagnostics(input) {
191
192
  traceId,
192
193
  spanId,
193
194
  seenAtMs
194
- });
195
- }
195
+ },
196
+ RECENT_LIMIT,
197
+ latestSeenFirst
198
+ );
196
199
  }
197
200
  }
198
- }
199
- const topSpansByCount = [...spansByName.entries()].map(([name, span]) => ({
200
- name,
201
- count: span.count,
202
- failureCount: span.failureCount,
203
- totalDurationMs: span.totalDurationMs,
204
- averageDurationMs: span.count > 0 ? span.totalDurationMs / span.count : 0,
205
- maxDurationMs: span.maxDurationMs
206
- })).toSorted((left, right) => right.count - left.count || right.maxDurationMs - left.maxDurationMs).slice(0, TOP_LIMIT);
207
- return {
208
- traceFilePath: input.traceFilePath,
209
- scannedFilePaths: input.scannedFilePaths,
210
- readAt,
211
- recordCount,
212
- parseErrorCount,
213
- firstSpanAt: firstSpanAtMs === null ? null : new Date(firstSpanAtMs).toISOString(),
214
- lastSpanAt: lastSpanAtMs === null ? null : new Date(lastSpanAtMs).toISOString(),
215
- failureCount,
216
- interruptionCount,
217
- slowSpanThresholdMs,
218
- slowSpanCount,
219
- logLevelCounts,
220
- topSpansByCount,
221
- slowestSpans: slowestSpans.map(({ name, durationMs, endedAtMs, traceId, spanId }) => ({
222
- name,
223
- durationMs,
224
- endedAt: new Date(endedAtMs).toISOString(),
225
- traceId,
226
- spanId
227
- })),
228
- commonFailures: [...failuresByKey.values()].toSorted((left, right) => right.count - left.count || right.lastSeenAtMs - left.lastSeenAtMs).slice(0, TOP_LIMIT).map(({ name, cause, count, lastSeenAt, traceId, spanId }) => ({
229
- name,
230
- cause,
231
- count,
232
- lastSeenAt,
233
- traceId,
234
- spanId
235
- })),
236
- latestFailures: latestFailures.toSorted((left, right) => right.endedAtMs - left.endedAtMs).slice(0, RECENT_LIMIT).map(({ name, cause, durationMs, endedAt, traceId, spanId }) => ({
201
+ };
202
+ const finish = (meta) => {
203
+ const topSpansByCount = [...spansByName.entries()].map(([name, span]) => ({
237
204
  name,
238
- cause,
239
- durationMs,
240
- endedAt,
241
- traceId,
242
- spanId
243
- })),
244
- latestWarningAndErrorLogs: latestWarningAndErrorLogs.toSorted((left, right) => right.seenAtMs - left.seenAtMs).slice(0, RECENT_LIMIT).map(({ spanName, level, message, seenAt, traceId, spanId }) => ({
245
- spanName,
246
- level,
247
- message,
248
- seenAt,
249
- traceId,
250
- spanId
251
- })),
252
- partialFailure: input.partialFailure ?? false,
253
- error: input.error ?? null
205
+ count: span.count,
206
+ failureCount: span.failureCount,
207
+ totalDurationMs: span.totalDurationMs,
208
+ averageDurationMs: span.count > 0 ? span.totalDurationMs / span.count : 0,
209
+ maxDurationMs: span.maxDurationMs
210
+ })).toSorted(
211
+ (left, right) => right.count - left.count || right.maxDurationMs - left.maxDurationMs
212
+ ).slice(0, TOP_LIMIT);
213
+ return {
214
+ traceFilePath: meta.traceFilePath,
215
+ scannedFilePaths: meta.scannedFilePaths,
216
+ readAt: meta.readAt,
217
+ recordCount,
218
+ parseErrorCount,
219
+ firstSpanAt: firstSpanAtMs === null ? null : new Date(firstSpanAtMs).toISOString(),
220
+ lastSpanAt: lastSpanAtMs === null ? null : new Date(lastSpanAtMs).toISOString(),
221
+ failureCount,
222
+ interruptionCount,
223
+ slowSpanThresholdMs,
224
+ slowSpanCount,
225
+ logLevelCounts,
226
+ topSpansByCount,
227
+ slowestSpans: slowestSpans.map(({ name, durationMs, endedAtMs, traceId, spanId }) => ({
228
+ name,
229
+ durationMs,
230
+ endedAt: new Date(endedAtMs).toISOString(),
231
+ traceId,
232
+ spanId
233
+ })),
234
+ commonFailures: [...failuresByKey.values()].toSorted(
235
+ (left, right) => right.count - left.count || right.lastSeenAtMs - left.lastSeenAtMs
236
+ ).slice(0, TOP_LIMIT).map(({ name, cause, count, lastSeenAt, traceId, spanId }) => ({
237
+ name,
238
+ cause,
239
+ count,
240
+ lastSeenAt,
241
+ traceId,
242
+ spanId
243
+ })),
244
+ latestFailures: latestFailures.map(
245
+ ({ name, cause, durationMs, endedAt, traceId, spanId }) => ({
246
+ name,
247
+ cause,
248
+ durationMs,
249
+ endedAt,
250
+ traceId,
251
+ spanId
252
+ })
253
+ ),
254
+ latestWarningAndErrorLogs: latestWarningAndErrorLogs.map(
255
+ ({ spanName, level, message, seenAt, traceId, spanId }) => ({
256
+ spanName,
257
+ level,
258
+ message,
259
+ seenAt,
260
+ traceId,
261
+ spanId
262
+ })
263
+ ),
264
+ partialFailure: meta.partialFailure ?? false,
265
+ error: meta.error ?? null
266
+ };
254
267
  };
268
+ return { addLine, finish };
255
269
  }
256
270
  function describeError(error) {
257
271
  return error instanceof Error ? error.message : String(error);
258
272
  }
259
- async function readCandidateFiles(fs, candidates) {
260
- const files = [];
273
+ async function streamCandidateFiles(fs, candidates, onLine) {
274
+ let foundFile = false;
261
275
  let readFailure = null;
262
276
  for (const path of candidates) {
263
277
  try {
@@ -266,13 +280,13 @@ async function readCandidateFiles(fs, candidates) {
266
280
  continue;
267
281
  }
268
282
  try {
269
- const text = await fs.readFile(path);
270
- files.push({ path, text });
283
+ for await (const line of fs.readLines(path)) onLine(line);
284
+ foundFile = true;
271
285
  } catch (error) {
272
286
  readFailure ??= { path, message: describeError(error) };
273
287
  }
274
288
  }
275
- return { files, readFailure };
289
+ return { foundFile, readFailure };
276
290
  }
277
291
  var SUMMARY_PAYLOAD_SCHEMA = { type: "object", additionalProperties: false };
278
292
  var SPAN_SUMMARY_SCHEMA = {
@@ -424,7 +438,6 @@ var traceDiagnosticsPlugin = definePlugin({
424
438
  if (traceFilePath === null) {
425
439
  return makeEmptyDiagnostics({
426
440
  traceFilePath: "",
427
- files: [],
428
441
  scannedFilePaths: [],
429
442
  readAt,
430
443
  error: {
@@ -435,10 +448,14 @@ var traceDiagnosticsPlugin = definePlugin({
435
448
  }
436
449
  const maxFiles = typeof config.maxFiles === "number" && Number.isFinite(config.maxFiles) ? config.maxFiles : DEFAULT_MAX_FILES;
437
450
  const candidates = toRotatedTracePaths(traceFilePath, maxFiles);
438
- const { files, readFailure } = await readCandidateFiles(ctx.fs, candidates);
439
- return aggregateTraceDiagnostics({
451
+ const aggregator = makeTraceDiagnosticsAggregator();
452
+ const { foundFile, readFailure } = await streamCandidateFiles(
453
+ ctx.fs,
454
+ candidates,
455
+ aggregator.addLine
456
+ );
457
+ const meta = {
440
458
  traceFilePath,
441
- files,
442
459
  scannedFilePaths: candidates,
443
460
  readAt,
444
461
  ...readFailure ? {
@@ -448,13 +465,23 @@ var traceDiagnosticsPlugin = definePlugin({
448
465
  message: `Failed to read local trace file '${readFailure.path}': ${readFailure.message}`
449
466
  }
450
467
  } : {}
451
- });
468
+ };
469
+ if (!foundFile) {
470
+ return makeEmptyDiagnostics({
471
+ ...meta,
472
+ error: meta.error ?? {
473
+ kind: "trace-file-not-found",
474
+ message: "No local trace files were found."
475
+ }
476
+ });
477
+ }
478
+ return aggregator.finish(meta);
452
479
  }
453
480
  });
454
481
  }
455
482
  });
456
483
  var index_default = traceDiagnosticsPlugin;
457
484
  export {
458
- aggregateTraceDiagnostics,
459
- index_default as default
485
+ index_default as default,
486
+ makeTraceDiagnosticsAggregator
460
487
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ycodium-ai/plugin-trace-diagnostics",
3
- "version": "0.2.2167",
3
+ "version": "0.2.2510",
4
4
  "description": "Reads this machine's trace files for the diagnostics page.",
5
5
  "keywords": [
6
6
  "diagnostics",
@@ -20,7 +20,7 @@
20
20
  "@effect/vitest": "4.0.0-beta.103",
21
21
  "esbuild": "0.28.1",
22
22
  "vite-plus": "0.2.2",
23
- "@ycodium-ai/plugin-api": "0.2.2167"
23
+ "@ycodium-ai/plugin-api": "0.2.2510"
24
24
  },
25
25
  "ycodium": {
26
26
  "plugin": "./dist/index.js",