@agent-inspect/mcp-server 6.31.6 → 6.31.7

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.
@@ -1488,6 +1488,8 @@ function filterTraces(traces, options) {
1488
1488
  }
1489
1489
 
1490
1490
  // packages/core/src/timeline.ts
1491
+ var ROOT_TIMELINE_DEPTH = 0;
1492
+ var MAX_TIMELINE_DEPTH = 1e3;
1491
1493
  function finite(n) {
1492
1494
  return typeof n === "number" && Number.isFinite(n);
1493
1495
  }
@@ -1558,16 +1560,51 @@ function buildRunTimeline(events, options = {}) {
1558
1560
  const computeDepth = (stepId) => {
1559
1561
  const cached = depthCache.get(stepId);
1560
1562
  if (cached !== void 0) return cached;
1561
- const node = steps.get(stepId);
1562
- if (!node) return 0;
1563
- const parent = node.parentId;
1564
- if (typeof parent !== "string" || parent.trim() === "" || !steps.has(parent)) {
1565
- depthCache.set(stepId, 0);
1566
- return 0;
1563
+ const ancestry = [];
1564
+ const ancestryIndexes = /* @__PURE__ */ new Map();
1565
+ let currentStepId = stepId;
1566
+ let resolvedDepth;
1567
+ while (resolvedDepth === void 0) {
1568
+ const cachedDepth = depthCache.get(currentStepId);
1569
+ if (cachedDepth !== void 0) {
1570
+ resolvedDepth = cachedDepth;
1571
+ continue;
1572
+ }
1573
+ const cycleStart = ancestryIndexes.get(currentStepId);
1574
+ if (cycleStart !== void 0) {
1575
+ const cycleStepIds = ancestry.splice(cycleStart);
1576
+ for (const cycleStepId of cycleStepIds) {
1577
+ depthCache.set(cycleStepId, ROOT_TIMELINE_DEPTH);
1578
+ }
1579
+ resolvedDepth = ROOT_TIMELINE_DEPTH;
1580
+ continue;
1581
+ }
1582
+ const currentStep = steps.get(currentStepId);
1583
+ if (!currentStep) {
1584
+ resolvedDepth = ROOT_TIMELINE_DEPTH;
1585
+ continue;
1586
+ }
1587
+ ancestryIndexes.set(currentStepId, ancestry.length);
1588
+ ancestry.push(currentStepId);
1589
+ const parentId = currentStep.parentId;
1590
+ if (typeof parentId !== "string" || parentId.trim() === "" || !steps.has(parentId)) {
1591
+ ancestry.pop();
1592
+ depthCache.set(currentStepId, ROOT_TIMELINE_DEPTH);
1593
+ resolvedDepth = ROOT_TIMELINE_DEPTH;
1594
+ continue;
1595
+ }
1596
+ currentStepId = parentId;
1597
+ }
1598
+ ancestry.reverse();
1599
+ for (const ancestorStepId of ancestry) {
1600
+ resolvedDepth = Math.min(MAX_TIMELINE_DEPTH, resolvedDepth + 1);
1601
+ depthCache.set(ancestorStepId, resolvedDepth);
1602
+ }
1603
+ const resolvedStepDepth = depthCache.get(stepId);
1604
+ if (resolvedStepDepth === void 0) {
1605
+ throw new Error(`Failed to resolve timeline depth for step "${stepId}"`);
1567
1606
  }
1568
- const d = Math.min(1e3, computeDepth(parent) + 1);
1569
- depthCache.set(stepId, d);
1570
- return d;
1607
+ return resolvedStepDepth;
1571
1608
  };
1572
1609
  const entries = [];
1573
1610
  for (const [stepId, s] of steps.entries()) {
@@ -5042,6 +5079,23 @@ function persistedInspectEventToInspectEvent(event) {
5042
5079
  if (event.durationMs !== void 0 && Number.isFinite(event.durationMs) && event.durationMs >= 0) {
5043
5080
  out.durationMs = event.durationMs;
5044
5081
  }
5082
+ if (event.startedAt !== void 0) {
5083
+ const started = parseIsoToMs3(event.startedAt);
5084
+ if (!started.invalidTimestamp) {
5085
+ attrs2.startedAtMs = started.ms;
5086
+ out.timestamp = started.ms;
5087
+ }
5088
+ }
5089
+ if (event.endedAt !== void 0) {
5090
+ const ended = parseIsoToMs3(event.endedAt);
5091
+ if (!ended.invalidTimestamp) {
5092
+ attrs2.endedAtMs = ended.ms;
5093
+ if (out.durationMs === void 0 && typeof attrs2.startedAtMs === "number" && ended.ms >= attrs2.startedAtMs) {
5094
+ out.durationMs = ended.ms - attrs2.startedAtMs;
5095
+ }
5096
+ }
5097
+ }
5098
+ out.attributes = compactAttributes3(attrs2);
5045
5099
  return out;
5046
5100
  }
5047
5101
  function persistedInspectEventsToInspectEvents(events, options) {
@@ -6103,20 +6157,38 @@ function extractOtlpDocument(root) {
6103
6157
  function mapOtlpStatus(status) {
6104
6158
  if (!isRecord10(status)) return void 0;
6105
6159
  const rawCode = status.code;
6106
- if (typeof rawCode !== "string") return void 0;
6107
- switch (rawCode.toUpperCase()) {
6108
- case "STATUS_CODE_OK":
6109
- case "OK":
6110
- return "ok";
6111
- case "STATUS_CODE_ERROR":
6112
- case "ERROR":
6113
- return "error";
6114
- case "STATUS_CODE_UNSET":
6115
- case "UNSET":
6116
- return "unknown";
6117
- default:
6118
- return "unknown";
6160
+ if (typeof rawCode === "number" && Number.isFinite(rawCode)) {
6161
+ switch (rawCode) {
6162
+ case 0:
6163
+ return "unknown";
6164
+ case 1:
6165
+ return "ok";
6166
+ case 2:
6167
+ return "error";
6168
+ default:
6169
+ return "unknown";
6170
+ }
6171
+ }
6172
+ if (typeof rawCode === "string") {
6173
+ const trimmed = rawCode.trim();
6174
+ if (/^\d+$/.test(trimmed)) {
6175
+ return mapOtlpStatus({ code: Number(trimmed) });
6176
+ }
6177
+ switch (trimmed.toUpperCase()) {
6178
+ case "STATUS_CODE_OK":
6179
+ case "OK":
6180
+ return "ok";
6181
+ case "STATUS_CODE_ERROR":
6182
+ case "ERROR":
6183
+ return "error";
6184
+ case "STATUS_CODE_UNSET":
6185
+ case "UNSET":
6186
+ return "unknown";
6187
+ default:
6188
+ return "unknown";
6189
+ }
6119
6190
  }
6191
+ return void 0;
6120
6192
  }
6121
6193
  function readOtlpKind(attributes, pathPrefix) {
6122
6194
  const warnings = [];
@@ -7368,6 +7440,10 @@ function exportOpenInference(tree, options) {
7368
7440
  warnings
7369
7441
  };
7370
7442
  }
7443
+ var OTLP_STATUS_CODE_UNSET = 0;
7444
+ var OTLP_STATUS_CODE_OK = 1;
7445
+ var OTLP_STATUS_CODE_ERROR = 2;
7446
+ var OTLP_SPAN_KIND_INTERNAL = 1;
7371
7447
  function hexFrom2(seed, byteLen) {
7372
7448
  return crypto.createHash("sha256").update(seed, "utf8").digest("hex").slice(0, byteLen * 2);
7373
7449
  }
@@ -7380,7 +7456,19 @@ function stringAttr2(key, value) {
7380
7456
  return { key, value: { stringValue: value } };
7381
7457
  }
7382
7458
  function intAttr(key, value) {
7383
- return { key, value: { intValue: String(value) } };
7459
+ return { key, value: { intValue: String(Math.trunc(value)) } };
7460
+ }
7461
+ function doubleAttr(key, value) {
7462
+ return { key, value: { doubleValue: value } };
7463
+ }
7464
+ function boolAttr(key, value) {
7465
+ return { key, value: { boolValue: value } };
7466
+ }
7467
+ function numberAttr(key, value) {
7468
+ if (Number.isFinite(value) && !Number.isInteger(value)) {
7469
+ return doubleAttr(key, value);
7470
+ }
7471
+ return intAttr(key, value);
7384
7472
  }
7385
7473
  function genAiOperationName(kind) {
7386
7474
  switch (kind) {
@@ -7394,10 +7482,27 @@ function genAiOperationName(kind) {
7394
7482
  return void 0;
7395
7483
  }
7396
7484
  }
7485
+ function resolveSpanTimes(ev) {
7486
+ const startedAtMs = typeof ev.attributes?.startedAtMs === "number" && Number.isFinite(ev.attributes.startedAtMs) ? ev.attributes.startedAtMs : void 0;
7487
+ const endedAtMs = typeof ev.attributes?.endedAtMs === "number" && Number.isFinite(ev.attributes.endedAtMs) ? ev.attributes.endedAtMs : void 0;
7488
+ const startMs = startedAtMs ?? ev.timestamp;
7489
+ const startNs = String(unixNano2(startMs));
7490
+ if (endedAtMs !== void 0) {
7491
+ return { startNs, endNs: String(unixNano2(endedAtMs)) };
7492
+ }
7493
+ if (ev.durationMs !== void 0 && Number.isFinite(ev.durationMs)) {
7494
+ return {
7495
+ startNs,
7496
+ endNs: String(unixNano2(startMs) + unixNano2(ev.durationMs))
7497
+ };
7498
+ }
7499
+ return { startNs };
7500
+ }
7397
7501
  function exportOtlpJson(tree, options) {
7398
7502
  const warnings = [
7399
7503
  "OTLP JSON export uses OTel GenAI-aligned attributes where applicable; experimental until verified against specific collectors.",
7400
- "Not OTLP gRPC/protobuf \u2014 JSON mapping only. Generated locally; no network upload."
7504
+ "Not OTLP gRPC/protobuf \u2014 JSON mapping only. Generated locally; no network upload.",
7505
+ "Status and span kind use OTLP numeric enums (StatusCode 0/1/2, SpanKind INTERNAL=1)."
7401
7506
  ];
7402
7507
  const traceId = hexFrom2(`trace:${tree.runId}`, 16);
7403
7508
  const includeAttributes = options?.includeAttributes ?? false;
@@ -7409,11 +7514,7 @@ function exportOtlpJson(tree, options) {
7409
7514
  const ev = n.event;
7410
7515
  const spanId = hexFrom2(`${tree.runId}:${ev.eventId}`, 8);
7411
7516
  const parentSpanId = ev.parentId ? hexFrom2(`${tree.runId}:${ev.parentId}`, 8) : void 0;
7412
- const startNs = String(unixNano2(ev.timestamp));
7413
- let endNs;
7414
- if (ev.durationMs !== void 0 && Number.isFinite(ev.durationMs)) {
7415
- endNs = String(unixNano2(ev.timestamp) + unixNano2(ev.durationMs));
7416
- }
7517
+ const { startNs, endNs } = resolveSpanTimes(ev);
7417
7518
  const attrs2 = [
7418
7519
  stringAttr2("agent_inspect.kind", ev.kind),
7419
7520
  stringAttr2("agent_inspect.confidence", ev.confidence),
@@ -7443,29 +7544,28 @@ function exportOtlpJson(tree, options) {
7443
7544
  if (includeAttributes && meta && typeof meta === "object") {
7444
7545
  for (const [k, v] of Object.entries(meta)) {
7445
7546
  if (k === "tokens" || k === "model") continue;
7446
- if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
7447
- attrs2.push(
7448
- stringAttr2(
7449
- `agent_inspect.preview.${k}`,
7450
- typeof v === "string" ? v.slice(0, maxLen) : String(v)
7451
- )
7452
- );
7547
+ if (typeof v === "string") {
7548
+ attrs2.push(stringAttr2(`agent_inspect.preview.${k}`, v.slice(0, maxLen)));
7549
+ } else if (typeof v === "number" && Number.isFinite(v)) {
7550
+ attrs2.push(numberAttr(`agent_inspect.preview.${k}`, v));
7551
+ } else if (typeof v === "boolean") {
7552
+ attrs2.push(boolAttr(`agent_inspect.preview.${k}`, v));
7453
7553
  }
7454
7554
  }
7455
7555
  }
7456
- let statusCode = "STATUS_CODE_UNSET";
7556
+ let statusCode = OTLP_STATUS_CODE_UNSET;
7457
7557
  let statusMessage;
7458
7558
  if (ev.status === "error") {
7459
- statusCode = "STATUS_CODE_ERROR";
7559
+ statusCode = OTLP_STATUS_CODE_ERROR;
7460
7560
  statusMessage = meta && typeof meta.error === "object" && meta.error !== null ? String(meta.error.message ?? "error").slice(0, maxLen) : "error";
7461
7561
  } else if (ev.status === "ok") {
7462
- statusCode = "STATUS_CODE_OK";
7562
+ statusCode = OTLP_STATUS_CODE_OK;
7463
7563
  }
7464
7564
  const spanJson = {
7465
7565
  traceId,
7466
7566
  spanId,
7467
7567
  name: ev.name,
7468
- kind: "SPAN_KIND_INTERNAL",
7568
+ kind: OTLP_SPAN_KIND_INTERNAL,
7469
7569
  startTimeUnixNano: startNs,
7470
7570
  attributes: attrs2,
7471
7571
  status: {
@@ -9130,5 +9230,5 @@ async function runReadOnlyMcpServer(options = {}) {
9130
9230
  }
9131
9231
 
9132
9232
  export { MCP_MAX_REQUEST_BYTES, MCP_PROTOCOL_VERSION, MCP_SERVER_INSTRUCTIONS, READ_ONLY_TOOLS, TRACE_DATA_UNTRUSTED_WARNING, callReadOnlyTool, createMcpServerContext, handleMcpProtocolLine, runReadOnlyMcpServer };
9133
- //# sourceMappingURL=chunk-P72E7RLD.mjs.map
9134
- //# sourceMappingURL=chunk-P72E7RLD.mjs.map
9233
+ //# sourceMappingURL=chunk-J2LULLKC.mjs.map
9234
+ //# sourceMappingURL=chunk-J2LULLKC.mjs.map