@aaac/observability 0.1.17 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,7 +59,7 @@ import {
59
59
  } from '@aaac/observability/__tests__/e2e/stack/helpers.js';
60
60
  import { SEED_SESSION_ID } from
61
61
  '@aaac/observability/__tests__/e2e/stack/__fixtures__/observability-stack-seed.js';
62
- import { loadStackConfig } from '@aaac/observability/src/e2e/load-stack-config.js';
62
+ import { loadStackConfig } from '@aaac/observability/__tests__/e2e/load-stack-config.js';
63
63
 
64
64
  // Example usage in a follow-up E2E test:
65
65
  const config = loadStackConfig();
@@ -157,6 +157,32 @@ function extractString(attrs, key) {
157
157
  // src/normalize/vendor-otel-mapper.ts
158
158
  var VENDOR_OTEL_SOURCE = "claude-code-otel";
159
159
  var VENDOR_OTEL_DEDUP_PREFIX = "vendor-otel";
160
+ var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
161
+ var optionalListOf = (value, is) => value === void 0 || Array.isArray(value) && value.every(is);
162
+ var optionalString = (value) => value === void 0 || typeof value === "string";
163
+ var optionalNumeric = (value) => value === void 0 || typeof value === "string" || typeof value === "number";
164
+ function isAnyValue(value) {
165
+ if (!isObject(value)) return false;
166
+ return optionalString(value.stringValue) && (value.boolValue === void 0 || typeof value.boolValue === "boolean") && optionalNumeric(value.intValue) && (value.doubleValue === void 0 || typeof value.doubleValue === "number");
167
+ }
168
+ function isKeyValue(value) {
169
+ if (!isObject(value)) return false;
170
+ return optionalString(value.key) && (value.value === void 0 || isAnyValue(value.value));
171
+ }
172
+ function isSpan(value) {
173
+ if (!isObject(value)) return false;
174
+ return optionalString(value.traceId) && optionalString(value.spanId) && optionalString(value.parentSpanId) && optionalString(value.name) && optionalNumeric(value.startTimeUnixNano) && optionalNumeric(value.endTimeUnixNano) && optionalListOf(value.attributes, isKeyValue);
175
+ }
176
+ function isScopeSpans(value) {
177
+ return isObject(value) && optionalListOf(value.spans, isSpan);
178
+ }
179
+ function isResourceSpans(value) {
180
+ if (!isObject(value)) return false;
181
+ return (value.resource === void 0 || isObject(value.resource) && optionalListOf(value.resource.attributes, isKeyValue)) && optionalListOf(value.scopeSpans, isScopeSpans);
182
+ }
183
+ function isOtlpTraceExport(value) {
184
+ return isObject(value) && optionalListOf(value.resourceSpans, isResourceSpans);
185
+ }
160
186
  function convertAnyValue(value) {
161
187
  if (value === void 0 || value === null) return void 0;
162
188
  if (typeof value.stringValue === "string") return value.stringValue;
@@ -212,9 +238,8 @@ function mapVendorOtelExport(doc, options = {}) {
212
238
  const source = options.source ?? VENDOR_OTEL_SOURCE;
213
239
  const events = [];
214
240
  let skipped = 0;
215
- const resourceSpans = doc?.resourceSpans;
216
- if (!Array.isArray(resourceSpans)) return { events, skipped };
217
- for (const rs of resourceSpans) {
241
+ if (!isOtlpTraceExport(doc)) return { events, skipped };
242
+ for (const rs of doc.resourceSpans ?? []) {
218
243
  const resourceAttrs = convertAttributes(rs?.resource?.attributes);
219
244
  const projectId = firstString(resourceAttrs, ["project.id", "service.name", "service.namespace"]) ?? void 0;
220
245
  for (const ss of rs?.scopeSpans ?? []) {
@@ -261,10 +286,13 @@ function mapVendorOtelExport(doc, options = {}) {
261
286
  }
262
287
 
263
288
  // src/correlate/cache-store.ts
289
+ var isCachedString = (value) => typeof value === "string";
290
+ var isCachedStringList = (value) => Array.isArray(value) && value.every(isCachedString);
264
291
  var MemoryCacheStore = class {
265
292
  map = /* @__PURE__ */ new Map();
266
- get(key) {
267
- return this.map.get(key);
293
+ get(key, is) {
294
+ const value = this.map.get(key);
295
+ return is(value) ? value : void 0;
268
296
  }
269
297
  set(key, value) {
270
298
  this.map.set(key, value);
@@ -278,14 +306,16 @@ var SqliteCacheStore = class {
278
306
  this.db = db;
279
307
  }
280
308
  db;
281
- get(key) {
309
+ get(key, is) {
310
+ let value;
282
311
  try {
283
312
  const row = this.db.prepare("SELECT value FROM kv_cache WHERE key = ?").get(key);
284
- if (row === void 0) return void 0;
285
- return JSON.parse(row.value);
313
+ if (typeof row?.value !== "string") return void 0;
314
+ value = JSON.parse(row.value);
286
315
  } catch {
287
316
  return void 0;
288
317
  }
318
+ return is(value) ? value : void 0;
289
319
  }
290
320
  set(key, value) {
291
321
  try {
@@ -361,9 +391,9 @@ var Correlator = class {
361
391
  cacheSet(key, value) {
362
392
  this.cache.set(key, value);
363
393
  }
364
- /** Retrieve a cached value; returns undefined if absent. */
365
- cacheGet(key) {
366
- return this.cache.get(key);
394
+ /** Retrieve a cached value; undefined if absent or not what `is` accepts. */
395
+ cacheGet(key, is) {
396
+ return this.cache.get(key, is);
367
397
  }
368
398
  /** Remove a cached entry. */
369
399
  cacheDel(key) {
@@ -445,6 +475,11 @@ function createArtifactRule(patterns) {
445
475
  }
446
476
 
447
477
  // src/enrich/rules/process.ts
478
+ function isActiveTaskList(value) {
479
+ return Array.isArray(value) && value.every(
480
+ (task) => typeof task === "object" && task !== null && typeof task.spanId === "string" && (task.taskId === void 0 || typeof task.taskId === "string")
481
+ );
482
+ }
448
483
  function activeTasksKey(sessionId) {
449
484
  return `session:${sessionId}:active_tasks`;
450
485
  }
@@ -460,10 +495,10 @@ function createProcessRule() {
460
495
  if (event.eventType === "process.task") {
461
496
  if (!sid) return;
462
497
  if (event.lifecycle === "open") {
463
- const tasks = ctx.api.cacheGet(activeTasksKey(sid)) ?? [];
498
+ const tasks = ctx.api.cacheGet(activeTasksKey(sid), isActiveTaskList) ?? [];
464
499
  tasks.push({ spanId: event.spanId, taskId: event.taskId });
465
500
  ctx.api.cacheSet(activeTasksKey(sid), tasks);
466
- const allSpans = ctx.api.cacheGet(allTaskSpansKey(sid)) ?? [];
501
+ const allSpans = ctx.api.cacheGet(allTaskSpansKey(sid), isCachedStringList) ?? [];
467
502
  if (!allSpans.includes(event.spanId)) {
468
503
  allSpans.push(event.spanId);
469
504
  ctx.api.cacheSet(allTaskSpansKey(sid), allSpans);
@@ -475,7 +510,7 @@ function createProcessRule() {
475
510
  return;
476
511
  }
477
512
  if (event.lifecycle === "close") {
478
- const tasks = ctx.api.cacheGet(activeTasksKey(sid)) ?? [];
513
+ const tasks = ctx.api.cacheGet(activeTasksKey(sid), isActiveTaskList) ?? [];
479
514
  const updated = tasks.filter((t) => t.spanId !== event.spanId);
480
515
  ctx.api.cacheSet(activeTasksKey(sid), updated);
481
516
  ctx.api.logDebug(
@@ -488,7 +523,7 @@ function createProcessRule() {
488
523
  if (event.eventType === "process.edit" && event.lifecycle === "instant") {
489
524
  if (event.parentSpanId) return;
490
525
  if (!sid) return;
491
- const tasks = ctx.api.cacheGet(activeTasksKey(sid)) ?? [];
526
+ const tasks = ctx.api.cacheGet(activeTasksKey(sid), isActiveTaskList) ?? [];
492
527
  if (tasks.length === 0) {
493
528
  return;
494
529
  }
@@ -498,7 +533,8 @@ function createProcessRule() {
498
533
  const fp = event.attributes["edit.path"];
499
534
  if (typeof fp === "string" && fp.length > 0) {
500
535
  const files = ctx.api.cacheGet(
501
- taskFilesKey(sid, tasks[0].spanId)
536
+ taskFilesKey(sid, tasks[0].spanId),
537
+ isCachedStringList
502
538
  ) ?? [];
503
539
  if (!files.includes(fp)) {
504
540
  files.push(fp);
@@ -515,7 +551,8 @@ function createProcessRule() {
515
551
  const owner = tasks.find(
516
552
  (t) => {
517
553
  const taskArtifact = ctx.api.cacheGet(
518
- `session:${sid}:task_artifact:${t.spanId}`
554
+ `session:${sid}:task_artifact:${t.spanId}`,
555
+ isCachedString
519
556
  );
520
557
  return taskArtifact === artifactId;
521
558
  }
@@ -525,7 +562,7 @@ function createProcessRule() {
525
562
  event.taskId = owner.taskId;
526
563
  const fp = event.attributes["edit.path"];
527
564
  if (typeof fp === "string" && fp.length > 0) {
528
- const files = ctx.api.cacheGet(taskFilesKey(sid, owner.spanId)) ?? [];
565
+ const files = ctx.api.cacheGet(taskFilesKey(sid, owner.spanId), isCachedStringList) ?? [];
529
566
  if (!files.includes(fp)) {
530
567
  files.push(fp);
531
568
  ctx.api.cacheSet(taskFilesKey(sid, owner.spanId), files);
@@ -546,7 +583,7 @@ function createProcessRule() {
546
583
  if (event.eventType === "process.tool" && event.lifecycle === "instant") {
547
584
  if (event.parentSpanId) return;
548
585
  if (!sid) return;
549
- const tasks = ctx.api.cacheGet(activeTasksKey(sid)) ?? [];
586
+ const tasks = ctx.api.cacheGet(activeTasksKey(sid), isActiveTaskList) ?? [];
550
587
  if (tasks.length === 0) {
551
588
  return;
552
589
  }
@@ -597,9 +634,9 @@ function createCrossAxisRule() {
597
634
  }
598
635
  if (committedFiles.length === 0) return;
599
636
  const committedSet = new Set(committedFiles);
600
- const allTaskSpans = ctx.api.cacheGet(allTaskSpansKey(sid)) ?? [];
637
+ const allTaskSpans = ctx.api.cacheGet(allTaskSpansKey(sid), isCachedStringList) ?? [];
601
638
  for (const taskSpanId of allTaskSpans) {
602
- const taskFiles = ctx.api.cacheGet(taskFilesKey(sid, taskSpanId)) ?? [];
639
+ const taskFiles = ctx.api.cacheGet(taskFilesKey(sid, taskSpanId), isCachedStringList) ?? [];
603
640
  const hasIntersection = taskFiles.some((f) => committedSet.has(f));
604
641
  if (hasIntersection) {
605
642
  ctx.api.addLink({
@@ -615,6 +652,11 @@ function createCrossAxisRule() {
615
652
  }
616
653
 
617
654
  // src/enrich/rules/promotion.ts
655
+ function isChangeRefList(value) {
656
+ return Array.isArray(value) && value.every(
657
+ (ref) => typeof ref === "object" && ref !== null && typeof ref.spanId === "string" && typeof ref.traceId === "string"
658
+ );
659
+ }
618
660
  function changesKey(sessionId, filePath) {
619
661
  return `session:${sessionId}:changes:${filePath}`;
620
662
  }
@@ -627,7 +669,7 @@ function createPromotionRule() {
627
669
  return;
628
670
  }
629
671
  const key = changesKey(sid, fp);
630
- const existing = ctx.api.cacheGet(key) ?? [];
672
+ const existing = ctx.api.cacheGet(key, isChangeRefList) ?? [];
631
673
  existing.push({ spanId: event.spanId, traceId: event.traceId });
632
674
  ctx.api.cacheSet(key, existing);
633
675
  ctx.api.logDebug(
@@ -657,7 +699,7 @@ function createPromotionRule() {
657
699
  const commitTraceId = event.traceId;
658
700
  for (const fp of committedFiles) {
659
701
  const cacheKey = changesKey(sid, fp);
660
- const changeRefs = ctx.api.cacheGet(cacheKey) ?? [];
702
+ const changeRefs = ctx.api.cacheGet(cacheKey, isChangeRefList) ?? [];
661
703
  const links = changeRefs.map((ref) => ({
662
704
  linkType: "contains_change",
663
705
  targetSpanId: ref.spanId,
@@ -696,6 +738,11 @@ function createPromotionRule() {
696
738
 
697
739
  // src/enrich/rules/worktree.ts
698
740
  var WORKTREE_WINDOW_MS = 60 * 60 * 1e3;
741
+ function isWorktreeRef(value) {
742
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
743
+ const ref = value;
744
+ return typeof ref.spanId === "string" && typeof ref.traceId === "string" && (ref.sessionId === void 0 || typeof ref.sessionId === "string") && typeof ref.timeMs === "number";
745
+ }
699
746
  function worktreeRepoKey(repoRoot) {
700
747
  return `worktree:repo:${repoRoot}`;
701
748
  }
@@ -724,7 +771,7 @@ function createWorktreeRule(windowMs = WORKTREE_WINDOW_MS) {
724
771
  sessionId: event.sessionId,
725
772
  timeMs: nanoToMs(event.timeUnixNano)
726
773
  };
727
- const existing = ctx.api.cacheGet(key);
774
+ const existing = ctx.api.cacheGet(key, isWorktreeRef);
728
775
  if (!existing || ref.timeMs >= existing.timeMs) {
729
776
  ctx.api.cacheSet(key, ref);
730
777
  ctx.api.logDebug(
@@ -741,7 +788,7 @@ function createWorktreeRule(windowMs = WORKTREE_WINDOW_MS) {
741
788
  ctx.api.logDebug("R8: promotion.commit has no repo.root \u2014 skip");
742
789
  return;
743
790
  }
744
- const wt = ctx.api.cacheGet(worktreeRepoKey(repoRoot));
791
+ const wt = ctx.api.cacheGet(worktreeRepoKey(repoRoot), isWorktreeRef);
745
792
  if (!wt) {
746
793
  ctx.api.logDebug(`R8: no cached worktree for repo.root=${repoRoot} \u2014 skip`);
747
794
  return;
@@ -761,9 +808,9 @@ function createWorktreeRule(windowMs = WORKTREE_WINDOW_MS) {
761
808
  const committedFiles = parseCommittedFiles(event.attributes["committed_files"]);
762
809
  if (committedFiles && committedFiles.length > 0 && wt.sessionId) {
763
810
  const committedSet = new Set(committedFiles);
764
- const taskSpans = ctx.api.cacheGet(allTaskSpansKey(wt.sessionId)) ?? [];
811
+ const taskSpans = ctx.api.cacheGet(allTaskSpansKey(wt.sessionId), isCachedStringList) ?? [];
765
812
  const overlap = taskSpans.some((taskSpanId) => {
766
- const files = ctx.api.cacheGet(taskFilesKey(wt.sessionId, taskSpanId)) ?? [];
813
+ const files = ctx.api.cacheGet(taskFilesKey(wt.sessionId, taskSpanId), isCachedStringList) ?? [];
767
814
  return files.some((f) => committedSet.has(f));
768
815
  });
769
816
  if (overlap) confidence = "probable";
@@ -794,7 +841,7 @@ function createIssueAnchorRule() {
794
841
  const attrIssueId = event.attributes["issue.id"];
795
842
  const attrIssueType = event.attributes["issue.type"];
796
843
  if (attrIssueId !== void 0 && attrIssueId !== "") {
797
- const cached = ctx.api.cacheGet(issueIdCacheKey(sid));
844
+ const cached = ctx.api.cacheGet(issueIdCacheKey(sid), isCachedString);
798
845
  if (cached === void 0) {
799
846
  ctx.api.cacheSet(issueIdCacheKey(sid), String(attrIssueId));
800
847
  ctx.api.logDebug(
@@ -803,7 +850,7 @@ function createIssueAnchorRule() {
803
850
  }
804
851
  }
805
852
  if (attrIssueType !== void 0 && attrIssueType !== "") {
806
- const cached = ctx.api.cacheGet(issueTypeCacheKey(sid));
853
+ const cached = ctx.api.cacheGet(issueTypeCacheKey(sid), isCachedString);
807
854
  if (cached === void 0) {
808
855
  ctx.api.cacheSet(issueTypeCacheKey(sid), String(attrIssueType));
809
856
  ctx.api.logDebug(
@@ -812,7 +859,7 @@ function createIssueAnchorRule() {
812
859
  }
813
860
  }
814
861
  if (attrIssueId === void 0 || attrIssueId === "") {
815
- const cachedId = ctx.api.cacheGet(issueIdCacheKey(sid));
862
+ const cachedId = ctx.api.cacheGet(issueIdCacheKey(sid), isCachedString);
816
863
  if (cachedId !== void 0) {
817
864
  event.attributes["issue.id"] = cachedId;
818
865
  ctx.api.logDebug(
@@ -823,7 +870,7 @@ function createIssueAnchorRule() {
823
870
  }
824
871
  }
825
872
  if ((attrIssueType === void 0 || attrIssueType === "") && event.attributes["issue.type"] === void 0) {
826
- const cachedType = ctx.api.cacheGet(issueTypeCacheKey(sid));
873
+ const cachedType = ctx.api.cacheGet(issueTypeCacheKey(sid), isCachedString);
827
874
  if (cachedType !== void 0) {
828
875
  event.attributes["issue.type"] = cachedType;
829
876
  }
@@ -865,7 +912,7 @@ var Enricher = class {
865
912
  const patterns = this.artifactPatterns;
866
913
  const correlator = this.correlator;
867
914
  const api = {
868
- cacheGet: (k) => correlator.cacheGet(k),
915
+ cacheGet: (k, is) => correlator.cacheGet(k, is),
869
916
  cacheSet: (k, v) => correlator.cacheSet(k, v),
870
917
  emitRecord: (e) => derived.push(e),
871
918
  addLink: (link) => mutableEvent.links.push(link),
@@ -1427,6 +1474,16 @@ function mapAllEventsToSpans(events) {
1427
1474
  import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
1428
1475
  import { mkdirSync as mkdirSync2 } from "fs";
1429
1476
  import { dirname as dirname2, resolve as resolve2 } from "path";
1477
+
1478
+ // src/types/canonical-event.ts
1479
+ function isAttrMap(value) {
1480
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
1481
+ return Object.values(value).every(
1482
+ (v) => typeof v === "string" || typeof v === "number" || typeof v === "boolean"
1483
+ );
1484
+ }
1485
+
1486
+ // src/query/sqlite-adapter.ts
1430
1487
  var SqliteQueryAdapter = class {
1431
1488
  db;
1432
1489
  ownsDb;
@@ -1580,11 +1637,13 @@ function parseLinkRow(row) {
1580
1637
  };
1581
1638
  }
1582
1639
  function safeParseJson(json) {
1640
+ let parsed;
1583
1641
  try {
1584
- return JSON.parse(json);
1642
+ parsed = JSON.parse(json);
1585
1643
  } catch {
1586
1644
  return {};
1587
1645
  }
1646
+ return isAttrMap(parsed) ? parsed : {};
1588
1647
  }
1589
1648
  function buildSpanFilter(filter) {
1590
1649
  const clauses = [];
@@ -1781,10 +1840,40 @@ function evaluateMapping(rule, hookInput) {
1781
1840
  }
1782
1841
  return { spans, links };
1783
1842
  }
1843
+ var optionalString2 = (value) => value === void 0 || typeof value === "string";
1844
+ function isSpanRule(value) {
1845
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
1846
+ const rule = value;
1847
+ return typeof rule.axis === "string" && typeof rule.name === "string" && (rule.lifecycle === "open" || rule.lifecycle === "close" || rule.lifecycle === "instant") && (rule.attributes === void 0 || typeof rule.attributes === "object" && rule.attributes !== null && !Array.isArray(rule.attributes) && Object.values(rule.attributes).every((v) => typeof v === "string")) && optionalString2(rule.each) && optionalString2(rule.condition);
1848
+ }
1849
+ function isLinkRule(value) {
1850
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
1851
+ const link = value;
1852
+ return typeof link.type === "string" && typeof link.from === "string" && typeof link.to === "string";
1853
+ }
1854
+ function isEventMappingRule(value) {
1855
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
1856
+ const rule = value;
1857
+ return Array.isArray(rule.spans) && rule.spans.every(isSpanRule) && (rule.links === void 0 || Array.isArray(rule.links) && rule.links.every(isLinkRule));
1858
+ }
1859
+ function isMappings(value) {
1860
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
1861
+ return Object.values(value).every(isEventMappingRule);
1862
+ }
1784
1863
  async function loadEventMappingConfig(filePath) {
1785
1864
  const content = await readFile(filePath, "utf8");
1786
1865
  const parsed = filePath.endsWith(".json") ? JSON.parse(content) : parseYaml(content);
1787
- const mappings = parsed.mappings ?? parsed.event_mapping ?? parsed;
1866
+ const candidates = typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? [
1867
+ parsed.mappings,
1868
+ parsed.event_mapping,
1869
+ parsed
1870
+ ] : [parsed];
1871
+ const mappings = candidates.find(isMappings);
1872
+ if (mappings === void 0) {
1873
+ throw new Error(
1874
+ `${filePath} is not an event-mapping config \u2014 expected \`mappings\` / \`event_mapping\` (or the document itself) to map each event name to { spans: [{ axis, name, lifecycle }], links?: [{ type, from, to }] }.`
1875
+ );
1876
+ }
1788
1877
  return { mappings };
1789
1878
  }
1790
1879
 
@@ -2693,6 +2782,7 @@ export {
2693
2782
  OtelEmitter,
2694
2783
  mapEventsToSpan,
2695
2784
  mapAllEventsToSpans,
2785
+ isAttrMap,
2696
2786
  SqliteQueryAdapter,
2697
2787
  resolveTemplate,
2698
2788
  evaluateMapping,
@@ -2716,4 +2806,4 @@ export {
2716
2806
  renderReportMarkdown,
2717
2807
  createPipeline
2718
2808
  };
2719
- //# sourceMappingURL=chunk-3DXZNA3E.js.map
2809
+ //# sourceMappingURL=chunk-4PNXLHYZ.js.map