agent-inspect 0.1.1 → 1.0.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.
@@ -1,13 +1,16 @@
1
1
  #!/usr/bin/env node
2
- import { realpathSync } from 'fs';
2
+ import { realpathSync, createReadStream } from 'fs';
3
3
  import path from 'path';
4
4
  import { fileURLToPath } from 'url';
5
5
  import { Command, Option } from 'commander';
6
- import { appendFile, readdir, stat, mkdir, readFile } from 'fs/promises';
7
- import os from 'os';
6
+ import { unlink, stat, mkdir, writeFile, appendFile, readdir, readFile, open } from 'fs/promises';
7
+ import crypto from 'crypto';
8
8
  import { nanoid } from 'nanoid';
9
+ import os from 'os';
9
10
  import { AsyncLocalStorage } from 'async_hooks';
10
- import chalk from 'chalk';
11
+ import { createInterface } from 'readline';
12
+ import chalk2 from 'chalk';
13
+ import { stdin, stdout } from 'process';
11
14
 
12
15
  // packages/core/src/types.ts
13
16
  var STEP_TYPES = [
@@ -19,9 +22,1030 @@ var STEP_TYPES = [
19
22
  "state",
20
23
  "custom"
21
24
  ];
25
+ function isRecord(value) {
26
+ return typeof value === "object" && value !== null && !Array.isArray(value);
27
+ }
22
28
  function isStepType(value) {
23
29
  return typeof value === "string" && STEP_TYPES.includes(value);
24
30
  }
31
+ function isTraceEvent(value) {
32
+ if (!isRecord(value)) return false;
33
+ if (value.schemaVersion !== "0.1") return false;
34
+ if (typeof value.timestamp !== "number") return false;
35
+ if (typeof value.event !== "string") return false;
36
+ switch (value.event) {
37
+ case "run_started": {
38
+ return typeof value.runId === "string" && typeof value.name === "string" && typeof value.startTime === "number";
39
+ }
40
+ case "run_completed": {
41
+ return typeof value.runId === "string" && (value.status === "success" || value.status === "error") && typeof value.endTime === "number" && typeof value.durationMs === "number";
42
+ }
43
+ case "step_started": {
44
+ return typeof value.runId === "string" && typeof value.stepId === "string" && typeof value.name === "string" && isStepType(value.type) && typeof value.startTime === "number";
45
+ }
46
+ case "step_completed": {
47
+ return typeof value.runId === "string" && typeof value.stepId === "string" && (value.status === "success" || value.status === "error") && typeof value.endTime === "number" && typeof value.durationMs === "number";
48
+ }
49
+ default:
50
+ return false;
51
+ }
52
+ }
53
+ function isRecord2(v) {
54
+ return typeof v === "object" && v !== null && !Array.isArray(v);
55
+ }
56
+ function isNonEmptyStringArray(v) {
57
+ return Array.isArray(v) && v.length > 0 && v.every((x) => typeof x === "string" && x.trim() !== "");
58
+ }
59
+ function validateRedact(redact) {
60
+ if (!Array.isArray(redact)) {
61
+ throw new Error("Invalid config: redact must be an array");
62
+ }
63
+ for (const r of redact) {
64
+ if (typeof r === "string") continue;
65
+ if (!isRecord2(r)) {
66
+ throw new Error("Invalid config: redact entries must be strings or objects");
67
+ }
68
+ if (typeof r.key !== "string" || r.key.trim() === "") {
69
+ throw new Error("Invalid config: redact.key must be a non-empty string");
70
+ }
71
+ if (r.strategy !== "full" && r.strategy !== "prefix" && r.strategy !== "hash") {
72
+ throw new Error(
73
+ `Invalid config: redact.strategy must be one of full, prefix, hash (got ${String(
74
+ r.strategy
75
+ )})`
76
+ );
77
+ }
78
+ if (r.keep !== void 0 && (typeof r.keep !== "number" || !Number.isFinite(r.keep) || r.keep < 0)) {
79
+ throw new Error("Invalid config: redact.keep must be a non-negative number when provided");
80
+ }
81
+ }
82
+ }
83
+ function validateMappings(mappings) {
84
+ if (!isRecord2(mappings)) {
85
+ throw new Error("Invalid config: mappings must be an object");
86
+ }
87
+ }
88
+ var DEFAULT_LOG_INGEST_CONFIG = {
89
+ runIdKeys: ["runId", "traceId", "requestId", "decisionId", "jobId"],
90
+ eventKey: "event",
91
+ timestampKey: "timestamp",
92
+ messageKey: "message",
93
+ levelKey: "level",
94
+ heuristicWindowMs: 2e3,
95
+ mappings: {
96
+ "*.error": { kind: "ERROR", status: "error" },
97
+ "*.failed": { kind: "ERROR", status: "error" },
98
+ "*.llm.*": { kind: "LLM" },
99
+ "*.tool.*": { kind: "TOOL" },
100
+ "*.agent.*": { kind: "AGENT" },
101
+ "*.retriever.*": { kind: "RETRIEVER" },
102
+ "*.result.*": { kind: "RESULT" }
103
+ }
104
+ };
105
+ function mergeLogIngestConfig(base, override) {
106
+ const merged = {
107
+ ...base,
108
+ ...override,
109
+ mappings: {
110
+ ...base.mappings ?? {},
111
+ ...override.mappings ?? {}
112
+ }
113
+ };
114
+ return merged;
115
+ }
116
+ async function loadLogIngestConfig(configPath) {
117
+ if (configPath === void 0 || configPath.trim() === "") {
118
+ return DEFAULT_LOG_INGEST_CONFIG;
119
+ }
120
+ let rawText;
121
+ try {
122
+ rawText = await readFile(configPath, "utf-8");
123
+ } catch (e) {
124
+ const msg = e instanceof Error ? e.message : String(e);
125
+ throw new Error(`Failed to read config file: ${configPath} (${msg})`);
126
+ }
127
+ let parsed;
128
+ try {
129
+ parsed = JSON.parse(rawText);
130
+ } catch (e) {
131
+ const msg = e instanceof Error ? e.message : String(e);
132
+ throw new Error(`Invalid JSON in config file: ${configPath} (${msg})`);
133
+ }
134
+ if (!isRecord2(parsed)) {
135
+ throw new Error("Invalid config: expected a JSON object at top-level");
136
+ }
137
+ const user = parsed;
138
+ if (user.runIdKeys !== void 0 && !isNonEmptyStringArray(user.runIdKeys)) {
139
+ throw new Error("Invalid config: runIdKeys must be a non-empty array of strings");
140
+ }
141
+ if (user.eventKey !== void 0 && (typeof user.eventKey !== "string" || user.eventKey.trim() === "")) {
142
+ throw new Error("Invalid config: eventKey must be a non-empty string");
143
+ }
144
+ for (const k of [
145
+ "timestampKey",
146
+ "messageKey",
147
+ "levelKey",
148
+ "parentIdKey",
149
+ "durationKey",
150
+ "statusKey"
151
+ ]) {
152
+ const v = user[k];
153
+ if (v !== void 0 && (typeof v !== "string" || v.trim() === "")) {
154
+ throw new Error(`Invalid config: ${k} must be a non-empty string when provided`);
155
+ }
156
+ }
157
+ if (user.mappings !== void 0) {
158
+ validateMappings(user.mappings);
159
+ }
160
+ if (user.redact !== void 0) {
161
+ validateRedact(user.redact);
162
+ }
163
+ if (user.heuristicWindowMs !== void 0 && (typeof user.heuristicWindowMs !== "number" || !Number.isFinite(user.heuristicWindowMs) || user.heuristicWindowMs < 0)) {
164
+ throw new Error("Invalid config: heuristicWindowMs must be a non-negative number when provided");
165
+ }
166
+ if (user.redact && JSON.stringify(user.redact).includes("=>")) {
167
+ throw new Error("Invalid config: function strings are not supported in redact rules");
168
+ }
169
+ return mergeLogIngestConfig(DEFAULT_LOG_INGEST_CONFIG, user);
170
+ }
171
+ function isRecord3(v) {
172
+ return typeof v === "object" && v !== null && !Array.isArray(v);
173
+ }
174
+ var JsonLogParser = class {
175
+ parseLines(lines, filePath) {
176
+ const records = [];
177
+ const warnings = [];
178
+ for (let i = 0; i < lines.length; i++) {
179
+ const lineNumber = i + 1;
180
+ const raw = lines[i] ?? "";
181
+ const trimmed = raw.trim();
182
+ if (trimmed === "") continue;
183
+ let parsed;
184
+ try {
185
+ parsed = JSON.parse(trimmed);
186
+ } catch {
187
+ warnings.push({
188
+ code: "MALFORMED_JSON",
189
+ message: "Malformed JSON log line",
190
+ file: filePath,
191
+ line: lineNumber,
192
+ raw: trimmed.slice(0, 500)
193
+ });
194
+ continue;
195
+ }
196
+ if (!isRecord3(parsed)) {
197
+ warnings.push({
198
+ code: "MALFORMED_JSON",
199
+ message: "JSON log line must be an object",
200
+ file: filePath,
201
+ line: lineNumber,
202
+ raw: trimmed.slice(0, 500)
203
+ });
204
+ continue;
205
+ }
206
+ records.push({
207
+ raw: parsed,
208
+ file: filePath,
209
+ line: lineNumber,
210
+ sourceType: "json-log"
211
+ });
212
+ }
213
+ return { records, warnings };
214
+ }
215
+ async parseFile(filePath) {
216
+ let text;
217
+ try {
218
+ text = await readFile(filePath, "utf-8");
219
+ } catch (e) {
220
+ const msg = e instanceof Error ? e.message : String(e);
221
+ throw new Error(`Failed to read log file: ${filePath} (${msg})`);
222
+ }
223
+ const lines = text.split(/\r?\n/);
224
+ return this.parseLines(lines, filePath);
225
+ }
226
+ };
227
+ function isRecord4(v) {
228
+ return typeof v === "object" && v !== null && !Array.isArray(v);
229
+ }
230
+ function findLastJsonObjectSubstring(line) {
231
+ let last;
232
+ for (let i = 0; i < line.length; i++) {
233
+ if (line[i] !== "{") continue;
234
+ let depth = 0;
235
+ let inString = false;
236
+ let escape = false;
237
+ for (let j = i; j < line.length; j++) {
238
+ const ch = line[j];
239
+ if (inString) {
240
+ if (escape) {
241
+ escape = false;
242
+ continue;
243
+ }
244
+ if (ch === "\\") {
245
+ escape = true;
246
+ continue;
247
+ }
248
+ if (ch === '"') {
249
+ inString = false;
250
+ }
251
+ continue;
252
+ }
253
+ if (ch === '"') {
254
+ inString = true;
255
+ continue;
256
+ }
257
+ if (ch === "{") depth += 1;
258
+ if (ch === "}") depth -= 1;
259
+ if (depth === 0) {
260
+ last = { start: i, end: j + 1 };
261
+ i = j;
262
+ break;
263
+ }
264
+ if (depth < 0) break;
265
+ }
266
+ }
267
+ if (!last) return void 0;
268
+ return line.slice(last.start, last.end);
269
+ }
270
+ var Log4jsParser = class {
271
+ parseLines(lines, filePath) {
272
+ const records = [];
273
+ const warnings = [];
274
+ for (let i = 0; i < lines.length; i++) {
275
+ const lineNumber = i + 1;
276
+ const rawLine = lines[i] ?? "";
277
+ const trimmed = rawLine.trim();
278
+ if (trimmed === "") continue;
279
+ const jsonText = findLastJsonObjectSubstring(trimmed);
280
+ if (!jsonText) {
281
+ warnings.push({
282
+ code: "UNSUPPORTED_LOG4JS_PAYLOAD",
283
+ message: "No embedded JSON object found in log4js line",
284
+ file: filePath,
285
+ line: lineNumber,
286
+ raw: trimmed.slice(0, 500)
287
+ });
288
+ continue;
289
+ }
290
+ let parsed;
291
+ try {
292
+ parsed = JSON.parse(jsonText);
293
+ } catch {
294
+ warnings.push({
295
+ code: "MALFORMED_JSON",
296
+ message: "Malformed embedded JSON object in log4js line",
297
+ file: filePath,
298
+ line: lineNumber,
299
+ raw: jsonText.slice(0, 500)
300
+ });
301
+ continue;
302
+ }
303
+ if (!isRecord4(parsed)) {
304
+ warnings.push({
305
+ code: "UNSUPPORTED_LOG4JS_PAYLOAD",
306
+ message: "Embedded JSON payload must be an object",
307
+ file: filePath,
308
+ line: lineNumber,
309
+ raw: jsonText.slice(0, 500)
310
+ });
311
+ continue;
312
+ }
313
+ records.push({
314
+ raw: parsed,
315
+ file: filePath,
316
+ line: lineNumber,
317
+ sourceType: "log4js"
318
+ });
319
+ }
320
+ return { records, warnings };
321
+ }
322
+ async parseFile(filePath) {
323
+ let text;
324
+ try {
325
+ text = await readFile(filePath, "utf-8");
326
+ } catch (e) {
327
+ const msg = e instanceof Error ? e.message : String(e);
328
+ throw new Error(`Failed to read log file: ${filePath} (${msg})`);
329
+ }
330
+ const lines = text.split(/\r?\n/);
331
+ return this.parseLines(lines, filePath);
332
+ }
333
+ };
334
+
335
+ // packages/core/src/logs/mapping.ts
336
+ function wildcardMatch(pattern, value) {
337
+ if (pattern === value) return true;
338
+ if (!pattern.includes("*")) return false;
339
+ const parts = pattern.split("*");
340
+ let idx = 0;
341
+ for (let i = 0; i < parts.length; i++) {
342
+ const part = parts[i];
343
+ if (part === "") continue;
344
+ const found = value.indexOf(part, idx);
345
+ if (found === -1) return false;
346
+ if (i === 0 && !pattern.startsWith("*") && found !== 0) return false;
347
+ idx = found + part.length;
348
+ }
349
+ if (!pattern.endsWith("*")) {
350
+ const last = parts[parts.length - 1];
351
+ if (last !== "" && !value.endsWith(last)) return false;
352
+ if (last === "" && !value.endsWith(parts[parts.length - 2] ?? "")) return false;
353
+ }
354
+ return true;
355
+ }
356
+ function matchMapping(eventName, mappings) {
357
+ if (!mappings) return void 0;
358
+ if (mappings[eventName]) return mappings[eventName];
359
+ let bestKey;
360
+ let bestScore = -1;
361
+ for (const key of Object.keys(mappings)) {
362
+ if (!key.includes("*")) continue;
363
+ if (!wildcardMatch(key, eventName)) continue;
364
+ const score = key.replaceAll("*", "").length;
365
+ if (score > bestScore) {
366
+ bestScore = score;
367
+ bestKey = key;
368
+ }
369
+ }
370
+ return bestKey ? mappings[bestKey] : void 0;
371
+ }
372
+ var DEFAULT_REDACT_KEYS = [
373
+ "authorization",
374
+ "cookie",
375
+ "token",
376
+ "apiKey",
377
+ "password",
378
+ "secret",
379
+ "email"
380
+ ];
381
+ function isRecord5(v) {
382
+ return typeof v === "object" && v !== null && !Array.isArray(v);
383
+ }
384
+ function toKey(s) {
385
+ return s.toLowerCase();
386
+ }
387
+ function stableHash(value) {
388
+ const h = crypto.createHash("sha256").update(value, "utf8").digest("hex");
389
+ return h.slice(0, 8);
390
+ }
391
+ function compileRules(rules) {
392
+ const out = /* @__PURE__ */ new Map();
393
+ const set = (r) => {
394
+ const k = toKey(r.key);
395
+ out.set(k, { ...r, key: k });
396
+ };
397
+ for (const k of DEFAULT_REDACT_KEYS) {
398
+ set({ key: k, strategy: "full" });
399
+ }
400
+ for (const r of rules ?? []) {
401
+ if (typeof r === "string") {
402
+ set({ key: r, strategy: "full" });
403
+ continue;
404
+ }
405
+ const key = r.key;
406
+ if (r.strategy === "full") set({ key, strategy: "full" });
407
+ if (r.strategy === "hash") set({ key, strategy: "hash" });
408
+ if (r.strategy === "prefix") {
409
+ set({ key, strategy: "prefix", keep: typeof r.keep === "number" ? r.keep : 8 });
410
+ }
411
+ }
412
+ return [...out.values()];
413
+ }
414
+ var Redactor = class {
415
+ #rules;
416
+ constructor(options) {
417
+ this.#rules = compileRules(options?.rules);
418
+ }
419
+ redactValue(key, value) {
420
+ const k = toKey(key);
421
+ const rule = this.#rules.find((r) => r.key === k);
422
+ if (!rule) {
423
+ return this.#redactNested(value);
424
+ }
425
+ if (rule.strategy === "full") return "[REDACTED]";
426
+ const asString = typeof value === "string" ? value : typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" ? String(value) : void 0;
427
+ if (rule.strategy === "prefix") {
428
+ if (asString === void 0) return "[REDACTED]";
429
+ const keep = Math.max(0, Math.floor(rule.keep));
430
+ return asString.length <= keep ? `${asString}\u2026` : `${asString.slice(0, keep)}\u2026`;
431
+ }
432
+ if (rule.strategy === "hash") {
433
+ if (asString === void 0) return "[HASH:unknown]";
434
+ return `[HASH:${stableHash(asString)}]`;
435
+ }
436
+ return this.#redactNested(value);
437
+ }
438
+ redactRecord(record) {
439
+ const out = {};
440
+ for (const [k, v] of Object.entries(record)) {
441
+ out[k] = this.redactValue(k, v);
442
+ }
443
+ return out;
444
+ }
445
+ #redactNested(value) {
446
+ if (Array.isArray(value)) {
447
+ return value.map((v) => this.#redactNested(v));
448
+ }
449
+ if (isRecord5(value)) {
450
+ const out = {};
451
+ for (const [k, v] of Object.entries(value)) {
452
+ out[k] = this.redactValue(k, v);
453
+ }
454
+ return out;
455
+ }
456
+ return value;
457
+ }
458
+ };
459
+ function isFiniteNumber(v) {
460
+ return typeof v === "number" && Number.isFinite(v);
461
+ }
462
+ function safeString(v) {
463
+ if (typeof v !== "string") return void 0;
464
+ const t = v.trim();
465
+ return t === "" ? void 0 : t;
466
+ }
467
+ function parseTimestamp(v) {
468
+ if (isFiniteNumber(v)) return v;
469
+ if (typeof v === "string") {
470
+ const t = Date.parse(v);
471
+ if (Number.isFinite(t)) return t;
472
+ }
473
+ return void 0;
474
+ }
475
+ function hasToken(hay, token) {
476
+ return hay.includes(token);
477
+ }
478
+ function inferKind(eventName) {
479
+ if (hasToken(eventName, ".llm.")) return "LLM";
480
+ if (hasToken(eventName, ".tool.")) return "TOOL";
481
+ if (hasToken(eventName, ".agent.")) return "AGENT";
482
+ if (hasToken(eventName, ".retriever.")) return "RETRIEVER";
483
+ if (hasToken(eventName, ".result.")) return "RESULT";
484
+ if (eventName.endsWith(".error") || eventName.endsWith(".failed") || eventName.includes(".error") || eventName.includes(".failed")) {
485
+ return "ERROR";
486
+ }
487
+ return "LOG";
488
+ }
489
+ function deriveName(eventName, kind) {
490
+ const parts = eventName.split(".");
491
+ const last = parts[parts.length - 1] ?? eventName;
492
+ if (kind === "LLM") return `llm:${last}`;
493
+ if (kind === "TOOL") return `tool:${last}`;
494
+ if (kind === "AGENT") return `agent:${last}`;
495
+ if (kind === "RESULT") return `result:${last}`;
496
+ if (kind === "RUN") return `run:${last}`;
497
+ if (kind === "RETRIEVER") return `retriever:${last}`;
498
+ if (kind === "ERROR") return `error:${last}`;
499
+ return eventName;
500
+ }
501
+ var EventNormalizer = class {
502
+ #config;
503
+ constructor(options) {
504
+ this.#config = options.config;
505
+ }
506
+ #normalizeInternal(record) {
507
+ const raw = record.raw;
508
+ const cfg = this.#config;
509
+ let runId;
510
+ for (const k of cfg.runIdKeys) {
511
+ const v = raw[k];
512
+ const s = safeString(v);
513
+ if (s) {
514
+ runId = s;
515
+ break;
516
+ }
517
+ }
518
+ if (!runId) {
519
+ return {
520
+ warning: {
521
+ code: "MISSING_RUN_ID",
522
+ message: "Missing run id (none of runIdKeys present)",
523
+ file: record.file,
524
+ line: record.line
525
+ }
526
+ };
527
+ }
528
+ const eventName = safeString(raw[cfg.eventKey]);
529
+ if (!eventName) {
530
+ return {
531
+ warning: {
532
+ code: "MISSING_EVENT",
533
+ message: `Missing event name (key: ${cfg.eventKey})`,
534
+ file: record.file,
535
+ line: record.line
536
+ }
537
+ };
538
+ }
539
+ const mapping = matchMapping(eventName, cfg.mappings);
540
+ const tsKey = cfg.timestampKey ?? "timestamp";
541
+ const tsRaw = raw[tsKey];
542
+ const parsedTs = parseTimestamp(tsRaw);
543
+ const timestamp = parsedTs ?? Date.now();
544
+ const timestampMissing = parsedTs === void 0;
545
+ const parentIdKey = cfg.parentIdKey;
546
+ const parentId = parentIdKey ? safeString(raw[parentIdKey]) : void 0;
547
+ let durationMs;
548
+ const durationKey = cfg.durationKey;
549
+ if (durationKey) {
550
+ const v = raw[durationKey];
551
+ if (isFiniteNumber(v)) durationMs = v;
552
+ } else if (isFiniteNumber(raw.durationMs)) {
553
+ durationMs = raw.durationMs;
554
+ }
555
+ let status;
556
+ const statusKey = cfg.statusKey;
557
+ const statusRaw = statusKey ? safeString(raw[statusKey]) : void 0;
558
+ if (statusRaw === "running" || statusRaw === "ok" || statusRaw === "error") {
559
+ status = statusRaw;
560
+ } else if (mapping?.status) {
561
+ status = mapping.status;
562
+ } else if (mapping?.kind === "ERROR") {
563
+ status = "error";
564
+ } else if (eventName.includes(".failed") || eventName.includes(".error")) {
565
+ status = "error";
566
+ } else if (mapping?.startsRun || mapping?.startsStep) {
567
+ status = "running";
568
+ } else if (mapping?.endsRun || mapping?.endsStep) {
569
+ status = "ok";
570
+ }
571
+ const kind = mapping?.kind ?? inferKind(eventName);
572
+ const name = mapping?.name ?? deriveName(eventName, kind);
573
+ let confidence = "correlated";
574
+ if (parentId || mapping?.startsRun) confidence = "explicit";
575
+ if (timestampMissing) confidence = "unknown";
576
+ const omit = new Set([
577
+ ...cfg.runIdKeys,
578
+ cfg.eventKey,
579
+ tsKey,
580
+ cfg.messageKey ?? "message",
581
+ cfg.levelKey ?? "level",
582
+ cfg.parentIdKey ?? "",
583
+ cfg.durationKey ?? "",
584
+ cfg.statusKey ?? "",
585
+ "durationMs"
586
+ ].filter((k) => k !== ""));
587
+ const attributes = {};
588
+ for (const [k, v] of Object.entries(raw)) {
589
+ if (omit.has(k)) continue;
590
+ attributes[k] = v;
591
+ }
592
+ const event = {
593
+ eventId: nanoid(10),
594
+ runId,
595
+ ...parentId ? { parentId } : {},
596
+ name,
597
+ kind,
598
+ timestamp,
599
+ ...status ? { status } : {},
600
+ ...durationMs !== void 0 ? { durationMs } : {},
601
+ ...Object.keys(attributes).length > 0 ? { attributes } : {},
602
+ confidence,
603
+ source: {
604
+ type: record.sourceType,
605
+ file: record.file,
606
+ line: record.line
607
+ }
608
+ };
609
+ if (timestampMissing) {
610
+ return {
611
+ event,
612
+ warning: {
613
+ code: "MISSING_TIMESTAMP",
614
+ message: `Missing or invalid timestamp (key: ${tsKey})`,
615
+ file: record.file,
616
+ line: record.line,
617
+ raw: JSON.stringify(raw).slice(0, 500)
618
+ }
619
+ };
620
+ }
621
+ return { event };
622
+ }
623
+ normalize(record) {
624
+ const r = this.#normalizeInternal(record);
625
+ return r.event ?? r.warning;
626
+ }
627
+ normalizeAll(records) {
628
+ const out = [];
629
+ const warnings = [];
630
+ for (const r of records) {
631
+ const normalized = this.#normalizeInternal(r);
632
+ if (normalized.event) out.push(normalized.event);
633
+ if (normalized.warning) warnings.push(normalized.warning);
634
+ }
635
+ return { records: out, warnings };
636
+ }
637
+ };
638
+
639
+ // packages/core/src/logs/tree-builder.ts
640
+ function inc(map, key) {
641
+ map[key] = (map[key] ?? 0) + 1;
642
+ }
643
+ function computeRunStatus(events) {
644
+ let hasRunning = false;
645
+ for (const e of events) {
646
+ if (e.status === "error") return "error";
647
+ if (e.status === "running") hasRunning = true;
648
+ }
649
+ if (hasRunning) return "running";
650
+ return "ok";
651
+ }
652
+ var TreeBuilder = class {
653
+ constructor(options) {
654
+ void options?.config;
655
+ }
656
+ build(events) {
657
+ const byRun = /* @__PURE__ */ new Map();
658
+ for (const e of events) {
659
+ if (!byRun.has(e.runId)) byRun.set(e.runId, []);
660
+ byRun.get(e.runId).push(e);
661
+ }
662
+ const out = [];
663
+ for (const [runId, runEvents] of byRun.entries()) {
664
+ const sorted = [...runEvents].sort((a, b) => a.timestamp - b.timestamp);
665
+ const nodes = /* @__PURE__ */ new Map();
666
+ for (const e of sorted) {
667
+ nodes.set(e.eventId, { event: e, children: [], depth: 0 });
668
+ }
669
+ const roots = [];
670
+ for (const node of nodes.values()) {
671
+ const parentId = node.event.parentId;
672
+ if (parentId && nodes.has(parentId)) {
673
+ nodes.get(parentId).children.push(node);
674
+ } else {
675
+ roots.push(node);
676
+ }
677
+ }
678
+ const assignDepth = (n, depth) => {
679
+ n.depth = depth;
680
+ for (const c of n.children) assignDepth(c, depth + 1);
681
+ };
682
+ for (const r of roots) assignDepth(r, 0);
683
+ const confidenceBreakdown = {
684
+ explicit: 0,
685
+ correlated: 0,
686
+ heuristic: 0,
687
+ unknown: 0
688
+ };
689
+ const kinds = {};
690
+ for (const e of sorted) {
691
+ inc(confidenceBreakdown, e.confidence);
692
+ kinds[e.kind] = (kinds[e.kind] ?? 0) + 1;
693
+ }
694
+ const startedAt = sorted.length > 0 ? sorted[0].timestamp : void 0;
695
+ const endedAt = sorted.length > 0 ? sorted[sorted.length - 1].timestamp : void 0;
696
+ const status = computeRunStatus(sorted);
697
+ const durationMs = startedAt !== void 0 && endedAt !== void 0 && Number.isFinite(startedAt) && Number.isFinite(endedAt) && endedAt >= startedAt && status !== "running" ? endedAt - startedAt : void 0;
698
+ const name = sorted.find((e) => e.kind === "RUN")?.name;
699
+ out.push({
700
+ runId,
701
+ name,
702
+ status,
703
+ startedAt,
704
+ endedAt: status === "running" ? void 0 : endedAt,
705
+ durationMs,
706
+ children: roots,
707
+ metadata: {
708
+ totalEvents: sorted.length,
709
+ confidenceBreakdown,
710
+ kinds
711
+ }
712
+ });
713
+ }
714
+ out.sort((a, b) => (b.startedAt ?? 0) - (a.startedAt ?? 0));
715
+ return out;
716
+ }
717
+ };
718
+
719
+ // packages/core/src/logs/tree-renderer.ts
720
+ function truncate(v, max) {
721
+ if (v.length <= max) return v;
722
+ return v.slice(0, Math.max(0, max - 1)) + "\u2026";
723
+ }
724
+ function fmtAttrValue(value, maxLen) {
725
+ if (value === null || value === void 0) return void 0;
726
+ if (typeof value === "string") return truncate(value, maxLen);
727
+ if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
728
+ return String(value);
729
+ }
730
+ if (typeof value === "object") {
731
+ try {
732
+ return truncate(JSON.stringify(value), maxLen);
733
+ } catch {
734
+ return "[object]";
735
+ }
736
+ }
737
+ return String(value);
738
+ }
739
+ function compactAttrs(attrs, maxLen) {
740
+ if (!attrs) return "";
741
+ const entries = Object.entries(attrs);
742
+ if (entries.length === 0) return "";
743
+ const picks = /* @__PURE__ */ new Map();
744
+ const set = (k, v) => {
745
+ const s = fmtAttrValue(v, maxLen);
746
+ if (s !== void 0) picks.set(k, s);
747
+ };
748
+ set("job", attrs.jobId ?? attrs.job ?? attrs.jobUuid);
749
+ set("user", attrs.userUuid ?? attrs.userId ?? attrs.user);
750
+ set("trip", attrs.tripUuid ?? attrs.tripId ?? attrs.trip);
751
+ set("msgs", attrs.messageCount ?? attrs.msgs);
752
+ set("trips", attrs.trips);
753
+ set("model", attrs.model);
754
+ const tokens = attrs.tokens;
755
+ if (tokens && typeof tokens === "object" && tokens !== null) {
756
+ const input3 = tokens.input;
757
+ const output2 = tokens.output;
758
+ if (typeof input3 === "number" || typeof output2 === "number") {
759
+ picks.set("tokens", `${input3 ?? "?"}/${output2 ?? "?"}`);
760
+ }
761
+ }
762
+ for (const k of ["shouldNotify", "variant"]) {
763
+ if (k in attrs) set(k, attrs[k]);
764
+ }
765
+ const rendered = [...picks.entries()].filter(([, v]) => v !== "").map(([k, v]) => `${k}=${v}`);
766
+ return rendered.length > 0 ? " " + rendered.join(" ") : "";
767
+ }
768
+ function statusMark(node) {
769
+ if (node.event.status === "error") return " \u2716";
770
+ if (node.event.status === "ok") return " \u2714";
771
+ return "";
772
+ }
773
+ function fmtDuration(ms) {
774
+ if (!Number.isFinite(ms) || ms < 0) return "";
775
+ if (ms < 1e3) return `${Math.round(ms)}ms`;
776
+ return `${(ms / 1e3).toFixed(2)}s`;
777
+ }
778
+ function renderNodeLines(node, prefix, isLast, options) {
779
+ const branch = prefix + (isLast ? "\u2514\u2500 " : "\u251C\u2500 ");
780
+ const nextPrefix = prefix + (isLast ? " " : "\u2502 ");
781
+ const attrs = compactAttrs(node.event.attributes, options.maxAttributeLength);
782
+ const dur = node.event.durationMs !== void 0 ? ` ${fmtDuration(node.event.durationMs)}` : "";
783
+ const line = `${branch}${node.event.name}${attrs}${statusMark(node)}${dur}`;
784
+ const lines = [line];
785
+ const showConf = options.showConfidence === "always" || options.showConfidence === "non-explicit" && node.event.confidence !== "explicit";
786
+ if (showConf) {
787
+ lines.push(`${nextPrefix}confidence: ${node.event.confidence}`);
788
+ }
789
+ const children = node.children;
790
+ for (let i = 0; i < children.length; i++) {
791
+ lines.push(...renderNodeLines(children[i], nextPrefix, i === children.length - 1, options));
792
+ }
793
+ return lines;
794
+ }
795
+ function renderRunTree(tree, options) {
796
+ const opts = {
797
+ verbose: options?.verbose ?? false,
798
+ showConfidence: options?.showConfidence ?? "always",
799
+ showMetadata: options?.showMetadata ?? false,
800
+ color: options?.color ?? false,
801
+ maxAttributeLength: options?.maxAttributeLength ?? 40,
802
+ summary: options?.summary ?? true
803
+ };
804
+ const header = `Run ${tree.runId}`;
805
+ const lines = [header];
806
+ const children = tree.children;
807
+ for (let i = 0; i < children.length; i++) {
808
+ lines.push(...renderNodeLines(children[i], "", i === children.length - 1, opts));
809
+ }
810
+ if (opts.summary) {
811
+ const cb = tree.metadata.confidenceBreakdown;
812
+ const tools = tree.metadata.kinds.TOOL ?? 0;
813
+ const llms = tree.metadata.kinds.LLM ?? 0;
814
+ lines.push("");
815
+ lines.push("Summary:");
816
+ lines.push(` Events: ${tree.metadata.totalEvents}`);
817
+ lines.push(` Tools: ${tools}`);
818
+ lines.push(` LLMs: ${llms}`);
819
+ lines.push(
820
+ ` Confidence: ${cb.explicit} explicit, ${cb.correlated} correlated, ${cb.heuristic} heuristic, ${cb.unknown} unknown`
821
+ );
822
+ lines.push("");
823
+ lines.push("Note:");
824
+ lines.push(" Flat timeline by default. Nesting only with explicit parentId.");
825
+ }
826
+ return lines.join("\n");
827
+ }
828
+ function renderRunTrees(trees, options) {
829
+ return trees.map((t) => renderRunTree(t, options)).join("\n\n");
830
+ }
831
+
832
+ // packages/core/src/logs/line-parser.ts
833
+ function shiftLineNumbers(res, options) {
834
+ const targetLine = typeof options.line === "number" && Number.isFinite(options.line) && options.line > 0 ? Math.floor(options.line) : void 0;
835
+ const file = typeof options.file === "string" && options.file.trim() !== "" ? options.file : void 0;
836
+ if (targetLine === void 0 && file === void 0) return res;
837
+ const mapRecord = (x) => ({
838
+ ...x,
839
+ ...targetLine !== void 0 ? { line: targetLine } : {},
840
+ ...file !== void 0 ? { file } : {}
841
+ });
842
+ const mapWarning = (x) => ({
843
+ ...x,
844
+ ...targetLine !== void 0 ? { line: targetLine } : {},
845
+ ...file !== void 0 ? { file } : {}
846
+ });
847
+ return {
848
+ records: res.records.map(mapRecord),
849
+ warnings: res.warnings.map(mapWarning)
850
+ };
851
+ }
852
+ function normalizeFormat(line, format) {
853
+ if (format && format !== "auto") return format;
854
+ const trimmed = line.trim();
855
+ if (trimmed.startsWith("{")) return "json";
856
+ return "log4js";
857
+ }
858
+ function parseLogLine(line, options = {}) {
859
+ const raw = typeof line === "string" ? line : "";
860
+ const trimmed = raw.trim();
861
+ if (trimmed === "") return { records: [], warnings: [] };
862
+ const format = normalizeFormat(raw, options.format);
863
+ const base = format === "json" ? new JsonLogParser().parseLines([raw], options.file) : new Log4jsParser().parseLines([raw], options.file);
864
+ return shiftLineNumbers(base, options);
865
+ }
866
+
867
+ // packages/core/src/logs/live-tree.ts
868
+ var LiveLogAccumulator = class {
869
+ #config;
870
+ #format;
871
+ #file;
872
+ #normalizer;
873
+ #redactor;
874
+ #treeBuilder;
875
+ #events = [];
876
+ #warnings = [];
877
+ #trees = [];
878
+ constructor(options) {
879
+ this.#config = mergeLogIngestConfig(options.config, {});
880
+ this.#format = options.format ?? "auto";
881
+ this.#file = options.file;
882
+ this.#normalizer = new EventNormalizer({ config: this.#config });
883
+ this.#redactor = new Redactor({ rules: this.#config.redact });
884
+ this.#treeBuilder = new TreeBuilder({ config: this.#config });
885
+ }
886
+ pushLine(line, lineNumber) {
887
+ try {
888
+ const parsed = parseLogLine(line, {
889
+ format: this.#format,
890
+ file: this.#file,
891
+ line: lineNumber
892
+ });
893
+ const normalized = this.#normalizer.normalizeAll(parsed.records);
894
+ const redactedEvents = normalized.records.map((e) => ({
895
+ ...e,
896
+ attributes: e.attributes ? this.#redactor.redactRecord(e.attributes) : void 0
897
+ }));
898
+ this.#events = [...this.#events, ...redactedEvents];
899
+ this.#warnings = [...this.#warnings, ...parsed.warnings, ...normalized.warnings];
900
+ this.#trees = this.#treeBuilder.build(this.#events);
901
+ return {
902
+ events: this.#events,
903
+ trees: this.#trees,
904
+ warnings: this.#warnings
905
+ };
906
+ } catch (e) {
907
+ const msg = e instanceof Error ? e.message : String(e);
908
+ const warning = {
909
+ code: "UNKNOWN",
910
+ message: `LiveLogAccumulator failed to process line (${msg})`,
911
+ file: this.#file,
912
+ line: lineNumber,
913
+ raw: typeof line === "string" ? line.slice(0, 500) : void 0
914
+ };
915
+ this.#warnings = [...this.#warnings, warning];
916
+ return { events: this.#events, trees: this.#trees, warnings: this.#warnings };
917
+ }
918
+ }
919
+ getEvents() {
920
+ return this.#events;
921
+ }
922
+ getTrees() {
923
+ return this.#trees;
924
+ }
925
+ getWarnings() {
926
+ return this.#warnings;
927
+ }
928
+ reset() {
929
+ this.#events = [];
930
+ this.#trees = [];
931
+ this.#warnings = [];
932
+ }
933
+ };
934
+
935
+ // packages/core/src/logs/index.ts
936
+ function firstNonEmptyLine(text) {
937
+ for (const line of text.split(/\r?\n/)) {
938
+ const t = line.trim();
939
+ if (t !== "") return t;
940
+ }
941
+ return void 0;
942
+ }
943
+ async function detectFormat(filePath) {
944
+ const text = await readFile(filePath, "utf-8");
945
+ const first = firstNonEmptyLine(text);
946
+ if (!first) return "log4js";
947
+ if (!first.startsWith("{")) return "log4js";
948
+ try {
949
+ const parsed = JSON.parse(first);
950
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) return "json";
951
+ } catch {
952
+ }
953
+ return "log4js";
954
+ }
955
+ function applyOverrides(cfg, options) {
956
+ const override = {};
957
+ for (const k of [
958
+ "runIdKeys",
959
+ "eventKey",
960
+ "timestampKey",
961
+ "messageKey",
962
+ "levelKey",
963
+ "parentIdKey",
964
+ "durationKey",
965
+ "statusKey"
966
+ ]) {
967
+ const v = options[k];
968
+ if (v !== void 0) override[k] = v;
969
+ }
970
+ return mergeLogIngestConfig(cfg, override);
971
+ }
972
+ async function parseLogsToTrees(filePath, options = {}) {
973
+ const base = options.config ?? await loadLogIngestConfig(options.configPath);
974
+ const config = applyOverrides(base, options);
975
+ const format = options.format === "auto" || options.format === void 0 ? await detectFormat(filePath) : options.format;
976
+ let parsed;
977
+ if (format === "json") {
978
+ parsed = await new JsonLogParser().parseFile(filePath);
979
+ } else {
980
+ parsed = await new Log4jsParser().parseFile(filePath);
981
+ }
982
+ const normalizer = new EventNormalizer({ config });
983
+ const normalized = normalizer.normalizeAll(parsed.records);
984
+ const redactor = new Redactor({ rules: config.redact });
985
+ const events = normalized.records.map((e) => ({
986
+ ...e,
987
+ attributes: e.attributes ? redactor.redactRecord(e.attributes) : void 0
988
+ }));
989
+ const trees = new TreeBuilder({ config }).build(events);
990
+ return {
991
+ events,
992
+ trees,
993
+ warnings: [...parsed.warnings, ...normalized.warnings]
994
+ };
995
+ }
996
+
997
+ // packages/core/src/utils/duration.ts
998
+ function parseDuration(duration) {
999
+ const raw = typeof duration === "string" ? duration.trim() : "";
1000
+ const match = raw.match(/^(\d+)(ms|[smhd])$/);
1001
+ if (!match) {
1002
+ throw new Error(
1003
+ `Invalid duration format: ${duration}. Use a positive integer followed by ms, s, m, h, or d (e.g. 500ms, 30s, 5m, 2h, 7d).`
1004
+ );
1005
+ }
1006
+ const amount = Number.parseInt(match[1], 10);
1007
+ const unit = match[2];
1008
+ if (!Number.isFinite(amount) || amount <= 0) {
1009
+ throw new Error(
1010
+ `Invalid duration amount: ${duration}. Amount must be a positive integer.`
1011
+ );
1012
+ }
1013
+ switch (unit) {
1014
+ case "ms":
1015
+ return amount;
1016
+ case "s":
1017
+ return amount * 1e3;
1018
+ case "m":
1019
+ return amount * 60 * 1e3;
1020
+ case "h":
1021
+ return amount * 60 * 60 * 1e3;
1022
+ case "d":
1023
+ return amount * 24 * 60 * 60 * 1e3;
1024
+ default: {
1025
+ throw new Error(`Unknown duration unit: ${unit}`);
1026
+ }
1027
+ }
1028
+ }
1029
+ function formatDuration(ms) {
1030
+ if (!Number.isFinite(ms)) {
1031
+ return "0ms";
1032
+ }
1033
+ if (ms < 0) {
1034
+ throw new Error(`formatDuration: ms must be non-negative (got ${ms})`);
1035
+ }
1036
+ if (ms < 1e3) {
1037
+ return `${Math.floor(ms)}ms`;
1038
+ }
1039
+ if (ms < 6e4) {
1040
+ return `${(ms / 1e3).toFixed(2)}s`;
1041
+ }
1042
+ if (ms < 36e5) {
1043
+ return `${(ms / 6e4).toFixed(1)}m`;
1044
+ }
1045
+ return `${(ms / 36e5).toFixed(1)}h`;
1046
+ }
1047
+
1048
+ // packages/core/src/utils.ts
25
1049
  var DEFAULT_TRACE_DIR_NAME = ".agent-inspect";
26
1050
  var RUNS_DIR_NAME = "runs";
27
1051
  var FALLBACK_TRACE_DIR = path.join(
@@ -33,15 +1057,8 @@ var MAX_NAME_LENGTH = 100;
33
1057
  function createStepId() {
34
1058
  return `step_${nanoid(10)}`;
35
1059
  }
36
- function formatDuration(ms) {
37
- if (!Number.isFinite(ms) || ms < 0) {
38
- return "0ms";
39
- }
40
- if (ms < 1e3) {
41
- return `${Math.floor(ms)}ms`;
42
- }
43
- const seconds = ms / 1e3;
44
- return `${(Math.round(seconds * 10) / 10).toFixed(1)}s`;
1060
+ function formatDuration2(ms) {
1061
+ return formatDuration(ms);
45
1062
  }
46
1063
  function formatTimestamp(timestamp) {
47
1064
  if (!Number.isFinite(timestamp)) {
@@ -60,6 +1077,10 @@ function formatTimestamp(timestamp) {
60
1077
  return `${y}-${mo}-${day} ${h}:${min}:${s}`;
61
1078
  }
62
1079
  function getDefaultTraceDir() {
1080
+ const envDir = process.env.AGENT_INSPECT_TRACE_DIR;
1081
+ if (typeof envDir === "string" && envDir.trim() !== "") {
1082
+ return envDir.trim();
1083
+ }
63
1084
  try {
64
1085
  const home = os.homedir();
65
1086
  if (typeof home !== "string" || home.trim() === "") {
@@ -228,7 +1249,7 @@ function runWithStepContext(stepId, fn) {
228
1249
  });
229
1250
  });
230
1251
  }
231
- function isRecord(value) {
1252
+ function isRecord6(value) {
232
1253
  return typeof value === "object" && value !== null && !Array.isArray(value);
233
1254
  }
234
1255
  function nonEmptyString(value) {
@@ -239,7 +1260,7 @@ function finiteNumber(value) {
239
1260
  }
240
1261
  function optionalErrorInfo(value) {
241
1262
  if (value === void 0) return true;
242
- if (!isRecord(value)) return false;
1263
+ if (!isRecord6(value)) return false;
243
1264
  if (typeof value.message !== "string") return false;
244
1265
  if ("stack" in value && value.stack !== void 0) {
245
1266
  if (typeof value.stack !== "string") return false;
@@ -247,7 +1268,7 @@ function optionalErrorInfo(value) {
247
1268
  return true;
248
1269
  }
249
1270
  function validateEvent(event) {
250
- if (!isRecord(event)) return false;
1271
+ if (!isRecord6(event)) return false;
251
1272
  if (event.schemaVersion !== "0.1") return false;
252
1273
  if (!finiteNumber(event.timestamp)) return false;
253
1274
  if (typeof event.event !== "string") return false;
@@ -256,7 +1277,7 @@ function validateEvent(event) {
256
1277
  if (!nonEmptyString(event.runId) || !nonEmptyString(event.name) || !finiteNumber(event.startTime)) {
257
1278
  return false;
258
1279
  }
259
- if (event.metadata !== void 0 && !isRecord(event.metadata)) {
1280
+ if (event.metadata !== void 0 && !isRecord6(event.metadata)) {
260
1281
  return false;
261
1282
  }
262
1283
  return true;
@@ -271,7 +1292,7 @@ function validateEvent(event) {
271
1292
  if (event.parentId !== void 0 && typeof event.parentId !== "string") {
272
1293
  return false;
273
1294
  }
274
- if (event.metadata !== void 0 && !isRecord(event.metadata)) {
1295
+ if (event.metadata !== void 0 && !isRecord6(event.metadata)) {
275
1296
  return false;
276
1297
  }
277
1298
  return true;
@@ -362,39 +1383,872 @@ async function readTraceEvents(runId, traceDir) {
362
1383
  }
363
1384
  return out;
364
1385
  }
365
- async function listTraceFiles(traceDir) {
1386
+ function resolveTraceDir(options = {}) {
1387
+ if (typeof options.dir === "string" && options.dir.trim() !== "") {
1388
+ return options.dir.trim();
1389
+ }
1390
+ const envDir = process.env.AGENT_INSPECT_TRACE_DIR;
1391
+ if (typeof envDir === "string" && envDir.trim() !== "") {
1392
+ return envDir.trim();
1393
+ }
1394
+ return getDefaultTraceDir();
1395
+ }
1396
+ var TraceDirectory = class {
1397
+ #dir;
1398
+ constructor(options = {}) {
1399
+ this.#dir = resolveTraceDir(options);
1400
+ }
1401
+ getPath(filename) {
1402
+ return filename ? path.join(this.#dir, filename) : this.#dir;
1403
+ }
1404
+ async list() {
1405
+ try {
1406
+ const files = await readdir(this.#dir);
1407
+ return files.filter((f) => f.endsWith(".jsonl"));
1408
+ } catch (e) {
1409
+ if (e && typeof e === "object" && "code" in e && e.code === "ENOENT") {
1410
+ return [];
1411
+ }
1412
+ throw e;
1413
+ }
1414
+ }
1415
+ async getFileStats(filename) {
1416
+ return await stat(this.getPath(filename));
1417
+ }
1418
+ };
1419
+ function isFiniteNumber2(v) {
1420
+ return typeof v === "number" && Number.isFinite(v);
1421
+ }
1422
+ function safeParseJson(line) {
366
1423
  try {
367
- const usable = path.resolve(traceDir);
368
- const names = await readdir(usable);
369
- const jsonl = names.filter((n) => n.endsWith(".jsonl"));
370
- const withStat = await Promise.all(
371
- jsonl.map(async (name) => {
372
- try {
373
- const st = await stat(path.join(usable, name));
374
- return { name, mtime: st.mtimeMs };
375
- } catch {
376
- return { name, mtime: 0 };
377
- }
378
- })
379
- );
380
- withStat.sort((a, b) => {
381
- if (b.mtime !== a.mtime) return b.mtime - a.mtime;
382
- return a.name.localeCompare(b.name);
1424
+ return JSON.parse(line);
1425
+ } catch {
1426
+ return void 0;
1427
+ }
1428
+ }
1429
+ async function extractMetadata(filePath, _quickScan) {
1430
+ const stats = await stat(filePath);
1431
+ let runIdFromFile = path.basename(filePath);
1432
+ if (runIdFromFile.endsWith(".jsonl")) {
1433
+ runIdFromFile = runIdFromFile.slice(0, -".jsonl".length);
1434
+ }
1435
+ const raw = await readFile(filePath, "utf-8");
1436
+ const lines = raw.split(/\r?\n/);
1437
+ let eventCount = 0;
1438
+ let runId;
1439
+ let name;
1440
+ let startedAt;
1441
+ let endedAt;
1442
+ let explicitDurationMs;
1443
+ let hasRunStarted = false;
1444
+ let hasRunCompleted = false;
1445
+ let runCompletedStatus;
1446
+ let anyStepError = false;
1447
+ let anyKnownEvent = false;
1448
+ for (const line of lines) {
1449
+ const trimmed = line.trim();
1450
+ if (trimmed === "") continue;
1451
+ const parsed = safeParseJson(trimmed);
1452
+ if (!parsed) continue;
1453
+ if (!isTraceEvent(parsed)) continue;
1454
+ const e = parsed;
1455
+ anyKnownEvent = true;
1456
+ eventCount += 1;
1457
+ if (runId === void 0 && typeof e.runId === "string") {
1458
+ runId = e.runId;
1459
+ }
1460
+ if (e.event === "run_started") {
1461
+ hasRunStarted = true;
1462
+ const rs = e;
1463
+ if (typeof rs.name === "string" && rs.name.trim() !== "") {
1464
+ name = rs.name;
1465
+ }
1466
+ if (isFiniteNumber2(rs.startTime)) {
1467
+ startedAt = rs.startTime;
1468
+ } else if (isFiniteNumber2(rs.timestamp)) {
1469
+ startedAt = rs.timestamp;
1470
+ }
1471
+ }
1472
+ if (e.event === "run_completed") {
1473
+ hasRunCompleted = true;
1474
+ const rc = e;
1475
+ runCompletedStatus = rc.status;
1476
+ if (isFiniteNumber2(rc.endTime)) endedAt = rc.endTime;
1477
+ else if (isFiniteNumber2(rc.timestamp)) endedAt = rc.timestamp;
1478
+ if (isFiniteNumber2(rc.durationMs)) explicitDurationMs = rc.durationMs;
1479
+ }
1480
+ if (e.event === "step_completed") {
1481
+ const sc = e;
1482
+ if (sc.status === "error") {
1483
+ anyStepError = true;
1484
+ }
1485
+ }
1486
+ }
1487
+ const resolvedRunId = runId ?? runIdFromFile;
1488
+ let status = "unknown";
1489
+ if (hasRunCompleted && (runCompletedStatus === "success" || runCompletedStatus === "error")) {
1490
+ status = runCompletedStatus;
1491
+ } else if (anyStepError) {
1492
+ status = "error";
1493
+ } else if (hasRunStarted && !hasRunCompleted) {
1494
+ status = "running";
1495
+ } else if (anyKnownEvent) {
1496
+ status = "unknown";
1497
+ } else {
1498
+ status = "unknown";
1499
+ }
1500
+ const durationMs = explicitDurationMs ?? (startedAt !== void 0 && endedAt !== void 0 && Number.isFinite(startedAt) && Number.isFinite(endedAt) && endedAt >= startedAt ? endedAt - startedAt : void 0);
1501
+ return {
1502
+ runId: resolvedRunId,
1503
+ name,
1504
+ status,
1505
+ startedAt,
1506
+ endedAt,
1507
+ durationMs,
1508
+ eventCount,
1509
+ filePath,
1510
+ fileSize: stats.size,
1511
+ createdAt: stats.birthtime
1512
+ };
1513
+ }
1514
+ function buildRunSummary(events) {
1515
+ const started = events.find(
1516
+ (e) => e.event === "run_started"
1517
+ );
1518
+ const completed = events.filter(
1519
+ (e) => e.event === "run_completed"
1520
+ );
1521
+ const lastCompleted = completed[completed.length - 1];
1522
+ const runId = started?.runId ?? events.find((e) => typeof e.runId === "string")?.runId ?? "unknown-run";
1523
+ const name = typeof started?.name === "string" && started.name.trim() !== "" ? started.name : void 0;
1524
+ const status = lastCompleted ? lastCompleted.status : started ? "running" : "unknown";
1525
+ const durationMs = lastCompleted && isFiniteNumber2(lastCompleted.durationMs) ? lastCompleted.durationMs : void 0;
1526
+ started && isFiniteNumber2(started.startTime) ? started.startTime : void 0;
1527
+ const steps = /* @__PURE__ */ new Map();
1528
+ for (const e of events) {
1529
+ if (e.event === "step_started") {
1530
+ const s = e;
1531
+ steps.set(s.stepId, {
1532
+ type: s.type,
1533
+ name: s.name,
1534
+ status: "running",
1535
+ parentId: s.parentId,
1536
+ tokensInput: typeof s.metadata?.tokens?.input === "number" ? s.metadata.tokens.input : void 0,
1537
+ tokensOutput: typeof s.metadata?.tokens?.output === "number" ? s.metadata.tokens.output : void 0
1538
+ });
1539
+ }
1540
+ }
1541
+ for (const e of events) {
1542
+ if (e.event === "step_completed") {
1543
+ const c = e;
1544
+ const existing = steps.get(c.stepId);
1545
+ if (!existing) continue;
1546
+ existing.status = c.status;
1547
+ existing.durationMs = c.durationMs;
1548
+ }
1549
+ }
1550
+ let totalSteps = 0;
1551
+ let llmSteps = 0;
1552
+ let toolSteps = 0;
1553
+ let logicSteps = 0;
1554
+ let errorSteps = 0;
1555
+ let maxDepth = 0;
1556
+ let longestStep;
1557
+ let totalTokensInput = 0;
1558
+ let totalTokensOutput = 0;
1559
+ let hasAnyTokens = false;
1560
+ const depthCache = /* @__PURE__ */ new Map();
1561
+ const computeDepth = (stepId) => {
1562
+ const cached = depthCache.get(stepId);
1563
+ if (cached !== void 0) return cached;
1564
+ const node = steps.get(stepId);
1565
+ if (!node) return 0;
1566
+ const parent = node.parentId;
1567
+ if (typeof parent !== "string" || parent.trim() === "" || !steps.has(parent)) {
1568
+ depthCache.set(stepId, 0);
1569
+ return 0;
1570
+ }
1571
+ const d = Math.min(1e3, computeDepth(parent) + 1);
1572
+ depthCache.set(stepId, d);
1573
+ return d;
1574
+ };
1575
+ for (const [id, s] of steps.entries()) {
1576
+ totalSteps += 1;
1577
+ if (s.type === "llm") llmSteps += 1;
1578
+ else if (s.type === "tool") toolSteps += 1;
1579
+ else logicSteps += 1;
1580
+ if (s.status === "error") errorSteps += 1;
1581
+ const depth = computeDepth(id);
1582
+ if (depth > maxDepth) maxDepth = depth;
1583
+ if (typeof s.durationMs === "number" && Number.isFinite(s.durationMs)) {
1584
+ if (!longestStep || s.durationMs > longestStep.durationMs) {
1585
+ longestStep = { name: s.name, durationMs: s.durationMs, type: s.type };
1586
+ }
1587
+ }
1588
+ if (typeof s.tokensInput === "number" || typeof s.tokensOutput === "number") {
1589
+ hasAnyTokens = true;
1590
+ if (typeof s.tokensInput === "number") totalTokensInput += s.tokensInput;
1591
+ if (typeof s.tokensOutput === "number") totalTokensOutput += s.tokensOutput;
1592
+ }
1593
+ }
1594
+ const summary = {
1595
+ runId,
1596
+ name,
1597
+ status,
1598
+ durationMs,
1599
+ totalSteps,
1600
+ llmSteps,
1601
+ toolSteps,
1602
+ logicSteps,
1603
+ errorSteps,
1604
+ maxDepth,
1605
+ ...longestStep ? { longestStep } : {},
1606
+ ...hasAnyTokens ? { totalTokens: { input: totalTokensInput, output: totalTokensOutput } } : {}
1607
+ };
1608
+ return summary;
1609
+ }
1610
+
1611
+ // packages/core/src/trace-filter.ts
1612
+ function toLower(s) {
1613
+ return typeof s === "string" ? s.toLowerCase() : "";
1614
+ }
1615
+ function filterTraces(traces, options) {
1616
+ const input3 = [...traces];
1617
+ let out = input3.filter((t) => {
1618
+ if (options.status && t.status !== options.status) return false;
1619
+ if (options.name) {
1620
+ const q = options.name.toLowerCase();
1621
+ const hay = `${toLower(t.name)} ${toLower(t.runId)}`;
1622
+ if (!hay.includes(q)) return false;
1623
+ }
1624
+ if (options.since) {
1625
+ const windowMs = parseDuration(options.since);
1626
+ const cutoff = Date.now() - windowMs;
1627
+ const started = typeof t.startedAt === "number" ? t.startedAt : void 0;
1628
+ const basis = started ?? t.createdAt.getTime();
1629
+ if (!Number.isFinite(basis) || basis < cutoff) return false;
1630
+ }
1631
+ return true;
1632
+ });
1633
+ out.sort((a, b) => {
1634
+ const aTime = (typeof a.startedAt === "number" ? a.startedAt : void 0) ?? a.createdAt.getTime();
1635
+ const bTime = (typeof b.startedAt === "number" ? b.startedAt : void 0) ?? b.createdAt.getTime();
1636
+ return bTime - aTime;
1637
+ });
1638
+ if (typeof options.limit === "number" && Number.isFinite(options.limit)) {
1639
+ const n = Math.max(0, Math.floor(options.limit));
1640
+ out = out.slice(0, n);
1641
+ }
1642
+ return out;
1643
+ }
1644
+ var KNOWN_EVENTS = /* @__PURE__ */ new Set([
1645
+ "run_started",
1646
+ "run_completed",
1647
+ "step_started",
1648
+ "step_completed"
1649
+ ]);
1650
+ function isRecord7(value) {
1651
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1652
+ }
1653
+ function safeParse(line) {
1654
+ try {
1655
+ return JSON.parse(line);
1656
+ } catch {
1657
+ return void 0;
1658
+ }
1659
+ }
1660
+ async function isAgentInspectTrace(filePath) {
1661
+ try {
1662
+ const rl = createInterface({
1663
+ input: createReadStream(filePath, { encoding: "utf8" }),
1664
+ crlfDelay: Infinity
383
1665
  });
384
- return withStat.map((x) => x.name);
1666
+ let checked = 0;
1667
+ for await (const line of rl) {
1668
+ const trimmed = line.trim();
1669
+ if (trimmed === "") continue;
1670
+ const parsed = safeParse(trimmed);
1671
+ if (!parsed) continue;
1672
+ if (!isRecord7(parsed)) continue;
1673
+ checked += 1;
1674
+ if (isTraceEvent(parsed)) return true;
1675
+ const ev = parsed.event;
1676
+ const runId = parsed.runId;
1677
+ if (typeof ev === "string" && KNOWN_EVENTS.has(ev) && typeof runId === "string") {
1678
+ return true;
1679
+ }
1680
+ if (checked >= 20) break;
1681
+ }
1682
+ return false;
385
1683
  } catch {
386
- return [];
1684
+ return false;
1685
+ }
1686
+ }
1687
+
1688
+ // packages/core/src/diff/comparable.ts
1689
+ function extractOutputPreview(meta) {
1690
+ if (meta === void 0) return void 0;
1691
+ if ("outputPreview" in meta) return meta.outputPreview;
1692
+ if ("resultPreview" in meta) return meta.resultPreview;
1693
+ return void 0;
1694
+ }
1695
+ function mapStepStatus(s) {
1696
+ if (s === void 0) return "running";
1697
+ return s;
1698
+ }
1699
+ function manualTraceEventsToComparableRun(events) {
1700
+ const started = events.find((e) => e.event === "run_started");
1701
+ if (!started || started.event !== "run_started") {
1702
+ throw new Error("Invalid trace: missing run_started");
1703
+ }
1704
+ const rs = started;
1705
+ const runId = rs.runId;
1706
+ const completedAll = events.filter((e) => e.event === "run_completed");
1707
+ const lastCompleted = completedAll[completedAll.length - 1];
1708
+ let runStatus;
1709
+ if (lastCompleted === void 0) runStatus = "running";
1710
+ else runStatus = lastCompleted.status;
1711
+ const durationMs = lastCompleted !== void 0 && Number.isFinite(lastCompleted.durationMs) ? lastCompleted.durationMs : void 0;
1712
+ const steps = /* @__PURE__ */ new Map();
1713
+ let order = 0;
1714
+ for (const e of events) {
1715
+ if (e.event !== "step_started") continue;
1716
+ const s = e;
1717
+ const meta = s.metadata ? { ...s.metadata } : void 0;
1718
+ steps.set(s.stepId, {
1719
+ id: s.stepId,
1720
+ parentId: s.parentId,
1721
+ name: s.name,
1722
+ type: s.type,
1723
+ order: order++,
1724
+ timestamp: s.timestamp,
1725
+ metadata: meta
1726
+ });
1727
+ }
1728
+ for (const e of events) {
1729
+ if (e.event !== "step_completed") continue;
1730
+ const acc = steps.get(e.stepId);
1731
+ if (!acc) continue;
1732
+ acc.status = e.status;
1733
+ acc.durationMs = e.durationMs;
1734
+ if (e.error?.message) acc.errorMsg = e.error.message;
1735
+ const extra = e;
1736
+ if (extra.metadata !== void 0 && typeof extra.metadata === "object") {
1737
+ acc.metadata = { ...acc.metadata ?? {}, ...extra.metadata };
1738
+ }
1739
+ }
1740
+ const nodes = /* @__PURE__ */ new Map();
1741
+ for (const acc of steps.values()) {
1742
+ let meta = acc.metadata ? { ...acc.metadata } : void 0;
1743
+ if (acc.parentId !== void 0 && !steps.has(acc.parentId)) {
1744
+ meta = { ...meta ?? {}, agent_inspect_diff_parent_missing: true };
1745
+ }
1746
+ const outputPreview = extractOutputPreview(meta);
1747
+ const sc = {
1748
+ id: acc.id,
1749
+ name: acc.name,
1750
+ type: acc.type,
1751
+ status: mapStepStatus(acc.status),
1752
+ durationMs: acc.durationMs,
1753
+ error: acc.errorMsg,
1754
+ metadata: meta && Object.keys(meta).length > 0 ? meta : void 0,
1755
+ outputPreview,
1756
+ children: []
1757
+ };
1758
+ nodes.set(acc.id, sc);
1759
+ }
1760
+ const roots = [];
1761
+ const sortByOrder = (a, b) => {
1762
+ const oa = steps.get(a.id)?.order ?? 0;
1763
+ const ob = steps.get(b.id)?.order ?? 0;
1764
+ return oa - ob;
1765
+ };
1766
+ for (const acc of steps.values()) {
1767
+ const node = nodes.get(acc.id);
1768
+ if (acc.parentId !== void 0 && nodes.has(acc.parentId)) {
1769
+ nodes.get(acc.parentId).children.push(node);
1770
+ } else {
1771
+ roots.push(node);
1772
+ }
1773
+ }
1774
+ roots.sort(sortByOrder);
1775
+ for (const n of nodes.values()) {
1776
+ n.children.sort(sortByOrder);
1777
+ }
1778
+ return {
1779
+ runId,
1780
+ name: rs.name,
1781
+ status: runStatus,
1782
+ durationMs,
1783
+ steps: roots
1784
+ };
1785
+ }
1786
+
1787
+ // packages/core/src/exporters/helpers.ts
1788
+ var REDACT_SUBSTRINGS = [
1789
+ "authorization",
1790
+ "cookie",
1791
+ "token",
1792
+ "apikey",
1793
+ "password",
1794
+ "secret",
1795
+ "email"
1796
+ ];
1797
+ function shouldRedactKey(key) {
1798
+ const k = key.toLowerCase();
1799
+ for (const s of REDACT_SUBSTRINGS) {
1800
+ if (k.includes(s)) return true;
1801
+ }
1802
+ return false;
1803
+ }
1804
+ function safeString2(value, maxLength) {
1805
+ if (value === null || value === void 0) return "";
1806
+ let s;
1807
+ if (typeof value === "string") s = value;
1808
+ else if (typeof value === "number" || typeof value === "boolean") s = String(value);
1809
+ else s = stableJson(value, false);
1810
+ if (maxLength !== void 0 && maxLength >= 0 && s.length > maxLength) {
1811
+ return `${s.slice(0, maxLength)}\u2026`;
1812
+ }
1813
+ return s;
1814
+ }
1815
+ function escapeMarkdown(value) {
1816
+ return value.replace(/\|/g, "\\|").replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, " ");
1817
+ }
1818
+ function escapeHtml(value) {
1819
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
1820
+ }
1821
+ function sortKeysDeep(input3) {
1822
+ if (input3 === null || typeof input3 !== "object") return input3;
1823
+ if (Array.isArray(input3)) return input3.map(sortKeysDeep);
1824
+ const o = input3;
1825
+ const out = {};
1826
+ for (const k of Object.keys(o).sort()) {
1827
+ out[k] = sortKeysDeep(o[k]);
1828
+ }
1829
+ return out;
1830
+ }
1831
+ function stableJson(value, pretty) {
1832
+ const sorted = sortKeysDeep(value);
1833
+ return pretty === true ? JSON.stringify(sorted, null, 2) : JSON.stringify(sorted);
1834
+ }
1835
+ function compactAttributes(attrs, options) {
1836
+ if (attrs === void 0) return {};
1837
+ const maxLen = options?.maxLength ?? 500;
1838
+ const redacted = options?.redacted ?? true;
1839
+ const out = {};
1840
+ for (const key of Object.keys(attrs).sort()) {
1841
+ if (redacted && shouldRedactKey(key)) {
1842
+ out[key] = "[REDACTED]";
1843
+ continue;
1844
+ }
1845
+ const v = attrs[key];
1846
+ out[key] = compactValue(v, maxLen, redacted);
1847
+ }
1848
+ return out;
1849
+ }
1850
+ function compactValue(value, maxLen, redacted) {
1851
+ if (value === null || typeof value !== "object") {
1852
+ return typeof value === "string" ? safeString2(value, maxLen) : value;
1853
+ }
1854
+ if (Array.isArray(value)) {
1855
+ const arr = value.slice(0, 20).map((x) => compactValue(x, maxLen, redacted));
1856
+ if (value.length > 20) arr.push(`\u2026(+${value.length - 20} more)`);
1857
+ return arr;
1858
+ }
1859
+ const o = value;
1860
+ const inner = {};
1861
+ for (const k of Object.keys(o)) {
1862
+ if (redacted && shouldRedactKey(k)) inner[k] = "[REDACTED]";
1863
+ else inner[k] = compactValue(o[k], maxLen, redacted);
1864
+ }
1865
+ return inner;
1866
+ }
1867
+ function flattenTree(tree) {
1868
+ const out = [];
1869
+ function walk(nodes) {
1870
+ for (const n of nodes) {
1871
+ out.push(n);
1872
+ if (n.children.length > 0) walk(n.children);
1873
+ }
1874
+ }
1875
+ walk(tree.children);
1876
+ return out;
1877
+ }
1878
+ function zeroKinds() {
1879
+ return {
1880
+ RUN: 0,
1881
+ AGENT: 0,
1882
+ LLM: 0,
1883
+ TOOL: 0,
1884
+ CHAIN: 0,
1885
+ RETRIEVER: 0,
1886
+ DECISION: 0,
1887
+ RESULT: 0,
1888
+ ERROR: 0,
1889
+ LOGIC: 0,
1890
+ LOG: 0
1891
+ };
1892
+ }
1893
+
1894
+ // packages/core/src/diff/engine.ts
1895
+ var DEFAULT_THRESHOLD_MS = 0;
1896
+ function pathSeg(step2, index) {
1897
+ return { index, name: step2.name, stepId: step2.id };
1898
+ }
1899
+ function buildPath(segments) {
1900
+ return { path: [...segments] };
1901
+ }
1902
+ function pairSteps(left, right) {
1903
+ const usedRight = /* @__PURE__ */ new Set();
1904
+ const pairs = [];
1905
+ for (let i = 0; i < left.length; i++) {
1906
+ const L = left[i];
1907
+ let R = right.find((r) => !usedRight.has(r.id) && r.id === L.id);
1908
+ if (R === void 0 && i < right.length && !usedRight.has(right[i].id)) {
1909
+ const cand = right[i];
1910
+ if (cand.name === L.name && (cand.type ?? "") === (L.type ?? "")) {
1911
+ R = cand;
1912
+ }
1913
+ }
1914
+ if (R === void 0) {
1915
+ R = right.find(
1916
+ (r) => !usedRight.has(r.id) && r.name === L.name && (r.type ?? "") === (L.type ?? "")
1917
+ );
1918
+ }
1919
+ if (R !== void 0) {
1920
+ usedRight.add(R.id);
1921
+ pairs.push([L, R]);
1922
+ } else {
1923
+ pairs.push([L, void 0]);
1924
+ }
1925
+ }
1926
+ for (const R of right) {
1927
+ if (!usedRight.has(R.id)) {
1928
+ pairs.push([void 0, R]);
1929
+ }
1930
+ }
1931
+ return pairs;
1932
+ }
1933
+ function compareLeafSteps(L, R, segments, opts, out) {
1934
+ const path7 = buildPath(segments);
1935
+ if (L.name !== R.name) {
1936
+ out.push({
1937
+ kind: "structure",
1938
+ severity: "warning",
1939
+ message: "Step name differs",
1940
+ path: path7,
1941
+ left: L.name,
1942
+ right: R.name
1943
+ });
1944
+ }
1945
+ if ((L.type ?? "") !== (R.type ?? "")) {
1946
+ out.push({
1947
+ kind: "step-type",
1948
+ severity: "warning",
1949
+ message: "Step type differs",
1950
+ path: path7,
1951
+ left: L.type,
1952
+ right: R.type
1953
+ });
1954
+ }
1955
+ if ((L.status ?? "") !== (R.status ?? "")) {
1956
+ out.push({
1957
+ kind: "step-status",
1958
+ severity: "warning",
1959
+ message: "Step status differs",
1960
+ path: path7,
1961
+ left: L.status,
1962
+ right: R.status
1963
+ });
1964
+ }
1965
+ const le = L.error ?? "";
1966
+ const re = R.error ?? "";
1967
+ if (le !== re) {
1968
+ out.push({
1969
+ kind: "error",
1970
+ severity: "error",
1971
+ message: "Step error message differs",
1972
+ path: path7,
1973
+ left: le || void 0,
1974
+ right: re || void 0
1975
+ });
1976
+ }
1977
+ if (!opts.ignoreDuration) {
1978
+ const ld = L.durationMs;
1979
+ const rd = R.durationMs;
1980
+ const th = opts.durationThresholdMs;
1981
+ let differs = false;
1982
+ if (ld === void 0 && rd === void 0) differs = false;
1983
+ else if (ld === void 0 || rd === void 0) differs = true;
1984
+ else differs = Math.abs(ld - rd) > th;
1985
+ if (differs) {
1986
+ out.push({
1987
+ kind: "duration",
1988
+ severity: "info",
1989
+ message: "Step duration differs",
1990
+ path: path7,
1991
+ left: ld,
1992
+ right: rd
1993
+ });
1994
+ }
1995
+ }
1996
+ const lm = stableJson(L.metadata ?? {});
1997
+ const rm = stableJson(R.metadata ?? {});
1998
+ if (lm !== rm) {
1999
+ out.push({
2000
+ kind: "metadata",
2001
+ severity: "info",
2002
+ message: "Step metadata differs",
2003
+ path: path7,
2004
+ left: L.metadata,
2005
+ right: R.metadata
2006
+ });
2007
+ }
2008
+ const lo = stableJson(L.outputPreview ?? null);
2009
+ const ro = stableJson(R.outputPreview ?? null);
2010
+ if (lo !== ro) {
2011
+ out.push({
2012
+ kind: "output",
2013
+ severity: "info",
2014
+ message: "Output preview differs",
2015
+ path: path7,
2016
+ left: L.outputPreview,
2017
+ right: R.outputPreview
2018
+ });
2019
+ }
2020
+ }
2021
+ function compareRecursive(L, R, segments, opts, out) {
2022
+ compareLeafSteps(L, R, segments, opts, out);
2023
+ const pairs = pairSteps(L.children, R.children);
2024
+ let ci = 0;
2025
+ for (const [lch, rch] of pairs) {
2026
+ if (lch !== void 0 && rch !== void 0) {
2027
+ compareRecursive(lch, rch, [...segments, pathSeg(lch, ci)], opts, out);
2028
+ } else if (lch !== void 0) {
2029
+ out.push({
2030
+ kind: "step-removed",
2031
+ severity: "warning",
2032
+ message: `Step only in left run: ${lch.name}`,
2033
+ path: buildPath([...segments, pathSeg(lch, ci)]),
2034
+ left: lch.id,
2035
+ right: void 0
2036
+ });
2037
+ } else if (rch !== void 0) {
2038
+ out.push({
2039
+ kind: "step-added",
2040
+ severity: "warning",
2041
+ message: `Step only in right run: ${rch.name}`,
2042
+ path: buildPath([...segments, pathSeg(rch, ci)]),
2043
+ left: void 0,
2044
+ right: rch.id
2045
+ });
2046
+ }
2047
+ ci += 1;
2048
+ }
2049
+ }
2050
+ function mergeDiffDefaults(options) {
2051
+ return {
2052
+ ignoreDuration: options?.ignoreDuration ?? false,
2053
+ durationThresholdMs: options?.durationThresholdMs !== void 0 ? options.durationThresholdMs : DEFAULT_THRESHOLD_MS,
2054
+ focus: options?.focus ?? "all",
2055
+ check: options?.check ?? "all"
2056
+ };
2057
+ }
2058
+ function kindMatchesFilter(kind, merged) {
2059
+ const { focus, check } = merged;
2060
+ if (check !== "all") {
2061
+ if (check === "structure") {
2062
+ if (!["step-added", "step-removed", "structure", "step-type"].includes(kind)) return false;
2063
+ } else if (check === "outputs") {
2064
+ if (!["metadata", "output"].includes(kind)) return false;
2065
+ } else if (check === "errors") {
2066
+ if (!["run-status", "step-status", "error"].includes(kind)) return false;
2067
+ } else if (check === "timing") {
2068
+ if (kind !== "duration") return false;
2069
+ }
2070
+ }
2071
+ if (focus !== "all") {
2072
+ if (focus === "errors") {
2073
+ if (!["run-status", "step-status", "error"].includes(kind)) return false;
2074
+ } else if (focus === "structure") {
2075
+ if (!["step-added", "step-removed", "structure", "step-type"].includes(kind)) return false;
2076
+ } else if (focus === "outputs") {
2077
+ if (!["metadata", "output"].includes(kind)) return false;
2078
+ }
2079
+ }
2080
+ return true;
2081
+ }
2082
+ function diffRuns(left, right, options) {
2083
+ const merged = mergeDiffDefaults(options);
2084
+ const opts = {
2085
+ ignoreDuration: merged.ignoreDuration,
2086
+ durationThresholdMs: merged.durationThresholdMs
2087
+ };
2088
+ const raw = [];
2089
+ if ((left.status ?? "") !== (right.status ?? "")) {
2090
+ raw.push({
2091
+ kind: "run-status",
2092
+ severity: "warning",
2093
+ message: "Run completion status differs",
2094
+ left: left.status,
2095
+ right: right.status
2096
+ });
2097
+ }
2098
+ if (!merged.ignoreDuration) {
2099
+ const ld = left.durationMs;
2100
+ const rd = right.durationMs;
2101
+ const th = merged.durationThresholdMs;
2102
+ let differs = false;
2103
+ if (ld === void 0 && rd === void 0) differs = false;
2104
+ else if (ld === void 0 || rd === void 0) differs = true;
2105
+ else differs = Math.abs(ld - rd) > th;
2106
+ if (differs) {
2107
+ raw.push({
2108
+ kind: "duration",
2109
+ severity: "info",
2110
+ message: "Run duration differs",
2111
+ left: ld,
2112
+ right: rd
2113
+ });
2114
+ }
2115
+ }
2116
+ const pairs = pairSteps(left.steps, right.steps);
2117
+ let idx = 0;
2118
+ for (const [ls, rs] of pairs) {
2119
+ if (ls !== void 0 && rs !== void 0) {
2120
+ compareRecursive(ls, rs, [pathSeg(ls, idx)], opts, raw);
2121
+ idx += 1;
2122
+ } else if (ls !== void 0) {
2123
+ raw.push({
2124
+ kind: "step-removed",
2125
+ severity: "warning",
2126
+ message: `Step only in left run: ${ls.name}`,
2127
+ path: buildPath([pathSeg(ls, idx)]),
2128
+ left: ls.id,
2129
+ right: void 0
2130
+ });
2131
+ idx += 1;
2132
+ } else if (rs !== void 0) {
2133
+ raw.push({
2134
+ kind: "step-added",
2135
+ severity: "warning",
2136
+ message: `Step only in right run: ${rs.name}`,
2137
+ path: buildPath([pathSeg(rs, idx)]),
2138
+ left: void 0,
2139
+ right: rs.id
2140
+ });
2141
+ idx += 1;
2142
+ }
2143
+ }
2144
+ const differences = raw.filter((d) => kindMatchesFilter(d.kind, merged));
2145
+ let errors = 0;
2146
+ let warnings = 0;
2147
+ let info = 0;
2148
+ for (const d of differences) {
2149
+ if (d.severity === "error") errors += 1;
2150
+ else if (d.severity === "warning") warnings += 1;
2151
+ else info += 1;
2152
+ }
2153
+ const firstVisible = differences[0];
2154
+ const firstDivergence = firstVisible !== void 0 ? {
2155
+ kind: "first-divergence",
2156
+ severity: firstVisible.severity,
2157
+ message: `First divergence: ${firstVisible.message}`,
2158
+ path: firstVisible.path,
2159
+ left: firstVisible.left,
2160
+ right: firstVisible.right
2161
+ } : void 0;
2162
+ const summary = {
2163
+ leftRunId: left.runId,
2164
+ rightRunId: right.runId,
2165
+ totalDifferences: differences.length,
2166
+ errors,
2167
+ warnings,
2168
+ info,
2169
+ firstDivergence
2170
+ };
2171
+ return { summary, differences };
2172
+ }
2173
+ function formatPath(path7) {
2174
+ if (path7 === void 0 || path7.path.length === 0) {
2175
+ return "(run)";
387
2176
  }
2177
+ return path7.path.map((s) => s.name).join(" > ");
388
2178
  }
389
- function getRunIdFromTraceFileName(fileName) {
390
- try {
391
- const base = path.basename(fileName);
392
- if (!base.endsWith(".jsonl")) return void 0;
393
- const id = base.slice(0, -".jsonl".length);
394
- return id === "" ? void 0 : id;
395
- } catch {
396
- return void 0;
2179
+ function formatValue(v, verbose) {
2180
+ if (v === void 0) return "(undefined)";
2181
+ if (typeof v === "string") return v;
2182
+ if (typeof v === "number" || typeof v === "boolean") return String(v);
2183
+ const s = JSON.stringify(v);
2184
+ if (verbose || s.length <= 120) return s;
2185
+ return `${s.slice(0, 117)}...`;
2186
+ }
2187
+ function renderRunDiff(result, options) {
2188
+ const verbose = options?.verbose === true;
2189
+ const sev = (s, level) => {
2190
+ return s;
2191
+ };
2192
+ const lines = [];
2193
+ const { summary } = result;
2194
+ lines.push("Run diff");
2195
+ lines.push(`Left: ${summary.leftRunId}`);
2196
+ lines.push(`Right: ${summary.rightRunId}`);
2197
+ lines.push("");
2198
+ lines.push("Summary:");
2199
+ lines.push(` Differences: ${summary.totalDifferences}`);
2200
+ lines.push(` Errors: ${summary.errors}`);
2201
+ lines.push(` Warnings: ${summary.warnings}`);
2202
+ lines.push(` Info: ${summary.info}`);
2203
+ lines.push("");
2204
+ const fd = summary.firstDivergence;
2205
+ const firstKind = result.differences[0]?.kind;
2206
+ if (fd !== void 0) {
2207
+ lines.push("First divergence:");
2208
+ const where = formatPath(fd.path);
2209
+ const displayKind = firstKind ?? fd.kind;
2210
+ lines.push(` ${displayKind} at ${where}`);
2211
+ if (fd.left !== void 0 || fd.right !== void 0) {
2212
+ lines.push(` left: ${formatValue(fd.left, verbose)}`);
2213
+ lines.push(` right: ${formatValue(fd.right, verbose)}`);
2214
+ }
2215
+ lines.push("");
2216
+ }
2217
+ lines.push("Differences:");
2218
+ if (result.differences.length === 0) {
2219
+ lines.push(" (none)");
2220
+ return lines.join("\n");
2221
+ }
2222
+ const showSides = (kind) => verbose || [
2223
+ "run-status",
2224
+ "step-status",
2225
+ "error",
2226
+ "duration",
2227
+ "step-type",
2228
+ "structure",
2229
+ "step-added",
2230
+ "step-removed"
2231
+ ].includes(kind);
2232
+ for (const d of result.differences) {
2233
+ const tag = sev(`[${d.severity}]`, d.severity);
2234
+ const pathStr = d.path !== void 0 ? ` ${formatPath(d.path)}` : "";
2235
+ lines.push(` ${tag} ${d.kind}${pathStr}`);
2236
+ lines.push(` ${d.message}`);
2237
+ if (d.left !== void 0 || d.right !== void 0) {
2238
+ if (showSides(d.kind)) {
2239
+ lines.push(` left: ${formatValue(d.left, verbose)}`);
2240
+ lines.push(` right: ${formatValue(d.right, verbose)}`);
2241
+ }
2242
+ }
397
2243
  }
2244
+ return lines.join("\n");
2245
+ }
2246
+
2247
+ // packages/core/src/diff/index.ts
2248
+ function diffTraceEvents(leftEvents, rightEvents, options) {
2249
+ const left = manualTraceEventsToComparableRun(leftEvents);
2250
+ const right = manualTraceEventsToComparableRun(rightEvents);
2251
+ return diffRuns(left, right, options);
398
2252
  }
399
2253
  var TERMINAL_INDENT = " ";
400
2254
  var MAX_TERMINAL_NAME_LENGTH = 80;
@@ -421,24 +2275,24 @@ function formatTerminalName(name) {
421
2275
  return truncateName(name, MAX_TERMINAL_NAME_LENGTH);
422
2276
  }
423
2277
  function getStatusIcon(status) {
424
- if (status === "success") return chalk.green("\u2714");
425
- if (status === "error") return chalk.red("\u2716");
426
- return chalk.yellow("\u23F3");
2278
+ if (status === "success") return chalk2.green("\u2714");
2279
+ if (status === "error") return chalk2.red("\u2716");
2280
+ return chalk2.yellow("\u23F3");
427
2281
  }
428
2282
  function renderStepLine(name, durationMs, status, depth) {
429
2283
  try {
430
2284
  const nm = formatTerminalName(name);
431
2285
  const ind = getIndent(depth ?? 0);
432
2286
  if (status === "running" && durationMs === void 0) {
433
- return `${ind}${chalk.yellow("\u23F3")} ${nm}`;
2287
+ return `${ind}${chalk2.yellow("\u23F3")} ${nm}`;
434
2288
  }
435
2289
  const hasDur = durationMs !== void 0 && Number.isFinite(durationMs);
436
- const dur = hasDur ? formatDuration(durationMs) : void 0;
2290
+ const dur = hasDur ? formatDuration2(durationMs) : void 0;
437
2291
  if (status === "running") {
438
- return dur !== void 0 ? `${ind}${chalk.yellow("\u23F3")} ${nm} (${dur})` : `${ind}${chalk.yellow("\u23F3")} ${nm}`;
2292
+ return dur !== void 0 ? `${ind}${chalk2.yellow("\u23F3")} ${nm} (${dur})` : `${ind}${chalk2.yellow("\u23F3")} ${nm}`;
439
2293
  }
440
2294
  if (!hasDur || dur === void 0) {
441
- return `${ind}${chalk.yellow("\u23F3")} ${nm}`;
2295
+ return `${ind}${chalk2.yellow("\u23F3")} ${nm}`;
442
2296
  }
443
2297
  if (status === "success") {
444
2298
  return `${ind}${getStatusIcon("success")} ${nm} (${dur})`;
@@ -607,6 +2461,773 @@ Object.assign(stepImpl, {
607
2461
  tool: stepTool
608
2462
  });
609
2463
 
2464
+ // packages/core/src/exporters/types.ts
2465
+ var EXPORT_PAYLOAD_VERSION = "0.1.2";
2466
+
2467
+ // packages/core/src/exporters/html-exporter.ts
2468
+ function renderTreeHtml(nodes, ulClass = "tree") {
2469
+ if (nodes.length === 0) return "";
2470
+ const parts = [`<ul class="${ulClass}">`];
2471
+ for (const n of nodes) {
2472
+ const ev = n.event;
2473
+ const status = ev.status ?? "?";
2474
+ const dur = ev.durationMs !== void 0 && Number.isFinite(ev.durationMs) ? `${ev.durationMs}ms` : "-";
2475
+ parts.push("<li>");
2476
+ parts.push(
2477
+ `<span class="nm">${escapeHtml(ev.name)}</span> <span class="meta">[${escapeHtml(ev.kind)}] ${escapeHtml(status)} (${escapeHtml(dur)})</span>`
2478
+ );
2479
+ if (n.children.length > 0) {
2480
+ parts.push(renderTreeHtml(n.children, "tree nested"));
2481
+ }
2482
+ parts.push("</li>");
2483
+ }
2484
+ parts.push("</ul>");
2485
+ return parts.join("");
2486
+ }
2487
+ function exportHtml(tree, options) {
2488
+ const warnings = [];
2489
+ const includeMetadata = options?.includeMetadata ?? true;
2490
+ const includeAttributes = options?.includeAttributes ?? false;
2491
+ const includeErrors = options?.includeErrors ?? true;
2492
+ const maxLen = options?.maxAttributeLength;
2493
+ const redacted = options?.redacted;
2494
+ const titleName = escapeHtml(tree.name ?? tree.runId);
2495
+ const summaryRows = [];
2496
+ summaryRows.push(
2497
+ `<tr><th scope="row">runId</th><td><code>${escapeHtml(tree.runId)}</code></td></tr>`
2498
+ );
2499
+ if (tree.name !== void 0) {
2500
+ summaryRows.push(`<tr><th scope="row">name</th><td>${escapeHtml(tree.name)}</td></tr>`);
2501
+ }
2502
+ summaryRows.push(
2503
+ `<tr><th scope="row">status</th><td>${escapeHtml(String(tree.status ?? "unknown"))}</td></tr>`
2504
+ );
2505
+ summaryRows.push(
2506
+ `<tr><th scope="row">durationMs</th><td>${tree.durationMs !== void 0 ? escapeHtml(String(tree.durationMs)) : "\u2014"}</td></tr>`
2507
+ );
2508
+ summaryRows.push(
2509
+ `<tr><th scope="row">startedAt</th><td>${tree.startedAt !== void 0 ? escapeHtml(String(tree.startedAt)) : "\u2014"}</td></tr>`
2510
+ );
2511
+ summaryRows.push(
2512
+ `<tr><th scope="row">endedAt</th><td>${tree.endedAt !== void 0 ? escapeHtml(String(tree.endedAt)) : "\u2014"}</td></tr>`
2513
+ );
2514
+ summaryRows.push(
2515
+ `<tr><th scope="row">totalEvents</th><td>${escapeHtml(String(tree.metadata.totalEvents))}</td></tr>`
2516
+ );
2517
+ let confidenceHtml = "";
2518
+ if (includeMetadata) {
2519
+ const cb = tree.metadata.confidenceBreakdown;
2520
+ confidenceHtml += "<h3>Confidence breakdown</h3><table><thead><tr><th>bucket</th><th>count</th></tr></thead><tbody>";
2521
+ for (const k of Object.keys(cb).sort()) {
2522
+ const key = k;
2523
+ confidenceHtml += `<tr><td>${escapeHtml(key)}</td><td>${cb[key]}</td></tr>`;
2524
+ }
2525
+ confidenceHtml += "</tbody></table>";
2526
+ confidenceHtml += "<h3>Kind breakdown</h3><table><thead><tr><th>kind</th><th>count</th></tr></thead><tbody>";
2527
+ for (const k of Object.keys(tree.metadata.kinds).sort()) {
2528
+ const key = k;
2529
+ const c = tree.metadata.kinds[key];
2530
+ if (c > 0) confidenceHtml += `<tr><td>${escapeHtml(key)}</td><td>${c}</td></tr>`;
2531
+ }
2532
+ confidenceHtml += "</tbody></table>";
2533
+ }
2534
+ const flat = flattenTree(tree);
2535
+ const errors = flat.filter((n) => n.event.status === "error");
2536
+ let errorsHtml = "";
2537
+ if (includeErrors && errors.length > 0) {
2538
+ errorsHtml += "<h2>Errors</h2><ul>";
2539
+ for (const n of errors) {
2540
+ const msg = n.event.attributes && typeof n.event.attributes.error === "object" ? safeString2(
2541
+ n.event.attributes.error.message,
2542
+ maxLen
2543
+ ) : "";
2544
+ errorsHtml += `<li><strong>${escapeHtml(n.event.name)}</strong> (${escapeHtml(n.event.eventId)}): ${escapeHtml(msg || "error")}</li>`;
2545
+ }
2546
+ errorsHtml += "</ul>";
2547
+ }
2548
+ let attrsHtml = "";
2549
+ if (includeAttributes) {
2550
+ attrsHtml += "<h2>Attributes (bounded)</h2>";
2551
+ for (const n of flat) {
2552
+ if (!n.event.attributes || Object.keys(n.event.attributes).length === 0) continue;
2553
+ const compact = compactAttributes(n.event.attributes, {
2554
+ maxLength: maxLen,
2555
+ redacted
2556
+ });
2557
+ attrsHtml += `<h3>${escapeHtml(n.event.name)}</h3><pre class="json">${escapeHtml(stableJson(compact, true))}</pre>`;
2558
+ }
2559
+ warnings.push(
2560
+ "Attributes may still contain sensitive data; review exports before sharing."
2561
+ );
2562
+ }
2563
+ const css = `
2564
+ body{font-family:system-ui,sans-serif;line-height:1.5;margin:1.5rem;max-width:960px;color:#111}
2565
+ h1{font-size:1.35rem}
2566
+ h2{font-size:1.1rem;margin-top:1.5rem}
2567
+ table{border-collapse:collapse;margin:0.75rem 0}
2568
+ th,td{border:1px solid #ccc;padding:0.35rem 0.6rem;text-align:left}
2569
+ th{background:#f5f5f5}
2570
+ pre.json{background:#f8f8f8;padding:0.75rem;overflow:auto;font-size:0.85rem}
2571
+ ul.tree{list-style:none;padding-left:1rem}
2572
+ ul.tree.nested{padding-left:1.25rem;border-left:1px solid #ddd;margin:0.25rem 0}
2573
+ .nm{font-weight:600}
2574
+ .meta{color:#555;font-size:0.9rem}
2575
+ footer{margin-top:2rem;font-size:0.85rem;color:#555}
2576
+ `.trim();
2577
+ const html = `<!doctype html>
2578
+ <html lang="en">
2579
+ <head>
2580
+ <meta charset="utf-8"/>
2581
+ <meta name="viewport" content="width=device-width, initial-scale=1"/>
2582
+ <title>${titleName}</title>
2583
+ <style>${css}</style>
2584
+ </head>
2585
+ <body>
2586
+ <header><h1>AgentInspect Run: ${titleName}</h1></header>
2587
+ <p class="note">Generated locally by AgentInspect.</p>
2588
+ ${includeMetadata ? `<section class="summary"><h2>Summary</h2><table>${summaryRows.join("")}</table>${confidenceHtml}</section>` : ""}
2589
+ <section class="tree"><h2>Execution tree</h2>${tree.children.length > 0 ? renderTreeHtml(tree.children) : "<p>No steps recorded.</p>"}</section>
2590
+ ${errorsHtml}
2591
+ ${attrsHtml}
2592
+ <footer>Generated locally by AgentInspect. Review for sensitive data before sharing.</footer>
2593
+ </body>
2594
+ </html>`;
2595
+ return {
2596
+ format: "html",
2597
+ content: html,
2598
+ contentType: "text/html",
2599
+ fileExtension: ".html",
2600
+ warnings
2601
+ };
2602
+ }
2603
+
2604
+ // packages/core/src/exporters/markdown-exporter.ts
2605
+ function renderTreeAscii(nodes, indent = "") {
2606
+ const lines = [];
2607
+ for (let i = 0; i < nodes.length; i++) {
2608
+ const n = nodes[i];
2609
+ const last = i === nodes.length - 1;
2610
+ const branch = last ? "\u2514\u2500 " : "\u251C\u2500 ";
2611
+ const ev = n.event;
2612
+ const status = ev.status ?? "?";
2613
+ const dur = ev.durationMs !== void 0 && Number.isFinite(ev.durationMs) ? `${ev.durationMs}ms` : "-";
2614
+ lines.push(`${indent}${branch}${escapeMarkdown(ev.name)} [${ev.kind}] ${status} (${dur})`);
2615
+ const nextIndent = indent + (last ? " " : "\u2502 ");
2616
+ if (n.children.length > 0) {
2617
+ const childStr = renderTreeAscii(n.children, nextIndent);
2618
+ if (childStr.length > 0) lines.push(childStr);
2619
+ }
2620
+ }
2621
+ return lines.join("\n");
2622
+ }
2623
+ function exportMarkdown(tree, options) {
2624
+ const warnings = [];
2625
+ const includeMetadata = options?.includeMetadata ?? true;
2626
+ const includeAttributes = options?.includeAttributes ?? false;
2627
+ const includeErrors = options?.includeErrors ?? true;
2628
+ const maxLen = options?.maxAttributeLength;
2629
+ const redacted = options?.redacted;
2630
+ const titleName = tree.name ?? tree.runId;
2631
+ const lines = [];
2632
+ lines.push(`# AgentInspect Run: ${escapeMarkdown(titleName)}`);
2633
+ lines.push("");
2634
+ lines.push("Generated locally by AgentInspect. Review for sensitive data before sharing.");
2635
+ lines.push("");
2636
+ if (includeMetadata) {
2637
+ lines.push("## Summary");
2638
+ lines.push("");
2639
+ lines.push(`- **runId**: ${escapeMarkdown(tree.runId)}`);
2640
+ if (tree.name !== void 0) lines.push(`- **name**: ${escapeMarkdown(tree.name)}`);
2641
+ lines.push(`- **status**: ${escapeMarkdown(String(tree.status ?? "unknown"))}`);
2642
+ lines.push(
2643
+ `- **durationMs**: ${tree.durationMs !== void 0 ? escapeMarkdown(String(tree.durationMs)) : "-"}`
2644
+ );
2645
+ lines.push(
2646
+ `- **startedAt**: ${tree.startedAt !== void 0 ? escapeMarkdown(String(tree.startedAt)) : "-"}`
2647
+ );
2648
+ lines.push(
2649
+ `- **endedAt**: ${tree.endedAt !== void 0 ? escapeMarkdown(String(tree.endedAt)) : "-"}`
2650
+ );
2651
+ lines.push(`- **totalEvents**: ${tree.metadata.totalEvents}`);
2652
+ lines.push("");
2653
+ lines.push("### Confidence breakdown");
2654
+ lines.push("");
2655
+ lines.push("| bucket | count |");
2656
+ lines.push("| --- | --- |");
2657
+ for (const k of Object.keys(tree.metadata.confidenceBreakdown).sort()) {
2658
+ const key = k;
2659
+ lines.push(
2660
+ `| ${escapeMarkdown(key)} | ${tree.metadata.confidenceBreakdown[key]} |`
2661
+ );
2662
+ }
2663
+ lines.push("");
2664
+ lines.push("### Kind breakdown");
2665
+ lines.push("");
2666
+ lines.push("| kind | count |");
2667
+ lines.push("| --- | --- |");
2668
+ for (const k of Object.keys(tree.metadata.kinds).sort()) {
2669
+ const key = k;
2670
+ const c = tree.metadata.kinds[key];
2671
+ if (c > 0) lines.push(`| ${escapeMarkdown(key)} | ${c} |`);
2672
+ }
2673
+ lines.push("");
2674
+ }
2675
+ lines.push("## Execution tree");
2676
+ lines.push("");
2677
+ lines.push("```text");
2678
+ lines.push(
2679
+ tree.children.length > 0 ? renderTreeAscii(tree.children) : "(no steps)"
2680
+ );
2681
+ lines.push("```");
2682
+ lines.push("");
2683
+ const flat = flattenTree(tree);
2684
+ const errors = flat.filter((n) => n.event.status === "error");
2685
+ if (includeErrors && errors.length > 0) {
2686
+ lines.push("## Errors");
2687
+ lines.push("");
2688
+ for (const n of errors) {
2689
+ const msg = n.event.attributes && typeof n.event.attributes.error === "object" ? safeString2(
2690
+ n.event.attributes.error.message,
2691
+ maxLen
2692
+ ) : "";
2693
+ lines.push(
2694
+ `- **${escapeMarkdown(n.event.name)}** (${escapeMarkdown(n.event.eventId)}): ${escapeMarkdown(msg || "error")}`
2695
+ );
2696
+ }
2697
+ lines.push("");
2698
+ }
2699
+ if (includeAttributes) {
2700
+ lines.push("## Attributes (bounded)");
2701
+ lines.push("");
2702
+ for (const n of flat) {
2703
+ if (!n.event.attributes || Object.keys(n.event.attributes).length === 0) continue;
2704
+ const compact = compactAttributes(n.event.attributes, {
2705
+ maxLength: maxLen,
2706
+ redacted
2707
+ });
2708
+ lines.push(`### ${escapeMarkdown(n.event.name)}`);
2709
+ lines.push("");
2710
+ lines.push("```json");
2711
+ lines.push(stableJson(compact, true));
2712
+ lines.push("```");
2713
+ lines.push("");
2714
+ }
2715
+ warnings.push(
2716
+ "Attributes may still contain sensitive data; review exports before sharing."
2717
+ );
2718
+ }
2719
+ return {
2720
+ format: "markdown",
2721
+ content: lines.join("\n"),
2722
+ contentType: "text/markdown",
2723
+ fileExtension: ".md",
2724
+ warnings
2725
+ };
2726
+ }
2727
+ function hexFrom(seed, byteLen) {
2728
+ return crypto.createHash("sha256").update(seed, "utf8").digest("hex").slice(0, byteLen * 2);
2729
+ }
2730
+ function mapInspectKindToOI(kind, warnings) {
2731
+ switch (kind) {
2732
+ case "LLM":
2733
+ return { openInferenceKind: "LLM" };
2734
+ case "TOOL":
2735
+ return { openInferenceKind: "TOOL" };
2736
+ case "CHAIN":
2737
+ return { openInferenceKind: "CHAIN" };
2738
+ case "RETRIEVER":
2739
+ return { openInferenceKind: "RETRIEVER" };
2740
+ case "AGENT":
2741
+ return { openInferenceKind: "AGENT" };
2742
+ case "DECISION":
2743
+ warnings.push(
2744
+ `Ambiguous kind DECISION mapped to CHAIN for span compatibility (${EXPORT_PAYLOAD_VERSION}).`
2745
+ );
2746
+ return { openInferenceKind: "CHAIN" };
2747
+ case "RESULT":
2748
+ warnings.push(
2749
+ `Ambiguous kind RESULT mapped to UNKNOWN for span compatibility (${EXPORT_PAYLOAD_VERSION}).`
2750
+ );
2751
+ return { openInferenceKind: "UNKNOWN" };
2752
+ case "ERROR":
2753
+ warnings.push(`ERROR kind mapped to CHAIN for span compatibility.`);
2754
+ return { openInferenceKind: "CHAIN" };
2755
+ case "LOG":
2756
+ case "LOGIC":
2757
+ case "RUN":
2758
+ warnings.push(`${kind} mapped to CHAIN for span compatibility.`);
2759
+ return { openInferenceKind: "CHAIN" };
2760
+ default:
2761
+ warnings.push(`Unhandled InspectKind ${kind} mapped to UNKNOWN.`);
2762
+ return { openInferenceKind: "UNKNOWN" };
2763
+ }
2764
+ }
2765
+ function exportOpenInference(tree, options) {
2766
+ const warnings = [
2767
+ "OpenInference-compatible JSON export is experimental until verified against specific backends.",
2768
+ "This file was generated locally and not sent anywhere."
2769
+ ];
2770
+ const traceId = hexFrom(`trace:${tree.runId}`, 16);
2771
+ const includeAttributes = options?.includeAttributes ?? false;
2772
+ const maxLen = options?.maxAttributeLength;
2773
+ const spans = [];
2774
+ for (const n of flattenTree(tree)) {
2775
+ const ev = n.event;
2776
+ const spanId = hexFrom(`${tree.runId}:${ev.eventId}`, 8);
2777
+ const parentSpanHex = ev.parentId ? hexFrom(`${tree.runId}:${ev.parentId}`, 8) : void 0;
2778
+ const startNs = Math.round(ev.timestamp * 1e6);
2779
+ let endNs;
2780
+ if (ev.durationMs !== void 0 && Number.isFinite(ev.durationMs)) {
2781
+ endNs = startNs + Math.round(ev.durationMs * 1e6);
2782
+ }
2783
+ const { openInferenceKind } = mapInspectKindToOI(ev.kind, warnings);
2784
+ const attrs = {
2785
+ "openinference.span.kind": openInferenceKind,
2786
+ "agent_inspect.kind": ev.kind,
2787
+ "agent_inspect.confidence": ev.confidence,
2788
+ "agent_inspect.source.type": ev.source.type,
2789
+ "agent_inspect.run_id": tree.runId,
2790
+ "agent_inspect.event_id": ev.eventId,
2791
+ "agent_inspect.status": ev.status ?? "unset"
2792
+ };
2793
+ if (ev.durationMs !== void 0) {
2794
+ attrs["agent_inspect.duration_ms"] = ev.durationMs;
2795
+ }
2796
+ const meta = ev.attributes;
2797
+ if (meta?.model !== void 0 && typeof meta.model === "string") {
2798
+ attrs["llm.model_name"] = meta.model;
2799
+ }
2800
+ const tokens = meta?.tokens;
2801
+ if (tokens && typeof tokens === "object" && tokens !== null) {
2802
+ const inp = tokens.input;
2803
+ const outp = tokens.output;
2804
+ if (typeof inp === "number") attrs["llm.token_count.prompt"] = inp;
2805
+ if (typeof outp === "number") attrs["llm.token_count.completion"] = outp;
2806
+ }
2807
+ if (includeAttributes && meta && typeof meta === "object") {
2808
+ for (const [k, v] of Object.entries(meta)) {
2809
+ if (k === "tokens" || k === "model") continue;
2810
+ if (v !== void 0 && v !== null && typeof v !== "object") {
2811
+ attrs[`agent_inspect.preview.${k}`] = typeof v === "string" ? v.slice(0, maxLen) : v;
2812
+ }
2813
+ }
2814
+ }
2815
+ let status;
2816
+ if (ev.status === "error") {
2817
+ const msg = meta && typeof meta.error === "object" && meta.error !== null ? String(meta.error.message ?? "error") : "error";
2818
+ status = { code: "ERROR", message: msg.slice(0, maxLen) };
2819
+ } else if (ev.status === "ok") {
2820
+ status = { code: "OK" };
2821
+ } else {
2822
+ status = { code: "UNSET" };
2823
+ }
2824
+ spans.push({
2825
+ trace_id: traceId,
2826
+ span_id: spanId,
2827
+ parent_span_id: parentSpanHex,
2828
+ name: ev.name,
2829
+ start_time_unix_nano: startNs,
2830
+ end_time_unix_nano: endNs,
2831
+ attributes: attrs,
2832
+ status
2833
+ });
2834
+ }
2835
+ const payload = {
2836
+ exporter: "agent-inspect",
2837
+ format: "openinference",
2838
+ compatibility: "openinference-compatible",
2839
+ version: EXPORT_PAYLOAD_VERSION,
2840
+ trace_id: traceId,
2841
+ spans,
2842
+ warnings
2843
+ };
2844
+ return {
2845
+ format: "openinference",
2846
+ content: JSON.stringify(payload, null, 2 ),
2847
+ contentType: "application/json",
2848
+ fileExtension: ".openinference.json",
2849
+ warnings
2850
+ };
2851
+ }
2852
+ function hexFrom2(seed, byteLen) {
2853
+ return crypto.createHash("sha256").update(seed, "utf8").digest("hex").slice(0, byteLen * 2);
2854
+ }
2855
+ function stringAttr(key, value) {
2856
+ return { key, value: { stringValue: value } };
2857
+ }
2858
+ function intAttr(key, value) {
2859
+ return { key, value: { intValue: String(value) } };
2860
+ }
2861
+ function genAiOperationName(kind) {
2862
+ switch (kind) {
2863
+ case "LLM":
2864
+ return "generate_content";
2865
+ case "TOOL":
2866
+ return "execute_tool";
2867
+ case "AGENT":
2868
+ return "invoke_agent";
2869
+ default:
2870
+ return void 0;
2871
+ }
2872
+ }
2873
+ function exportOtlpJson(tree, options) {
2874
+ const warnings = [
2875
+ "OTLP JSON export uses OTel GenAI-aligned attributes where applicable; experimental until verified against specific collectors.",
2876
+ "Not OTLP gRPC/protobuf \u2014 JSON mapping only. Generated locally; no network upload."
2877
+ ];
2878
+ const traceId = hexFrom2(`trace:${tree.runId}`, 16);
2879
+ const includeAttributes = options?.includeAttributes ?? false;
2880
+ const maxLen = options?.maxAttributeLength;
2881
+ const flat = flattenTree(tree);
2882
+ const spans = [];
2883
+ for (const n of flat) {
2884
+ const ev = n.event;
2885
+ const spanId = hexFrom2(`${tree.runId}:${ev.eventId}`, 8);
2886
+ const parentSpanId = ev.parentId ? hexFrom2(`${tree.runId}:${ev.parentId}`, 8) : void 0;
2887
+ const startNs = String(Math.round(ev.timestamp * 1e6));
2888
+ let endNs;
2889
+ if (ev.durationMs !== void 0 && Number.isFinite(ev.durationMs)) {
2890
+ endNs = String(Math.round(ev.timestamp * 1e6 + ev.durationMs * 1e6));
2891
+ }
2892
+ const attrs = [
2893
+ stringAttr("agent_inspect.kind", ev.kind),
2894
+ stringAttr("agent_inspect.confidence", ev.confidence),
2895
+ stringAttr("agent_inspect.source.type", ev.source.type),
2896
+ stringAttr("agent_inspect.run_id", tree.runId),
2897
+ stringAttr("agent_inspect.event_id", ev.eventId),
2898
+ stringAttr("agent_inspect.status", ev.status ?? "unset")
2899
+ ];
2900
+ if (ev.durationMs !== void 0) {
2901
+ attrs.push(intAttr("agent_inspect.duration_ms", ev.durationMs));
2902
+ }
2903
+ const op = genAiOperationName(ev.kind);
2904
+ if (op !== void 0) {
2905
+ attrs.push(stringAttr("gen_ai.operation.name", op));
2906
+ }
2907
+ const meta = ev.attributes;
2908
+ if (meta?.model !== void 0 && typeof meta.model === "string") {
2909
+ attrs.push(stringAttr("gen_ai.request.model", meta.model.slice(0, maxLen)));
2910
+ }
2911
+ const tokens = meta?.tokens;
2912
+ if (tokens && typeof tokens === "object" && tokens !== null) {
2913
+ const inp = tokens.input;
2914
+ const outp = tokens.output;
2915
+ if (typeof inp === "number") attrs.push(intAttr("gen_ai.usage.input_tokens", inp));
2916
+ if (typeof outp === "number") attrs.push(intAttr("gen_ai.usage.output_tokens", outp));
2917
+ }
2918
+ if (includeAttributes && meta && typeof meta === "object") {
2919
+ for (const [k, v] of Object.entries(meta)) {
2920
+ if (k === "tokens" || k === "model") continue;
2921
+ if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
2922
+ attrs.push(
2923
+ stringAttr(
2924
+ `agent_inspect.preview.${k}`,
2925
+ typeof v === "string" ? v.slice(0, maxLen) : String(v)
2926
+ )
2927
+ );
2928
+ }
2929
+ }
2930
+ }
2931
+ let statusCode = "STATUS_CODE_UNSET";
2932
+ let statusMessage;
2933
+ if (ev.status === "error") {
2934
+ statusCode = "STATUS_CODE_ERROR";
2935
+ statusMessage = meta && typeof meta.error === "object" && meta.error !== null ? String(meta.error.message ?? "error").slice(0, maxLen) : "error";
2936
+ } else if (ev.status === "ok") {
2937
+ statusCode = "STATUS_CODE_OK";
2938
+ }
2939
+ const spanJson = {
2940
+ traceId,
2941
+ spanId,
2942
+ name: ev.name,
2943
+ kind: "SPAN_KIND_INTERNAL",
2944
+ startTimeUnixNano: startNs,
2945
+ attributes: attrs,
2946
+ status: {
2947
+ code: statusCode,
2948
+ ...statusMessage !== void 0 ? { message: statusMessage } : {}
2949
+ }
2950
+ };
2951
+ if (parentSpanId !== void 0) {
2952
+ spanJson.parentSpanId = parentSpanId;
2953
+ }
2954
+ if (endNs !== void 0) {
2955
+ spanJson.endTimeUnixNano = endNs;
2956
+ }
2957
+ spans.push(spanJson);
2958
+ }
2959
+ const payload = {
2960
+ resourceSpans: [
2961
+ {
2962
+ resource: {
2963
+ attributes: [stringAttr("service.name", "agent-inspect")]
2964
+ },
2965
+ scopeSpans: [
2966
+ {
2967
+ scope: { name: "agent-inspect" },
2968
+ spans
2969
+ }
2970
+ ]
2971
+ }
2972
+ ]
2973
+ };
2974
+ return {
2975
+ format: "otlp-json",
2976
+ content: JSON.stringify(payload, null, 2 ),
2977
+ contentType: "application/json",
2978
+ fileExtension: ".otlp.json",
2979
+ warnings
2980
+ };
2981
+ }
2982
+
2983
+ // packages/core/src/exporters/validation.ts
2984
+ var EXPERIMENTAL = "Experimental compatibility export \u2014 verify against your target tooling before relying on it.";
2985
+ function validateExportContent(format, content) {
2986
+ const errors = [];
2987
+ const warnings = [EXPERIMENTAL];
2988
+ if (format === "markdown") {
2989
+ if (!content.startsWith("# AgentInspect Run")) {
2990
+ errors.push('Markdown export must start with "# AgentInspect Run"');
2991
+ }
2992
+ return { ok: errors.length === 0, format, errors, warnings };
2993
+ }
2994
+ if (format === "html") {
2995
+ const lower = content.toLowerCase();
2996
+ if (!lower.includes("<!doctype html")) {
2997
+ errors.push("HTML export must include <!doctype html>");
2998
+ }
2999
+ if (/<\s*script\b/i.test(content)) {
3000
+ errors.push("HTML export must not contain script tags");
3001
+ }
3002
+ if (/<\s*link\b[^>]*href\s*=/i.test(content)) {
3003
+ warnings.push("HTML export contains link tags \u2014 ensure no external stylesheets.");
3004
+ }
3005
+ return { ok: errors.length === 0, format, errors, warnings };
3006
+ }
3007
+ if (format === "openinference") {
3008
+ let parsed;
3009
+ try {
3010
+ parsed = JSON.parse(content);
3011
+ } catch {
3012
+ errors.push("OpenInference export is not valid JSON");
3013
+ return { ok: false, format, errors, warnings };
3014
+ }
3015
+ if (!parsed || typeof parsed !== "object") {
3016
+ errors.push("OpenInference export JSON must be an object");
3017
+ return { ok: false, format, errors, warnings };
3018
+ }
3019
+ const o = parsed;
3020
+ if (o.format !== "openinference") {
3021
+ errors.push('OpenInference export must include format: "openinference"');
3022
+ }
3023
+ if (!Array.isArray(o.spans)) {
3024
+ errors.push("OpenInference export must include a spans array");
3025
+ }
3026
+ warnings.push("OpenInference-compatible JSON is not guaranteed for every backend.");
3027
+ return { ok: errors.length === 0, format, errors, warnings };
3028
+ }
3029
+ if (format === "otlp-json") {
3030
+ let parsed;
3031
+ try {
3032
+ parsed = JSON.parse(content);
3033
+ } catch {
3034
+ errors.push("OTLP JSON export is not valid JSON");
3035
+ return { ok: false, format, errors, warnings };
3036
+ }
3037
+ if (!parsed || typeof parsed !== "object") {
3038
+ errors.push("OTLP JSON export must be an object");
3039
+ return { ok: false, format, errors, warnings };
3040
+ }
3041
+ const o = parsed;
3042
+ if (!Array.isArray(o.resourceSpans)) {
3043
+ errors.push("OTLP JSON export must include resourceSpans array");
3044
+ }
3045
+ warnings.push(
3046
+ "OTLP JSON mapping uses OTel GenAI-aligned attributes where applicable; collectors may require transformation."
3047
+ );
3048
+ return { ok: errors.length === 0, format, errors, warnings };
3049
+ }
3050
+ errors.push(`Unsupported export format`);
3051
+ return { ok: false, format, errors, warnings };
3052
+ }
3053
+
3054
+ // packages/core/src/exporters/manual-trace-adapter.ts
3055
+ function stepTypeToInspectKind(t) {
3056
+ switch (t) {
3057
+ case "llm":
3058
+ return "LLM";
3059
+ case "tool":
3060
+ return "TOOL";
3061
+ case "decision":
3062
+ return "DECISION";
3063
+ case "run":
3064
+ return "CHAIN";
3065
+ default:
3066
+ return "LOGIC";
3067
+ }
3068
+ }
3069
+ function mapStepStatus2(s) {
3070
+ if (s === void 0) return "running";
3071
+ if (s === "success") return "ok";
3072
+ return "error";
3073
+ }
3074
+ function manualTraceEventsToRunTree(events) {
3075
+ const started = events.find((e) => e.event === "run_started");
3076
+ if (!started || started.event !== "run_started") {
3077
+ throw new Error("Invalid trace: missing run_started");
3078
+ }
3079
+ const runId = started.runId;
3080
+ const runName = started.name;
3081
+ const completedAll = events.filter((e) => e.event === "run_completed");
3082
+ const lastCompleted = completedAll[completedAll.length - 1];
3083
+ let runStatus;
3084
+ if (lastCompleted === void 0) {
3085
+ runStatus = "running";
3086
+ } else if (lastCompleted.status === "success") {
3087
+ runStatus = "ok";
3088
+ } else {
3089
+ runStatus = "error";
3090
+ }
3091
+ const startedAt = started.startTime;
3092
+ const endedAt = lastCompleted !== void 0 && runStatus !== "running" ? lastCompleted.endTime : void 0;
3093
+ const durationMs = lastCompleted !== void 0 && Number.isFinite(lastCompleted.durationMs) ? lastCompleted.durationMs : void 0;
3094
+ const steps = /* @__PURE__ */ new Map();
3095
+ for (const e of events) {
3096
+ if (e.event !== "step_started") continue;
3097
+ const s = e;
3098
+ steps.set(s.stepId, {
3099
+ id: s.stepId,
3100
+ parentId: s.parentId,
3101
+ name: s.name,
3102
+ type: s.type,
3103
+ startTime: s.startTime,
3104
+ timestamp: s.timestamp,
3105
+ metadata: s.metadata
3106
+ });
3107
+ }
3108
+ for (const e of events) {
3109
+ if (e.event !== "step_completed") continue;
3110
+ const acc = steps.get(e.stepId);
3111
+ if (!acc) continue;
3112
+ acc.status = e.status;
3113
+ acc.endTime = e.endTime;
3114
+ acc.durationMs = e.durationMs;
3115
+ if (e.error?.message) {
3116
+ acc.error = e.error;
3117
+ }
3118
+ }
3119
+ const inspectNodes = /* @__PURE__ */ new Map();
3120
+ for (const acc of steps.values()) {
3121
+ const kind = stepTypeToInspectKind(acc.type);
3122
+ const status = mapStepStatus2(acc.status);
3123
+ const attrs = { ...acc.metadata ?? {} };
3124
+ if (acc.error?.message) {
3125
+ attrs.error = acc.error;
3126
+ }
3127
+ const evt = {
3128
+ eventId: acc.id,
3129
+ runId,
3130
+ parentId: acc.parentId,
3131
+ name: acc.name,
3132
+ kind,
3133
+ timestamp: acc.timestamp,
3134
+ status,
3135
+ durationMs: acc.durationMs,
3136
+ attributes: Object.keys(attrs).length > 0 ? attrs : void 0,
3137
+ confidence: "explicit",
3138
+ source: { type: "manual" }
3139
+ };
3140
+ inspectNodes.set(acc.id, { event: evt, children: [], depth: 0 });
3141
+ }
3142
+ const roots = [];
3143
+ const sortByStart = (a, b) => a.event.timestamp - b.event.timestamp;
3144
+ for (const node of inspectNodes.values()) {
3145
+ const pid = node.event.parentId;
3146
+ if (pid !== void 0 && inspectNodes.has(pid)) {
3147
+ inspectNodes.get(pid).children.push(node);
3148
+ } else {
3149
+ roots.push(node);
3150
+ }
3151
+ }
3152
+ roots.sort(sortByStart);
3153
+ for (const n of inspectNodes.values()) {
3154
+ n.children.sort(sortByStart);
3155
+ }
3156
+ const assignDepth = (n, depth) => {
3157
+ n.depth = depth;
3158
+ for (const c of n.children) assignDepth(c, depth + 1);
3159
+ };
3160
+ for (const r of roots) assignDepth(r, 0);
3161
+ const confidenceBreakdown = {
3162
+ explicit: 0,
3163
+ correlated: 0,
3164
+ heuristic: 0,
3165
+ unknown: 0
3166
+ };
3167
+ const kinds = zeroKinds();
3168
+ function countWalk(nodes) {
3169
+ for (const n of nodes) {
3170
+ confidenceBreakdown[n.event.confidence] += 1;
3171
+ kinds[n.event.kind] += 1;
3172
+ if (n.children.length > 0) countWalk(n.children);
3173
+ }
3174
+ }
3175
+ countWalk(roots);
3176
+ return {
3177
+ runId,
3178
+ name: runName,
3179
+ status: runStatus,
3180
+ startedAt,
3181
+ endedAt,
3182
+ durationMs,
3183
+ children: roots,
3184
+ metadata: {
3185
+ totalEvents: inspectNodes.size,
3186
+ confidenceBreakdown,
3187
+ kinds
3188
+ }
3189
+ };
3190
+ }
3191
+
3192
+ // packages/core/src/exporters/index.ts
3193
+ function mergeExportDefaults(options) {
3194
+ return {
3195
+ format: options.format,
3196
+ includeMetadata: options.includeMetadata ?? true,
3197
+ includeAttributes: options.includeAttributes ?? false,
3198
+ includeErrors: options.includeErrors ?? true,
3199
+ pretty: options.pretty,
3200
+ redacted: options.redacted,
3201
+ maxAttributeLength: options.maxAttributeLength
3202
+ };
3203
+ }
3204
+ function exportRunTree(tree, options) {
3205
+ const opts = mergeExportDefaults(options);
3206
+ switch (opts.format) {
3207
+ case "markdown":
3208
+ return exportMarkdown(tree, opts);
3209
+ case "html":
3210
+ return exportHtml(tree, opts);
3211
+ case "openinference":
3212
+ return exportOpenInference(tree, opts);
3213
+ case "otlp-json":
3214
+ return exportOtlpJson(tree, opts);
3215
+ default: {
3216
+ const _x = opts.format;
3217
+ throw new Error(`Unsupported export format: ${String(_x)}`);
3218
+ }
3219
+ }
3220
+ }
3221
+ function validateExport(result) {
3222
+ const base = validateExportContent(result.format, result.content);
3223
+ return {
3224
+ ok: base.ok,
3225
+ format: base.format,
3226
+ errors: base.errors,
3227
+ warnings: [...result.warnings, ...base.warnings]
3228
+ };
3229
+ }
3230
+
610
3231
  // packages/cli/src/list.ts
611
3232
  function parseLimit(raw) {
612
3233
  const fallback = 20;
@@ -615,95 +3236,247 @@ function parseLimit(raw) {
615
3236
  if (!Number.isFinite(n) || n <= 0) return fallback;
616
3237
  return Math.min(n, 100);
617
3238
  }
618
- function isStatusFilter(value) {
619
- return value === "running" || value === "success" || value === "error";
620
- }
621
- function buildRunSummary(runId, events) {
622
- const started = events.find(
623
- (e) => e.event === "run_started"
624
- );
625
- if (!started) return void 0;
626
- const completed = events.filter(
627
- (e) => e.event === "run_completed"
628
- );
629
- const last = completed[completed.length - 1];
630
- const status = last ? last.status : "running";
631
- const startTime = Number.isFinite(started.startTime) ? started.startTime : Number.isFinite(started.timestamp) ? started.timestamp : 0;
632
- const name = typeof started.name === "string" && started.name.trim() !== "" ? started.name.trim() : "unknown-run";
633
- return {
634
- runId,
635
- name,
636
- status,
637
- durationMs: last !== void 0 ? last.durationMs : void 0,
638
- startTime
639
- };
640
- }
641
3239
  function statusIcon(status) {
642
3240
  if (status === "success") return "\u2713";
643
3241
  if (status === "error") return "\u2717";
644
- return "\u23F3";
3242
+ if (status === "running") return "\u23F3";
3243
+ return "?";
645
3244
  }
646
3245
  function durationCell(status, durationMs) {
647
- if (status === "running") return "-";
3246
+ if (status === "running" || status === "unknown") return "-";
648
3247
  if (durationMs !== void 0 && Number.isFinite(durationMs)) {
649
- return formatDuration(durationMs);
3248
+ return formatDuration2(durationMs);
650
3249
  }
651
3250
  return "-";
652
3251
  }
653
- function timestampCell(startTime) {
654
- if (!Number.isFinite(startTime) || startTime <= 0) return "Invalid date";
655
- const s = formatTimestamp(startTime);
3252
+ function timestampCell(startedAt, createdAt) {
3253
+ const t = typeof startedAt === "number" && Number.isFinite(startedAt) && startedAt > 0 ? startedAt : createdAt instanceof Date ? createdAt.getTime() : NaN;
3254
+ const s = formatTimestamp(t);
656
3255
  return s === "Invalid date" ? "Invalid date" : s;
657
3256
  }
658
3257
  async function list(options = {}) {
659
3258
  try {
660
- const traceDir = typeof options.dir === "string" && options.dir.trim() !== "" ? options.dir.trim() : getDefaultTraceDir();
661
- const files = await listTraceFiles(traceDir);
3259
+ const traceDir = resolveTraceDir({ dir: options.dir });
3260
+ const td = new TraceDirectory({ dir: traceDir });
3261
+ if (typeof options.since === "string" && options.since.trim() !== "") {
3262
+ parseDuration(options.since.trim());
3263
+ }
3264
+ const files = await td.list();
3265
+ if (files.length === 0) {
3266
+ if (options.json) {
3267
+ console.log("[]");
3268
+ } else {
3269
+ console.log("No AgentInspect runs found");
3270
+ console.log(`Trace directory: ${traceDir}`);
3271
+ }
3272
+ return;
3273
+ }
3274
+ const metas = [];
3275
+ for (const fileName of files) {
3276
+ try {
3277
+ const filePath = td.getPath(fileName);
3278
+ const meta = await extractMetadata(filePath);
3279
+ metas.push(meta);
3280
+ } catch {
3281
+ }
3282
+ }
3283
+ if (metas.length === 0) {
3284
+ if (options.json) {
3285
+ console.log("[]");
3286
+ } else {
3287
+ console.log("No AgentInspect runs found");
3288
+ console.log(`Trace directory: ${traceDir}`);
3289
+ }
3290
+ return;
3291
+ }
3292
+ const limit = parseLimit(options.limit);
3293
+ const filtered = filterTraces(metas, {
3294
+ status: options.status,
3295
+ name: options.name,
3296
+ since: options.since,
3297
+ limit
3298
+ });
3299
+ const shown = filtered.slice(0, limit);
3300
+ if (options.json) {
3301
+ console.log(JSON.stringify(shown, null, 2));
3302
+ return;
3303
+ }
3304
+ console.log("Recent AgentInspect Runs");
3305
+ for (const s of shown) {
3306
+ const icon = statusIcon(s.status);
3307
+ const dur = durationCell(s.status, s.durationMs);
3308
+ const ts = timestampCell(s.startedAt, s.createdAt);
3309
+ const nm = truncateName(s.name ?? "unnamed", 80);
3310
+ console.log(`${icon} ${s.runId} | ${nm} | ${dur} | ${ts}`);
3311
+ }
3312
+ console.log("");
3313
+ console.log(`Showing ${shown.length} of ${filtered.length} runs`);
3314
+ console.log(`Trace directory: ${traceDir}`);
3315
+ } catch (e) {
3316
+ const msg = e instanceof Error ? e.message : String(e);
3317
+ console.error(`[AgentInspect] list failed: ${msg}`);
3318
+ process.exitCode = 1;
3319
+ }
3320
+ }
3321
+ function parseKeep(raw) {
3322
+ const trimmed = typeof raw === "string" ? raw.trim() : "";
3323
+ if (trimmed === "") {
3324
+ throw new Error(`Invalid --keep value: ${raw}. Provide a positive integer.`);
3325
+ }
3326
+ const n = Number.parseInt(trimmed, 10);
3327
+ if (!Number.isFinite(n) || n <= 0) {
3328
+ throw new Error(`Invalid --keep value: ${raw}. Provide a positive integer.`);
3329
+ }
3330
+ return n;
3331
+ }
3332
+ function basisTimeMs(meta) {
3333
+ const started = typeof meta.startedAt === "number" ? meta.startedAt : void 0;
3334
+ const t = started ?? meta.createdAt.getTime();
3335
+ return Number.isFinite(t) ? t : 0;
3336
+ }
3337
+ function stableSortNewestFirst(a, b) {
3338
+ const dt = basisTimeMs(b) - basisTimeMs(a);
3339
+ if (dt !== 0) return dt;
3340
+ return a.runId.localeCompare(b.runId);
3341
+ }
3342
+ async function confirmDeletion(count) {
3343
+ const { createInterface: createInterface2 } = await import('readline/promises');
3344
+ const rl = createInterface2({ input: stdin, output: stdout });
3345
+ try {
3346
+ const answer = await rl.question(
3347
+ `Delete ${count} AgentInspect trace file(s)? Type "yes" to continue: `
3348
+ );
3349
+ return answer.trim() === "yes";
3350
+ } finally {
3351
+ rl.close();
3352
+ }
3353
+ }
3354
+ async function clean(options = {}) {
3355
+ try {
3356
+ const hasOlder = typeof options.olderThan === "string" && options.olderThan.trim() !== "";
3357
+ const hasKeep = typeof options.keep === "string" && options.keep.trim() !== "";
3358
+ if (!hasOlder && !hasKeep) {
3359
+ console.error("clean requires either --older-than <duration> or --keep <count>");
3360
+ process.exitCode = 1;
3361
+ return;
3362
+ }
3363
+ if (hasOlder && hasKeep) {
3364
+ console.error("Use either --older-than or --keep (not both).");
3365
+ process.exitCode = 1;
3366
+ return;
3367
+ }
3368
+ if (hasOlder) {
3369
+ parseDuration(options.olderThan.trim());
3370
+ }
3371
+ if (hasKeep) {
3372
+ parseKeep(options.keep);
3373
+ }
3374
+ const traceDir = resolveTraceDir({ dir: options.dir });
3375
+ const td = new TraceDirectory({ dir: traceDir });
3376
+ const files = await td.list();
662
3377
  if (files.length === 0) {
663
- console.log("No AgentInspect runs found");
3378
+ console.log("No runs to clean.");
3379
+ console.log(`Trace directory: ${traceDir}`);
3380
+ return;
3381
+ }
3382
+ const verified = [];
3383
+ const skipped = [];
3384
+ for (const fileName of files) {
3385
+ const filePath = td.getPath(fileName);
3386
+ const ok = await isAgentInspectTrace(filePath);
3387
+ if (!ok) {
3388
+ skipped.push(fileName);
3389
+ continue;
3390
+ }
3391
+ try {
3392
+ verified.push(await extractMetadata(filePath));
3393
+ } catch {
3394
+ skipped.push(fileName);
3395
+ }
3396
+ }
3397
+ if (verified.length === 0) {
3398
+ console.log("No runs to clean.");
3399
+ console.log(`Trace directory: ${traceDir}`);
3400
+ if (skipped.length > 0) {
3401
+ console.log("");
3402
+ console.log(`Skipped ${skipped.length} non-AgentInspect file(s).`);
3403
+ }
3404
+ return;
3405
+ }
3406
+ let toDelete = [];
3407
+ if (hasOlder) {
3408
+ const windowMs = parseDuration(options.olderThan.trim());
3409
+ const cutoff = Date.now() - windowMs;
3410
+ toDelete = verified.filter((m) => basisTimeMs(m) < cutoff).sort(stableSortNewestFirst);
3411
+ } else {
3412
+ const keepN = parseKeep(options.keep);
3413
+ const sorted = [...verified].sort(stableSortNewestFirst);
3414
+ toDelete = sorted.slice(keepN);
3415
+ }
3416
+ if (toDelete.length === 0) {
3417
+ console.log("No runs to clean.");
664
3418
  console.log(`Trace directory: ${traceDir}`);
3419
+ if (skipped.length > 0) {
3420
+ console.log("");
3421
+ console.log(`Skipped ${skipped.length} non-AgentInspect file(s).`);
3422
+ }
3423
+ return;
3424
+ }
3425
+ if (options.dryRun) {
3426
+ console.log(`Would delete ${toDelete.length} run(s):`);
3427
+ for (const m of toDelete) {
3428
+ console.log(`- ${m.filePath}`);
3429
+ }
3430
+ if (skipped.length > 0) {
3431
+ console.log("");
3432
+ console.log(`Skipped ${skipped.length} non-AgentInspect file(s).`);
3433
+ }
665
3434
  return;
666
3435
  }
667
- const summaries = [];
668
- for (const fileName of files) {
3436
+ if (options.yes !== true) {
3437
+ if (stdin.isTTY !== true) {
3438
+ console.error(
3439
+ "Refusing to delete without --yes in a non-interactive terminal."
3440
+ );
3441
+ process.exitCode = 1;
3442
+ return;
3443
+ }
3444
+ const ok = await confirmDeletion(toDelete.length);
3445
+ if (!ok) {
3446
+ console.log("Cancelled.");
3447
+ return;
3448
+ }
3449
+ }
3450
+ let deleted = 0;
3451
+ for (const m of toDelete) {
3452
+ const ok = await isAgentInspectTrace(m.filePath);
3453
+ if (!ok) {
3454
+ skipped.push(m.filePath);
3455
+ continue;
3456
+ }
669
3457
  try {
670
- const runId = getRunIdFromTraceFileName(fileName);
671
- if (runId === void 0) continue;
672
- const events = await readTraceEvents(runId, traceDir);
673
- const row = buildRunSummary(runId, events);
674
- if (row !== void 0) summaries.push(row);
3458
+ await unlink(m.filePath);
3459
+ deleted += 1;
675
3460
  } catch {
676
3461
  }
677
3462
  }
678
- if (summaries.length === 0) {
679
- console.log("No AgentInspect runs found");
680
- console.log(`Trace directory: ${traceDir}`);
681
- return;
682
- }
683
- summaries.sort((a, b) => b.startTime - a.startTime);
684
- const statusFilter = isStatusFilter(options.status) ? options.status : void 0;
685
- const filtered = statusFilter === void 0 ? summaries : summaries.filter((s) => s.status === statusFilter);
686
- const limit = parseLimit(options.limit);
687
- const shown = filtered.slice(0, limit);
688
- console.log("Recent AgentInspect Runs");
689
- for (const s of shown) {
690
- const icon = statusIcon(s.status);
691
- const dur = durationCell(s.status, s.durationMs);
692
- const ts = timestampCell(s.startTime);
693
- const nm = truncateName(s.name, 80);
694
- console.log(`${icon} ${s.runId} | ${nm} | ${dur} | ${ts}`);
695
- }
696
- console.log("");
697
- console.log(`Showing ${shown.length} of ${filtered.length} runs`);
3463
+ console.log(`Deleted ${deleted} run(s).`);
698
3464
  console.log(`Trace directory: ${traceDir}`);
3465
+ if (skipped.length > 0) {
3466
+ console.log("");
3467
+ console.log(`Skipped ${skipped.length} non-AgentInspect file(s).`);
3468
+ }
699
3469
  } catch (e) {
700
3470
  const msg = e instanceof Error ? e.message : String(e);
701
- console.error(`[AgentInspect] list failed: ${msg}`);
3471
+ console.error(`[AgentInspect] clean failed: ${msg}`);
702
3472
  process.exitCode = 1;
703
3473
  }
704
3474
  }
705
3475
 
706
3476
  // packages/cli/src/view.ts
3477
+ function isModuleNotFound(e) {
3478
+ return e !== null && typeof e === "object" && "code" in e && e.code === "ERR_MODULE_NOT_FOUND";
3479
+ }
707
3480
  function buildStepTree(events) {
708
3481
  const nodes = /* @__PURE__ */ new Map();
709
3482
  for (const e of events) {
@@ -776,6 +3549,60 @@ function printStepTree(nodes, depth, verbose) {
776
3549
  printStepTree(node.children, depth + 1, verbose);
777
3550
  }
778
3551
  }
3552
+ function pickMode(options) {
3553
+ if (options.summary) return "summary";
3554
+ if (options.metadata) return "metadata";
3555
+ if (options.errorsOnly) return "errors-only";
3556
+ return "tree";
3557
+ }
3558
+ function printSummary(summary) {
3559
+ console.log("Run Summary");
3560
+ console.log(`ID: ${summary.runId}`);
3561
+ console.log(`Name: ${summary.name ?? "unnamed"}`);
3562
+ console.log(`Status: ${summary.status}`);
3563
+ console.log(
3564
+ `Duration: ${summary.durationMs !== void 0 ? formatDuration2(summary.durationMs) : "-"}`
3565
+ );
3566
+ console.log(`Total steps: ${summary.totalSteps}`);
3567
+ console.log(`LLM steps: ${summary.llmSteps}`);
3568
+ console.log(`Tool steps: ${summary.toolSteps}`);
3569
+ console.log(`Logic steps: ${summary.logicSteps}`);
3570
+ console.log(`Error steps: ${summary.errorSteps}`);
3571
+ console.log(`Max depth: ${summary.maxDepth}`);
3572
+ if (summary.longestStep) {
3573
+ console.log(
3574
+ `Longest step: ${summary.longestStep.name} (${formatDuration2(
3575
+ summary.longestStep.durationMs
3576
+ )}, ${summary.longestStep.type})`
3577
+ );
3578
+ }
3579
+ }
3580
+ function printMetadata(meta) {
3581
+ console.log("Trace Metadata");
3582
+ console.log(`ID: ${meta.runId}`);
3583
+ console.log(`Name: ${meta.name ?? "unnamed"}`);
3584
+ console.log(`Status: ${meta.status}`);
3585
+ console.log(
3586
+ `Started: ${meta.startedAt !== void 0 ? formatTimestamp(meta.startedAt) : "-"}`
3587
+ );
3588
+ console.log(
3589
+ `Ended: ${meta.endedAt !== void 0 ? formatTimestamp(meta.endedAt) : "-"}`
3590
+ );
3591
+ console.log(
3592
+ `Duration: ${meta.durationMs !== void 0 ? formatDuration2(meta.durationMs) : "-"}`
3593
+ );
3594
+ console.log(`Event count: ${meta.eventCount}`);
3595
+ console.log(`File path: ${meta.filePath}`);
3596
+ console.log(`File size: ${meta.fileSize}`);
3597
+ console.log(`Created at: ${meta.createdAt.toISOString()}`);
3598
+ }
3599
+ function filterErrorEvents(events) {
3600
+ return events.filter((e) => {
3601
+ if (e.event === "run_completed") return e.status === "error";
3602
+ if (e.event === "step_completed") return e.status === "error";
3603
+ return false;
3604
+ });
3605
+ }
779
3606
  async function view(runId, options = {}) {
780
3607
  try {
781
3608
  const id = typeof runId === "string" && runId.trim() !== "" ? runId.trim() : "";
@@ -784,7 +3611,38 @@ async function view(runId, options = {}) {
784
3611
  process.exitCode = 1;
785
3612
  return;
786
3613
  }
787
- const traceDir = typeof options.dir === "string" && options.dir.trim() !== "" ? options.dir.trim() : getDefaultTraceDir();
3614
+ if (options.tui) {
3615
+ const conflict = options.json || options.summary || options.metadata || options.errorsOnly;
3616
+ if (conflict) {
3617
+ console.error(
3618
+ "--tui cannot be combined with --json, --summary, --metadata, or --errors-only"
3619
+ );
3620
+ process.exitCode = 1;
3621
+ return;
3622
+ }
3623
+ try {
3624
+ const mod = await import('@agent-inspect/tui');
3625
+ await mod.runTraceViewer({ runId: id, dir: options.dir });
3626
+ } catch (e) {
3627
+ const msg = e instanceof Error ? e.message : String(e);
3628
+ if (msg.includes("interactive terminal")) {
3629
+ console.error(msg);
3630
+ process.exitCode = 1;
3631
+ return;
3632
+ }
3633
+ if (isModuleNotFound(e)) {
3634
+ console.error(
3635
+ "TUI support is optional. Install @agent-inspect/tui to use --tui."
3636
+ );
3637
+ process.exitCode = 1;
3638
+ return;
3639
+ }
3640
+ console.error(`[AgentInspect] TUI failed: ${msg}`);
3641
+ process.exitCode = 1;
3642
+ }
3643
+ return;
3644
+ }
3645
+ const traceDir = resolveTraceDir({ dir: options.dir });
788
3646
  const events = await readTraceEvents(id, traceDir);
789
3647
  if (events.length === 0) {
790
3648
  console.log(`Run not found: ${id}`);
@@ -792,6 +3650,38 @@ async function view(runId, options = {}) {
792
3650
  process.exitCode = 1;
793
3651
  return;
794
3652
  }
3653
+ const mode = pickMode(options);
3654
+ const filePath = getTraceFilePath(id, traceDir);
3655
+ if (mode === "summary") {
3656
+ const summary = buildRunSummary(events);
3657
+ if (options.json) {
3658
+ console.log(JSON.stringify(summary, null, 2));
3659
+ } else {
3660
+ printSummary(summary);
3661
+ }
3662
+ return;
3663
+ }
3664
+ if (mode === "metadata") {
3665
+ const meta = await extractMetadata(filePath);
3666
+ if (options.json) {
3667
+ console.log(JSON.stringify(meta, null, 2));
3668
+ } else {
3669
+ printMetadata(meta);
3670
+ }
3671
+ return;
3672
+ }
3673
+ if (mode === "errors-only") {
3674
+ const errEvents = filterErrorEvents(events);
3675
+ if (options.json) {
3676
+ console.log(JSON.stringify(errEvents, null, 2));
3677
+ } else if (errEvents.length === 0) {
3678
+ console.log("No errors found in trace");
3679
+ } else {
3680
+ console.log("Error events");
3681
+ console.log(JSON.stringify(errEvents, null, 2));
3682
+ }
3683
+ return;
3684
+ }
795
3685
  if (options.json) {
796
3686
  console.log(JSON.stringify(events, null, 2));
797
3687
  return;
@@ -809,7 +3699,7 @@ async function view(runId, options = {}) {
809
3699
  );
810
3700
  const last = completed[completed.length - 1];
811
3701
  const status = last ? last.status : "running";
812
- const durationLine = last !== void 0 && Number.isFinite(last.durationMs) ? formatDuration(last.durationMs) : "-";
3702
+ const durationLine = last !== void 0 && Number.isFinite(last.durationMs) ? formatDuration2(last.durationMs) : "-";
813
3703
  const startedTs = Number.isFinite(started.startTime) ? started.startTime : started.timestamp;
814
3704
  const startedLabel = formatTimestamp(startedTs);
815
3705
  console.log(`AgentInspect Run: ${started.name}`);
@@ -826,7 +3716,7 @@ async function view(runId, options = {}) {
826
3716
  printStepTree(tree, 0, options.verbose === true);
827
3717
  }
828
3718
  console.log("");
829
- console.log(`Trace file: ${getTraceFilePath(id, traceDir)}`);
3719
+ console.log(`Trace file: ${filePath}`);
830
3720
  } catch (e) {
831
3721
  const msg = e instanceof Error ? e.message : String(e);
832
3722
  console.error(`[AgentInspect] view failed: ${msg}`);
@@ -834,6 +3724,535 @@ async function view(runId, options = {}) {
834
3724
  }
835
3725
  }
836
3726
 
3727
+ // packages/cli/src/logs.ts
3728
+ function parseRunIdKeys(raw) {
3729
+ if (typeof raw !== "string") return void 0;
3730
+ const parts = raw.split(",").map((s) => s.trim()).filter((s) => s !== "");
3731
+ return parts.length > 0 ? parts : void 0;
3732
+ }
3733
+ function summarizeWarnings(warnings) {
3734
+ const out = {};
3735
+ for (const w of warnings) {
3736
+ out[w.code] = (out[w.code] ?? 0) + 1;
3737
+ }
3738
+ return out;
3739
+ }
3740
+ function formatWarningLine(w) {
3741
+ const loc = w.line !== void 0 ? `line ${w.line}` : w.file ? "file" : "unknown";
3742
+ return `- ${loc} ${w.code}: ${w.message}`;
3743
+ }
3744
+ async function logs(filePath, options = {}) {
3745
+ try {
3746
+ const fp = typeof filePath === "string" ? filePath.trim() : "";
3747
+ if (fp === "") {
3748
+ console.error("Log file path is required");
3749
+ process.exitCode = 1;
3750
+ return;
3751
+ }
3752
+ const warningsMode = options.warnings ?? "summary";
3753
+ if (warningsMode !== "none" && warningsMode !== "summary" && warningsMode !== "all") {
3754
+ console.error(`Invalid --warnings value: ${String(options.warnings)}`);
3755
+ process.exitCode = 1;
3756
+ return;
3757
+ }
3758
+ const res = await parseLogsToTrees(fp, {
3759
+ format: options.format ?? "auto",
3760
+ configPath: options.config,
3761
+ runIdKeys: parseRunIdKeys(options.runIdKey),
3762
+ eventKey: options.eventKey,
3763
+ timestampKey: options.timestampKey,
3764
+ messageKey: options.messageKey,
3765
+ levelKey: options.levelKey,
3766
+ parentIdKey: options.parentIdKey,
3767
+ durationKey: options.durationKey,
3768
+ statusKey: options.statusKey,
3769
+ warnings: warningsMode
3770
+ });
3771
+ const summary = {
3772
+ runs: res.trees.length,
3773
+ events: res.events.length,
3774
+ warnings: res.warnings.length
3775
+ };
3776
+ const hasOutput = res.events.length > 0 && res.trees.length > 0;
3777
+ if (options.json) {
3778
+ const payload = warningsMode === "none" ? { events: res.events, trees: res.trees, warnings: [], summary } : { ...res, summary };
3779
+ console.log(JSON.stringify(payload, null, 2));
3780
+ if (!hasOutput) process.exitCode = 1;
3781
+ return;
3782
+ }
3783
+ if (!hasOutput) {
3784
+ console.error("No valid events found.");
3785
+ process.exitCode = 1;
3786
+ } else {
3787
+ const treeText = renderRunTrees(res.trees, {
3788
+ summary: options.summary ?? true,
3789
+ showConfidence: "always"
3790
+ });
3791
+ console.log(treeText);
3792
+ }
3793
+ if (warningsMode === "none") return;
3794
+ const warnings = res.warnings;
3795
+ const counts = summarizeWarnings(warnings);
3796
+ console.log("");
3797
+ console.log("Warnings:");
3798
+ console.log(` Total: ${warnings.length}`);
3799
+ if (warningsMode === "summary") {
3800
+ for (const [code, count] of Object.entries(counts)) {
3801
+ console.log(` ${code}: ${count}`);
3802
+ }
3803
+ return;
3804
+ }
3805
+ for (const w of warnings) {
3806
+ console.log(formatWarningLine(w));
3807
+ }
3808
+ } catch (e) {
3809
+ const msg = e instanceof Error ? e.message : String(e);
3810
+ console.error(`[AgentInspect] logs failed: ${msg}`);
3811
+ process.exitCode = 1;
3812
+ }
3813
+ }
3814
+ function parseRunIdKeys2(raw) {
3815
+ if (typeof raw !== "string") return void 0;
3816
+ const parts = raw.split(",").map((s) => s.trim()).filter((s) => s !== "");
3817
+ return parts.length > 0 ? parts : void 0;
3818
+ }
3819
+ function parseRefreshMs(raw) {
3820
+ const fallback = 250;
3821
+ if (raw === void 0 || raw.trim() === "") return fallback;
3822
+ const n = Number.parseInt(raw.trim(), 10);
3823
+ if (!Number.isFinite(n) || n <= 0) {
3824
+ throw new Error(`Invalid --refresh value: ${raw}. Provide a positive integer (ms).`);
3825
+ }
3826
+ return n;
3827
+ }
3828
+ function summarizeWarnings2(warnings) {
3829
+ const out = {};
3830
+ for (const w of warnings) {
3831
+ out[w.code] = (out[w.code] ?? 0) + 1;
3832
+ }
3833
+ return out;
3834
+ }
3835
+ function formatWarningLine2(w) {
3836
+ const loc = w.line !== void 0 ? `line ${w.line}` : w.file ? "file" : "unknown";
3837
+ return `- ${loc} ${w.code}: ${w.message}`;
3838
+ }
3839
+ function clearScreen() {
3840
+ process.stdout.write("\x1B[2J\x1B[0f");
3841
+ }
3842
+ function sleep(ms) {
3843
+ return new Promise((resolve) => setTimeout(resolve, ms));
3844
+ }
3845
+ async function* readStdinLines() {
3846
+ const { createInterface: createInterface2 } = await import('readline');
3847
+ const rl = createInterface2({ input: stdin, crlfDelay: Infinity });
3848
+ try {
3849
+ for await (const line of rl) {
3850
+ yield line;
3851
+ }
3852
+ } finally {
3853
+ rl.close();
3854
+ }
3855
+ }
3856
+ async function readFileOnce(filePath, onLine) {
3857
+ const fh = await open(filePath, "r");
3858
+ try {
3859
+ const text = await fh.readFile({ encoding: "utf-8" });
3860
+ const lines = text.split(/\r?\n/);
3861
+ for (let i = 0; i < lines.length; i++) {
3862
+ const line = lines[i] ?? "";
3863
+ onLine(line, i + 1);
3864
+ }
3865
+ } finally {
3866
+ await fh.close();
3867
+ }
3868
+ }
3869
+ async function followFile(filePath, options, onLine, shouldStop) {
3870
+ let pos = 0;
3871
+ let lineNumber = 0;
3872
+ let carry = "";
3873
+ const st = await stat(filePath);
3874
+ if (options.once) {
3875
+ await readFileOnce(filePath, onLine);
3876
+ return;
3877
+ }
3878
+ pos = st.size;
3879
+ while (!shouldStop()) {
3880
+ let next;
3881
+ try {
3882
+ next = await stat(filePath);
3883
+ } catch (e) {
3884
+ const msg = e instanceof Error ? e.message : String(e);
3885
+ throw new Error(`Failed to stat file: ${filePath} (${msg})`);
3886
+ }
3887
+ if (next.size > pos) {
3888
+ const fh = await open(filePath, "r");
3889
+ try {
3890
+ const len = next.size - pos;
3891
+ const buf = Buffer.allocUnsafe(Number(len));
3892
+ const { bytesRead } = await fh.read(buf, 0, buf.length, pos);
3893
+ pos += bytesRead;
3894
+ const chunk = carry + buf.toString("utf-8", 0, bytesRead);
3895
+ const parts = chunk.split(/\r?\n/);
3896
+ carry = parts.pop() ?? "";
3897
+ for (const p of parts) {
3898
+ lineNumber += 1;
3899
+ onLine(p, lineNumber);
3900
+ }
3901
+ } finally {
3902
+ await fh.close();
3903
+ }
3904
+ }
3905
+ await sleep(options.refreshMs);
3906
+ }
3907
+ }
3908
+ async function tail(options = {}) {
3909
+ try {
3910
+ const warningsMode = options.warnings ?? "summary";
3911
+ if (warningsMode !== "none" && warningsMode !== "summary" && warningsMode !== "all") {
3912
+ console.error(`Invalid --warnings value: ${String(options.warnings)}`);
3913
+ process.exitCode = 1;
3914
+ return;
3915
+ }
3916
+ const refreshMs = parseRefreshMs(options.refresh);
3917
+ const cfgBase = await loadLogIngestConfig(options.config);
3918
+ const override = {};
3919
+ const runIdKeys = parseRunIdKeys2(options.runIdKey);
3920
+ if (runIdKeys !== void 0) override.runIdKeys = runIdKeys;
3921
+ for (const k of [
3922
+ "eventKey",
3923
+ "timestampKey",
3924
+ "messageKey",
3925
+ "levelKey",
3926
+ "parentIdKey",
3927
+ "durationKey",
3928
+ "statusKey"
3929
+ ]) {
3930
+ const v = options[k];
3931
+ if (v !== void 0) override[k] = v;
3932
+ }
3933
+ const config = mergeLogIngestConfig(cfgBase, override);
3934
+ const format = options.format ?? "auto";
3935
+ const filePath = typeof options.file === "string" && options.file.trim() !== "" ? options.file.trim() : void 0;
3936
+ const acc = new LiveLogAccumulator({
3937
+ config,
3938
+ format,
3939
+ file: filePath ?? "stdin"
3940
+ });
3941
+ const isTty = Boolean(process.stdout.isTTY);
3942
+ const doClear = isTty && options.noClear !== true;
3943
+ let stop = false;
3944
+ const shouldStop = () => stop;
3945
+ const onSigInt = () => {
3946
+ stop = true;
3947
+ };
3948
+ process.once("SIGINT", onSigInt);
3949
+ let dirty = false;
3950
+ let lastRenderedKey = "";
3951
+ let waitingShown = false;
3952
+ const renderNow = () => {
3953
+ const events = acc.getEvents();
3954
+ const trees = acc.getTrees();
3955
+ const warnings = acc.getWarnings();
3956
+ const summary = {
3957
+ runs: trees.length,
3958
+ events: events.length,
3959
+ warnings: warnings.length
3960
+ };
3961
+ if (options.json) {
3962
+ const payload = warningsMode === "none" ? { events, trees, warnings: [], summary } : { events, trees, warnings, summary };
3963
+ process.stdout.write(JSON.stringify(payload) + "\n");
3964
+ return;
3965
+ }
3966
+ if (doClear) clearScreen();
3967
+ if (!waitingShown && events.length === 0 && isTty) {
3968
+ console.log("Waiting for logs...");
3969
+ waitingShown = true;
3970
+ }
3971
+ if (trees.length > 0) {
3972
+ const text = renderRunTrees(trees, { summary: true, showConfidence: "always" });
3973
+ console.log(text);
3974
+ }
3975
+ if (warningsMode === "none") return;
3976
+ console.log("");
3977
+ console.log("Warnings:");
3978
+ console.log(` Total: ${warnings.length}`);
3979
+ if (warningsMode === "summary") {
3980
+ const counts = summarizeWarnings2(warnings);
3981
+ for (const [code, count] of Object.entries(counts)) {
3982
+ console.log(` ${code}: ${count}`);
3983
+ }
3984
+ } else {
3985
+ for (const w of warnings) console.log(formatWarningLine2(w));
3986
+ }
3987
+ };
3988
+ const renderLoop = async () => {
3989
+ while (!shouldStop()) {
3990
+ if (dirty) {
3991
+ const key = `${acc.getEvents().length}:${acc.getWarnings().length}:${acc.getTrees().length}`;
3992
+ if (key !== lastRenderedKey) {
3993
+ lastRenderedKey = key;
3994
+ renderNow();
3995
+ }
3996
+ dirty = false;
3997
+ }
3998
+ await sleep(refreshMs);
3999
+ }
4000
+ };
4001
+ const renderTask = renderLoop();
4002
+ const onLine = (line, lineNumber) => {
4003
+ acc.pushLine(line, lineNumber);
4004
+ dirty = true;
4005
+ };
4006
+ let endedNaturally = false;
4007
+ if (filePath) {
4008
+ try {
4009
+ await stat(filePath);
4010
+ } catch (e) {
4011
+ const msg = e instanceof Error ? e.message : String(e);
4012
+ console.error(`Log file does not exist: ${filePath} (${msg})`);
4013
+ process.exitCode = 1;
4014
+ stop = true;
4015
+ return;
4016
+ }
4017
+ await followFile(
4018
+ filePath,
4019
+ { refreshMs, once: options.once === true },
4020
+ onLine,
4021
+ shouldStop
4022
+ );
4023
+ endedNaturally = options.once === true;
4024
+ } else {
4025
+ let lineNumber = 0;
4026
+ for await (const line of readStdinLines()) {
4027
+ lineNumber += 1;
4028
+ onLine(line, lineNumber);
4029
+ if (shouldStop()) break;
4030
+ }
4031
+ endedNaturally = !shouldStop();
4032
+ stop = true;
4033
+ }
4034
+ dirty = true;
4035
+ renderNow();
4036
+ if (endedNaturally && acc.getEvents().length === 0) {
4037
+ if (!options.json) {
4038
+ console.error("No valid events found.");
4039
+ }
4040
+ process.exitCode = 1;
4041
+ }
4042
+ stop = true;
4043
+ await renderTask;
4044
+ } catch (e) {
4045
+ const msg = e instanceof Error ? e.message : String(e);
4046
+ console.error(`[AgentInspect] tail failed: ${msg}`);
4047
+ process.exitCode = 1;
4048
+ }
4049
+ }
4050
+ function parseExportFormat(s) {
4051
+ const v = (s ?? "markdown").trim().toLowerCase();
4052
+ if (v === "markdown" || v === "html" || v === "openinference" || v === "otlp-json") {
4053
+ return v;
4054
+ }
4055
+ throw new Error(
4056
+ `Unsupported --format "${s ?? ""}". Use markdown, html, openinference, or otlp-json.`
4057
+ );
4058
+ }
4059
+ async function exportCommand(runId, options = {}) {
4060
+ const id = typeof runId === "string" && runId.trim() !== "" ? runId.trim() : "";
4061
+ if (id === "") {
4062
+ console.error("Run id is required");
4063
+ process.exitCode = 1;
4064
+ return;
4065
+ }
4066
+ let format;
4067
+ try {
4068
+ format = parseExportFormat(options.format);
4069
+ } catch (e) {
4070
+ const msg = e instanceof Error ? e.message : String(e);
4071
+ console.error(msg);
4072
+ process.exitCode = 1;
4073
+ return;
4074
+ }
4075
+ const traceDir = resolveTraceDir({ dir: options.dir });
4076
+ let events;
4077
+ try {
4078
+ events = await readTraceEvents(id, traceDir);
4079
+ } catch (e) {
4080
+ const msg = e instanceof Error ? e.message : String(e);
4081
+ console.error(`[AgentInspect] export failed: ${msg}`);
4082
+ process.exitCode = 1;
4083
+ return;
4084
+ }
4085
+ if (events.length === 0) {
4086
+ console.error(`Run not found or trace is empty: ${id}
4087
+ Trace directory: ${traceDir}`);
4088
+ process.exitCode = 1;
4089
+ return;
4090
+ }
4091
+ let tree;
4092
+ try {
4093
+ tree = manualTraceEventsToRunTree(events);
4094
+ } catch (e) {
4095
+ const msg = e instanceof Error ? e.message : String(e);
4096
+ console.error(`[AgentInspect] export failed: ${msg}`);
4097
+ process.exitCode = 1;
4098
+ return;
4099
+ }
4100
+ const exportOpts = {
4101
+ format,
4102
+ includeMetadata: options.noMetadata === true ? false : true,
4103
+ includeAttributes: options.includeAttributes === true,
4104
+ includeErrors: options.noErrors === true ? false : true,
4105
+ pretty: true,
4106
+ redacted: true,
4107
+ maxAttributeLength: 500
4108
+ };
4109
+ const result = exportRunTree(tree, exportOpts);
4110
+ const validation = options.validate === true ? validateExport(result) : void 0;
4111
+ if (validation !== void 0 && !validation.ok) {
4112
+ process.exitCode = 1;
4113
+ }
4114
+ const outPath = options.output !== void 0 && options.output.trim() !== "" ? path.resolve(options.output.trim()) : void 0;
4115
+ if (outPath !== void 0) {
4116
+ await mkdir(path.dirname(outPath), { recursive: true });
4117
+ await writeFile(outPath, result.content, "utf-8");
4118
+ const vlabel = validation !== void 0 ? validation.ok ? "ok" : "failed" : "skipped";
4119
+ console.log(`Wrote ${result.fileExtension} export to ${outPath} (validation: ${vlabel})`);
4120
+ if (validation !== void 0 && !validation.ok) {
4121
+ console.error("Validation errors:", validation.errors.join("; "));
4122
+ }
4123
+ }
4124
+ if (options.json === true) {
4125
+ const payload = {
4126
+ format: result.format,
4127
+ contentType: result.contentType,
4128
+ fileExtension: result.fileExtension,
4129
+ warnings: [...result.warnings, ...validation?.warnings ?? []],
4130
+ validation
4131
+ };
4132
+ if (outPath === void 0) {
4133
+ payload.content = result.content;
4134
+ }
4135
+ console.log(JSON.stringify(payload, null, 2));
4136
+ if (validation !== void 0 && !validation.ok) {
4137
+ console.error("Validation errors:", validation.errors.join("; "));
4138
+ }
4139
+ } else if (outPath === void 0) {
4140
+ console.log(result.content);
4141
+ if (options.validate === true && validation !== void 0) {
4142
+ if (validation.ok) {
4143
+ console.error(`Validation: ok (${validation.warnings.length} warning(s))`);
4144
+ } else {
4145
+ console.error("Validation failed:", validation.errors.join("; "));
4146
+ }
4147
+ if (validation.warnings.length > 0) {
4148
+ console.error("Warnings:", validation.warnings.join("; "));
4149
+ }
4150
+ }
4151
+ }
4152
+ }
4153
+
4154
+ // packages/cli/src/diff.ts
4155
+ function parseFocus(s) {
4156
+ const v = (s ?? "all").trim().toLowerCase();
4157
+ if (v === "all" || v === "errors" || v === "structure" || v === "outputs") {
4158
+ return v;
4159
+ }
4160
+ throw new Error(
4161
+ `Invalid --focus "${s ?? ""}". Use all, errors, structure, or outputs.`
4162
+ );
4163
+ }
4164
+ function parseCheck(s) {
4165
+ const v = (s ?? "all").trim().toLowerCase();
4166
+ if (v === "all" || v === "structure" || v === "outputs" || v === "errors" || v === "timing") {
4167
+ return v;
4168
+ }
4169
+ throw new Error(
4170
+ `Invalid --check "${s ?? ""}". Use all, structure, outputs, errors, or timing.`
4171
+ );
4172
+ }
4173
+ async function diffCommand(leftRunId, rightRunId, options = {}) {
4174
+ const leftId = typeof leftRunId === "string" && leftRunId.trim() !== "" ? leftRunId.trim() : "";
4175
+ const rightId = typeof rightRunId === "string" && rightRunId.trim() !== "" ? rightRunId.trim() : "";
4176
+ if (leftId === "" || rightId === "") {
4177
+ console.error("Both left and right run ids are required");
4178
+ process.exitCode = 1;
4179
+ return;
4180
+ }
4181
+ let focus;
4182
+ let check;
4183
+ try {
4184
+ focus = parseFocus(options.focus);
4185
+ check = parseCheck(options.check);
4186
+ } catch (e) {
4187
+ const msg = e instanceof Error ? e.message : String(e);
4188
+ console.error(msg);
4189
+ process.exitCode = 1;
4190
+ return;
4191
+ }
4192
+ let durationThresholdMs;
4193
+ if (options.durationThreshold !== void 0 && options.durationThreshold.trim() !== "") {
4194
+ try {
4195
+ durationThresholdMs = parseDuration(options.durationThreshold.trim());
4196
+ } catch (e) {
4197
+ const msg = e instanceof Error ? e.message : String(e);
4198
+ console.error(`Invalid --duration-threshold: ${msg}`);
4199
+ process.exitCode = 1;
4200
+ return;
4201
+ }
4202
+ }
4203
+ const traceDir = resolveTraceDir({ dir: options.dir });
4204
+ let leftEvents;
4205
+ let rightEvents;
4206
+ try {
4207
+ leftEvents = await readTraceEvents(leftId, traceDir);
4208
+ rightEvents = await readTraceEvents(rightId, traceDir);
4209
+ } catch (e) {
4210
+ const msg = e instanceof Error ? e.message : String(e);
4211
+ console.error(`[AgentInspect] diff failed: ${msg}`);
4212
+ process.exitCode = 1;
4213
+ return;
4214
+ }
4215
+ if (leftEvents.length === 0) {
4216
+ console.error(
4217
+ `Run not found or trace is empty: ${leftId}
4218
+ Trace directory: ${traceDir}`
4219
+ );
4220
+ process.exitCode = 1;
4221
+ return;
4222
+ }
4223
+ if (rightEvents.length === 0) {
4224
+ console.error(
4225
+ `Run not found or trace is empty: ${rightId}
4226
+ Trace directory: ${traceDir}`
4227
+ );
4228
+ process.exitCode = 1;
4229
+ return;
4230
+ }
4231
+ const diffOpts = {
4232
+ ignoreDuration: options.ignoreDuration === true,
4233
+ durationThresholdMs,
4234
+ focus,
4235
+ check
4236
+ };
4237
+ let result;
4238
+ try {
4239
+ result = diffTraceEvents(leftEvents, rightEvents, diffOpts);
4240
+ } catch (e) {
4241
+ const msg = e instanceof Error ? e.message : String(e);
4242
+ console.error(`[AgentInspect] diff failed: ${msg}`);
4243
+ process.exitCode = 1;
4244
+ return;
4245
+ }
4246
+ if (options.json === true) {
4247
+ console.log(JSON.stringify(result, null, 2));
4248
+ return;
4249
+ }
4250
+ console.log(
4251
+ renderRunDiff(result, {
4252
+ verbose: options.verbose === true})
4253
+ );
4254
+ }
4255
+
837
4256
  // packages/cli/src/index.ts
838
4257
  function runCommand(action) {
839
4258
  void action().catch((error) => {
@@ -848,18 +4267,100 @@ function createCliProgram() {
848
4267
  new Option("--status <status>", "filter by run status").choices([
849
4268
  "running",
850
4269
  "success",
851
- "error"
4270
+ "error",
4271
+ "unknown"
852
4272
  ])
853
- ).action(
4273
+ ).option("--name <query>", "filter by run name or id (substring match)").option(
4274
+ "--since <duration>",
4275
+ "only include runs since a duration (e.g. 30s, 5m, 2h, 7d)"
4276
+ ).option("--json", "print runs as JSON").action(
854
4277
  (opts) => {
855
4278
  runCommand(() => list(opts));
856
4279
  }
857
4280
  );
858
- program.command("view").description("View a single run trace").argument("<run-id>", "run id (e.g. from list output)").option("--dir <path>", "trace directory").option("--verbose", "show extra detail (types, metadata, error stacks)").option("--json", "print raw trace events as JSON").action(
4281
+ program.command("view").description("View a single run trace").argument("<run-id>", "run id (e.g. from list output)").option("--dir <path>", "trace directory").option("--summary", "print a run summary (counts, duration, max depth)").option("--metadata", "print trace metadata (file path/size, timestamps)").option("--errors-only", "show only error events / failed steps").option("--verbose", "show extra detail (types, metadata, error stacks)").option("--json", "print raw trace events as JSON").option(
4282
+ "--tui",
4283
+ "open optional interactive TUI viewer (requires @agent-inspect/tui)"
4284
+ ).action(
859
4285
  (runId, opts) => {
860
4286
  runCommand(() => view(runId, opts));
861
4287
  }
862
4288
  );
4289
+ program.command("clean").description("Safely delete old AgentInspect run traces").option("--dir <path>", "trace directory").option(
4290
+ "--older-than <duration>",
4291
+ "delete runs older than a duration (e.g. 30s, 5m, 2h, 7d)"
4292
+ ).option("--keep <count>", "keep N most recent runs (delete the rest)").option("--dry-run", "print what would be deleted (no changes)").option("--yes", "skip confirmation prompt").action(
4293
+ (opts) => {
4294
+ runCommand(() => clean(opts));
4295
+ }
4296
+ );
4297
+ program.command("logs").description("Parse structured logs into execution trees").argument("<file>", "path to log file").addOption(
4298
+ new Option("--format <format>", "log format").choices([
4299
+ "auto",
4300
+ "json",
4301
+ "log4js"
4302
+ ])
4303
+ ).option("--config <path>", "path to log ingest config (JSON)").option(
4304
+ "--run-id-key <keys>",
4305
+ "override run id keys (comma-separated, e.g. decisionId,requestId,jobId)"
4306
+ ).option("--event-key <key>", "override event key").option("--timestamp-key <key>", "override timestamp key").option("--message-key <key>", "override message key").option("--level-key <key>", "override level key").option("--parent-id-key <key>", "override parent id key").option("--duration-key <key>", "override duration key").option("--status-key <key>", "override status key").option("--json", "print result as JSON").option("--summary", "include summary section in human output").addOption(
4307
+ new Option("--warnings <mode>", "warning output mode").choices([
4308
+ "summary",
4309
+ "all",
4310
+ "none"
4311
+ ])
4312
+ ).option("--verbose", "show more detail (reserved for future)").option("--no-color", "disable color output").action((file, opts) => {
4313
+ runCommand(() => logs(file, opts));
4314
+ });
4315
+ program.command("tail").description("Live tail structured logs into execution trees").option("--file <path>", "tail a log file (default: read from stdin)").addOption(
4316
+ new Option("--format <format>", "log format").choices([
4317
+ "auto",
4318
+ "json",
4319
+ "log4js"
4320
+ ])
4321
+ ).option("--config <path>", "path to log ingest config (JSON)").option(
4322
+ "--run-id-key <keys>",
4323
+ "override run id keys (comma-separated, e.g. decisionId,requestId,jobId)"
4324
+ ).option("--event-key <key>", "override event key").option("--timestamp-key <key>", "override timestamp key").option("--message-key <key>", "override message key").option("--level-key <key>", "override level key").option("--parent-id-key <key>", "override parent id key").option("--duration-key <key>", "override duration key").option("--status-key <key>", "override status key").addOption(
4325
+ new Option("--warnings <mode>", "warning output mode").choices([
4326
+ "summary",
4327
+ "all",
4328
+ "none"
4329
+ ])
4330
+ ).option("--refresh <ms>", "minimum time between renders (ms)").option("--once", "read once and exit (for --file)").option("--json", "print newline-delimited JSON updates").option("--no-clear", "do not clear screen between renders").option("--verbose", "show more detail (reserved for future)").option("--no-color", "disable color output").action((opts) => {
4331
+ runCommand(() => tail(opts));
4332
+ });
4333
+ program.command("export").description("Export a manual trace run (Markdown, HTML, OpenInference-compatible JSON, OTLP JSON)").argument("<run-id>", "run id (e.g. from list output)").option("--dir <path>", "trace directory").addOption(
4334
+ new Option("--format <format>", "export format (default: markdown)").choices([
4335
+ "markdown",
4336
+ "html",
4337
+ "openinference",
4338
+ "otlp-json"
4339
+ ])
4340
+ ).option("-o, --output <path>", "write export to file (creates parent dirs)").option("--json", "emit JSON wrapper about the export (includes content when writing to stdout)").option("--validate", "validate exported payload shape after generation").option("--include-attributes", "include bounded attributes (review before sharing)").option("--no-metadata", "omit summary / metadata sections").option("--no-errors", "omit error sections").action((runId, opts) => {
4341
+ runCommand(() => exportCommand(runId, opts));
4342
+ });
4343
+ program.command("diff").description("Compare two local AgentInspect JSONL traces (read-only)").argument("<left-run-id>", "first run id").argument("<right-run-id>", "second run id").option("--dir <path>", "trace directory").option("--json", "print diff result as JSON").option("--ignore-duration", "omit duration comparisons").option(
4344
+ "--duration-threshold <duration>",
4345
+ "ignore duration deltas at or below this (e.g. 500ms, 2s, 1m)"
4346
+ ).addOption(
4347
+ new Option("--focus <scope>", "limit categories shown").choices([
4348
+ "all",
4349
+ "errors",
4350
+ "structure",
4351
+ "outputs"
4352
+ ])
4353
+ ).addOption(
4354
+ new Option("--check <scope>", "limit categories compared").choices([
4355
+ "all",
4356
+ "structure",
4357
+ "outputs",
4358
+ "errors",
4359
+ "timing"
4360
+ ])
4361
+ ).option("--verbose", "show more left/right detail").action((leftRunId, rightRunId, opts) => {
4362
+ runCommand(() => diffCommand(leftRunId, rightRunId, opts));
4363
+ });
863
4364
  return program;
864
4365
  }
865
4366
  function isPrimaryModule() {