@contractspec/lib.observability 3.7.17 → 3.7.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/anomaly/alert-manager.js +1 -23
  3. package/dist/anomaly/anomaly-detector.js +1 -101
  4. package/dist/anomaly/baseline-calculator.js +1 -39
  5. package/dist/anomaly/root-cause-analyzer.js +1 -31
  6. package/dist/index.js +5 -1128
  7. package/dist/intent/aggregator.js +1 -109
  8. package/dist/intent/detector.js +1 -132
  9. package/dist/logging/index.js +1 -41
  10. package/dist/metrics/index.js +1 -30
  11. package/dist/node/anomaly/alert-manager.js +1 -23
  12. package/dist/node/anomaly/anomaly-detector.js +1 -101
  13. package/dist/node/anomaly/baseline-calculator.js +1 -39
  14. package/dist/node/anomaly/root-cause-analyzer.js +1 -31
  15. package/dist/node/index.js +5 -1128
  16. package/dist/node/intent/aggregator.js +1 -109
  17. package/dist/node/intent/detector.js +1 -132
  18. package/dist/node/logging/index.js +1 -41
  19. package/dist/node/metrics/index.js +1 -30
  20. package/dist/node/pipeline/evolution-pipeline.js +1 -299
  21. package/dist/node/pipeline/lifecycle-pipeline.js +1 -85
  22. package/dist/node/telemetry/model-selection-telemetry.js +1 -30
  23. package/dist/node/telemetry/posthog-baseline-reader.js +5 -308
  24. package/dist/node/telemetry/posthog-telemetry.js +1 -60
  25. package/dist/node/tracing/core.js +1 -52
  26. package/dist/node/tracing/index.js +1 -75
  27. package/dist/node/tracing/middleware.js +1 -171
  28. package/dist/node/tracing/model-selection.span.js +1 -72
  29. package/dist/pipeline/evolution-pipeline.js +1 -299
  30. package/dist/pipeline/lifecycle-pipeline.js +1 -85
  31. package/dist/telemetry/model-selection-telemetry.js +1 -30
  32. package/dist/telemetry/posthog-baseline-reader.js +5 -308
  33. package/dist/telemetry/posthog-telemetry.js +1 -60
  34. package/dist/tracing/core.js +1 -52
  35. package/dist/tracing/index.js +1 -75
  36. package/dist/tracing/middleware.js +1 -171
  37. package/dist/tracing/model-selection.span.js +1 -72
  38. package/package.json +7 -7
@@ -1,171 +1 @@
1
- // src/metrics/index.ts
2
- import {
3
- metrics
4
- } from "@opentelemetry/api";
5
- var DEFAULT_METER_NAME = "@contractspec/lib.observability";
6
- function getMeter(name = DEFAULT_METER_NAME) {
7
- return metrics.getMeter(name);
8
- }
9
- function createCounter(name, description, meterName) {
10
- return getMeter(meterName).createCounter(name, { description });
11
- }
12
- function createUpDownCounter(name, description, meterName) {
13
- return getMeter(meterName).createUpDownCounter(name, { description });
14
- }
15
- function createHistogram(name, description, meterName) {
16
- return getMeter(meterName).createHistogram(name, { description });
17
- }
18
- var standardMetrics = {
19
- httpRequests: createCounter("http_requests_total", "Total HTTP requests"),
20
- httpDuration: createHistogram("http_request_duration_seconds", "HTTP request duration"),
21
- operationErrors: createCounter("operation_errors_total", "Total operation errors"),
22
- workflowDuration: createHistogram("workflow_duration_seconds", "Workflow execution duration")
23
- };
24
-
25
- // src/tracing/core.ts
26
- import {
27
- SpanStatusCode,
28
- trace
29
- } from "@opentelemetry/api";
30
- var DEFAULT_TRACER_NAME = "@contractspec/lib.observability";
31
- function getTracer(name = DEFAULT_TRACER_NAME) {
32
- return trace.getTracer(name);
33
- }
34
- async function traceAsync(name, fn, tracerName) {
35
- const tracer = getTracer(tracerName);
36
- return tracer.startActiveSpan(name, async (span) => {
37
- try {
38
- const result = await fn(span);
39
- span.setStatus({ code: SpanStatusCode.OK });
40
- return result;
41
- } catch (error) {
42
- span.recordException(error);
43
- span.setStatus({
44
- code: SpanStatusCode.ERROR,
45
- message: error instanceof Error ? error.message : String(error)
46
- });
47
- throw error;
48
- } finally {
49
- span.end();
50
- }
51
- });
52
- }
53
- function traceSync(name, fn, tracerName) {
54
- const tracer = getTracer(tracerName);
55
- return tracer.startActiveSpan(name, (span) => {
56
- try {
57
- const result = fn(span);
58
- span.setStatus({ code: SpanStatusCode.OK });
59
- return result;
60
- } catch (error) {
61
- span.recordException(error);
62
- span.setStatus({
63
- code: SpanStatusCode.ERROR,
64
- message: error instanceof Error ? error.message : String(error)
65
- });
66
- throw error;
67
- } finally {
68
- span.end();
69
- }
70
- });
71
- }
72
-
73
- // src/tracing/model-selection.span.ts
74
- async function traceModelSelection(fn, input) {
75
- const startMs = performance.now();
76
- return traceAsync("model.selection", async (span) => {
77
- const result = await fn();
78
- const durationMs = performance.now() - startMs;
79
- span.setAttribute("model.selected", input.modelId);
80
- span.setAttribute("model.provider", input.providerKey);
81
- span.setAttribute("model.score", input.score);
82
- span.setAttribute("model.alternatives_count", input.alternativesCount);
83
- span.setAttribute("model.selection_duration_ms", durationMs);
84
- span.setAttribute("model.reason", input.reason);
85
- if (input.dimension) {
86
- span.setAttribute("model.dimension", input.dimension);
87
- }
88
- if (input.constraints) {
89
- span.setAttribute("model.constraints", JSON.stringify(input.constraints));
90
- }
91
- return result;
92
- });
93
- }
94
- // src/tracing/middleware.ts
95
- function createTracingMiddleware(options = {}) {
96
- return async (req, next) => {
97
- const method = req.method;
98
- const url = new URL(req.url);
99
- const path = url.pathname;
100
- standardMetrics.httpRequests.add(1, { method, path });
101
- const startTime = performance.now();
102
- return traceAsync(`HTTP ${method} ${path}`, async (span) => {
103
- span.setAttribute("http.method", method);
104
- span.setAttribute("http.url", req.url);
105
- try {
106
- const response = await next();
107
- span.setAttribute("http.status_code", response.status);
108
- const duration = (performance.now() - startTime) / 1000;
109
- standardMetrics.httpDuration.record(duration, {
110
- method,
111
- path,
112
- status: response.status.toString()
113
- });
114
- emitTelemetrySample({
115
- req,
116
- res: response,
117
- span,
118
- success: true,
119
- durationMs: duration * 1000,
120
- options
121
- });
122
- return response;
123
- } catch (error) {
124
- standardMetrics.operationErrors.add(1, { method, path });
125
- emitTelemetrySample({
126
- req,
127
- span,
128
- success: false,
129
- durationMs: performance.now() - startTime,
130
- error,
131
- options
132
- });
133
- throw error;
134
- }
135
- });
136
- };
137
- }
138
- function emitTelemetrySample({
139
- req,
140
- res,
141
- span,
142
- success,
143
- durationMs,
144
- error,
145
- options
146
- }) {
147
- if (!options.onSample || !options.resolveOperation)
148
- return;
149
- const operation = options.resolveOperation({ req, res });
150
- if (!operation)
151
- return;
152
- const sample = {
153
- operation,
154
- durationMs,
155
- success,
156
- timestamp: new Date,
157
- errorCode: !success && error instanceof Error ? error.name : success ? undefined : "unknown",
158
- tenantId: options.tenantResolver?.(req),
159
- actorId: options.actorResolver?.(req),
160
- traceId: span.spanContext().traceId,
161
- metadata: {
162
- method: req.method,
163
- path: new URL(req.url).pathname,
164
- status: res?.status
165
- }
166
- };
167
- options.onSample(sample);
168
- }
169
- export {
170
- createTracingMiddleware
171
- };
1
+ import{metrics as y}from"@opentelemetry/api";var R="@contractspec/lib.observability";function g(r=R){return y.getMeter(r)}function p(r,t,a){return g(a).createCounter(r,{description:t})}function A(r,t,a){return g(a).createUpDownCounter(r,{description:t})}function T(r,t,a){return g(a).createHistogram(r,{description:t})}var m={httpRequests:p("http_requests_total","Total HTTP requests"),httpDuration:T("http_request_duration_seconds","HTTP request duration"),operationErrors:p("operation_errors_total","Total operation errors"),workflowDuration:T("workflow_duration_seconds","Workflow execution duration")};import{SpanStatusCode as d,trace as S}from"@opentelemetry/api";var h="@contractspec/lib.observability";function l(r=h){return S.getTracer(r)}async function u(r,t,a){return l(a).startActiveSpan(r,async(n)=>{try{let e=await t(n);return n.setStatus({code:d.OK}),e}catch(e){throw n.recordException(e),n.setStatus({code:d.ERROR,message:e instanceof Error?e.message:String(e)}),e}finally{n.end()}})}function E(r,t,a){return l(a).startActiveSpan(r,(n)=>{try{let e=t(n);return n.setStatus({code:d.OK}),e}catch(e){throw n.recordException(e),n.setStatus({code:d.ERROR,message:e instanceof Error?e.message:String(e)}),e}finally{n.end()}})}async function D(r,t){let a=performance.now();return u("model.selection",async(o)=>{let n=await r(),e=performance.now()-a;if(o.setAttribute("model.selected",t.modelId),o.setAttribute("model.provider",t.providerKey),o.setAttribute("model.score",t.score),o.setAttribute("model.alternatives_count",t.alternativesCount),o.setAttribute("model.selection_duration_ms",e),o.setAttribute("model.reason",t.reason),t.dimension)o.setAttribute("model.dimension",t.dimension);if(t.constraints)o.setAttribute("model.constraints",JSON.stringify(t.constraints));return n})}function k(r={}){return async(t,a)=>{let o=t.method,e=new URL(t.url).pathname;m.httpRequests.add(1,{method:o,path:e});let s=performance.now();return u(`HTTP ${o} ${e}`,async(i)=>{i.setAttribute("http.method",o),i.setAttribute("http.url",t.url);try{let c=await a();i.setAttribute("http.status_code",c.status);let f=(performance.now()-s)/1000;return m.httpDuration.record(f,{method:o,path:e,status:c.status.toString()}),w({req:t,res:c,span:i,success:!0,durationMs:f*1000,options:r}),c}catch(c){throw m.operationErrors.add(1,{method:o,path:e}),w({req:t,span:i,success:!1,durationMs:performance.now()-s,error:c,options:r}),c}})}}function w({req:r,res:t,span:a,success:o,durationMs:n,error:e,options:s}){if(!s.onSample||!s.resolveOperation)return;let i=s.resolveOperation({req:r,res:t});if(!i)return;let c={operation:i,durationMs:n,success:o,timestamp:new Date,errorCode:!o&&e instanceof Error?e.name:o?void 0:"unknown",tenantId:s.tenantResolver?.(r),actorId:s.actorResolver?.(r),traceId:a.spanContext().traceId,metadata:{method:r.method,path:new URL(r.url).pathname,status:t?.status}};s.onSample(c)}export{k as createTracingMiddleware};
@@ -1,72 +1 @@
1
- // src/tracing/core.ts
2
- import {
3
- SpanStatusCode,
4
- trace
5
- } from "@opentelemetry/api";
6
- var DEFAULT_TRACER_NAME = "@contractspec/lib.observability";
7
- function getTracer(name = DEFAULT_TRACER_NAME) {
8
- return trace.getTracer(name);
9
- }
10
- async function traceAsync(name, fn, tracerName) {
11
- const tracer = getTracer(tracerName);
12
- return tracer.startActiveSpan(name, async (span) => {
13
- try {
14
- const result = await fn(span);
15
- span.setStatus({ code: SpanStatusCode.OK });
16
- return result;
17
- } catch (error) {
18
- span.recordException(error);
19
- span.setStatus({
20
- code: SpanStatusCode.ERROR,
21
- message: error instanceof Error ? error.message : String(error)
22
- });
23
- throw error;
24
- } finally {
25
- span.end();
26
- }
27
- });
28
- }
29
- function traceSync(name, fn, tracerName) {
30
- const tracer = getTracer(tracerName);
31
- return tracer.startActiveSpan(name, (span) => {
32
- try {
33
- const result = fn(span);
34
- span.setStatus({ code: SpanStatusCode.OK });
35
- return result;
36
- } catch (error) {
37
- span.recordException(error);
38
- span.setStatus({
39
- code: SpanStatusCode.ERROR,
40
- message: error instanceof Error ? error.message : String(error)
41
- });
42
- throw error;
43
- } finally {
44
- span.end();
45
- }
46
- });
47
- }
48
-
49
- // src/tracing/model-selection.span.ts
50
- async function traceModelSelection(fn, input) {
51
- const startMs = performance.now();
52
- return traceAsync("model.selection", async (span) => {
53
- const result = await fn();
54
- const durationMs = performance.now() - startMs;
55
- span.setAttribute("model.selected", input.modelId);
56
- span.setAttribute("model.provider", input.providerKey);
57
- span.setAttribute("model.score", input.score);
58
- span.setAttribute("model.alternatives_count", input.alternativesCount);
59
- span.setAttribute("model.selection_duration_ms", durationMs);
60
- span.setAttribute("model.reason", input.reason);
61
- if (input.dimension) {
62
- span.setAttribute("model.dimension", input.dimension);
63
- }
64
- if (input.constraints) {
65
- span.setAttribute("model.constraints", JSON.stringify(input.constraints));
66
- }
67
- return result;
68
- });
69
- }
70
- export {
71
- traceModelSelection
72
- };
1
+ import{SpanStatusCode as g,trace as R}from"@opentelemetry/api";var m="@contractspec/lib.observability";function E(o=m){return R.getTracer(o)}async function T(o,e,S){return E(S).startActiveSpan(o,async(t)=>{try{let r=await e(t);return t.setStatus({code:g.OK}),r}catch(r){throw t.recordException(r),t.setStatus({code:g.ERROR,message:r instanceof Error?r.message:String(r)}),r}finally{t.end()}})}function f(o,e,S){return E(S).startActiveSpan(o,(t)=>{try{let r=e(t);return t.setStatus({code:g.OK}),r}catch(r){throw t.recordException(r),t.setStatus({code:g.ERROR,message:r instanceof Error?r.message:String(r)}),r}finally{t.end()}})}async function v(o,e){let S=performance.now();return T("model.selection",async(c)=>{let t=await o(),r=performance.now()-S;if(c.setAttribute("model.selected",e.modelId),c.setAttribute("model.provider",e.providerKey),c.setAttribute("model.score",e.score),c.setAttribute("model.alternatives_count",e.alternativesCount),c.setAttribute("model.selection_duration_ms",r),c.setAttribute("model.reason",e.reason),e.dimension)c.setAttribute("model.dimension",e.dimension);if(e.constraints)c.setAttribute("model.constraints",JSON.stringify(e.constraints));return t})}export{v as traceModelSelection};
@@ -1,300 +1,2 @@
1
1
  // @bun
2
- // src/intent/aggregator.ts
3
- var DEFAULT_WINDOW_MS = 15 * 60 * 1000;
4
-
5
- class IntentAggregator {
6
- windowMs;
7
- sequenceSampleSize;
8
- samples = [];
9
- constructor(options = {}) {
10
- this.windowMs = options.windowMs ?? DEFAULT_WINDOW_MS;
11
- this.sequenceSampleSize = options.sequenceSampleSize ?? 1000;
12
- }
13
- add(sample) {
14
- this.samples.push(sample);
15
- }
16
- flush(now = new Date) {
17
- const minTimestamp = now.getTime() - this.windowMs;
18
- const windowSamples = this.samples.filter((sample) => sample.timestamp.getTime() >= minTimestamp);
19
- this.samples.length = 0;
20
- const metrics = this.aggregateMetrics(windowSamples);
21
- const sequences = this.buildSequences(windowSamples);
22
- const timestamps = windowSamples.map((sample) => sample.timestamp.getTime());
23
- return {
24
- metrics,
25
- sequences,
26
- sampleCount: windowSamples.length,
27
- windowStart: timestamps.length ? new Date(Math.min(...timestamps)) : undefined,
28
- windowEnd: timestamps.length ? new Date(Math.max(...timestamps)) : undefined
29
- };
30
- }
31
- aggregateMetrics(samples) {
32
- if (!samples.length)
33
- return [];
34
- const groups = new Map;
35
- for (const sample of samples) {
36
- const key = `${sample.operation.name}.v${sample.operation.version}`;
37
- const arr = groups.get(key) ?? [];
38
- arr.push(sample);
39
- groups.set(key, arr);
40
- }
41
- return [...groups.values()].map((group) => {
42
- const first = group[0];
43
- if (!first)
44
- throw new Error("Empty group in aggregation");
45
- const durations = group.map((s) => s.durationMs).sort((a, b) => a - b);
46
- const errors = group.filter((s) => !s.success);
47
- const totalCalls = group.length;
48
- const topErrors = errors.reduce((acc, sample) => {
49
- if (!sample.errorCode)
50
- return acc;
51
- acc[sample.errorCode] = (acc[sample.errorCode] ?? 0) + 1;
52
- return acc;
53
- }, {});
54
- const timestamps = group.map((s) => s.timestamp.getTime());
55
- return {
56
- operation: first.operation,
57
- totalCalls,
58
- successRate: (totalCalls - errors.length) / totalCalls,
59
- errorRate: errors.length / totalCalls,
60
- averageLatencyMs: durations.reduce((sum, value) => sum + value, 0) / totalCalls,
61
- p95LatencyMs: percentile(durations, 0.95),
62
- p99LatencyMs: percentile(durations, 0.99),
63
- maxLatencyMs: Math.max(...durations),
64
- windowStart: new Date(Math.min(...timestamps)),
65
- windowEnd: new Date(Math.max(...timestamps)),
66
- topErrors
67
- };
68
- });
69
- }
70
- buildSequences(samples) {
71
- const byTrace = new Map;
72
- for (const sample of samples.slice(-this.sequenceSampleSize)) {
73
- if (!sample.traceId)
74
- continue;
75
- const arr = byTrace.get(sample.traceId) ?? [];
76
- arr.push(sample);
77
- byTrace.set(sample.traceId, arr);
78
- }
79
- const sequences = {};
80
- for (const events of byTrace.values()) {
81
- const ordered = events.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());
82
- const steps = ordered.map((event) => event.operation.name);
83
- if (steps.length < 2)
84
- continue;
85
- const key = `${steps.join(">")}@${ordered[0]?.tenantId ?? "global"}`;
86
- const existing = sequences[key];
87
- if (existing) {
88
- existing.count += 1;
89
- } else {
90
- sequences[key] = {
91
- steps,
92
- tenantId: ordered[0]?.tenantId,
93
- count: 1
94
- };
95
- }
96
- }
97
- return Object.values(sequences).sort((a, b) => b.count - a.count);
98
- }
99
- }
100
- function percentile(values, ratio) {
101
- if (!values.length)
102
- return 0;
103
- if (values.length === 1)
104
- return values[0] ?? 0;
105
- const index = Math.min(values.length - 1, Math.floor(ratio * values.length));
106
- return values[index] ?? 0;
107
- }
108
-
109
- // src/intent/detector.ts
110
- import { randomUUID } from "crypto";
111
- var DEFAULTS = {
112
- errorRateThreshold: 0.05,
113
- latencyP99ThresholdMs: 750,
114
- throughputDropThreshold: 0.3,
115
- minSequenceLength: 3
116
- };
117
-
118
- class IntentDetector {
119
- options;
120
- constructor(options = {}) {
121
- this.options = {
122
- errorRateThreshold: options.errorRateThreshold ?? DEFAULTS.errorRateThreshold,
123
- latencyP99ThresholdMs: options.latencyP99ThresholdMs ?? DEFAULTS.latencyP99ThresholdMs,
124
- throughputDropThreshold: options.throughputDropThreshold ?? DEFAULTS.throughputDropThreshold,
125
- minSequenceLength: options.minSequenceLength ?? DEFAULTS.minSequenceLength
126
- };
127
- }
128
- detectFromMetrics(current, previous) {
129
- const signals = [];
130
- const baseline = new Map((previous ?? []).map((metric) => [
131
- `${metric.operation.name}.v${metric.operation.version}`,
132
- metric
133
- ]));
134
- for (const metric of current) {
135
- if (metric.errorRate >= this.options.errorRateThreshold) {
136
- signals.push({
137
- id: randomUUID(),
138
- type: "error-spike",
139
- operation: metric.operation,
140
- confidence: Math.min(1, metric.errorRate / this.options.errorRateThreshold),
141
- description: `Error rate ${metric.errorRate.toFixed(2)} exceeded threshold`,
142
- metadata: {
143
- errorRate: metric.errorRate,
144
- topErrors: metric.topErrors
145
- },
146
- evidence: [
147
- {
148
- type: "metric",
149
- description: "error-rate",
150
- data: {
151
- errorRate: metric.errorRate,
152
- threshold: this.options.errorRateThreshold
153
- }
154
- }
155
- ]
156
- });
157
- continue;
158
- }
159
- if (metric.p99LatencyMs >= this.options.latencyP99ThresholdMs) {
160
- signals.push({
161
- id: randomUUID(),
162
- type: "latency-regression",
163
- operation: metric.operation,
164
- confidence: Math.min(1, metric.p99LatencyMs / this.options.latencyP99ThresholdMs),
165
- description: `P99 latency ${metric.p99LatencyMs}ms exceeded threshold`,
166
- metadata: { p99LatencyMs: metric.p99LatencyMs },
167
- evidence: [
168
- {
169
- type: "metric",
170
- description: "p99-latency",
171
- data: {
172
- p99LatencyMs: metric.p99LatencyMs,
173
- threshold: this.options.latencyP99ThresholdMs
174
- }
175
- }
176
- ]
177
- });
178
- continue;
179
- }
180
- const base = baseline.get(`${metric.operation.name}.v${metric.operation.version}`);
181
- if (base) {
182
- const drop = (base.totalCalls - metric.totalCalls) / Math.max(base.totalCalls, 1);
183
- if (drop >= this.options.throughputDropThreshold) {
184
- signals.push({
185
- id: randomUUID(),
186
- type: "throughput-drop",
187
- operation: metric.operation,
188
- confidence: Math.min(1, drop / this.options.throughputDropThreshold),
189
- description: `Throughput dropped ${(drop * 100).toFixed(1)}% vs baseline`,
190
- metadata: {
191
- baselineCalls: base.totalCalls,
192
- currentCalls: metric.totalCalls
193
- },
194
- evidence: [
195
- {
196
- type: "metric",
197
- description: "throughput-drop",
198
- data: {
199
- baselineCalls: base.totalCalls,
200
- currentCalls: metric.totalCalls
201
- }
202
- }
203
- ]
204
- });
205
- }
206
- }
207
- }
208
- return signals;
209
- }
210
- detectSequentialIntents(sequences) {
211
- const signals = [];
212
- for (const sequence of sequences) {
213
- if (sequence.steps.length < this.options.minSequenceLength)
214
- continue;
215
- const description = sequence.steps.join(" \u2192 ");
216
- signals.push({
217
- id: randomUUID(),
218
- type: "missing-workflow-step",
219
- confidence: 0.6,
220
- description: `Repeated workflow detected: ${description}`,
221
- metadata: {
222
- steps: sequence.steps,
223
- tenantId: sequence.tenantId,
224
- occurrences: sequence.count
225
- },
226
- evidence: [
227
- {
228
- type: "sequence",
229
- description: "sequential-calls",
230
- data: { steps: sequence.steps, count: sequence.count }
231
- }
232
- ]
233
- });
234
- }
235
- return signals;
236
- }
237
- }
238
-
239
- // src/pipeline/evolution-pipeline.ts
240
- import { EventEmitter } from "events";
241
- class EvolutionPipeline {
242
- detector;
243
- aggregator;
244
- emitter;
245
- onIntent;
246
- onSnapshot;
247
- timer;
248
- previousMetrics;
249
- constructor(options = {}) {
250
- this.detector = options.detector ?? new IntentDetector;
251
- this.aggregator = options.aggregator ?? new IntentAggregator;
252
- this.emitter = options.emitter ?? new EventEmitter;
253
- this.onIntent = options.onIntent;
254
- this.onSnapshot = options.onSnapshot;
255
- }
256
- ingest(sample) {
257
- this.aggregator.add(sample);
258
- }
259
- on(listener) {
260
- this.emitter.on("event", listener);
261
- }
262
- start(intervalMs = 5 * 60 * 1000) {
263
- this.stop();
264
- this.timer = setInterval(() => {
265
- this.run();
266
- }, intervalMs);
267
- }
268
- stop() {
269
- if (this.timer) {
270
- clearInterval(this.timer);
271
- this.timer = undefined;
272
- }
273
- }
274
- async run() {
275
- const snapshot = this.aggregator.flush();
276
- this.emit({
277
- type: "telemetry.window",
278
- payload: { sampleCount: snapshot.sampleCount }
279
- });
280
- if (this.onSnapshot)
281
- await this.onSnapshot(snapshot);
282
- if (!snapshot.sampleCount)
283
- return;
284
- const metricSignals = this.detector.detectFromMetrics(snapshot.metrics, this.previousMetrics);
285
- const sequenceSignals = this.detector.detectSequentialIntents(snapshot.sequences);
286
- this.previousMetrics = snapshot.metrics;
287
- const signals = [...metricSignals, ...sequenceSignals];
288
- for (const signal of signals) {
289
- if (this.onIntent)
290
- await this.onIntent(signal);
291
- this.emit({ type: "intent.detected", payload: signal });
292
- }
293
- }
294
- emit(event) {
295
- this.emitter.emit("event", event);
296
- }
297
- }
298
- export {
299
- EvolutionPipeline
300
- };
2
+ class g{windowMs;sequenceSampleSize;samples=[];constructor(e={}){this.windowMs=e.windowMs??900000,this.sequenceSampleSize=e.sequenceSampleSize??1000}add(e){this.samples.push(e)}flush(e=new Date){let o=e.getTime()-this.windowMs,n=this.samples.filter((i)=>i.timestamp.getTime()>=o);this.samples.length=0;let r=this.aggregateMetrics(n),t=this.buildSequences(n),s=n.map((i)=>i.timestamp.getTime());return{metrics:r,sequences:t,sampleCount:n.length,windowStart:s.length?new Date(Math.min(...s)):void 0,windowEnd:s.length?new Date(Math.max(...s)):void 0}}aggregateMetrics(e){if(!e.length)return[];let o=new Map;for(let n of e){let r=`${n.operation.name}.v${n.operation.version}`,t=o.get(r)??[];t.push(n),o.set(r,t)}return[...o.values()].map((n)=>{let r=n[0];if(!r)throw Error("Empty group in aggregation");let t=n.map((a)=>a.durationMs).sort((a,p)=>a-p),s=n.filter((a)=>!a.success),i=n.length,l=s.reduce((a,p)=>{if(!p.errorCode)return a;return a[p.errorCode]=(a[p.errorCode]??0)+1,a},{}),c=n.map((a)=>a.timestamp.getTime());return{operation:r.operation,totalCalls:i,successRate:(i-s.length)/i,errorRate:s.length/i,averageLatencyMs:t.reduce((a,p)=>a+p,0)/i,p95LatencyMs:u(t,0.95),p99LatencyMs:u(t,0.99),maxLatencyMs:Math.max(...t),windowStart:new Date(Math.min(...c)),windowEnd:new Date(Math.max(...c)),topErrors:l}})}buildSequences(e){let o=new Map;for(let r of e.slice(-this.sequenceSampleSize)){if(!r.traceId)continue;let t=o.get(r.traceId)??[];t.push(r),o.set(r.traceId,t)}let n={};for(let r of o.values()){let t=r.sort((c,a)=>c.timestamp.getTime()-a.timestamp.getTime()),s=t.map((c)=>c.operation.name);if(s.length<2)continue;let i=`${s.join(">")}@${t[0]?.tenantId??"global"}`,l=n[i];if(l)l.count+=1;else n[i]={steps:s,tenantId:t[0]?.tenantId,count:1}}return Object.values(n).sort((r,t)=>t.count-r.count)}}function u(e,o){if(!e.length)return 0;if(e.length===1)return e[0]??0;let n=Math.min(e.length-1,Math.floor(o*e.length));return e[n]??0}import{randomUUID as d}from"crypto";var h={errorRateThreshold:0.05,latencyP99ThresholdMs:750,throughputDropThreshold:0.3,minSequenceLength:3};class m{options;constructor(e={}){this.options={errorRateThreshold:e.errorRateThreshold??h.errorRateThreshold,latencyP99ThresholdMs:e.latencyP99ThresholdMs??h.latencyP99ThresholdMs,throughputDropThreshold:e.throughputDropThreshold??h.throughputDropThreshold,minSequenceLength:e.minSequenceLength??h.minSequenceLength}}detectFromMetrics(e,o){let n=[],r=new Map((o??[]).map((t)=>[`${t.operation.name}.v${t.operation.version}`,t]));for(let t of e){if(t.errorRate>=this.options.errorRateThreshold){n.push({id:d(),type:"error-spike",operation:t.operation,confidence:Math.min(1,t.errorRate/this.options.errorRateThreshold),description:`Error rate ${t.errorRate.toFixed(2)} exceeded threshold`,metadata:{errorRate:t.errorRate,topErrors:t.topErrors},evidence:[{type:"metric",description:"error-rate",data:{errorRate:t.errorRate,threshold:this.options.errorRateThreshold}}]});continue}if(t.p99LatencyMs>=this.options.latencyP99ThresholdMs){n.push({id:d(),type:"latency-regression",operation:t.operation,confidence:Math.min(1,t.p99LatencyMs/this.options.latencyP99ThresholdMs),description:`P99 latency ${t.p99LatencyMs}ms exceeded threshold`,metadata:{p99LatencyMs:t.p99LatencyMs},evidence:[{type:"metric",description:"p99-latency",data:{p99LatencyMs:t.p99LatencyMs,threshold:this.options.latencyP99ThresholdMs}}]});continue}let s=r.get(`${t.operation.name}.v${t.operation.version}`);if(s){let i=(s.totalCalls-t.totalCalls)/Math.max(s.totalCalls,1);if(i>=this.options.throughputDropThreshold)n.push({id:d(),type:"throughput-drop",operation:t.operation,confidence:Math.min(1,i/this.options.throughputDropThreshold),description:`Throughput dropped ${(i*100).toFixed(1)}% vs baseline`,metadata:{baselineCalls:s.totalCalls,currentCalls:t.totalCalls},evidence:[{type:"metric",description:"throughput-drop",data:{baselineCalls:s.totalCalls,currentCalls:t.totalCalls}}]})}}return n}detectSequentialIntents(e){let o=[];for(let n of e){if(n.steps.length<this.options.minSequenceLength)continue;let r=n.steps.join(" \u2192 ");o.push({id:d(),type:"missing-workflow-step",confidence:0.6,description:`Repeated workflow detected: ${r}`,metadata:{steps:n.steps,tenantId:n.tenantId,occurrences:n.count},evidence:[{type:"sequence",description:"sequential-calls",data:{steps:n.steps,count:n.count}}]})}return o}}import{EventEmitter as y}from"events";class S{detector;aggregator;emitter;onIntent;onSnapshot;timer;previousMetrics;constructor(e={}){this.detector=e.detector??new m,this.aggregator=e.aggregator??new g,this.emitter=e.emitter??new y,this.onIntent=e.onIntent,this.onSnapshot=e.onSnapshot}ingest(e){this.aggregator.add(e)}on(e){this.emitter.on("event",e)}start(e=300000){this.stop(),this.timer=setInterval(()=>{this.run()},e)}stop(){if(this.timer)clearInterval(this.timer),this.timer=void 0}async run(){let e=this.aggregator.flush();if(this.emit({type:"telemetry.window",payload:{sampleCount:e.sampleCount}}),this.onSnapshot)await this.onSnapshot(e);if(!e.sampleCount)return;let o=this.detector.detectFromMetrics(e.metrics,this.previousMetrics),n=this.detector.detectSequentialIntents(e.sequences);this.previousMetrics=e.metrics;let r=[...o,...n];for(let t of r){if(this.onIntent)await this.onIntent(t);this.emit({type:"intent.detected",payload:t})}}emit(e){this.emitter.emit("event",e)}}export{S as EvolutionPipeline};
@@ -1,86 +1,2 @@
1
1
  // @bun
2
- // src/metrics/index.ts
3
- import {
4
- metrics
5
- } from "@opentelemetry/api";
6
- var DEFAULT_METER_NAME = "@contractspec/lib.observability";
7
- function getMeter(name = DEFAULT_METER_NAME) {
8
- return metrics.getMeter(name);
9
- }
10
- function createCounter(name, description, meterName) {
11
- return getMeter(meterName).createCounter(name, { description });
12
- }
13
- function createUpDownCounter(name, description, meterName) {
14
- return getMeter(meterName).createUpDownCounter(name, { description });
15
- }
16
- function createHistogram(name, description, meterName) {
17
- return getMeter(meterName).createHistogram(name, { description });
18
- }
19
- var standardMetrics = {
20
- httpRequests: createCounter("http_requests_total", "Total HTTP requests"),
21
- httpDuration: createHistogram("http_request_duration_seconds", "HTTP request duration"),
22
- operationErrors: createCounter("operation_errors_total", "Total operation errors"),
23
- workflowDuration: createHistogram("workflow_duration_seconds", "Workflow execution duration")
24
- };
25
-
26
- // src/pipeline/lifecycle-pipeline.ts
27
- import { EventEmitter } from "events";
28
- import { getStageLabel } from "@contractspec/lib.lifecycle";
29
- class LifecycleKpiPipeline {
30
- assessmentCounter;
31
- confidenceHistogram;
32
- stageUpDownCounter;
33
- emitter;
34
- lowConfidenceThreshold;
35
- currentStageByTenant = new Map;
36
- constructor(options = {}) {
37
- const meterName = options.meterName ?? "@contractspec/lib.lifecycle-kpi";
38
- this.assessmentCounter = createCounter("lifecycle_assessments_total", "Total lifecycle assessments", meterName);
39
- this.confidenceHistogram = createHistogram("lifecycle_assessment_confidence", "Lifecycle assessment confidence distribution", meterName);
40
- this.stageUpDownCounter = createUpDownCounter("lifecycle_stage_tenants", "Current tenants per lifecycle stage", meterName);
41
- this.emitter = options.emitter ?? new EventEmitter;
42
- this.lowConfidenceThreshold = options.lowConfidenceThreshold ?? 0.4;
43
- }
44
- recordAssessment(assessment, tenantId) {
45
- const stageLabel = getStageLabel(assessment.stage);
46
- const attributes = { stage: stageLabel, tenantId };
47
- this.assessmentCounter.add(1, attributes);
48
- this.confidenceHistogram.record(assessment.confidence, attributes);
49
- this.ensureStageCounters(assessment.stage, tenantId);
50
- this.emitter.emit("event", {
51
- type: "assessment.recorded",
52
- payload: { tenantId, stage: assessment.stage }
53
- });
54
- if (assessment.confidence < this.lowConfidenceThreshold) {
55
- this.emitter.emit("event", {
56
- type: "confidence.low",
57
- payload: { tenantId, confidence: assessment.confidence }
58
- });
59
- }
60
- }
61
- on(listener) {
62
- this.emitter.on("event", listener);
63
- }
64
- ensureStageCounters(stage, tenantId) {
65
- if (!tenantId)
66
- return;
67
- const previous = this.currentStageByTenant.get(tenantId);
68
- if (previous === stage)
69
- return;
70
- if (previous !== undefined) {
71
- this.stageUpDownCounter.add(-1, {
72
- stage: getStageLabel(previous),
73
- tenantId
74
- });
75
- }
76
- this.stageUpDownCounter.add(1, { stage: getStageLabel(stage), tenantId });
77
- this.currentStageByTenant.set(tenantId, stage);
78
- this.emitter.emit("event", {
79
- type: "stage.changed",
80
- payload: { tenantId, previousStage: previous, nextStage: stage }
81
- });
82
- }
83
- }
84
- export {
85
- LifecycleKpiPipeline
86
- };
2
+ import{metrics as l}from"@opentelemetry/api";var p="@contractspec/lib.observability";function o(e=p){return l.getMeter(e)}function n(e,t,r){return o(r).createCounter(e,{description:t})}function a(e,t,r){return o(r).createUpDownCounter(e,{description:t})}function i(e,t,r){return o(r).createHistogram(e,{description:t})}var d={httpRequests:n("http_requests_total","Total HTTP requests"),httpDuration:i("http_request_duration_seconds","HTTP request duration"),operationErrors:n("operation_errors_total","Total operation errors"),workflowDuration:i("workflow_duration_seconds","Workflow execution duration")};import{EventEmitter as f}from"events";import{getStageLabel as s}from"@contractspec/lib.lifecycle";class g{assessmentCounter;confidenceHistogram;stageUpDownCounter;emitter;lowConfidenceThreshold;currentStageByTenant=new Map;constructor(e={}){let t=e.meterName??"@contractspec/lib.lifecycle-kpi";this.assessmentCounter=n("lifecycle_assessments_total","Total lifecycle assessments",t),this.confidenceHistogram=i("lifecycle_assessment_confidence","Lifecycle assessment confidence distribution",t),this.stageUpDownCounter=a("lifecycle_stage_tenants","Current tenants per lifecycle stage",t),this.emitter=e.emitter??new f,this.lowConfidenceThreshold=e.lowConfidenceThreshold??0.4}recordAssessment(e,t){let c={stage:s(e.stage),tenantId:t};if(this.assessmentCounter.add(1,c),this.confidenceHistogram.record(e.confidence,c),this.ensureStageCounters(e.stage,t),this.emitter.emit("event",{type:"assessment.recorded",payload:{tenantId:t,stage:e.stage}}),e.confidence<this.lowConfidenceThreshold)this.emitter.emit("event",{type:"confidence.low",payload:{tenantId:t,confidence:e.confidence}})}on(e){this.emitter.on("event",e)}ensureStageCounters(e,t){if(!t)return;let r=this.currentStageByTenant.get(t);if(r===e)return;if(r!==void 0)this.stageUpDownCounter.add(-1,{stage:s(r),tenantId:t});this.stageUpDownCounter.add(1,{stage:s(e),tenantId:t}),this.currentStageByTenant.set(t,e),this.emitter.emit("event",{type:"stage.changed",payload:{tenantId:t,previousStage:r,nextStage:e}})}}export{g as LifecycleKpiPipeline};
@@ -1,31 +1,2 @@
1
1
  // @bun
2
- // src/telemetry/model-selection-telemetry.ts
3
- class ModelSelectionTelemetry {
4
- provider;
5
- eventName;
6
- constructor(provider, options) {
7
- this.provider = provider;
8
- this.eventName = options?.eventName ?? "$model_selection";
9
- }
10
- async trackSelection(distinctId, properties) {
11
- await this.provider.capture({
12
- distinctId,
13
- event: this.eventName,
14
- timestamp: new Date,
15
- properties: {
16
- $model_id: properties.modelId,
17
- $model_provider: properties.providerKey,
18
- $model_score: properties.score,
19
- $model_dimension: properties.dimension ?? null,
20
- $model_reason: properties.reason,
21
- $model_alternatives_count: properties.alternativesCount,
22
- $model_cost_estimate_input: properties.costEstimateInput ?? null,
23
- $model_cost_estimate_output: properties.costEstimateOutput ?? null,
24
- $model_selection_duration_ms: properties.selectionDurationMs ?? null
25
- }
26
- });
27
- }
28
- }
29
- export {
30
- ModelSelectionTelemetry
31
- };
2
+ class x{provider;eventName;constructor(q,j){this.provider=q,this.eventName=j?.eventName??"$model_selection"}async trackSelection(q,j){await this.provider.capture({distinctId:q,event:this.eventName,timestamp:new Date,properties:{$model_id:j.modelId,$model_provider:j.providerKey,$model_score:j.score,$model_dimension:j.dimension??null,$model_reason:j.reason,$model_alternatives_count:j.alternativesCount,$model_cost_estimate_input:j.costEstimateInput??null,$model_cost_estimate_output:j.costEstimateOutput??null,$model_selection_duration_ms:j.selectionDurationMs??null}})}}export{x as ModelSelectionTelemetry};