@netgreener/runtime 0.1.8 → 0.1.10

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 (114) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/TUTORIAL.md +120 -29
  3. package/dist/aggregator.d.ts +12 -2
  4. package/dist/aggregator.js +135 -8
  5. package/dist/analyze/buildResourceFindings.d.ts +4 -0
  6. package/dist/analyze/buildResourceFindings.js +17 -0
  7. package/dist/analyze/cli.d.ts +1 -0
  8. package/dist/analyze/cli.js +22 -1
  9. package/dist/analyze/declaredExternalApis.d.ts +31 -0
  10. package/dist/analyze/declaredExternalApis.js +174 -0
  11. package/dist/analyze/index.d.ts +1 -0
  12. package/dist/analyze/index.js +1 -0
  13. package/dist/auto.d.ts +7 -0
  14. package/dist/auto.js +8 -0
  15. package/dist/bullmq.d.ts +2 -2
  16. package/dist/bullmq.js +28 -58
  17. package/dist/carbonLedger.d.ts +28 -0
  18. package/dist/carbonLedger.js +52 -0
  19. package/dist/cgroupV2.d.ts +44 -0
  20. package/dist/cgroupV2.js +174 -0
  21. package/dist/cli/helpText.d.ts +3 -3
  22. package/dist/cli/helpText.js +42 -35
  23. package/dist/cli/netgreener.d.ts +7 -7
  24. package/dist/cli/netgreener.js +23 -16
  25. package/dist/cli/runCli.d.ts +20 -0
  26. package/dist/cli/runCli.js +249 -0
  27. package/dist/cli/runPreload.d.ts +5 -0
  28. package/dist/cli/runPreload.js +43 -0
  29. package/dist/collectorMetadata.d.ts +1 -1
  30. package/dist/collectorMetadata.js +2 -0
  31. package/dist/config.d.ts +2 -0
  32. package/dist/config.js +6 -2
  33. package/dist/exporter.d.ts +7 -2
  34. package/dist/exporter.js +12 -6
  35. package/dist/express.js +17 -6
  36. package/dist/externalApiMeter.d.ts +51 -2
  37. package/dist/externalApiMeter.js +379 -26
  38. package/dist/fastify.js +17 -6
  39. package/dist/index.d.ts +12 -2
  40. package/dist/index.js +11 -1
  41. package/dist/init.d.ts +30 -0
  42. package/dist/init.js +31 -0
  43. package/dist/lifecycle.d.ts +45 -0
  44. package/dist/lifecycle.js +112 -0
  45. package/dist/managedCollectorLauncher.d.ts +37 -0
  46. package/dist/managedCollectorLauncher.js +162 -0
  47. package/dist/mcpToolMeter.d.ts +101 -0
  48. package/dist/mcpToolMeter.js +496 -0
  49. package/dist/n4/deploymentMatrix.d.ts +9 -5
  50. package/dist/n4/deploymentMatrix.js +13 -9
  51. package/dist/n4/missingnessInventory.js +3 -3
  52. package/dist/next.d.ts +89 -0
  53. package/dist/next.js +328 -0
  54. package/dist/nextConfig.d.ts +34 -0
  55. package/dist/nextConfig.js +73 -0
  56. package/dist/nextEntryTransform.d.ts +9 -0
  57. package/dist/nextEntryTransform.js +181 -0
  58. package/dist/occupancyTracker.d.ts +27 -0
  59. package/dist/occupancyTracker.js +87 -0
  60. package/dist/optimize/apply.d.ts +38 -0
  61. package/dist/optimize/apply.js +173 -0
  62. package/dist/optimize/cli.d.ts +26 -0
  63. package/dist/optimize/cli.js +323 -0
  64. package/dist/optimize/index.d.ts +6 -0
  65. package/dist/optimize/index.js +5 -0
  66. package/dist/optimize/llmApply.d.ts +22 -0
  67. package/dist/optimize/llmApply.js +80 -0
  68. package/dist/optimize/llmSession.d.ts +88 -0
  69. package/dist/optimize/llmSession.js +135 -0
  70. package/dist/optimize/provider.d.ts +17 -0
  71. package/dist/optimize/provider.js +119 -0
  72. package/dist/optimize/safePatches.d.ts +18 -0
  73. package/dist/optimize/safePatches.js +247 -0
  74. package/dist/optimize/types.d.ts +79 -0
  75. package/dist/optimize/types.js +6 -0
  76. package/dist/optimize/verification.d.ts +16 -0
  77. package/dist/optimize/verification.js +165 -0
  78. package/dist/processResources.d.ts +5 -0
  79. package/dist/processResources.js +28 -4
  80. package/dist/processRuntime.js +7 -28
  81. package/dist/processTree.d.ts +47 -0
  82. package/dist/processTree.js +148 -0
  83. package/dist/runContextEnrichment.d.ts +11 -0
  84. package/dist/runContextEnrichment.js +92 -0
  85. package/dist/runtime.d.ts +36 -1
  86. package/dist/runtime.js +140 -17
  87. package/dist/sdk/client.d.ts +152 -0
  88. package/dist/sdk/client.js +396 -0
  89. package/dist/sdk/gitUrl.d.ts +6 -0
  90. package/dist/sdk/gitUrl.js +87 -0
  91. package/dist/sdk/index.d.ts +5 -0
  92. package/dist/sdk/index.js +5 -0
  93. package/dist/serverlessHints.d.ts +9 -6
  94. package/dist/serverlessHints.js +10 -7
  95. package/dist/taskMeter.d.ts +14 -0
  96. package/dist/taskMeter.js +53 -0
  97. package/dist/types.d.ts +43 -0
  98. package/examples/aca-container-gate/Dockerfile +20 -0
  99. package/examples/aca-container-gate/azure-container-app.yaml +27 -0
  100. package/examples/aca-container-gate/gate.env +7 -0
  101. package/examples/aca-container-gate/server.mjs +117 -0
  102. package/examples/azure-functions-gate/.funcignore +6 -0
  103. package/examples/azure-functions-gate/README.md +25 -0
  104. package/examples/azure-functions-gate/host.json +15 -0
  105. package/examples/azure-functions-gate/package-lock.json +161 -0
  106. package/examples/azure-functions-gate/package.json +14 -0
  107. package/examples/azure-functions-gate/src/functions/health.js +27 -0
  108. package/examples/express-local-live-smoke.mjs +127 -0
  109. package/examples/extractRunId.mjs +11 -0
  110. package/examples/next-pilot-dry-run.mjs +32 -0
  111. package/examples/serverless-thin-gate.mjs +89 -0
  112. package/fixtures/run-smoke/hello.mjs +4 -0
  113. package/loaders/next-entry-loader.cjs +14 -0
  114. package/package.json +25 -2
@@ -6,11 +6,13 @@
6
6
  * - global ``fetch`` (primary)
7
7
  * - ``axios`` Axios.prototype.request when axios is resolvable (optional peer)
8
8
  * - ``undici.request`` / Dispatcher.request when undici is resolvable
9
+ * (body teed via ``StreamUsageMeter`` for SSE/JSON usage — Python twin)
9
10
  *
10
11
  * Uploads restore unwrapped ``fetch``. Axios→fetch adapter paths skip the
11
12
  * fetch meter via ALS so the same call is not double-counted.
12
13
  */
13
14
  import { AsyncLocalStorage } from 'node:async_hooks';
15
+ import { activeMcpToolCallId, recordHttpInToolContext } from './mcpToolMeter.js';
14
16
  import { createRequire } from 'node:module';
15
17
  import { getTenantContext } from './tenantContext.js';
16
18
  import { attributionServiceUnit } from './requestContext.js';
@@ -38,7 +40,13 @@ const HOST_PROVIDER_PATTERNS = [
38
40
  ['textract.', 'aws_textract'],
39
41
  ['documentai.googleapis.com', 'google_document_ai'],
40
42
  ['vision.googleapis.com', 'google_document_ai'],
43
+ ['aiplatform.googleapis.com', 'vertex'],
44
+ ['gmail.googleapis.com', 'gmail'],
41
45
  ['generativelanguage.googleapis.com', 'google'],
46
+ ['graph.microsoft.com', 'microsoft_graph'],
47
+ ['api.pipedream.com', 'pipedream'],
48
+ ['pipedream.com', 'pipedream'],
49
+ ['finmo.ca', 'finmo'],
42
50
  ['googleapis.com', 'google'],
43
51
  ['cohere.ai', 'cohere'],
44
52
  ['cohere.com', 'cohere'],
@@ -51,8 +59,18 @@ function registeredDomain(host) {
51
59
  return parts.slice(-2).join('.');
52
60
  return host;
53
61
  }
54
- export function providerForHost(host) {
62
+ export function providerForHost(host, url) {
55
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';
56
74
  for (const [needle, provider] of HOST_PROVIDER_PATTERNS) {
57
75
  if (h.includes(needle))
58
76
  return provider;
@@ -336,9 +354,234 @@ export async function extractUsageFromResponse(response, url) {
336
354
  return { input: 0, output: 0, model };
337
355
  }
338
356
  }
357
+ /**
358
+ * Accumulate SSE/NDJSON usage from a streamed body, then report once on finish.
359
+ * Twin of Python ``_StreamUsageMeter`` — does not steal the body from the caller;
360
+ * wraps read paths so chunks are teed into a bounded buffer.
361
+ */
362
+ export class StreamUsageMeter {
363
+ opts;
364
+ _buf = '';
365
+ _reported = false;
366
+ _inTok = 0;
367
+ _outTok = 0;
368
+ _model;
369
+ _error;
370
+ _finishPromise;
371
+ _resolveFinish = () => { };
372
+ constructor(opts) {
373
+ this.opts = opts;
374
+ this._error = opts.error;
375
+ this._model = modelFromUrl(opts.url);
376
+ this._finishPromise = new Promise((resolve) => {
377
+ this._resolveFinish = resolve;
378
+ });
379
+ }
380
+ /** Resolves after ``finish`` (for flush / test coordination). */
381
+ whenFinished() {
382
+ return this._finishPromise;
383
+ }
384
+ feed(chunk) {
385
+ if (this._reported || chunk == null)
386
+ return;
387
+ try {
388
+ let piece = '';
389
+ if (typeof chunk === 'string')
390
+ piece = chunk;
391
+ else if (Buffer.isBuffer(chunk))
392
+ piece = chunk.toString('utf8');
393
+ else if (chunk instanceof Uint8Array)
394
+ piece = Buffer.from(chunk).toString('utf8');
395
+ else if (typeof chunk === 'object' && ArrayBuffer.isView(chunk)) {
396
+ const view = chunk;
397
+ piece = Buffer.from(view.buffer, view.byteOffset, view.byteLength).toString('utf8');
398
+ }
399
+ else
400
+ piece = String(chunk);
401
+ if (!piece)
402
+ return;
403
+ this._buf += piece;
404
+ if (this._buf.length > MAX_USAGE_PARSE_BYTES) {
405
+ this._buf = this._buf.slice(-MAX_USAGE_PARSE_BYTES);
406
+ }
407
+ if (this._buf.includes('\n')) {
408
+ const extracted = usageFromSseText(this._buf);
409
+ if (extracted.input)
410
+ this._inTok = extracted.input;
411
+ if (extracted.output)
412
+ this._outTok = extracted.output;
413
+ if (extracted.model)
414
+ this._model = extracted.model;
415
+ if (this._buf.length > 8192)
416
+ this._buf = this._buf.slice(-4096);
417
+ }
418
+ }
419
+ catch {
420
+ /* never break caller stream */
421
+ }
422
+ }
423
+ applyUsage(usage) {
424
+ if (this._reported)
425
+ return;
426
+ if (usage.input)
427
+ this._inTok = usage.input;
428
+ if (usage.output)
429
+ this._outTok = usage.output;
430
+ if (usage.model)
431
+ this._model = usage.model;
432
+ }
433
+ finish(extra) {
434
+ if (this._reported) {
435
+ this._resolveFinish();
436
+ return;
437
+ }
438
+ this._reported = true;
439
+ if (extra && typeof extra.error === 'boolean')
440
+ this._error = extra.error;
441
+ try {
442
+ if (this._buf) {
443
+ const extracted = usageFromSseText(this._buf);
444
+ if (extracted.input)
445
+ this._inTok = extracted.input;
446
+ if (extracted.output)
447
+ this._outTok = extracted.output;
448
+ if (extracted.model)
449
+ this._model = extracted.model;
450
+ }
451
+ }
452
+ catch {
453
+ /* zeros already held */
454
+ }
455
+ this._buf = '';
456
+ // recordMeteredCall is defined later in this module; called only at runtime after init.
457
+ recordMeteredCall({
458
+ host: this.opts.host,
459
+ url: this.opts.url,
460
+ durationMs: performance.now() - this.opts.start,
461
+ error: this._error,
462
+ usage: { input: this._inTok, output: this._outTok, model: this._model },
463
+ serviceUnit: this.opts.serviceUnit,
464
+ tenantId: this.opts.tenantId,
465
+ parentToolCallId: this.opts.parentToolCallId ?? null,
466
+ });
467
+ this._resolveFinish();
468
+ }
469
+ }
470
+ /**
471
+ * Tee usage from a Readable-like body without replacing it (keeps undici helpers).
472
+ * Returns false when the body cannot be instrumented.
473
+ */
474
+ export function attachStreamUsageMeter(body, meter) {
475
+ if (!body || typeof body !== 'object')
476
+ return false;
477
+ const stream = body;
478
+ if (stream._netgreenerUsageMeterAttached)
479
+ return true;
480
+ stream._netgreenerUsageMeterAttached = true;
481
+ let attached = false;
482
+ const asyncIter = stream[Symbol.asyncIterator];
483
+ if (typeof asyncIter === 'function') {
484
+ stream[Symbol.asyncIterator] = async function* netgreenerUsageAsyncIter() {
485
+ // undici returns an AsyncIterator (not always an AsyncIterable). Drive
486
+ // ``next()`` directly so ``body.text()`` / ``dump()`` keep working.
487
+ const iterator = asyncIter.call(stream);
488
+ try {
489
+ while (true) {
490
+ const step = await iterator.next();
491
+ if (step.done)
492
+ break;
493
+ meter.feed(step.value);
494
+ yield step.value;
495
+ }
496
+ }
497
+ catch (err) {
498
+ meter.finish({ error: true });
499
+ throw err;
500
+ }
501
+ finally {
502
+ meter.finish();
503
+ }
504
+ };
505
+ attached = true;
506
+ }
507
+ let wrappedConsumeHelper = false;
508
+ for (const method of ['text', 'json', 'arrayBuffer', 'dump']) {
509
+ const orig = stream[method];
510
+ if (typeof orig !== 'function')
511
+ continue;
512
+ const bound = orig.bind(stream);
513
+ wrappedConsumeHelper = true;
514
+ stream[method] = async (...args) => {
515
+ try {
516
+ const result = await bound(...args);
517
+ if (method === 'text' && typeof result === 'string') {
518
+ meter.feed(result);
519
+ }
520
+ else if (method === 'json' &&
521
+ result &&
522
+ typeof result === 'object' &&
523
+ !Array.isArray(result)) {
524
+ const extracted = usageFromPayload(result);
525
+ meter.applyUsage({
526
+ input: extracted.input,
527
+ output: extracted.output,
528
+ model: extracted.model,
529
+ });
530
+ }
531
+ else if (method === 'arrayBuffer' && result instanceof ArrayBuffer) {
532
+ meter.feed(Buffer.from(result));
533
+ }
534
+ return result;
535
+ }
536
+ catch (err) {
537
+ meter.finish({ error: true });
538
+ throw err;
539
+ }
540
+ finally {
541
+ meter.finish();
542
+ }
543
+ };
544
+ attached = true;
545
+ }
546
+ if (typeof stream.on === 'function') {
547
+ const originalOn = stream.on.bind(stream);
548
+ const wrapListener = (event, listener) => {
549
+ // Preserve ``this`` — undici ``body.text()`` end handlers use ``this[kConsume]``.
550
+ if (event === 'data') {
551
+ return function (chunk, ...rest) {
552
+ meter.feed(chunk);
553
+ return listener.call(this, chunk, ...rest);
554
+ };
555
+ }
556
+ if (event === 'end' || event === 'close') {
557
+ return function (...args) {
558
+ // When text/json/dump are wrapped, those helpers finish after feed.
559
+ // Finishing here races await resume (microtasks) and drops SSE tokens.
560
+ if (!wrappedConsumeHelper)
561
+ meter.finish();
562
+ return listener.call(this, ...args);
563
+ };
564
+ }
565
+ if (event === 'error') {
566
+ return function (...args) {
567
+ meter.finish({ error: true });
568
+ return listener.call(this, ...args);
569
+ };
570
+ }
571
+ return listener;
572
+ };
573
+ stream.on = ((event, listener) => originalOn(event, wrapListener(event, listener)));
574
+ if (typeof stream.addListener === 'function') {
575
+ stream.addListener = stream.on;
576
+ }
577
+ attached = true;
578
+ }
579
+ return attached;
580
+ }
339
581
  /**
340
582
  * Usage extraction for axios (and similar) bodies already buffered by the client.
341
- * Does not read streams; returns zeros for stream/Buffer/unknown shapes.
583
+ * Does not read streams; returns zeros for stream/Buffer/unknown shapes
584
+ * (stream tokens are recovered via ``attachStreamUsageMeter``).
342
585
  */
343
586
  export function usageFromClientBody(data, url, contentType) {
344
587
  const model = modelFromUrl(url);
@@ -419,6 +662,7 @@ function emptyProvider(key) {
419
662
  input_tokens: 0,
420
663
  output_tokens: 0,
421
664
  calls_with_usage: 0,
665
+ calls_unknown: 0,
422
666
  hosts: new Set(),
423
667
  models: new Set(),
424
668
  by_model: new Map(),
@@ -432,6 +676,9 @@ export class ExternalApiAggregator {
432
676
  setExcludedHosts(hosts) {
433
677
  this.excludedHosts = new Set([...hosts].map((h) => h.toLowerCase().trim()).filter(Boolean));
434
678
  }
679
+ excludesHost(host) {
680
+ return this.isExcluded(host);
681
+ }
435
682
  isExcluded(host) {
436
683
  if (isPrivateOrLocalHost(host))
437
684
  return true;
@@ -474,6 +721,8 @@ export class ExternalApiAggregator {
474
721
  const hasUsage = opts.inputTokens > 0 || opts.outputTokens > 0;
475
722
  if (hasUsage)
476
723
  p.calls_with_usage += 1;
724
+ else
725
+ p.calls_unknown += 1;
477
726
  if (opts.inputTokens > 0)
478
727
  p.input_tokens += opts.inputTokens;
479
728
  if (opts.outputTokens > 0)
@@ -506,6 +755,8 @@ export class ExternalApiAggregator {
506
755
  mAgg.errors += 1;
507
756
  if (hasUsage)
508
757
  mAgg.calls_with_usage += 1;
758
+ else
759
+ mAgg.calls_unknown = (mAgg.calls_unknown ?? 0) + 1;
509
760
  if (opts.inputTokens > 0)
510
761
  mAgg.input_tokens += opts.inputTokens;
511
762
  if (opts.outputTokens > 0)
@@ -514,7 +765,7 @@ export class ExternalApiAggregator {
514
765
  record(opts) {
515
766
  if (this.isExcluded(opts.host))
516
767
  return;
517
- const provider = providerForHost(opts.host);
768
+ const provider = providerForHost(opts.host, opts.url);
518
769
  const durationMs = opts.durationMs ?? 0;
519
770
  const error = Boolean(opts.error);
520
771
  const inputTokens = opts.inputTokens ?? 0;
@@ -574,6 +825,7 @@ export class ExternalApiAggregator {
574
825
  calls: p.calls,
575
826
  errors: p.errors,
576
827
  calls_with_usage: p.calls_with_usage,
828
+ ...(p.calls_unknown > 0 ? { calls_unknown: p.calls_unknown } : {}),
577
829
  };
578
830
  if (p.calls > 0) {
579
831
  row.duration_ms_avg = Math.round((p.duration_ms_total / p.calls) * 1000) / 1000;
@@ -599,6 +851,7 @@ export class ExternalApiAggregator {
599
851
  calls: m.calls,
600
852
  errors: m.errors,
601
853
  calls_with_usage: m.calls_with_usage,
854
+ ...((m.calls_unknown ?? 0) > 0 ? { calls_unknown: m.calls_unknown } : {}),
602
855
  };
603
856
  if (m.input_tokens)
604
857
  mRow.input_tokens = m.input_tokens;
@@ -675,6 +928,7 @@ export class ExternalApiAggregator {
675
928
  target.input_tokens += source.input_tokens;
676
929
  target.output_tokens += source.output_tokens;
677
930
  target.calls_with_usage += source.calls_with_usage;
931
+ target.calls_unknown += source.calls_unknown;
678
932
  for (const h of source.hosts) {
679
933
  if (target.hosts.size >= 8)
680
934
  break;
@@ -707,6 +961,7 @@ export class ExternalApiAggregator {
707
961
  dst.input_tokens += src.input_tokens;
708
962
  dst.output_tokens += src.output_tokens;
709
963
  dst.calls_with_usage += src.calls_with_usage;
964
+ dst.calls_unknown = (dst.calls_unknown ?? 0) + (src.calls_unknown ?? 0);
710
965
  }
711
966
  }
712
967
  mergeSnapshot(snapshot) {
@@ -761,6 +1016,35 @@ let originalFetch = null;
761
1016
  let restoreAxios = null;
762
1017
  let restoreUndici = null;
763
1018
  const pendingUsageParses = [];
1019
+ let outboundHttpActive = 0;
1020
+ const outboundIdleWaiters = [];
1021
+ function outboundHttpEnter() {
1022
+ outboundHttpActive += 1;
1023
+ }
1024
+ function outboundHttpLeave() {
1025
+ if (outboundHttpActive > 0)
1026
+ outboundHttpActive -= 1;
1027
+ if (outboundHttpActive === 0) {
1028
+ const waiters = outboundIdleWaiters.splice(0);
1029
+ for (const wake of waiters)
1030
+ wake();
1031
+ }
1032
+ }
1033
+ /** In-flight metered ``fetch`` calls, including a stream usage parse still running. */
1034
+ export function outboundHttpInFlight() {
1035
+ return outboundHttpActive;
1036
+ }
1037
+ /** Resolves immediately when no metered ``fetch`` is in flight. */
1038
+ export function whenOutboundHttpIdle() {
1039
+ if (outboundHttpActive === 0)
1040
+ return Promise.resolve();
1041
+ return new Promise((resolve) => {
1042
+ outboundIdleWaiters.push(resolve);
1043
+ });
1044
+ }
1045
+ function trackStreamMeter(meter) {
1046
+ pendingUsageParses.push(meter.whenFinished());
1047
+ }
764
1048
  function recordMeteredCall(opts) {
765
1049
  try {
766
1050
  globalAgg.record({
@@ -772,7 +1056,16 @@ function recordMeteredCall(opts) {
772
1056
  model: opts.usage.model || modelFromUrl(opts.url),
773
1057
  serviceUnit: opts.serviceUnit,
774
1058
  tenantId: opts.tenantId,
1059
+ url: opts.url,
775
1060
  });
1061
+ if (opts.parentToolCallId && !globalAgg.excludesHost(opts.host)) {
1062
+ recordHttpInToolContext({
1063
+ toolCallId: opts.parentToolCallId,
1064
+ host: opts.host,
1065
+ durationMs: opts.durationMs,
1066
+ error: opts.error,
1067
+ });
1068
+ }
776
1069
  }
777
1070
  catch {
778
1071
  /* never break caller */
@@ -852,9 +1145,18 @@ function installFetchWrap() {
852
1145
  // Snapshot inbound attribution when the outbound call starts — not after await.
853
1146
  const serviceUnitAtStart = attributionServiceUnit();
854
1147
  const tenantIdAtStart = getTenantContext()?.tenantId ?? null;
1148
+ const parentToolCallId = activeMcpToolCallId();
855
1149
  const start = performance.now();
856
1150
  let error = false;
857
1151
  let response;
1152
+ outboundHttpEnter();
1153
+ let released = false;
1154
+ const release = () => {
1155
+ if (released)
1156
+ return;
1157
+ released = true;
1158
+ outboundHttpLeave();
1159
+ };
858
1160
  try {
859
1161
  response = await outboundMeterOwnerAls.run('fetch', () => base(input, init));
860
1162
  if (response.status >= 400)
@@ -876,33 +1178,41 @@ function installFetchWrap() {
876
1178
  usage,
877
1179
  serviceUnit: serviceUnitAtStart,
878
1180
  tenantId: tenantIdAtStart,
1181
+ parentToolCallId,
879
1182
  });
880
1183
  };
881
- if (!response) {
882
- recordUsage({ input: 0, output: 0, model: modelFromUrl(url) });
883
- }
884
- else {
885
- const captured = response;
886
- const parse = async () => {
887
- try {
888
- return await extractUsageFromResponse(captured, url);
889
- }
890
- catch {
891
- return { input: 0, output: 0, model: modelFromUrl(url) };
892
- }
893
- };
894
- if (isStreamingContentType(captured)) {
895
- pendingUsageParses.push(parse().then(recordUsage));
1184
+ try {
1185
+ if (!response) {
1186
+ recordUsage({ input: 0, output: 0, model: modelFromUrl(url) });
1187
+ release();
896
1188
  }
897
1189
  else {
898
- try {
899
- recordUsage(await parse());
1190
+ const captured = response;
1191
+ const parse = async () => {
1192
+ try {
1193
+ return await extractUsageFromResponse(captured, url);
1194
+ }
1195
+ catch {
1196
+ return { input: 0, output: 0, model: modelFromUrl(url) };
1197
+ }
1198
+ };
1199
+ if (isStreamingContentType(captured)) {
1200
+ pendingUsageParses.push(parse().then(recordUsage).finally(release));
900
1201
  }
901
- catch {
902
- recordUsage({ input: 0, output: 0, model: modelFromUrl(url) });
1202
+ else {
1203
+ try {
1204
+ recordUsage(await parse());
1205
+ }
1206
+ catch {
1207
+ recordUsage({ input: 0, output: 0, model: modelFromUrl(url) });
1208
+ }
1209
+ release();
903
1210
  }
904
1211
  }
905
1212
  }
1213
+ catch {
1214
+ release();
1215
+ }
906
1216
  }
907
1217
  };
908
1218
  wrapped._netgreenerWrapped = true;
@@ -938,6 +1248,7 @@ function installAxiosWrap() {
938
1248
  const host = hostFromUrl(url);
939
1249
  const serviceUnitAtStart = attributionServiceUnit();
940
1250
  const tenantIdAtStart = getTenantContext()?.tenantId ?? null;
1251
+ const parentToolCallId = activeMcpToolCallId();
941
1252
  const start = performance.now();
942
1253
  let error = false;
943
1254
  const invoke = () => originalRequest.apply(this, args);
@@ -952,6 +1263,7 @@ function installAxiosWrap() {
952
1263
  usage: { input: 0, output: 0, model: modelFromUrl(url) },
953
1264
  serviceUnit: serviceUnitAtStart,
954
1265
  tenantId: tenantIdAtStart,
1266
+ parentToolCallId,
955
1267
  });
956
1268
  return result;
957
1269
  }
@@ -964,11 +1276,29 @@ function installAxiosWrap() {
964
1276
  /* ignore */
965
1277
  }
966
1278
  const responseType = String(resp?.config?.responseType || cfg.responseType || '').toLowerCase();
967
- const streaming = responseType === 'stream';
1279
+ const ctype = headerContentType(resp?.headers);
1280
+ const streaming = responseType === 'stream' ||
1281
+ (ctype || '').toLowerCase().includes('text/event-stream') ||
1282
+ (ctype || '').toLowerCase().includes('application/stream+json');
1283
+ if (streaming && resp?.data && typeof resp.data === 'object') {
1284
+ const meter = new StreamUsageMeter({
1285
+ host,
1286
+ url,
1287
+ start,
1288
+ error,
1289
+ serviceUnit: serviceUnitAtStart,
1290
+ tenantId: tenantIdAtStart,
1291
+ parentToolCallId,
1292
+ });
1293
+ if (attachStreamUsageMeter(resp.data, meter)) {
1294
+ trackStreamMeter(meter);
1295
+ return result;
1296
+ }
1297
+ }
968
1298
  let usage = { input: 0, output: 0, model: modelFromUrl(url) };
969
- if (!streaming && resp) {
1299
+ if (resp) {
970
1300
  try {
971
- usage = usageFromClientBody(resp.data, url, headerContentType(resp.headers));
1301
+ usage = usageFromClientBody(resp.data, url, ctype);
972
1302
  }
973
1303
  catch {
974
1304
  /* zeros */
@@ -982,6 +1312,7 @@ function installAxiosWrap() {
982
1312
  usage,
983
1313
  serviceUnit: serviceUnitAtStart,
984
1314
  tenantId: tenantIdAtStart,
1315
+ parentToolCallId,
985
1316
  });
986
1317
  return result;
987
1318
  };
@@ -1038,6 +1369,7 @@ function installUndiciWrap() {
1038
1369
  const host = hostFromUrl(url);
1039
1370
  const serviceUnitAtStart = attributionServiceUnit();
1040
1371
  const tenantIdAtStart = getTenantContext()?.tenantId ?? null;
1372
+ const parentToolCallId = activeMcpToolCallId();
1041
1373
  const start = performance.now();
1042
1374
  const finish = (result, threw) => {
1043
1375
  const durationMs = performance.now() - start;
@@ -1047,7 +1379,25 @@ function installUndiciWrap() {
1047
1379
  if (typeof status === 'number' && status >= 400)
1048
1380
  error = true;
1049
1381
  }
1050
- // Do not consume undici body streams — call/error/duration only (tokens via fetch/axios JSON).
1382
+ const body = result && typeof result === 'object'
1383
+ ? result.body
1384
+ : undefined;
1385
+ if (!threw && body != null) {
1386
+ const meter = new StreamUsageMeter({
1387
+ host,
1388
+ url,
1389
+ start,
1390
+ error,
1391
+ serviceUnit: serviceUnitAtStart,
1392
+ tenantId: tenantIdAtStart,
1393
+ parentToolCallId,
1394
+ });
1395
+ if (attachStreamUsageMeter(body, meter)) {
1396
+ trackStreamMeter(meter);
1397
+ return result;
1398
+ }
1399
+ }
1400
+ // No body / attach failed — record call/error/duration without tokens.
1051
1401
  recordMeteredCall({
1052
1402
  host,
1053
1403
  url,
@@ -1056,6 +1406,7 @@ function installUndiciWrap() {
1056
1406
  usage: { input: 0, output: 0, model: modelFromUrl(url) },
1057
1407
  serviceUnit: serviceUnitAtStart,
1058
1408
  tenantId: tenantIdAtStart,
1409
+ parentToolCallId,
1059
1410
  });
1060
1411
  return result;
1061
1412
  };
@@ -1114,6 +1465,8 @@ function installUndiciWrap() {
1114
1465
  /** Test helper — reset install state. */
1115
1466
  export function _resetOutboundInstrumentationForTests() {
1116
1467
  pendingUsageParses.length = 0;
1468
+ outboundHttpActive = 0;
1469
+ outboundIdleWaiters.length = 0;
1117
1470
  if (originalFetch && typeof globalThis.fetch === 'function') {
1118
1471
  globalThis.fetch = originalFetch;
1119
1472
  }
package/dist/fastify.js CHANGED
@@ -3,6 +3,8 @@ import { enterTenantStore, resolveTenantFromHttpRequest, runWithTenantContext, s
3
3
  import { enterRequestAttribution } from './requestContext.js';
4
4
  import { UNKNOWN_ROUTE_TEMPLATE } from './express.js';
5
5
  import { beginProcessResourceSample, endProcessResourceSample, } from './processResources.js';
6
+ import { getOccupancyTracker } from './occupancyTracker.js';
7
+ import { randomUUID } from 'node:crypto';
6
8
  const MAX_ROUTE_TEMPLATE_LEN = 512;
7
9
  const REQ_STATE = Symbol.for('netgreener.fastify.requestState');
8
10
  /**
@@ -72,12 +74,14 @@ export function netgreenerFastifyPlugin(opts = {}) {
72
74
  ...opts,
73
75
  });
74
76
  runtime.start();
75
- const onSignal = () => {
76
- void runtime.shutdown();
77
- };
78
- process.once('beforeExit', onSignal);
79
- process.once('SIGINT', onSignal);
80
- process.once('SIGTERM', onSignal);
77
+ if (runtime.lifecycle === 'persistent') {
78
+ const onSignal = () => {
79
+ void runtime.shutdown();
80
+ };
81
+ process.once('beforeExit', onSignal);
82
+ process.once('SIGINT', onSignal);
83
+ process.once('SIGTERM', onSignal);
84
+ }
81
85
  const tenantConfig = tenantConfigFromRuntime(runtime.config);
82
86
  const plugin = async function netgreenerFastify(fastify) {
83
87
  if (!runtime.enabled)
@@ -92,13 +96,17 @@ export function netgreenerFastifyPlugin(opts = {}) {
92
96
  resolveServiceUnit,
93
97
  serviceUnit: resolveServiceUnit(),
94
98
  });
99
+ const requestId = randomUUID();
100
+ getOccupancyTracker().begin(requestId);
95
101
  setRequestState(req, {
96
102
  tenantStore,
97
103
  method,
98
104
  started: process.hrtime.bigint(),
99
105
  resourceMark: beginProcessResourceSample(),
100
106
  recorded: false,
107
+ requestId,
101
108
  });
109
+ runtime.beginRequest();
102
110
  if (typeof done === 'function') {
103
111
  ;
104
112
  done();
@@ -119,6 +127,8 @@ export function netgreenerFastifyPlugin(opts = {}) {
119
127
  const statusCode = typeof reply?.statusCode === 'number'
120
128
  ? reply.statusCode
121
129
  : 200;
130
+ const serviceUnit = httpServiceUnit(state.method, req);
131
+ getOccupancyTracker().end(state.requestId, serviceUnit);
122
132
  runWithTenantContext(state.tenantStore.current, () => {
123
133
  runtime.recordHttp({
124
134
  method: state.method,
@@ -129,6 +139,7 @@ export function netgreenerFastifyPlugin(opts = {}) {
129
139
  peakRssKb: resources.rssKb,
130
140
  });
131
141
  });
142
+ runtime.endRequest();
132
143
  if (typeof done === 'function')
133
144
  done();
134
145
  };