@netgreener/runtime 0.1.10 → 0.1.12

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 (39) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/aggregator.d.ts +3 -0
  3. package/dist/aggregator.js +36 -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/cli/helpText.js +1 -0
  17. package/dist/cli/netgreener.js +8 -0
  18. package/dist/cli/runtimeCli.d.ts +2 -0
  19. package/dist/cli/runtimeCli.js +35 -0
  20. package/dist/config.js +3 -1
  21. package/dist/exporter.js +9 -0
  22. package/dist/externalApiMeter.js +12 -7
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.js +1 -1
  25. package/dist/managedCollectorLauncher.d.ts +2 -1
  26. package/dist/managedCollectorLauncher.js +2 -1
  27. package/dist/mcpToolMeter.js +6 -1
  28. package/dist/processResources.js +5 -2
  29. package/dist/runtime.d.ts +4 -5
  30. package/dist/runtime.js +86 -50
  31. package/dist/runtimeControls.d.ts +5 -0
  32. package/dist/runtimeControls.js +69 -0
  33. package/dist/runtimeHealth.d.ts +15 -0
  34. package/dist/runtimeHealth.js +47 -1
  35. package/dist/tenantContext.d.ts +4 -0
  36. package/dist/tenantContext.js +36 -10
  37. package/dist/uploader.js +45 -1
  38. package/fixtures/git/canonical_git_key_vectors.v1.json +66 -0
  39. package/package.json +1 -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
+ }
@@ -10,6 +10,7 @@ Usage:
10
10
  netgreener help [command]
11
11
 
12
12
  Commands:
13
+ runtime status [--json] Check local runtime configuration (no network)
13
14
  analyze <projectDir> Local resource Analyze (manifest + findings)
14
15
  run <script> [projectDir] Execute script with process metering (+ optional upload)
15
16
  optimize <projectDir> Local proposals; --llm for api_server optimize session
@@ -14,6 +14,7 @@ import { runAnalyzeCli } from '../analyze/cli.js';
14
14
  import { formatOptimizeHelp, runOptimizeCli } from '../optimize/cli.js';
15
15
  import { CLI_VERSION, formatAnalyzeHelp, formatRootHelp, } from './helpText.js';
16
16
  import { formatRunHelp, runRunCli } from './runCli.js';
17
+ import { formatRuntimeHelp, runRuntimeCli } from './runtimeCli.js';
17
18
  function printHelpFor(topic) {
18
19
  const key = (topic || '').trim().toLowerCase();
19
20
  if (!key || key === 'help') {
@@ -28,6 +29,10 @@ function printHelpFor(topic) {
28
29
  process.stdout.write(formatRunHelp());
29
30
  return 0;
30
31
  }
32
+ if (key === 'runtime') {
33
+ process.stdout.write(formatRuntimeHelp());
34
+ return 0;
35
+ }
31
36
  if (key === 'optimize') {
32
37
  process.stdout.write(formatOptimizeHelp());
33
38
  return 0;
@@ -56,6 +61,9 @@ export function runNetgreenerCli(argv) {
56
61
  if (cmd === 'run') {
57
62
  return runRunCli(rest);
58
63
  }
64
+ if (cmd === 'runtime') {
65
+ return runRuntimeCli(rest);
66
+ }
59
67
  if (cmd === 'optimize') {
60
68
  return runOptimizeCli(rest);
61
69
  }
@@ -0,0 +1,2 @@
1
+ export declare function formatRuntimeHelp(): string;
2
+ export declare function runRuntimeCli(argv: string[]): number;
@@ -0,0 +1,35 @@
1
+ import { inspectRuntimeSetup } from '../runtimeHealth.js';
2
+ export function formatRuntimeHelp() {
3
+ return `Usage: netgreener runtime status [--json]
4
+
5
+ Check local runtime configuration without contacting the API.
6
+ Use NETGREENER_TOKEN and NETGREENER_PROJECT_ID; NETGREENER_SERVICE_RUNTIME=0 disables monitoring.
7
+ This does not verify running hooks, tenant binding, provider coverage, authentication, or uploads.
8
+
9
+ Options:
10
+ --json Print a credential-free JSON report
11
+ -h, --help Show this help
12
+ `;
13
+ }
14
+ export function runRuntimeCli(argv) {
15
+ if (!argv.length || argv.includes('--help') || argv.includes('-h')) {
16
+ process.stdout.write(formatRuntimeHelp());
17
+ return 0;
18
+ }
19
+ if (argv[0] !== 'status' || argv.slice(1).some(arg => arg !== '--json')) {
20
+ process.stderr.write(formatRuntimeHelp());
21
+ return 2;
22
+ }
23
+ const report = inspectRuntimeSetup();
24
+ if (argv.includes('--json')) {
25
+ process.stdout.write(`${JSON.stringify(report)}\n`);
26
+ }
27
+ else {
28
+ process.stdout.write('NetGreener runtime status (local configuration only)\n');
29
+ for (const check of report.checks) {
30
+ process.stdout.write(` ${check.name}: ${check.status} — ${check.detail}\n`);
31
+ }
32
+ process.stdout.write(report.ready ? 'Local configuration is ready; live verification is still required.\n' : 'Runtime upload is not ready.\n');
33
+ }
34
+ return report.ready ? 0 : 1;
35
+ }
package/dist/config.js CHANGED
@@ -39,10 +39,12 @@ function isPositiveSafeInteger(value) {
39
39
  * API URL: NETGREENER_API_URL (default https://core-api.netgreener.com)
40
40
  */
41
41
  export function loadRuntimeConfig(env = process.env) {
42
- const enabled = envTruthy(env, 'NETGREENER_SERVICE_RUNTIME');
43
42
  const apiUrl = (env.NETGREENER_API_URL || 'https://core-api.netgreener.com').replace(/\/+$/, '');
44
43
  const token = (env.NETGREENER_TOKEN || '').trim();
45
44
  const projectId = envPositiveSafeInteger(env, 'NETGREENER_PROJECT_ID', 0);
45
+ const enabled = (env.NETGREENER_SERVICE_RUNTIME || '').trim()
46
+ ? envTruthy(env, 'NETGREENER_SERVICE_RUNTIME')
47
+ : Boolean(token && projectId > 0 && /^[0-9]+$/.test((env.NETGREENER_PROJECT_ID || '').trim()));
46
48
  const organizationIdRaw = envPositiveSafeInteger(env, 'NETGREENER_ORG_ID', 0);
47
49
  const organizationSlug = (env.NETGREENER_ORG_SLUG || '').trim().toLowerCase() || null;
48
50
  const flushMinutes = envNumberInRange(env, 'NETGREENER_RUNTIME_FLUSH_MINUTES', DEFAULT_RUNTIME_FLUSH_MINUTES, MIN_RUNTIME_FLUSH_MINUTES, MAX_RUNTIME_FLUSH_MINUTES);
package/dist/exporter.js CHANGED
@@ -8,6 +8,7 @@
8
8
  * ``NETGREENER_MP2_SHADOW_COMPARE=1`` logs canonical payload digests only
9
9
  * (no dual-POST, no default flip). Matches Python bridge digest semantics.
10
10
  */
11
+ import { meteringAllowed, startRuntimeControls } from './runtimeControls.js';
11
12
  import { createHash } from 'node:crypto';
12
13
  import { sendCollectorEvent, newCollectorEventId, CollectorIpcError, } from './collectorIpc.js';
13
14
  import { uploadRunSession, } from './uploader.js';
@@ -142,6 +143,10 @@ export class ThinRunSessionExporter {
142
143
  export class CollectorRunSessionExporter {
143
144
  mode = 'collector';
144
145
  async exportRunSessionWindow(config, payload, _fetchImpl) {
146
+ startRuntimeControls(config, _fetchImpl ?? fetch);
147
+ if (!config.dryRun && !meteringAllowed(config)) {
148
+ return { ok: false, status: 423, error: 'Metering is paused by your organization administrator', mode: 'collector', durabilityGrade: 'durable_sibling_spool' };
149
+ }
145
150
  const endpoint = (process.env.NETGREENER_COLLECTOR_ENDPOINT || '').trim();
146
151
  if (!endpoint) {
147
152
  return {
@@ -218,6 +223,10 @@ export class CollectorRunSessionExporter {
218
223
  }
219
224
  }
220
225
  async exportObservationBatch(config, batch, options) {
226
+ startRuntimeControls(config, fetch);
227
+ if (!config.dryRun && !meteringAllowed(config)) {
228
+ return { ok: false, status: 423, error: 'Metering is paused by your organization administrator', mode: 'collector', durabilityGrade: 'durable_sibling_spool' };
229
+ }
221
230
  const endpoint = (process.env.NETGREENER_COLLECTOR_ENDPOINT || '').trim();
222
231
  if (!endpoint) {
223
232
  return {
@@ -11,10 +11,11 @@
11
11
  * Uploads restore unwrapped ``fetch``. Axios→fetch adapter paths skip the
12
12
  * fetch meter via ALS so the same call is not double-counted.
13
13
  */
14
+ import { meteringAllowed } from './runtimeControls.js';
14
15
  import { AsyncLocalStorage } from 'node:async_hooks';
15
16
  import { activeMcpToolCallId, recordHttpInToolContext } from './mcpToolMeter.js';
16
17
  import { createRequire } from 'node:module';
17
- import { getTenantContext } from './tenantContext.js';
18
+ import { getTenantContext, tenantBucketId } from './tenantContext.js';
18
19
  import { attributionServiceUnit } from './requestContext.js';
19
20
  const requireFromHere = createRequire(import.meta.url);
20
21
  /**
@@ -803,10 +804,11 @@ export class ExternalApiAggregator {
803
804
  model,
804
805
  });
805
806
  if (tenantId) {
806
- let tenantBucket = this.byTenant.get(tenantId);
807
+ const bucketId = tenantBucketId(tenantId, this.byTenant);
808
+ let tenantBucket = this.byTenant.get(bucketId);
807
809
  if (!tenantBucket) {
808
810
  tenantBucket = new Map();
809
- this.byTenant.set(tenantId, tenantBucket);
811
+ this.byTenant.set(bucketId, tenantBucket);
810
812
  }
811
813
  this.recordProvider(tenantBucket, {
812
814
  provider,
@@ -986,7 +988,8 @@ export class ExternalApiAggregator {
986
988
  ExternalApiAggregator.mergeProvider(dst, src);
987
989
  }
988
990
  }
989
- for (const [tenant, srcBucket] of snapshot.byTenant.entries()) {
991
+ for (const [sourceTenant, srcBucket] of snapshot.byTenant.entries()) {
992
+ const tenant = tenantBucketId(sourceTenant, this.byTenant);
990
993
  let dstBucket = this.byTenant.get(tenant);
991
994
  if (!dstBucket) {
992
995
  this.byTenant.set(tenant, srcBucket);
@@ -1046,6 +1049,8 @@ function trackStreamMeter(meter) {
1046
1049
  pendingUsageParses.push(meter.whenFinished());
1047
1050
  }
1048
1051
  function recordMeteredCall(opts) {
1052
+ if (!meteringAllowed())
1053
+ return;
1049
1054
  try {
1050
1055
  globalAgg.record({
1051
1056
  host: opts.host,
@@ -1133,7 +1138,7 @@ function installFetchWrap() {
1133
1138
  const base = originalFetch;
1134
1139
  const wrapped = async (input, init) => {
1135
1140
  // Axios (or another outer wrap) already owns this call.
1136
- if (outboundMeterOwnerAls.getStore()) {
1141
+ if (!meteringAllowed() || outboundMeterOwnerAls.getStore()) {
1137
1142
  return base(input, init);
1138
1143
  }
1139
1144
  const url = typeof input === 'string'
@@ -1234,7 +1239,7 @@ function installAxiosWrap() {
1234
1239
  return;
1235
1240
  const originalRequest = proto.request;
1236
1241
  const wrappedRequest = function netgreenerAxiosRequest(...args) {
1237
- if (outboundMeterOwnerAls.getStore()) {
1242
+ if (!meteringAllowed() || outboundMeterOwnerAls.getStore()) {
1238
1243
  return originalRequest.apply(this, args);
1239
1244
  }
1240
1245
  const configOrUrl = args[0];
@@ -1349,7 +1354,7 @@ function installUndiciWrap() {
1349
1354
  const wrapUndiciRequest = (original) => {
1350
1355
  const wrapped = function netgreenerUndiciRequest(...args) {
1351
1356
  // Outer fetch/axios already recorded — undici is the transport only.
1352
- if (outboundMeterOwnerAls.getStore()) {
1357
+ if (!meteringAllowed() || outboundMeterOwnerAls.getStore()) {
1353
1358
  return original.apply(this, args);
1354
1359
  }
1355
1360
  // Dispatcher.prototype.request(opts) vs undici.request(url, opts)
package/dist/index.d.ts CHANGED
@@ -17,7 +17,7 @@ export { CgroupV2Reader, resolveCgroupV2Path, snapshotDelta, type CgroupV2Snapsh
17
17
  export { sampleProcessTree, processTreeAvailability, collectDescendantPids, CPU_SOURCE_PROCESS_TREE, MEMORY_SOURCE_PROCESS_TREE, type ProcessTreeSample, } from './processTree.js';
18
18
  export { startManagedCollector, normalizedCollectorMode, childEnvForManagedCollector, reserveTcpEndpoint, type ManagedCollectorHandle, type CollectorMode, } from './managedCollectorLauncher.js';
19
19
  export { OccupancyTracker, getOccupancyTracker, type OccupancyWindowSnapshot, } from './occupancyTracker.js';
20
- export { buildRuntimeHealthV0, mergeHealthIntoMetadata, markHooksActive, type RuntimeHealthV0, type RuntimeHealthStatus, } from './runtimeHealth.js';
20
+ export { buildRuntimeHealthV0, inspectRuntimeSetup, mergeHealthIntoMetadata, markHooksActive, type RuntimeHealthV0, type RuntimeHealthStatus, type RuntimeSetupCheck, } from './runtimeHealth.js';
21
21
  export { newRuntimeWindowId } from './windowId.js';
22
22
  export { uploadRunSession, type RunSessionCreatePayload, type UploadResult, } from './uploader.js';
23
23
  export { resolveObservationExporter, exportModeFromEnv, DirectRunSessionExporter, ThinRunSessionExporter, CollectorRunSessionExporter, type ExportMode, type ExportResult, type ObservationExporter, type ObservationBatchDocument, } from './exporter.js';
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ export { CgroupV2Reader, resolveCgroupV2Path, snapshotDelta, } from './cgroupV2.
16
16
  export { sampleProcessTree, processTreeAvailability, collectDescendantPids, CPU_SOURCE_PROCESS_TREE, MEMORY_SOURCE_PROCESS_TREE, } from './processTree.js';
17
17
  export { startManagedCollector, normalizedCollectorMode, childEnvForManagedCollector, reserveTcpEndpoint, } from './managedCollectorLauncher.js';
18
18
  export { OccupancyTracker, getOccupancyTracker, } from './occupancyTracker.js';
19
- export { buildRuntimeHealthV0, mergeHealthIntoMetadata, markHooksActive, } from './runtimeHealth.js';
19
+ export { buildRuntimeHealthV0, inspectRuntimeSetup, mergeHealthIntoMetadata, markHooksActive, } from './runtimeHealth.js';
20
20
  export { newRuntimeWindowId } from './windowId.js';
21
21
  export { uploadRunSession, } from './uploader.js';
22
22
  export { resolveObservationExporter, exportModeFromEnv, DirectRunSessionExporter, ThinRunSessionExporter, CollectorRunSessionExporter, } from './exporter.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';
@@ -12,6 +12,7 @@
12
12
  *
13
13
  * Raw tool arguments, results, and HTTP bodies are not stored.
14
14
  */
15
+ import { meteringAllowed } from './runtimeControls.js';
15
16
  import { AsyncLocalStorage } from 'node:async_hooks';
16
17
  import { randomUUID } from 'node:crypto';
17
18
  import { connect } from 'node:net';
@@ -171,6 +172,8 @@ export class McpToolLedger {
171
172
  return this.calls.length > 0;
172
173
  }
173
174
  beginToolCall(server, tool) {
175
+ if (!meteringAllowed())
176
+ return { callId: randomUUID(), server, tool, attempt: 1, action: 'allow', reason: null };
174
177
  const serverName = clean(server, 'unknown');
175
178
  const toolName = clean(tool, 'unknown');
176
179
  const scope = 'process';
@@ -203,6 +206,8 @@ export class McpToolLedger {
203
206
  };
204
207
  }
205
208
  finishToolCall(decision, opts) {
209
+ if (!meteringAllowed())
210
+ return;
206
211
  const blocked = decision.action === 'block';
207
212
  const row = {
208
213
  server: decision.server,
@@ -370,7 +375,7 @@ export function finishToolCall(decision, opts) {
370
375
  ledger.finishToolCall(decision, opts);
371
376
  }
372
377
  export function recordHttpInToolContext(row) {
373
- if (!row.toolCallId)
378
+ if (!meteringAllowed() || !row.toolCallId)
374
379
  return;
375
380
  ledger.recordHttpLink({
376
381
  toolCallId: row.toolCallId,
@@ -1,3 +1,4 @@
1
+ import { meteringAllowed } from './runtimeControls.js';
1
2
  /**
2
3
  * Process-level CPU / RSS sampling for Node Runtime (CAP-R4 / N5).
3
4
  *
@@ -16,7 +17,7 @@ let lastRssKb = 0;
16
17
  let lastRssAtMs = 0;
17
18
  /** Start a CPU interval (call at request/task entry). */
18
19
  export function beginProcessResourceSample() {
19
- return { cpuStart: process.cpuUsage() };
20
+ return { cpuStart: meteringAllowed() ? process.cpuUsage() : { user: 0, system: 0 } };
20
21
  }
21
22
  function readRssKb(force = false) {
22
23
  const now = performance.now();
@@ -37,6 +38,8 @@ function readRssKb(force = false) {
37
38
  * ``cpuUsage(previous)`` returns user+system microseconds since ``previous``.
38
39
  */
39
40
  export function endProcessResourceSample(mark) {
41
+ if (!meteringAllowed())
42
+ return { cpuTimeMs: 0, rssKb: 0 };
40
43
  const delta = process.cpuUsage(mark.cpuStart);
41
44
  const cpuTimeMs = (delta.user + delta.system) / 1000;
42
45
  return {
@@ -46,7 +49,7 @@ export function endProcessResourceSample(mark) {
46
49
  }
47
50
  /** Point-in-time RSS (KiB) when no interval mark is available. */
48
51
  export function currentRssKb() {
49
- return readRssKb(true);
52
+ return meteringAllowed() ? readRssKb(true) : 0;
50
53
  }
51
54
  /** Test helper — clear RSS cache. */
52
55
  export function _resetRssSampleCacheForTests() {
package/dist/runtime.d.ts CHANGED
@@ -37,11 +37,9 @@ export declare class NetGreenerRuntime {
37
37
  /** In-flight HTTP/task units — gates ephemeral idle flush. */
38
38
  private inFlight;
39
39
  private invokeFlushScheduled;
40
- /**
41
- * Sticky window id until a successful export — preserves idempotency across
42
- * retry of the same in-memory snapshot (serverless / network retry).
43
- */
44
- private stickyWindowId;
40
+ /** Exact pending window; observations arriving during retries stay separate. */
41
+ private pendingPayload;
42
+ private cpuWindowMark;
45
43
  constructor(opts?: NetGreenerRuntimeOptions);
46
44
  get lifecycle(): ResolvedRuntimeLifecycle['lifecycle'];
47
45
  get enabled(): boolean;
@@ -87,6 +85,7 @@ export declare class NetGreenerRuntime {
87
85
  cpuTimeMs?: number;
88
86
  peakRssKb?: number;
89
87
  }): void;
88
+ private exportPendingWindow;
90
89
  flush(kind?: FlushKind): Promise<UploadResult | null>;
91
90
  shutdown(): Promise<UploadResult | null>;
92
91
  }