@netgreener/runtime 0.1.9 → 0.1.11

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 (44) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/aggregator.d.ts +2 -0
  3. package/dist/aggregator.js +33 -13
  4. package/dist/analyze/buildResourceFindings.d.ts +1 -1
  5. package/dist/analyze/buildResourceFindings.js +4 -2
  6. package/dist/analyze/findingGuidance.d.ts +14 -0
  7. package/dist/analyze/findingGuidance.js +52 -0
  8. package/dist/analyze/index.d.ts +3 -2
  9. package/dist/analyze/index.js +3 -2
  10. package/dist/analyze/projectScan.d.ts +3 -1
  11. package/dist/analyze/projectScan.js +29 -6
  12. package/dist/analyze/resourceFindingCandidate.d.ts +6 -0
  13. package/dist/analyze/resourceFindingCandidate.js +11 -0
  14. package/dist/analyze/serviceDiscovery.d.ts +23 -1
  15. package/dist/analyze/serviceDiscovery.js +164 -4
  16. package/dist/externalApiMeter.d.ts +8 -1
  17. package/dist/externalApiMeter.js +118 -25
  18. package/dist/index.d.ts +3 -1
  19. package/dist/index.js +3 -1
  20. package/dist/managedCollectorLauncher.d.ts +2 -1
  21. package/dist/managedCollectorLauncher.js +2 -1
  22. package/dist/mcpToolMeter.d.ts +101 -0
  23. package/dist/mcpToolMeter.js +496 -0
  24. package/dist/next.d.ts +28 -0
  25. package/dist/next.js +143 -10
  26. package/dist/nextConfig.d.ts +34 -0
  27. package/dist/nextConfig.js +73 -0
  28. package/dist/nextEntryTransform.d.ts +9 -0
  29. package/dist/nextEntryTransform.js +181 -0
  30. package/dist/runtime.d.ts +12 -6
  31. package/dist/runtime.js +113 -52
  32. package/dist/runtimeHealth.js +1 -1
  33. package/dist/tenantContext.d.ts +3 -0
  34. package/dist/tenantContext.js +35 -9
  35. package/dist/types.d.ts +8 -0
  36. package/examples/azure-functions-gate/.funcignore +6 -0
  37. package/examples/azure-functions-gate/README.md +25 -0
  38. package/examples/azure-functions-gate/host.json +15 -0
  39. package/examples/azure-functions-gate/package-lock.json +161 -0
  40. package/examples/azure-functions-gate/package.json +14 -0
  41. package/examples/azure-functions-gate/src/functions/health.js +27 -0
  42. package/fixtures/git/canonical_git_key_vectors.v1.json +66 -0
  43. package/loaders/next-entry-loader.cjs +14 -0
  44. package/package.json +3 -1
@@ -22,6 +22,36 @@ const FRAMEWORK_IMPORT_HINTS = [
22
22
  const HTTP_ROUTE = /\b(?:app|router|server|fastify|instance)\.(get|post|put|delete|patch|head|options)\(\s*['"`]([^'"`]+)['"`]/gi;
23
23
  const NEST_MAPPING = /@(Get|Post|Put|Delete|Patch|Head|Options)\(\s*(?:['"`]([^'"`]*)['"`])?\s*\)/g;
24
24
  const BULLMQ_QUEUE_ADD = /\.add\(\s*['"`]([A-Za-z0-9_.:-]+)['"`]/g;
25
+ const NEXT_HTTP_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];
26
+ const NEXT_EXPORT_FN = /\bexport\s+(?:async\s+)?function\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\b/g;
27
+ const NEXT_EXPORT_CONST = /\bexport\s+const\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\s*=/g;
28
+ const NEXT_PAGES_METHOD = /\b(?:req|request)\.method\s*===?\s*['"`](GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)['"`]/gi;
29
+ /**
30
+ * HTTP path implied by a Next.js App Router ``route`` file or Pages ``pages/api`` file.
31
+ * Route groups ``(marketing)`` and parallel slots ``@modal`` are dropped.
32
+ */
33
+ export function nextFileRoutePath(relPath) {
34
+ const n = relPath.replace(/\\/g, '/').replace(/^\.\//, '');
35
+ const app = n.match(/(?:^|\/)(?:src\/)?app\/(.+)\/route\.(?:[cm]?[jt]sx?)$/i);
36
+ if (app) {
37
+ const segs = app[1].split('/').filter((s) => {
38
+ if (!s)
39
+ return false;
40
+ if (s.startsWith('(') && s.endsWith(')'))
41
+ return false;
42
+ if (s.startsWith('@'))
43
+ return false;
44
+ return true;
45
+ });
46
+ return segs.length ? `/${segs.join('/')}` : '/';
47
+ }
48
+ const pages = n.match(/(?:^|\/)(?:src\/)?pages\/api\/(.+)\.(?:[cm]?[jt]sx?)$/i);
49
+ if (pages) {
50
+ const route = pages[1].replace(/\/index$/i, '');
51
+ return route ? `/api/${route}` : '/api';
52
+ }
53
+ return null;
54
+ }
25
55
  function lineOf(source, index) {
26
56
  if (index <= 0)
27
57
  return 1;
@@ -33,7 +63,10 @@ function lineOf(source, index) {
33
63
  return line;
34
64
  }
35
65
  function looksDynamic(path) {
36
- return path.includes(':') || path.includes('*') || path.includes('{');
66
+ return (path.includes(':') ||
67
+ path.includes('*') ||
68
+ path.includes('{') ||
69
+ path.includes('['));
37
70
  }
38
71
  function normalizePath(path) {
39
72
  if (!path)
@@ -41,13 +74,17 @@ function normalizePath(path) {
41
74
  return path.startsWith('/') ? path : `/${path}`;
42
75
  }
43
76
  /** Frameworks whose imports / requires appear in ``source``. */
44
- export function detectFrameworks(source) {
77
+ export function detectFrameworks(source, relPath = '') {
45
78
  const found = [];
46
79
  for (const hint of FRAMEWORK_IMPORT_HINTS) {
80
+ hint.pattern.lastIndex = 0;
47
81
  if (hint.pattern.test(source) && !found.includes(hint.id)) {
48
82
  found.push(hint.id);
49
83
  }
50
84
  }
85
+ if (nextFileRoutePath(relPath) && !found.includes('next')) {
86
+ found.push('next');
87
+ }
51
88
  return found;
52
89
  }
53
90
  function collectHttpRoutes(source, relPath) {
@@ -101,6 +138,64 @@ function collectNestRoutes(source, relPath) {
101
138
  }
102
139
  return endpoints;
103
140
  }
141
+ function collectNextFileRoutes(source, relPath) {
142
+ const routePath = nextFileRoutePath(relPath);
143
+ if (!routePath)
144
+ return [];
145
+ const firstLine = new Map();
146
+ const remember = (method, index) => {
147
+ const key = method.toUpperCase();
148
+ if (!firstLine.has(key))
149
+ firstLine.set(key, lineOf(source, index));
150
+ };
151
+ for (const src of [NEXT_EXPORT_FN, NEXT_EXPORT_CONST]) {
152
+ const re = new RegExp(src.source, 'g');
153
+ let match;
154
+ while ((match = re.exec(source)) !== null) {
155
+ remember(match[1] || 'GET', match.index);
156
+ }
157
+ }
158
+ const reExport = /\bexport\s*\{([^}]+)\}/g;
159
+ let exported;
160
+ while ((exported = reExport.exec(source)) !== null) {
161
+ const body = exported[1] || '';
162
+ for (const method of NEXT_HTTP_METHODS) {
163
+ if (new RegExp(`\\b${method}\\b`).test(body)) {
164
+ remember(method, exported.index);
165
+ }
166
+ }
167
+ }
168
+ const posix = relPath.replace(/\\/g, '/');
169
+ if (firstLine.size === 0 && /(?:^|\/)(?:src\/)?pages\/api\//.test(posix)) {
170
+ const re = new RegExp(NEXT_PAGES_METHOD.source, 'gi');
171
+ let match;
172
+ while ((match = re.exec(source)) !== null) {
173
+ remember(match[1] || 'GET', match.index);
174
+ }
175
+ if (firstLine.size === 0)
176
+ firstLine.set('GET', 1);
177
+ }
178
+ const entries = [...firstLine.entries()].sort((a, b) => a[1] - b[1]);
179
+ const totalLines = source.split(/\r?\n/).length;
180
+ return entries.map(([method, start], i) => {
181
+ const nextStart = entries[i + 1]?.[1];
182
+ const end = nextStart ? nextStart - 1 : totalLines;
183
+ return {
184
+ service_unit: `${method} ${routePath}`,
185
+ unit_type: 'endpoint',
186
+ method,
187
+ path: routePath,
188
+ handler: method,
189
+ file: relPath,
190
+ line: start,
191
+ end_line: Math.max(start, end),
192
+ is_async: true,
193
+ dynamic: looksDynamic(routePath),
194
+ prefix_resolved: true,
195
+ evidence: 'heuristic_source',
196
+ };
197
+ });
198
+ }
104
199
  function collectBullMqTasks(source, relPath) {
105
200
  const tasks = [];
106
201
  const seen = new Set();
@@ -143,10 +238,11 @@ function dedupeUnits(items) {
143
238
  */
144
239
  export function extractServiceUnits(sourceCode, relPath = '<unknown>') {
145
240
  const source = String(sourceCode || '');
146
- const frameworks = detectFrameworks(source);
241
+ const frameworks = detectFrameworks(source, relPath);
147
242
  const endpoints = [
148
243
  ...collectHttpRoutes(source, relPath),
149
244
  ...(frameworks.includes('nest') ? collectNestRoutes(source, relPath) : []),
245
+ ...collectNextFileRoutes(source, relPath),
150
246
  ];
151
247
  const tasks = frameworks.includes('bullmq') || /\bbullmq\b/.test(source)
152
248
  ? collectBullMqTasks(source, relPath)
@@ -175,8 +271,11 @@ export function buildServiceManifest(fileSources) {
175
271
  endpoints.push(...extracted.endpoints);
176
272
  tasks.push(...extracted.tasks);
177
273
  }
274
+ const hasNextUnits = endpoints.some((e) => nextFileRoutePath(e.file));
178
275
  let framework = null;
179
- if (endpoints.length && frameworksDetected.includes('express'))
276
+ if (hasNextUnits)
277
+ framework = 'next';
278
+ else if (endpoints.length && frameworksDetected.includes('express'))
180
279
  framework = 'express';
181
280
  else if (endpoints.length && frameworksDetected.includes('fastify'))
182
281
  framework = 'fastify';
@@ -201,6 +300,7 @@ export function buildServiceManifest(fileSources) {
201
300
  },
202
301
  notes: [
203
302
  'MP4 scaffold: heuristic JS/TS discovery only — not TypeScript AST, not CLI/VSCE wired, not resource findings',
303
+ 'Next App Router / Pages API: file-path route discovery (route.ts / pages/api)',
204
304
  'Does not modify RunSession upload or runtime metering pipelines',
205
305
  ],
206
306
  };
@@ -208,3 +308,63 @@ export function buildServiceManifest(fileSources) {
208
308
  export function isServiceProject(manifest) {
209
309
  return Boolean(manifest.stats.endpoints || manifest.stats.tasks);
210
310
  }
311
+ /**
312
+ * Attribute a finding to the owning endpoint/task when the file+line is unambiguous.
313
+ * Same rule as Python ``attach_service_units``: innermost span, else sole unit on the file.
314
+ */
315
+ export function attachServiceUnitsToFindings(findings, manifest) {
316
+ const index = new Map();
317
+ const add = (file, start, end, id, kind) => {
318
+ const key = file.replace(/\\/g, '/');
319
+ const list = index.get(key) || [];
320
+ list.push({ start, end: Math.max(start, end), id, kind });
321
+ index.set(key, list);
322
+ };
323
+ for (const unit of manifest.endpoints) {
324
+ add(unit.file, unit.line, unit.end_line, unit.service_unit, 'service_unit');
325
+ }
326
+ for (const unit of manifest.tasks) {
327
+ add(unit.file, unit.line, unit.end_line, unit.service_unit, 'task');
328
+ }
329
+ for (const list of index.values()) {
330
+ list.sort((a, b) => a.start - b.start || b.end - a.end);
331
+ }
332
+ const matchFile = (fp) => {
333
+ if (index.has(fp))
334
+ return index.get(fp) || [];
335
+ for (const [key, spans] of index) {
336
+ if (fp.endsWith(key) || key.endsWith(fp))
337
+ return spans;
338
+ }
339
+ return [];
340
+ };
341
+ return findings.map((finding) => {
342
+ const loc = finding.scope.source_locations[0];
343
+ if (!loc)
344
+ return finding;
345
+ const spans = matchFile(loc.path.replace(/\\/g, '/'));
346
+ if (!spans.length)
347
+ return finding;
348
+ const line = loc.start_line;
349
+ const containing = spans.filter((s) => s.start <= line && line <= s.end);
350
+ let chosen = containing.length
351
+ ? containing.reduce((a, b) => (b.end - b.start < a.end - a.start ? b : a))
352
+ : null;
353
+ if (!chosen && spans.length === 1)
354
+ chosen = spans[0] || null;
355
+ if (!chosen && spans.length > 1) {
356
+ const before = spans.filter((s) => s.start <= line).sort((a, b) => b.start - a.start);
357
+ chosen = before[0] || null;
358
+ }
359
+ if (!chosen)
360
+ return finding;
361
+ return {
362
+ ...finding,
363
+ scope: {
364
+ ...finding.scope,
365
+ kind: chosen.kind,
366
+ id: chosen.id.slice(0, 200),
367
+ },
368
+ };
369
+ });
370
+ }
@@ -12,7 +12,7 @@
12
12
  * fetch meter via ALS so the same call is not double-counted.
13
13
  */
14
14
  import type { ExternalApiV0 } from './types.js';
15
- export declare function providerForHost(host: string): string;
15
+ export declare function providerForHost(host: string, url?: string): string;
16
16
  export declare function hostFromUrl(url: unknown): string;
17
17
  export declare function modelFromUrl(url: unknown): string | null;
18
18
  export declare function tokensFromUsage(usage: Record<string, unknown>): [number, number];
@@ -48,6 +48,7 @@ export declare class StreamUsageMeter {
48
48
  error: boolean;
49
49
  serviceUnit: string | null;
50
50
  tenantId: string | null;
51
+ parentToolCallId?: string | null;
51
52
  });
52
53
  /** Resolves after ``finish`` (for flush / test coordination). */
53
54
  whenFinished(): Promise<void>;
@@ -82,6 +83,7 @@ export declare class ExternalApiAggregator {
82
83
  private byTenant;
83
84
  private excludedHosts;
84
85
  setExcludedHosts(hosts: Iterable<string>): void;
86
+ excludesHost(host: string): boolean;
85
87
  private isExcluded;
86
88
  hasData(): boolean;
87
89
  private recordProvider;
@@ -94,6 +96,7 @@ export declare class ExternalApiAggregator {
94
96
  serviceUnit?: string | null;
95
97
  model?: string | null;
96
98
  tenantId?: string | null;
99
+ url?: string;
97
100
  }): void;
98
101
  private providerRow;
99
102
  buildV0(): ExternalApiV0 | null;
@@ -102,6 +105,10 @@ export declare class ExternalApiAggregator {
102
105
  mergeSnapshot(snapshot: ExternalApiAggregator): void;
103
106
  clear(): void;
104
107
  }
108
+ /** In-flight metered ``fetch`` calls, including a stream usage parse still running. */
109
+ export declare function outboundHttpInFlight(): number;
110
+ /** Resolves immediately when no metered ``fetch`` is in flight. */
111
+ export declare function whenOutboundHttpIdle(): Promise<void>;
105
112
  export declare function _awaitPendingUsageParses(): Promise<void>;
106
113
  export declare function getExternalAggregator(): ExternalApiAggregator;
107
114
  export declare function getOriginalFetch(): typeof fetch | null;
@@ -12,8 +12,9 @@
12
12
  * fetch meter via ALS so the same call is not double-counted.
13
13
  */
14
14
  import { AsyncLocalStorage } from 'node:async_hooks';
15
+ import { activeMcpToolCallId, recordHttpInToolContext } from './mcpToolMeter.js';
15
16
  import { createRequire } from 'node:module';
16
- import { getTenantContext } from './tenantContext.js';
17
+ import { getTenantContext, tenantBucketId } from './tenantContext.js';
17
18
  import { attributionServiceUnit } from './requestContext.js';
18
19
  const requireFromHere = createRequire(import.meta.url);
19
20
  /**
@@ -39,7 +40,13 @@ const HOST_PROVIDER_PATTERNS = [
39
40
  ['textract.', 'aws_textract'],
40
41
  ['documentai.googleapis.com', 'google_document_ai'],
41
42
  ['vision.googleapis.com', 'google_document_ai'],
43
+ ['aiplatform.googleapis.com', 'vertex'],
44
+ ['gmail.googleapis.com', 'gmail'],
42
45
  ['generativelanguage.googleapis.com', 'google'],
46
+ ['graph.microsoft.com', 'microsoft_graph'],
47
+ ['api.pipedream.com', 'pipedream'],
48
+ ['pipedream.com', 'pipedream'],
49
+ ['finmo.ca', 'finmo'],
43
50
  ['googleapis.com', 'google'],
44
51
  ['cohere.ai', 'cohere'],
45
52
  ['cohere.com', 'cohere'],
@@ -52,8 +59,18 @@ function registeredDomain(host) {
52
59
  return parts.slice(-2).join('.');
53
60
  return host;
54
61
  }
55
- export function providerForHost(host) {
62
+ export function providerForHost(host, url) {
56
63
  const h = (host || '').toLowerCase().trim();
64
+ let path = '';
65
+ try {
66
+ path = new URL(String(url || '')).pathname.toLowerCase();
67
+ }
68
+ catch {
69
+ path = '';
70
+ }
71
+ // Drive shares www.googleapis.com with other Google APIs. The path is the identity.
72
+ if (h.includes('googleapis.com') && path.includes('/drive/'))
73
+ return 'google_drive';
57
74
  for (const [needle, provider] of HOST_PROVIDER_PATTERNS) {
58
75
  if (h.includes(needle))
59
76
  return provider;
@@ -445,6 +462,7 @@ export class StreamUsageMeter {
445
462
  usage: { input: this._inTok, output: this._outTok, model: this._model },
446
463
  serviceUnit: this.opts.serviceUnit,
447
464
  tenantId: this.opts.tenantId,
465
+ parentToolCallId: this.opts.parentToolCallId ?? null,
448
466
  });
449
467
  this._resolveFinish();
450
468
  }
@@ -644,6 +662,7 @@ function emptyProvider(key) {
644
662
  input_tokens: 0,
645
663
  output_tokens: 0,
646
664
  calls_with_usage: 0,
665
+ calls_unknown: 0,
647
666
  hosts: new Set(),
648
667
  models: new Set(),
649
668
  by_model: new Map(),
@@ -657,6 +676,9 @@ export class ExternalApiAggregator {
657
676
  setExcludedHosts(hosts) {
658
677
  this.excludedHosts = new Set([...hosts].map((h) => h.toLowerCase().trim()).filter(Boolean));
659
678
  }
679
+ excludesHost(host) {
680
+ return this.isExcluded(host);
681
+ }
660
682
  isExcluded(host) {
661
683
  if (isPrivateOrLocalHost(host))
662
684
  return true;
@@ -699,6 +721,8 @@ export class ExternalApiAggregator {
699
721
  const hasUsage = opts.inputTokens > 0 || opts.outputTokens > 0;
700
722
  if (hasUsage)
701
723
  p.calls_with_usage += 1;
724
+ else
725
+ p.calls_unknown += 1;
702
726
  if (opts.inputTokens > 0)
703
727
  p.input_tokens += opts.inputTokens;
704
728
  if (opts.outputTokens > 0)
@@ -731,6 +755,8 @@ export class ExternalApiAggregator {
731
755
  mAgg.errors += 1;
732
756
  if (hasUsage)
733
757
  mAgg.calls_with_usage += 1;
758
+ else
759
+ mAgg.calls_unknown = (mAgg.calls_unknown ?? 0) + 1;
734
760
  if (opts.inputTokens > 0)
735
761
  mAgg.input_tokens += opts.inputTokens;
736
762
  if (opts.outputTokens > 0)
@@ -739,7 +765,7 @@ export class ExternalApiAggregator {
739
765
  record(opts) {
740
766
  if (this.isExcluded(opts.host))
741
767
  return;
742
- const provider = providerForHost(opts.host);
768
+ const provider = providerForHost(opts.host, opts.url);
743
769
  const durationMs = opts.durationMs ?? 0;
744
770
  const error = Boolean(opts.error);
745
771
  const inputTokens = opts.inputTokens ?? 0;
@@ -777,10 +803,11 @@ export class ExternalApiAggregator {
777
803
  model,
778
804
  });
779
805
  if (tenantId) {
780
- let tenantBucket = this.byTenant.get(tenantId);
806
+ const bucketId = tenantBucketId(tenantId, this.byTenant);
807
+ let tenantBucket = this.byTenant.get(bucketId);
781
808
  if (!tenantBucket) {
782
809
  tenantBucket = new Map();
783
- this.byTenant.set(tenantId, tenantBucket);
810
+ this.byTenant.set(bucketId, tenantBucket);
784
811
  }
785
812
  this.recordProvider(tenantBucket, {
786
813
  provider,
@@ -799,6 +826,7 @@ export class ExternalApiAggregator {
799
826
  calls: p.calls,
800
827
  errors: p.errors,
801
828
  calls_with_usage: p.calls_with_usage,
829
+ ...(p.calls_unknown > 0 ? { calls_unknown: p.calls_unknown } : {}),
802
830
  };
803
831
  if (p.calls > 0) {
804
832
  row.duration_ms_avg = Math.round((p.duration_ms_total / p.calls) * 1000) / 1000;
@@ -824,6 +852,7 @@ export class ExternalApiAggregator {
824
852
  calls: m.calls,
825
853
  errors: m.errors,
826
854
  calls_with_usage: m.calls_with_usage,
855
+ ...((m.calls_unknown ?? 0) > 0 ? { calls_unknown: m.calls_unknown } : {}),
827
856
  };
828
857
  if (m.input_tokens)
829
858
  mRow.input_tokens = m.input_tokens;
@@ -900,6 +929,7 @@ export class ExternalApiAggregator {
900
929
  target.input_tokens += source.input_tokens;
901
930
  target.output_tokens += source.output_tokens;
902
931
  target.calls_with_usage += source.calls_with_usage;
932
+ target.calls_unknown += source.calls_unknown;
903
933
  for (const h of source.hosts) {
904
934
  if (target.hosts.size >= 8)
905
935
  break;
@@ -932,6 +962,7 @@ export class ExternalApiAggregator {
932
962
  dst.input_tokens += src.input_tokens;
933
963
  dst.output_tokens += src.output_tokens;
934
964
  dst.calls_with_usage += src.calls_with_usage;
965
+ dst.calls_unknown = (dst.calls_unknown ?? 0) + (src.calls_unknown ?? 0);
935
966
  }
936
967
  }
937
968
  mergeSnapshot(snapshot) {
@@ -956,7 +987,8 @@ export class ExternalApiAggregator {
956
987
  ExternalApiAggregator.mergeProvider(dst, src);
957
988
  }
958
989
  }
959
- for (const [tenant, srcBucket] of snapshot.byTenant.entries()) {
990
+ for (const [sourceTenant, srcBucket] of snapshot.byTenant.entries()) {
991
+ const tenant = tenantBucketId(sourceTenant, this.byTenant);
960
992
  let dstBucket = this.byTenant.get(tenant);
961
993
  if (!dstBucket) {
962
994
  this.byTenant.set(tenant, srcBucket);
@@ -986,6 +1018,32 @@ let originalFetch = null;
986
1018
  let restoreAxios = null;
987
1019
  let restoreUndici = null;
988
1020
  const pendingUsageParses = [];
1021
+ let outboundHttpActive = 0;
1022
+ const outboundIdleWaiters = [];
1023
+ function outboundHttpEnter() {
1024
+ outboundHttpActive += 1;
1025
+ }
1026
+ function outboundHttpLeave() {
1027
+ if (outboundHttpActive > 0)
1028
+ outboundHttpActive -= 1;
1029
+ if (outboundHttpActive === 0) {
1030
+ const waiters = outboundIdleWaiters.splice(0);
1031
+ for (const wake of waiters)
1032
+ wake();
1033
+ }
1034
+ }
1035
+ /** In-flight metered ``fetch`` calls, including a stream usage parse still running. */
1036
+ export function outboundHttpInFlight() {
1037
+ return outboundHttpActive;
1038
+ }
1039
+ /** Resolves immediately when no metered ``fetch`` is in flight. */
1040
+ export function whenOutboundHttpIdle() {
1041
+ if (outboundHttpActive === 0)
1042
+ return Promise.resolve();
1043
+ return new Promise((resolve) => {
1044
+ outboundIdleWaiters.push(resolve);
1045
+ });
1046
+ }
989
1047
  function trackStreamMeter(meter) {
990
1048
  pendingUsageParses.push(meter.whenFinished());
991
1049
  }
@@ -1000,7 +1058,16 @@ function recordMeteredCall(opts) {
1000
1058
  model: opts.usage.model || modelFromUrl(opts.url),
1001
1059
  serviceUnit: opts.serviceUnit,
1002
1060
  tenantId: opts.tenantId,
1061
+ url: opts.url,
1003
1062
  });
1063
+ if (opts.parentToolCallId && !globalAgg.excludesHost(opts.host)) {
1064
+ recordHttpInToolContext({
1065
+ toolCallId: opts.parentToolCallId,
1066
+ host: opts.host,
1067
+ durationMs: opts.durationMs,
1068
+ error: opts.error,
1069
+ });
1070
+ }
1004
1071
  }
1005
1072
  catch {
1006
1073
  /* never break caller */
@@ -1080,9 +1147,18 @@ function installFetchWrap() {
1080
1147
  // Snapshot inbound attribution when the outbound call starts — not after await.
1081
1148
  const serviceUnitAtStart = attributionServiceUnit();
1082
1149
  const tenantIdAtStart = getTenantContext()?.tenantId ?? null;
1150
+ const parentToolCallId = activeMcpToolCallId();
1083
1151
  const start = performance.now();
1084
1152
  let error = false;
1085
1153
  let response;
1154
+ outboundHttpEnter();
1155
+ let released = false;
1156
+ const release = () => {
1157
+ if (released)
1158
+ return;
1159
+ released = true;
1160
+ outboundHttpLeave();
1161
+ };
1086
1162
  try {
1087
1163
  response = await outboundMeterOwnerAls.run('fetch', () => base(input, init));
1088
1164
  if (response.status >= 400)
@@ -1104,33 +1180,41 @@ function installFetchWrap() {
1104
1180
  usage,
1105
1181
  serviceUnit: serviceUnitAtStart,
1106
1182
  tenantId: tenantIdAtStart,
1183
+ parentToolCallId,
1107
1184
  });
1108
1185
  };
1109
- if (!response) {
1110
- recordUsage({ input: 0, output: 0, model: modelFromUrl(url) });
1111
- }
1112
- else {
1113
- const captured = response;
1114
- const parse = async () => {
1115
- try {
1116
- return await extractUsageFromResponse(captured, url);
1117
- }
1118
- catch {
1119
- return { input: 0, output: 0, model: modelFromUrl(url) };
1120
- }
1121
- };
1122
- if (isStreamingContentType(captured)) {
1123
- pendingUsageParses.push(parse().then(recordUsage));
1186
+ try {
1187
+ if (!response) {
1188
+ recordUsage({ input: 0, output: 0, model: modelFromUrl(url) });
1189
+ release();
1124
1190
  }
1125
1191
  else {
1126
- try {
1127
- recordUsage(await parse());
1192
+ const captured = response;
1193
+ const parse = async () => {
1194
+ try {
1195
+ return await extractUsageFromResponse(captured, url);
1196
+ }
1197
+ catch {
1198
+ return { input: 0, output: 0, model: modelFromUrl(url) };
1199
+ }
1200
+ };
1201
+ if (isStreamingContentType(captured)) {
1202
+ pendingUsageParses.push(parse().then(recordUsage).finally(release));
1128
1203
  }
1129
- catch {
1130
- recordUsage({ input: 0, output: 0, model: modelFromUrl(url) });
1204
+ else {
1205
+ try {
1206
+ recordUsage(await parse());
1207
+ }
1208
+ catch {
1209
+ recordUsage({ input: 0, output: 0, model: modelFromUrl(url) });
1210
+ }
1211
+ release();
1131
1212
  }
1132
1213
  }
1133
1214
  }
1215
+ catch {
1216
+ release();
1217
+ }
1134
1218
  }
1135
1219
  };
1136
1220
  wrapped._netgreenerWrapped = true;
@@ -1166,6 +1250,7 @@ function installAxiosWrap() {
1166
1250
  const host = hostFromUrl(url);
1167
1251
  const serviceUnitAtStart = attributionServiceUnit();
1168
1252
  const tenantIdAtStart = getTenantContext()?.tenantId ?? null;
1253
+ const parentToolCallId = activeMcpToolCallId();
1169
1254
  const start = performance.now();
1170
1255
  let error = false;
1171
1256
  const invoke = () => originalRequest.apply(this, args);
@@ -1180,6 +1265,7 @@ function installAxiosWrap() {
1180
1265
  usage: { input: 0, output: 0, model: modelFromUrl(url) },
1181
1266
  serviceUnit: serviceUnitAtStart,
1182
1267
  tenantId: tenantIdAtStart,
1268
+ parentToolCallId,
1183
1269
  });
1184
1270
  return result;
1185
1271
  }
@@ -1204,6 +1290,7 @@ function installAxiosWrap() {
1204
1290
  error,
1205
1291
  serviceUnit: serviceUnitAtStart,
1206
1292
  tenantId: tenantIdAtStart,
1293
+ parentToolCallId,
1207
1294
  });
1208
1295
  if (attachStreamUsageMeter(resp.data, meter)) {
1209
1296
  trackStreamMeter(meter);
@@ -1227,6 +1314,7 @@ function installAxiosWrap() {
1227
1314
  usage,
1228
1315
  serviceUnit: serviceUnitAtStart,
1229
1316
  tenantId: tenantIdAtStart,
1317
+ parentToolCallId,
1230
1318
  });
1231
1319
  return result;
1232
1320
  };
@@ -1283,6 +1371,7 @@ function installUndiciWrap() {
1283
1371
  const host = hostFromUrl(url);
1284
1372
  const serviceUnitAtStart = attributionServiceUnit();
1285
1373
  const tenantIdAtStart = getTenantContext()?.tenantId ?? null;
1374
+ const parentToolCallId = activeMcpToolCallId();
1286
1375
  const start = performance.now();
1287
1376
  const finish = (result, threw) => {
1288
1377
  const durationMs = performance.now() - start;
@@ -1303,6 +1392,7 @@ function installUndiciWrap() {
1303
1392
  error,
1304
1393
  serviceUnit: serviceUnitAtStart,
1305
1394
  tenantId: tenantIdAtStart,
1395
+ parentToolCallId,
1306
1396
  });
1307
1397
  if (attachStreamUsageMeter(body, meter)) {
1308
1398
  trackStreamMeter(meter);
@@ -1318,6 +1408,7 @@ function installUndiciWrap() {
1318
1408
  usage: { input: 0, output: 0, model: modelFromUrl(url) },
1319
1409
  serviceUnit: serviceUnitAtStart,
1320
1410
  tenantId: tenantIdAtStart,
1411
+ parentToolCallId,
1321
1412
  });
1322
1413
  return result;
1323
1414
  };
@@ -1376,6 +1467,8 @@ function installUndiciWrap() {
1376
1467
  /** Test helper — reset install state. */
1377
1468
  export function _resetOutboundInstrumentationForTests() {
1378
1469
  pendingUsageParses.length = 0;
1470
+ outboundHttpActive = 0;
1471
+ outboundIdleWaiters.length = 0;
1379
1472
  if (originalFetch && typeof globalThis.fetch === 'function') {
1380
1473
  globalThis.fetch = originalFetch;
1381
1474
  }
package/dist/index.d.ts CHANGED
@@ -29,10 +29,12 @@ export { NetGreenerRuntime, getRuntime, _resetRuntimeForTests, type FlushKind, t
29
29
  export { netgreenerExpressMiddleware, getExpressRuntime, type ExpressRequest, type ExpressResponse, type ExpressNext, } from './express.js';
30
30
  export { netgreenerFastifyPlugin, getFastifyRuntime, fastifyRouteTemplate, bindFastifyTenant, type FastifyLike, type FastifyRequestLike, type FastifyReplyLike, } from './fastify.js';
31
31
  export { applyNetGreenerNestHooks, detectNestHttpAdapter, getNestRuntime, netgreenerNestExpressMiddleware, netgreenerNestFastifyPlugin, type NestApplicationLike, type NestHttpAdapterKind, } from './nest.js';
32
- export { UNKNOWN_NEXT_ROUTE_TEMPLATE, flushNetGreenerNext, getNextRuntime, nextRouteTemplate, registerNetGreenerNext, scheduleNetGreenerFlush, withNetGreenerRoute, type NetGreenerNextOptions, type NextRequestLike, type NextRouteContext, } from './next.js';
32
+ export { UNKNOWN_NEXT_ROUTE_TEMPLATE, flushNetGreenerNext, getNextRuntime, nextRouteTemplate, observeNextAction, observeNextPage, observeNextRoute, registerNetGreenerNext, scheduleNetGreenerFlush, withNetGreenerRoute, type NetGreenerNextOptions, type NextRequestLike, type NextRouteContext, } from './next.js';
33
+ export { withNetGreener, type NextConfigLike } from './nextConfig.js';
33
34
  export { attachWindowTenantToRunContext, getTenantContext, loadTenantConfig, normalizeTenantId, resolveTenantFromHttpRequest, resolveTenantFromTaskData, runWithTenantContext, setTenantContext, tenantConfigFromRuntime, type TenantConfig, type TenantContext, } from './tenantContext.js';
34
35
  export { ExternalApiAggregator, buildExternalV0, clearExternalAggregator, getExternalAggregator, installOutboundInstrumentation, providerForHost, usageFromClientBody, _resetOutboundInstrumentationForTests, } from './externalApiMeter.js';
35
36
  export { attributionServiceUnit, getRequestAttribution, runWithRequestAttribution, } from './requestContext.js';
37
+ export { McpToolBudgetBlocked, activeMcpToolCallId, beginToolCall, budgetMode, buildMcpLatencyV0, buildMcpToolV0, clearMcpToolLedger, finishToolCall, getMcpToolLedger, installMcpClientInstrumentation, recordHttpInToolContext, runWithMcpToolCall, type McpBudgetAction, type McpHttpLinkRow, type McpToolCallRow, type McpToolDecision, } from './mcpToolMeter.js';
36
38
  export { getBullMqRuntime, netgreenerBullMqProcessor, runBullMqJobWithNetGreener, type BullMqJobLike, type BullMqProcessorOptions, } from './bullmq.js';
37
39
  export { bindProcessTenant, flushProcessRuntime, runWithProcessTenant, startProcessRuntime, type ProcessRuntimeOptions, } from './processRuntime.js';
38
40
  export { buildServiceManifest, detectFrameworks, extractServiceUnits, isServiceProject, buildServiceManifestFromPaths, listCandidateSourceFiles, NON_RESOURCE_CATEGORIES, RESOURCE_DOMAINS, admitAnalyzeCandidate, isNonResourceCategory, languageForPath, toResourceFindingCandidate, detectRetryAmplificationHits, detectApiOverconsumptionHits, detectUnboundedParallelismHits, detectPollingIdleHits, detectProcessChurnHits, detectRepeatedWorkHits, detectPerItemInsteadOfBatchHits, detectModelLoadInLoopHits, detectFullDatasetInMemoryHits, detectRepeatedFileReadHits, detectAllHitsAst, detectHotPathHitsAst, detectApiOverconsumptionHitsAst, detectPollingIdleHitsAst, detectProcessChurnHitsAst, detectRepeatedFileReadHitsAst, detectRepeatedWorkHitsAst, detectRetryAmplificationHitsAst, detectUnboundedParallelismHitsAst, detectFullDatasetInMemoryHitsAst, detectPerItemInsteadOfBatchHitsAst, detectModelLoadInLoopHitsAst, collectPreferAstHits, detectApiOverconsumptionHitsPreferAst, detectPollingIdleHitsPreferAst, detectProcessChurnHitsPreferAst, detectRepeatedFileReadHitsPreferAst, detectRepeatedWorkHitsPreferAst, detectRetryAmplificationHitsPreferAst, detectUnboundedParallelismHitsPreferAst, detectFullDatasetInMemoryHitsPreferAst, detectPerItemInsteadOfBatchHitsPreferAst, detectModelLoadInLoopHitsPreferAst, isTypescriptAnalyzeAvailable, tryLoadTypescript, allResourceFindingsValid, filterValidResourceFindings, validateResourceFindingCandidate, countGateFindings, findingsExceedFailThreshold, formatAnalyzeGateFailureMessage, resolveAnalyzeGateLimit, buildAnalyzeGateSummary, buildResourceAnalyzeResultFromPaths, buildResourceFindingsFromSources, type ServiceEndpointUnit, type ServiceFileExtraction, type ServiceManifestV0, type ServiceTaskUnit, type ListCandidateSourceFilesOptions, type AdmissionDecision, type NonResourceCategory, type ResourceDomain, type ResourceCandidateHit, type ResourceFindingCandidateV1, type ResourcePrimaryDomain, type BuildResourceFindingsOptions, type ResourceAnalyzeResultV0, type ResourceFindingValidation, type ResourceFindingValidationError, type AnalyzeGateLimit, type AnalyzeGateSummaryV0, type AstHitBundle, } from './analyze/index.js';
package/dist/index.js CHANGED
@@ -28,10 +28,12 @@ export { NetGreenerRuntime, getRuntime, _resetRuntimeForTests, } from './runtime
28
28
  export { netgreenerExpressMiddleware, getExpressRuntime, } from './express.js';
29
29
  export { netgreenerFastifyPlugin, getFastifyRuntime, fastifyRouteTemplate, bindFastifyTenant, } from './fastify.js';
30
30
  export { applyNetGreenerNestHooks, detectNestHttpAdapter, getNestRuntime, netgreenerNestExpressMiddleware, netgreenerNestFastifyPlugin, } from './nest.js';
31
- export { UNKNOWN_NEXT_ROUTE_TEMPLATE, flushNetGreenerNext, getNextRuntime, nextRouteTemplate, registerNetGreenerNext, scheduleNetGreenerFlush, withNetGreenerRoute, } from './next.js';
31
+ export { UNKNOWN_NEXT_ROUTE_TEMPLATE, flushNetGreenerNext, getNextRuntime, nextRouteTemplate, observeNextAction, observeNextPage, observeNextRoute, registerNetGreenerNext, scheduleNetGreenerFlush, withNetGreenerRoute, } from './next.js';
32
+ export { withNetGreener } from './nextConfig.js';
32
33
  export { attachWindowTenantToRunContext, getTenantContext, loadTenantConfig, normalizeTenantId, resolveTenantFromHttpRequest, resolveTenantFromTaskData, runWithTenantContext, setTenantContext, tenantConfigFromRuntime, } from './tenantContext.js';
33
34
  export { ExternalApiAggregator, buildExternalV0, clearExternalAggregator, getExternalAggregator, installOutboundInstrumentation, providerForHost, usageFromClientBody, _resetOutboundInstrumentationForTests, } from './externalApiMeter.js';
34
35
  export { attributionServiceUnit, getRequestAttribution, runWithRequestAttribution, } from './requestContext.js';
36
+ export { McpToolBudgetBlocked, activeMcpToolCallId, beginToolCall, budgetMode, buildMcpLatencyV0, buildMcpToolV0, clearMcpToolLedger, finishToolCall, getMcpToolLedger, installMcpClientInstrumentation, recordHttpInToolContext, runWithMcpToolCall, } from './mcpToolMeter.js';
35
37
  export { getBullMqRuntime, netgreenerBullMqProcessor, runBullMqJobWithNetGreener, } from './bullmq.js';
36
38
  export { bindProcessTenant, flushProcessRuntime, runWithProcessTenant, startProcessRuntime, } from './processRuntime.js';
37
39
  export { buildServiceManifest, detectFrameworks, extractServiceUnits, isServiceProject, buildServiceManifestFromPaths, listCandidateSourceFiles, NON_RESOURCE_CATEGORIES, RESOURCE_DOMAINS, admitAnalyzeCandidate, isNonResourceCategory, languageForPath, toResourceFindingCandidate, detectRetryAmplificationHits, detectApiOverconsumptionHits, detectUnboundedParallelismHits, detectPollingIdleHits, detectProcessChurnHits, detectRepeatedWorkHits, detectPerItemInsteadOfBatchHits, detectModelLoadInLoopHits, detectFullDatasetInMemoryHits, detectRepeatedFileReadHits, detectAllHitsAst, detectHotPathHitsAst, detectApiOverconsumptionHitsAst, detectPollingIdleHitsAst, detectProcessChurnHitsAst, detectRepeatedFileReadHitsAst, detectRepeatedWorkHitsAst, detectRetryAmplificationHitsAst, detectUnboundedParallelismHitsAst, detectFullDatasetInMemoryHitsAst, detectPerItemInsteadOfBatchHitsAst, detectModelLoadInLoopHitsAst, collectPreferAstHits, detectApiOverconsumptionHitsPreferAst, detectPollingIdleHitsPreferAst, detectProcessChurnHitsPreferAst, detectRepeatedFileReadHitsPreferAst, detectRepeatedWorkHitsPreferAst, detectRetryAmplificationHitsPreferAst, detectUnboundedParallelismHitsPreferAst, detectFullDatasetInMemoryHitsPreferAst, detectPerItemInsteadOfBatchHitsPreferAst, detectModelLoadInLoopHitsPreferAst, isTypescriptAnalyzeAvailable, tryLoadTypescript, allResourceFindingsValid, filterValidResourceFindings, validateResourceFindingCandidate, countGateFindings, findingsExceedFailThreshold, formatAnalyzeGateFailureMessage, resolveAnalyzeGateLimit, buildAnalyzeGateSummary, buildResourceAnalyzeResultFromPaths, buildResourceFindingsFromSources, } from './analyze/index.js';
@@ -6,7 +6,8 @@
6
6
  * 2. Point child at ``NETGREENER_EXPORT_MODE=collector`` + endpoint
7
7
  * 3. On child exit: terminate collector (drain)
8
8
  *
9
- * Does **not** flip the package default (unset mode → direct).
9
+ * Unset mode stays direct. BullMQ meters in-process and does not use this
10
+ * switch. Python Celery workers default to managed inside the Python launcher.
10
11
  * Windows: ``tcp://`` only.
11
12
  */
12
13
  import { spawn, type ChildProcess } from 'node:child_process';
@@ -6,7 +6,8 @@
6
6
  * 2. Point child at ``NETGREENER_EXPORT_MODE=collector`` + endpoint
7
7
  * 3. On child exit: terminate collector (drain)
8
8
  *
9
- * Does **not** flip the package default (unset mode → direct).
9
+ * Unset mode stays direct. BullMQ meters in-process and does not use this
10
+ * switch. Python Celery workers default to managed inside the Python launcher.
10
11
  * Windows: ``tcp://`` only.
11
12
  */
12
13
  import { spawn } from 'node:child_process';