@netgreener/runtime 0.1.15 → 0.1.16

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.
@@ -0,0 +1,4 @@
1
+ export declare function writeAnalyzeCompletion(projectDir: string, outputFile: string, options: {
2
+ report?: Record<string, unknown>;
3
+ error?: string;
4
+ }): void;
@@ -0,0 +1,27 @@
1
+ /** Local report + completion event, matching Python's terminal/editor sync path. */
2
+ import { mkdirSync, writeFileSync } from 'node:fs';
3
+ import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
4
+ export function writeAnalyzeCompletion(projectDir, outputFile, options) {
5
+ const root = resolve(projectDir);
6
+ const target = resolve(root, outputFile);
7
+ const eventPath = join(root, '.netgreener', 'last_event.json');
8
+ if (target === eventPath)
9
+ throw new Error('Analyze report cannot overwrite last_event.json.');
10
+ const timestamp = new Date().toISOString();
11
+ if (options.report) {
12
+ mkdirSync(dirname(target), { recursive: true });
13
+ writeFileSync(target, JSON.stringify({ ...options.report, project_dir: root, completed_at: timestamp }, null, 2) + '\n', 'utf8');
14
+ }
15
+ const rel = relative(root, target);
16
+ if (isAbsolute(rel) || rel === '..' || rel.startsWith('../') || rel.startsWith('..\\'))
17
+ return;
18
+ const projectId = Number(options.report?.project_id);
19
+ mkdirSync(dirname(eventPath), { recursive: true });
20
+ writeFileSync(eventPath, JSON.stringify({
21
+ type: 'analyze', source: 'node_analyze_local', timestamp,
22
+ status: options.error ? 'failed' : 'complete',
23
+ ...(Number.isSafeInteger(projectId) && projectId > 0 ? { project_id: projectId } : {}),
24
+ ...(options.report ? { report_path: rel.replace(/\\/g, '/') } : {}),
25
+ ...(options.error ? { error: options.error.slice(0, 400) } : {}),
26
+ }, null, 2) + '\n', 'utf8');
27
+ }
@@ -11,6 +11,8 @@ export type BuildResourceFindingsOptions = ListCandidateSourceFilesOptions & {
11
11
  projectId?: string;
12
12
  /** Max candidate findings to return (default 50). */
13
13
  maxFindings?: number;
14
+ /** Optional human progress; does not alter the JSON result. */
15
+ onProgress?: (message: string) => void;
14
16
  };
15
17
  export type ResourceAnalyzeResultV0 = {
16
18
  schema: 'resource_analyze_result_v0';
@@ -21,9 +21,13 @@ function readProjectDependencyMap(projectDir) {
21
21
  return null;
22
22
  }
23
23
  }
24
- function collectHitsFromSources(sources) {
24
+ function collectHitsFromSources(sources, onProgress) {
25
25
  const hits = [];
26
- for (const [rel, source] of Object.entries(sources)) {
26
+ const entries = Object.entries(sources);
27
+ for (const [index, [rel, source]] of entries.entries()) {
28
+ if (index === 0 || (index + 1) % 25 === 0 || index === entries.length - 1) {
29
+ onProgress?.(`Static checks ${index + 1}/${entries.length}: ${rel}`);
30
+ }
27
31
  hits.push(...collectPreferAstHits(source, rel));
28
32
  }
29
33
  return hits;
@@ -35,7 +39,7 @@ export function buildResourceFindingsFromSources(sources, opts = {}) {
35
39
  const maxFindings = Math.max(1, opts.maxFindings ?? 50);
36
40
  const projectId = opts.projectId ?? 'local';
37
41
  const out = [];
38
- for (const hit of collectHitsFromSources(sources)) {
42
+ for (const hit of collectHitsFromSources(sources, opts.onProgress)) {
39
43
  const finding = toResourceFindingCandidate(hit, projectId);
40
44
  if (!finding)
41
45
  continue;
@@ -67,6 +71,7 @@ export function buildResourceAnalyzeResultFromPaths(projectDir, relPaths, opts =
67
71
  continue;
68
72
  }
69
73
  }
74
+ opts.onProgress?.(`Building service map for ${Object.keys(sources).length} files ...`);
70
75
  const manifest = buildServiceManifestFromPaths(projectDir, Object.keys(sources), opts);
71
76
  const findings = attachServiceUnitsToFindings(buildResourceFindingsFromSources(sources, opts), manifest);
72
77
  const by_mechanism = {};
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
3
  * Thin Node Analyze CLI — print ``service_manifest_v0`` + ``resource_finding_v1``
4
- * candidates for a project path. Never uploads; never touches RunSession.
4
+ * candidates for a project path. Reviews JS/TS with static checks and LLM; never uploads findings or touches RunSession.
5
5
  *
6
6
  * npx netgreener-analyze ./my-app
7
7
  * npm run example:analyze-cli -- ./fixtures/analyze-sample
@@ -14,8 +14,19 @@
14
14
  * --json-summary print machine-readable gate summary only (CI)
15
15
  * --fail-on any|<mechanism>[,…] CI gate (exit 1 when exceeded)
16
16
  * --max-at-or-above <n> with --fail-on: allow up to N matches (default 0)
17
- * --depth quick|standard|thorough Python-parity LLM file budget (accepted; static analyze ignores)
17
+ * --depth quick|standard|thorough LLM file budget (1 / 5 / 10 files)
18
18
  * --quiet suppress non-JSON status lines on stderr
19
19
  */
20
20
  /** Run analyze subcommand; ``argv`` is args after ``analyze`` / bin name. */
21
- export declare function runAnalyzeCli(argv: string[]): number;
21
+ export declare function runAnalyzeCli(argv: string[], options?: {
22
+ env?: NodeJS.ProcessEnv;
23
+ fetchImpl?: typeof fetch;
24
+ configDir?: string;
25
+ stdout?: {
26
+ write: (text: string) => unknown;
27
+ };
28
+ stderr?: {
29
+ write: (text: string) => unknown;
30
+ isTTY?: boolean;
31
+ };
32
+ }): Promise<number>;
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
3
  * Thin Node Analyze CLI — print ``service_manifest_v0`` + ``resource_finding_v1``
4
- * candidates for a project path. Never uploads; never touches RunSession.
4
+ * candidates for a project path. Reviews JS/TS with static checks and LLM; never uploads findings or touches RunSession.
5
5
  *
6
6
  * npx netgreener-analyze ./my-app
7
7
  * npm run example:analyze-cli -- ./fixtures/analyze-sample
@@ -14,21 +14,29 @@
14
14
  * --json-summary print machine-readable gate summary only (CI)
15
15
  * --fail-on any|<mechanism>[,…] CI gate (exit 1 when exceeded)
16
16
  * --max-at-or-above <n> with --fail-on: allow up to N matches (default 0)
17
- * --depth quick|standard|thorough Python-parity LLM file budget (accepted; static analyze ignores)
17
+ * --depth quick|standard|thorough LLM file budget (1 / 5 / 10 files)
18
18
  * --quiet suppress non-JSON status lines on stderr
19
19
  */
20
- import { accessSync, constants, statSync } from 'node:fs';
21
- import { resolve } from 'node:path';
20
+ import { accessSync, constants, readFileSync, statSync } from 'node:fs';
21
+ import { homedir } from 'node:os';
22
+ import { join, resolve } from 'node:path';
22
23
  import { pathToFileURL } from 'node:url';
23
24
  import { buildAnalyzeGateSummary, findingsExceedFailThreshold, formatAnalyzeGateFailureMessage, resolveAnalyzeGateLimit, } from './analyzeGate.js';
24
25
  import { buildResourceAnalyzeResultFromPaths } from './buildResourceFindings.js';
25
26
  import { formatAnalyzeHelp } from '../cli/helpText.js';
27
+ import { listCandidateSourceFiles } from './projectScan.js';
28
+ import { resolveNodeLlmConfig, runNodeLlmResourceReview } from './llmResourceReview.js';
29
+ import { loadAnalyzeRepositoryConfig } from './repoConfig.js';
30
+ import { writeAnalyzeCompletion } from './analyzeReport.js';
31
+ import { NetGreenerClient } from '../sdk/client.js';
32
+ import { loadRuntimeConfig } from '../config.js';
26
33
  const LLM_DEPTHS = new Set(['quick', 'standard', 'thorough']);
27
34
  function printHelp() {
28
35
  process.stdout.write(formatAnalyzeHelp());
29
36
  }
30
37
  function parseArgs(argv) {
31
38
  const flags = {
39
+ outputFile: null,
32
40
  projectId: 'local',
33
41
  paths: null,
34
42
  findingsOnly: false,
@@ -37,6 +45,7 @@ function parseArgs(argv) {
37
45
  failOn: null,
38
46
  maxAtOrAbove: null,
39
47
  depth: 'standard',
48
+ llm: null,
40
49
  quiet: false,
41
50
  help: false,
42
51
  positional: [],
@@ -77,32 +86,50 @@ function parseArgs(argv) {
77
86
  flags.depth = value;
78
87
  continue;
79
88
  }
89
+ if (arg === '--llm' || arg === '--no-llm') {
90
+ flags.llm = arg === '--llm';
91
+ continue;
92
+ }
93
+ if (arg === '--no-upload')
94
+ continue;
95
+ if (arg === '--output-file' || arg.startsWith('--output-file=')) {
96
+ const value = arg === '--output-file' ? argv[++i] : arg.slice('--output-file='.length);
97
+ if (!value || value.startsWith('-'))
98
+ throw new Error('--output-file requires a path.');
99
+ flags.outputFile = value;
100
+ continue;
101
+ }
80
102
  if (arg === '--quiet') {
81
103
  flags.quiet = true;
82
104
  continue;
83
105
  }
84
106
  if (arg === '--project-id') {
85
- flags.projectId = String(argv[++i] || 'local');
107
+ const value = argv[++i];
108
+ if (!value || value.startsWith('-'))
109
+ throw new Error('--project-id requires a value.');
110
+ flags.projectId = value;
86
111
  continue;
87
112
  }
88
113
  if (arg.startsWith('--project-id=')) {
89
- flags.projectId = arg.slice('--project-id='.length) || 'local';
114
+ flags.projectId = arg.slice('--project-id='.length);
115
+ if (!flags.projectId.trim())
116
+ throw new Error('--project-id requires a value.');
90
117
  continue;
91
118
  }
92
119
  if (arg === '--paths') {
93
120
  const raw = String(argv[++i] || '');
94
- flags.paths = raw
95
- .split(',')
96
- .map((p) => p.trim())
97
- .filter(Boolean);
121
+ flags.paths = [...(flags.paths ?? []), ...raw
122
+ .split(',')
123
+ .map((p) => p.trim())
124
+ .filter(Boolean)];
98
125
  continue;
99
126
  }
100
127
  if (arg.startsWith('--paths=')) {
101
- flags.paths = arg
102
- .slice('--paths='.length)
103
- .split(',')
104
- .map((p) => p.trim())
105
- .filter(Boolean);
128
+ flags.paths = [...(flags.paths ?? []), ...arg
129
+ .slice('--paths='.length)
130
+ .split(',')
131
+ .map((p) => p.trim())
132
+ .filter(Boolean)];
106
133
  continue;
107
134
  }
108
135
  if (arg === '--fail-on') {
@@ -129,35 +156,41 @@ function parseArgs(argv) {
129
156
  return flags;
130
157
  }
131
158
  /** Run analyze subcommand; ``argv`` is args after ``analyze`` / bin name. */
132
- export function runAnalyzeCli(argv) {
159
+ export async function runAnalyzeCli(argv, options = {}) {
160
+ const stdout = options.stdout ?? process.stdout;
161
+ const stderr = options.stderr ?? process.stderr;
133
162
  let flags;
134
163
  try {
135
164
  flags = parseArgs(argv);
136
165
  }
137
166
  catch (err) {
138
- process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
167
+ stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
139
168
  printHelp();
140
169
  return 2;
141
170
  }
142
- if (flags.help || flags.positional.length === 0) {
171
+ if (flags.help) {
143
172
  printHelp();
144
- return flags.help ? 0 : 2;
173
+ return 0;
174
+ }
175
+ if (flags.positional.length > 1) {
176
+ stderr.write('Analyze accepts one project directory. Use --paths to select files.\n');
177
+ return 2;
145
178
  }
146
179
  const modeCount = [flags.findingsOnly, flags.manifestOnly, flags.jsonSummary].filter(Boolean)
147
180
  .length;
148
181
  if (modeCount > 1) {
149
- process.stderr.write('Choose at most one of --findings-only / --manifest-only / --json-summary\n');
182
+ stderr.write('Choose at most one of --findings-only / --manifest-only / --json-summary\n');
150
183
  return 2;
151
184
  }
152
185
  if (flags.maxAtOrAbove != null && flags.failOn == null) {
153
- process.stderr.write('--max-at-or-above requires --fail-on\n');
186
+ stderr.write('--max-at-or-above requires --fail-on\n');
154
187
  return 2;
155
188
  }
156
189
  if (flags.maxAtOrAbove != null && (!Number.isFinite(flags.maxAtOrAbove) || flags.maxAtOrAbove < 0)) {
157
- process.stderr.write('--max-at-or-above must be a non-negative number\n');
190
+ stderr.write('--max-at-or-above must be a non-negative number\n');
158
191
  return 2;
159
192
  }
160
- const projectDir = resolve(flags.positional[0]);
193
+ const projectDir = resolve(flags.positional[0] || '.');
161
194
  try {
162
195
  accessSync(projectDir, constants.R_OK);
163
196
  if (!statSync(projectDir).isDirectory()) {
@@ -165,12 +198,135 @@ export function runAnalyzeCli(argv) {
165
198
  }
166
199
  }
167
200
  catch {
168
- process.stderr.write(`Project path is not a readable directory: ${projectDir}\n`);
201
+ stderr.write(`Project path is not a readable directory: ${projectDir}\n`);
169
202
  return 2;
170
203
  }
171
- const result = buildResourceAnalyzeResultFromPaths(projectDir, flags.paths, {
172
- projectId: flags.projectId,
204
+ const env = options.env ?? process.env;
205
+ const configDir = options.configDir ?? join(homedir(), '.netgreener');
206
+ const saved = {};
207
+ for (const name of ['config', 'credentials']) {
208
+ try {
209
+ saved[name] = JSON.parse(readFileSync(join(configDir, `${name}.json`), 'utf8'));
210
+ }
211
+ catch {
212
+ saved[name] = {};
213
+ }
214
+ }
215
+ let repo;
216
+ try {
217
+ repo = loadAnalyzeRepositoryConfig(projectDir);
218
+ }
219
+ catch (err) {
220
+ stderr.write(`Analyze failed: ${err instanceof Error ? err.message : String(err)}\n`);
221
+ if (flags.outputFile) {
222
+ try {
223
+ writeAnalyzeCompletion(projectDir, flags.outputFile, { error: err instanceof Error ? err.message : String(err) });
224
+ }
225
+ catch { /* Preserve the analysis error. */ }
226
+ }
227
+ return 2;
228
+ }
229
+ const cfg = loadRuntimeConfig({
230
+ ...env,
231
+ NETGREENER_TOKEN: env.NETGREENER_TOKEN || env.NETGREENER_SERVICE_TOKEN || String(saved.credentials?.access_token || ''),
232
+ NETGREENER_API_URL: env.NETGREENER_API_URL || repo.apiUrl || String(saved.config?.api_url || 'https://core-api.netgreener.com'),
233
+ NETGREENER_ORG_ID: env.NETGREENER_ORG_ID || String(saved.config?.organization_id || ''),
234
+ NETGREENER_ORG_SLUG: env.NETGREENER_ORG_SLUG || repo.orgSlug || String(saved.config?.organization_slug || ''),
173
235
  });
236
+ if (flags.projectId === 'local')
237
+ flags.projectId = env.NETGREENER_PROJECT_ID?.trim() || repo.projectId || 'local';
238
+ const large = (saved.config?.llm_large ?? {});
239
+ const byokKey = env.NETGREENER_LLM_KEY || String(large.key || saved.config?.llm_key || '');
240
+ const byokModel = env.NETGREENER_LLM_MODEL || String(large.model || saved.config?.llm_model || '');
241
+ const byokProvider = env.NETGREENER_LLM_PROVIDER || String(large.provider || saved.config?.llm_provider || '');
242
+ const byokEndpoint = env.NETGREENER_LLM_ENDPOINT || String(large.endpoint || saved.config?.llm_endpoint || '');
243
+ const byokConfigured = Boolean(byokKey || byokModel || byokProvider || byokEndpoint);
244
+ const useLlm = flags.llm ?? Boolean(cfg.token || byokConfigured);
245
+ const started = Date.now();
246
+ let currentStage = 'Preparing analysis';
247
+ const progress = (message) => {
248
+ currentStage = message;
249
+ if (!flags.quiet)
250
+ stderr.write(`${message}\n`);
251
+ };
252
+ const ticker = !flags.quiet && stderr.isTTY ? setInterval(() => {
253
+ stderr.write(`\r\x1b[2K${['|', '/', '-', '\\'][Math.floor((Date.now() - started) / 150) % 4]} ${currentStage} (${Math.floor((Date.now() - started) / 1000)}s)\r`);
254
+ }, 150) : undefined;
255
+ ticker?.unref();
256
+ let result;
257
+ let llmReview;
258
+ try {
259
+ let llmConfig;
260
+ if (useLlm) {
261
+ if (!byokConfigured && !cfg.token)
262
+ throw new Error('LLM review requires login or a BYOK profile; use --no-llm for static analysis.');
263
+ if (byokConfigured) {
264
+ llmConfig = resolveNodeLlmConfig({ api_key: byokKey, model: byokModel, provider: byokProvider || undefined, azure_endpoint: byokEndpoint });
265
+ }
266
+ const fetchImpl = (input, init) => (options.fetchImpl ?? fetch)(input, {
267
+ ...init, signal: init?.signal ?? AbortSignal.timeout(15_000),
268
+ });
269
+ const client = new NetGreenerClient({
270
+ token: cfg.token, baseUrl: cfg.apiUrl.endsWith('/api/v1') ? cfg.apiUrl : `${cfg.apiUrl}/api/v1`,
271
+ orgId: cfg.organizationId ?? undefined, fetchImpl,
272
+ environment: env.NETGREENER_ENV || repo.environment || String(saved.config?.environment || 'prod'),
273
+ });
274
+ if (cfg.token && cfg.organizationSlug)
275
+ await client.resolveOrgSlug(cfg.organizationSlug);
276
+ if (flags.projectId !== 'local') {
277
+ const projectId = Number(flags.projectId);
278
+ if (!Number.isSafeInteger(projectId) || projectId <= 0)
279
+ throw new Error('LLM review requires a positive numeric project ID.');
280
+ if (cfg.token) {
281
+ progress(`Checking project #${projectId} ...`);
282
+ await client.getProject(projectId);
283
+ }
284
+ }
285
+ if (!llmConfig) {
286
+ progress('Loading managed LLM configuration ...');
287
+ llmConfig = await client.getLlmConfig();
288
+ if (!llmConfig.api_key || !llmConfig.model || !llmConfig.azure_endpoint)
289
+ throw new Error('Managed LLM configuration is incomplete.');
290
+ }
291
+ }
292
+ else if (flags.llm !== false) {
293
+ progress('LLM review skipped: no login or BYOK configured. Set NETGREENER_TOKEN or NETGREENER_LLM_KEY + NETGREENER_LLM_MODEL.');
294
+ }
295
+ progress('Scanning JavaScript/TypeScript files ...');
296
+ const paths = flags.paths ?? listCandidateSourceFiles(projectDir);
297
+ result = buildResourceAnalyzeResultFromPaths(projectDir, paths, { projectId: flags.projectId, onProgress: progress });
298
+ if (llmConfig) {
299
+ const review = await runNodeLlmResourceReview({
300
+ projectDir, projectId: flags.projectId, paths, staticFindings: result.findings,
301
+ config: llmConfig, depth: flags.depth, fetchImpl: options.fetchImpl, progress,
302
+ });
303
+ llmReview = { filesReviewed: review.filesReviewed, filesPlanned: review.filesPlanned, errors: review.errors };
304
+ result.findings = review.findings;
305
+ result.stats.findings = result.findings.length;
306
+ result.stats.by_mechanism = {};
307
+ for (const finding of result.findings)
308
+ result.stats.by_mechanism[finding.mechanism_id] = (result.stats.by_mechanism[finding.mechanism_id] ?? 0) + 1;
309
+ for (const error of review.errors)
310
+ stderr.write(`LLM review failed: ${error}\n`);
311
+ progress(`LLM review complete: ${review.filesReviewed}/${review.filesPlanned} files (${Math.floor((Date.now() - started) / 1000)}s)`);
312
+ }
313
+ }
314
+ catch (err) {
315
+ stderr.write(`Analyze failed: ${err instanceof Error ? err.message : String(err)}\n`);
316
+ if (flags.outputFile) {
317
+ try {
318
+ writeAnalyzeCompletion(projectDir, flags.outputFile, { error: err instanceof Error ? err.message : String(err) });
319
+ }
320
+ catch { /* Preserve the analysis error. */ }
321
+ }
322
+ return 2;
323
+ }
324
+ finally {
325
+ if (ticker) {
326
+ clearInterval(ticker);
327
+ stderr.write('\r\x1b[2K');
328
+ }
329
+ }
174
330
  const summary = buildAnalyzeGateSummary({
175
331
  projectId: flags.projectId,
176
332
  filesScanned: result.stats.files_scanned,
@@ -178,30 +334,48 @@ export function runAnalyzeCli(argv) {
178
334
  failOn: flags.failOn,
179
335
  maxAtOrAbove: flags.maxAtOrAbove,
180
336
  });
337
+ const report = { ...result, ...(llmReview ? { llm_review: llmReview } : {}) };
338
+ if (flags.outputFile) {
339
+ try {
340
+ writeAnalyzeCompletion(projectDir, flags.outputFile, {
341
+ report, error: llmReview?.errors.length ? llmReview.errors.join('; ') : undefined,
342
+ });
343
+ }
344
+ catch (err) {
345
+ stderr.write(`Could not save Analyze report: ${err instanceof Error ? err.message : String(err)}\n`);
346
+ return 2;
347
+ }
348
+ }
349
+ const outputSummary = llmReview?.errors.length ? { ...summary, ok: false, exit_code: 2, errors: llmReview.errors } : summary;
181
350
  const payload = flags.jsonSummary
182
- ? summary
351
+ ? outputSummary
183
352
  : flags.findingsOnly
184
353
  ? result.findings
185
354
  : flags.manifestOnly
186
355
  ? result.manifest
187
- : result;
188
- process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
356
+ : report;
357
+ stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
189
358
  if (!flags.quiet && !flags.jsonSummary) {
190
- process.stderr.write(`netgreener analyze: files=${result.stats.files_scanned} findings=${result.stats.findings} declared_apis=${result.stats.declared_external_apis} upload=never\n`);
359
+ stderr.write(`netgreener analyze: files=${result.stats.files_scanned} findings=${result.stats.findings} declared_apis=${result.stats.declared_external_apis} upload=never\n`);
191
360
  }
361
+ if (llmReview?.errors.length)
362
+ return 2;
192
363
  const limit = resolveAnalyzeGateLimit(flags.failOn, flags.maxAtOrAbove);
193
364
  if (flags.failOn && limit != null) {
194
365
  if (findingsExceedFailThreshold(result.findings, flags.failOn, limit)) {
195
366
  if (!flags.jsonSummary) {
196
- process.stderr.write(`${formatAnalyzeGateFailureMessage(result.findings, flags.failOn, limit)}\n`);
367
+ stderr.write(`${formatAnalyzeGateFailureMessage(result.findings, flags.failOn, limit)}\n`);
197
368
  }
198
369
  return 1;
199
370
  }
200
371
  }
201
- return summary.exit_code;
372
+ return llmReview?.errors.length ? 2 : summary.exit_code;
202
373
  }
203
374
  const invokedDirectly = typeof process.argv[1] === 'string' &&
204
375
  pathToFileURL(resolve(process.argv[1])).href === import.meta.url;
205
376
  if (invokedDirectly) {
206
- process.exitCode = runAnalyzeCli(process.argv.slice(2));
377
+ runAnalyzeCli(process.argv.slice(2)).then(code => { process.exitCode = code; }).catch(err => {
378
+ process.stderr.write(`Analyze failed: ${err instanceof Error ? err.message : String(err)}\n`);
379
+ process.exitCode = 2;
380
+ });
207
381
  }
@@ -15,3 +15,4 @@ export { allResourceFindingsValid, filterValidResourceFindings, validateResource
15
15
  export { buildAnalyzeGateSummary, countGateFindings, findingsExceedFailThreshold, formatAnalyzeGateFailureMessage, resolveAnalyzeGateLimit, type AnalyzeGateLimit, type AnalyzeGateSummaryV0, } from './analyzeGate.js';
16
16
  export { buildResourceAnalyzeResultFromPaths, buildResourceFindingsFromSources, type BuildResourceFindingsOptions, type ResourceAnalyzeResultV0, } from './buildResourceFindings.js';
17
17
  export { collectDeclaredExternalApis, dependencyMapFromPackageJson, extractImportPackageRoots, type CollectDeclaredExternalApisInput, type DeclaredExternalApiEntry, } from './declaredExternalApis.js';
18
+ export { resolveNodeLlmConfig, runNodeLlmResourceReview, type NodeLlmReviewOptions, type NodeLlmReviewResult, type NodeLlmDepth, type LlmConfig } from './llmResourceReview.js';
@@ -15,3 +15,4 @@ export { allResourceFindingsValid, filterValidResourceFindings, validateResource
15
15
  export { buildAnalyzeGateSummary, countGateFindings, findingsExceedFailThreshold, formatAnalyzeGateFailureMessage, resolveAnalyzeGateLimit, } from './analyzeGate.js';
16
16
  export { buildResourceAnalyzeResultFromPaths, buildResourceFindingsFromSources, } from './buildResourceFindings.js';
17
17
  export { collectDeclaredExternalApis, dependencyMapFromPackageJson, extractImportPackageRoots, } from './declaredExternalApis.js';
18
+ export { resolveNodeLlmConfig, runNodeLlmResourceReview } from './llmResourceReview.js';
@@ -0,0 +1,32 @@
1
+ import { type ResourceFindingCandidateV1 } from './resourceFindingCandidate.js';
2
+ declare const DEPTH_MAX: Record<"quick" | "standard" | "thorough", number>;
3
+ export type NodeLlmDepth = keyof typeof DEPTH_MAX;
4
+ export type LlmConfig = {
5
+ api_key: string;
6
+ provider?: string;
7
+ azure_endpoint?: string;
8
+ model?: string;
9
+ api_version?: string;
10
+ };
11
+ /** One provider validation path shared by npm and the editor adapter. */
12
+ export declare function resolveNodeLlmConfig(config: LlmConfig): LlmConfig;
13
+ export type NodeLlmReviewOptions = {
14
+ projectDir: string;
15
+ projectId: string;
16
+ staticFindings: ResourceFindingCandidateV1[];
17
+ paths: string[];
18
+ config: LlmConfig;
19
+ depth?: NodeLlmDepth;
20
+ fetchImpl?: typeof fetch;
21
+ timeoutMs?: number;
22
+ progress?: (message: string) => void;
23
+ };
24
+ export type NodeLlmReviewResult = {
25
+ findings: ResourceFindingCandidateV1[];
26
+ filesReviewed: number;
27
+ filesPlanned: number;
28
+ errors: string[];
29
+ };
30
+ /** Bounded review; static results survive errors, which are always reported. */
31
+ export declare function runNodeLlmResourceReview(opts: NodeLlmReviewOptions): Promise<NodeLlmReviewResult>;
32
+ export {};
@@ -0,0 +1,285 @@
1
+ /** Shared Node Analyze LLM review, extracted from the editor reviewer. */
2
+ import { readFileSync, realpathSync, statSync } from 'node:fs';
3
+ import { isAbsolute, relative, resolve } from 'node:path';
4
+ import { toResourceFindingCandidate } from './resourceFindingCandidate.js';
5
+ import { filterValidResourceFindings } from './validateResourceFinding.js';
6
+ const MAX_SOURCE_CHARS = 12_000;
7
+ const ALLOWED_MECHANISMS = new Set([
8
+ "per_item_instead_of_batch",
9
+ "retry_amplification",
10
+ "external_api_overconsumption",
11
+ "unbounded_parallelism",
12
+ "polling_or_idle_work",
13
+ "repeated_work",
14
+ "process_or_runtime_churn",
15
+ "memory_retention_or_materialization",
16
+ ]);
17
+ const DEPTH_MAX = {
18
+ quick: 1,
19
+ standard: 5,
20
+ thorough: 10,
21
+ };
22
+ /** One provider validation path shared by npm and the editor adapter. */
23
+ export function resolveNodeLlmConfig(config) {
24
+ const endpoint = (config.azure_endpoint || '').trim().replace(/\/+$/, '');
25
+ const provider = (config.provider || (/openai\.azure\.com|cognitiveservices\.azure\.com/i.test(endpoint) ? 'azure_openai' : 'openai')).trim().toLowerCase();
26
+ if (!['openai', 'azure_openai', 'anthropic', 'ollama', 'custom'].includes(provider)) {
27
+ throw new Error(`Unsupported Node LLM provider: ${provider}`);
28
+ }
29
+ if (!config.model?.trim())
30
+ throw new Error('LLM model is required.');
31
+ if (['openai', 'azure_openai', 'anthropic'].includes(provider) && !config.api_key?.trim()) {
32
+ throw new Error(`LLM provider ${provider} requires an API key.`);
33
+ }
34
+ if (['azure_openai', 'ollama', 'custom'].includes(provider) && !endpoint) {
35
+ throw new Error(`LLM provider ${provider} requires an endpoint.`);
36
+ }
37
+ return { ...config, provider, api_key: (config.api_key || '').trim(), model: config.model.trim(), azure_endpoint: endpoint };
38
+ }
39
+ function systemPrompt() {
40
+ const mechs = [...ALLOWED_MECHANISMS].join(", ");
41
+ return `You are NetGreener Node Analyze (resource scope only).
42
+ Find resource-consumption risks in JavaScript/TypeScript that waste CPU, memory, network, or paid external API cost.
43
+
44
+ Respond with ONE JSON object (no markdown):
45
+ {
46
+ "findings": [
47
+ {
48
+ "mechanism_id": one of [${mechs}],
49
+ "primary_domain": "compute.cpu"|"memory.host"|"network.io"|"external_api"|"storage.io",
50
+ "title": "short resource title",
51
+ "start_line": integer or null,
52
+ "evidence": "short quote",
53
+ "recommended_fix": "actionable fix",
54
+ "confidence": 0.0-1.0
55
+ }
56
+ ]
57
+ }
58
+
59
+ Rules:
60
+ - Only emit resource mechanisms from the list. Never security, secrets, style, or architecture.
61
+ - Do not invent files or lines not present in the payload.
62
+ - Prefer new issues not already listed in static_candidates.
63
+ - If nothing resource-relevant, return {"findings":[]}.`;
64
+ }
65
+ function userPayload(filePath, source, staticCandidates) {
66
+ let code = source;
67
+ const payload = {
68
+ schema_version: 1,
69
+ task: "node_resource_llm_v0",
70
+ file_path: filePath,
71
+ };
72
+ if (code.length > MAX_SOURCE_CHARS) {
73
+ code = code.slice(0, MAX_SOURCE_CHARS);
74
+ payload.source_code_truncated_note = `truncated to ${MAX_SOURCE_CHARS} chars`;
75
+ }
76
+ payload.source_code = code;
77
+ payload.static_candidates = staticCandidates
78
+ .filter((f) => (f.scope.source_locations[0]?.path || "").replace(/\\/g, "/") === filePath.replace(/\\/g, "/"))
79
+ .map((f) => ({
80
+ mechanism_id: f.mechanism_id,
81
+ title: f.title,
82
+ start_line: f.scope.source_locations[0]?.start_line,
83
+ }));
84
+ return payload;
85
+ }
86
+ function extractJsonObject(raw) {
87
+ const cleaned = raw.replace(/```json/gi, "").replace(/```/g, "").trim();
88
+ const m = cleaned.match(/\{[\s\S]*\}\s*$/);
89
+ return JSON.parse(m ? m[0] : cleaned);
90
+ }
91
+ function parseHits(raw, filePath) {
92
+ const data = extractJsonObject(raw);
93
+ if (!data || !Array.isArray(data.findings))
94
+ throw new Error("Invalid LLM response: expected findings array");
95
+ const list = data.findings;
96
+ const out = [];
97
+ for (const item of list) {
98
+ if (!item || typeof item !== "object")
99
+ continue;
100
+ const row = item;
101
+ const mech = String(row.mechanism_id || "").trim();
102
+ if (!ALLOWED_MECHANISMS.has(mech))
103
+ continue;
104
+ const title = String(row.title || mech).trim().slice(0, 200);
105
+ if (title.length < 8)
106
+ continue;
107
+ const domain = String(row.primary_domain || "compute.cpu").trim();
108
+ let line = null;
109
+ if (typeof row.start_line === "number" && Number.isSafeInteger(row.start_line) && row.start_line > 0) {
110
+ line = row.start_line;
111
+ }
112
+ let conf = 0.5;
113
+ try {
114
+ conf = Math.max(0, Math.min(1, Number(row.confidence ?? 0.5)));
115
+ }
116
+ catch {
117
+ conf = 0.5;
118
+ }
119
+ out.push({
120
+ mechanism_id: mech,
121
+ title,
122
+ primary_domain: domain,
123
+ path: filePath,
124
+ start_line: line,
125
+ evidence: String(row.evidence || "").slice(0, 500),
126
+ recommended_fix: String(row.recommended_fix || "").slice(0, 500),
127
+ confidence: conf,
128
+ });
129
+ }
130
+ return out;
131
+ }
132
+ async function chatCompletion(cfg, messages, fetchImpl, signal) {
133
+ const model = cfg.model || "gpt-4o-mini";
134
+ const endpoint = (cfg.azure_endpoint || "").replace(/\/+$/, "");
135
+ let url;
136
+ const headers = { "Content-Type": "application/json" };
137
+ if (cfg.provider === 'anthropic') {
138
+ const base = endpoint || 'https://api.anthropic.com/v1';
139
+ const url = base.endsWith('/messages') ? base : `${base.endsWith('/v1') ? base : `${base}/v1`}/messages`;
140
+ const response = await fetchImpl(url, {
141
+ method: 'POST', signal,
142
+ headers: { ...headers, 'x-api-key': cfg.api_key, 'anthropic-version': cfg.api_version || '2023-06-01' },
143
+ body: JSON.stringify({ model, max_tokens: 1800,
144
+ system: messages.filter(message => message.role === 'system').map(message => message.content).join('\n\n'),
145
+ messages: messages.filter(message => message.role !== 'system'),
146
+ }),
147
+ });
148
+ if (!response.ok)
149
+ throw new Error(`LLM HTTP ${response.status}`);
150
+ const data = await response.json();
151
+ return (data.content ?? []).filter(block => block.type === 'text').map(block => block.text || '').join('');
152
+ }
153
+ if (cfg.provider === "azure_openai" || (endpoint && /openai\.azure\.com|cognitiveservices\.azure\.com/i.test(endpoint))) {
154
+ url = `${endpoint}/openai/deployments/${encodeURIComponent(model)}/chat/completions?api-version=${encodeURIComponent(cfg.api_version || "2024-02-15-preview")}`;
155
+ headers["api-key"] = cfg.api_key;
156
+ }
157
+ else if (endpoint) {
158
+ const base = cfg.provider === 'ollama' && !/\/v1(?:\/|$)/.test(endpoint) ? `${endpoint}/v1` : endpoint;
159
+ url = base.endsWith('/chat/completions') ? base : `${base}/chat/completions`;
160
+ if (cfg.api_key)
161
+ headers.Authorization = `Bearer ${cfg.api_key}`;
162
+ }
163
+ else {
164
+ url = "https://api.openai.com/v1/chat/completions";
165
+ headers.Authorization = `Bearer ${cfg.api_key}`;
166
+ }
167
+ const body = {
168
+ messages,
169
+ };
170
+ if (/^(o[1-9]|gpt-5|gpt-6)/i.test(model)) {
171
+ body.max_completion_tokens = 1800;
172
+ }
173
+ else {
174
+ body.max_tokens = 1800;
175
+ body.temperature = 0;
176
+ }
177
+ // Azure deployment path does not need body.model; openai-compat does.
178
+ if (!/\/deployments\//i.test(url)) {
179
+ body.model = model;
180
+ }
181
+ // Azure deployment aliases do not identify their underlying model. Match the
182
+ // Python client's bounded unsupported-parameter fallback, under one timeout.
183
+ for (let attempt = 0; attempt < 3; attempt++) {
184
+ const resp = await fetchImpl(url, {
185
+ signal, method: 'POST', headers, body: JSON.stringify(body),
186
+ });
187
+ if (resp.ok) {
188
+ const json = await resp.json();
189
+ return String(json.choices?.[0]?.message?.content || '');
190
+ }
191
+ if (resp.status === 400 && attempt < 2) {
192
+ const data = await resp.json().catch(() => null);
193
+ const parameter = String(data?.error?.param || '').toLowerCase();
194
+ const detail = `${data?.error?.code || ''} ${data?.error?.message || ''}`.toLowerCase();
195
+ if (/unsupported|not supported|only the default/.test(detail)) {
196
+ if ('max_tokens' in body && (parameter === 'max_tokens' || detail.includes('max_tokens'))) {
197
+ body.max_completion_tokens = body.max_tokens;
198
+ delete body.max_tokens;
199
+ delete body.temperature;
200
+ continue;
201
+ }
202
+ if ('temperature' in body && (parameter === 'temperature' || detail.includes('temperature'))) {
203
+ delete body.temperature;
204
+ continue;
205
+ }
206
+ }
207
+ }
208
+ throw new Error(`LLM HTTP ${resp.status}`);
209
+ }
210
+ throw new Error('LLM rejected supported request parameters');
211
+ }
212
+ /** Bounded review; static results survive errors, which are always reported. */
213
+ export async function runNodeLlmResourceReview(opts) {
214
+ const depth = opts.depth ?? 'standard';
215
+ const counts = new Map();
216
+ for (const finding of opts.staticFindings) {
217
+ const rel = finding.scope.source_locations[0]?.path;
218
+ if (rel)
219
+ counts.set(rel, (counts.get(rel) ?? 0) + 1);
220
+ }
221
+ const files = [...new Set(opts.paths.map(p => p.replace(/\\/g, '/')))]
222
+ .sort((a, b) => (counts.get(b) ?? 0) - (counts.get(a) ?? 0) || a.localeCompare(b))
223
+ .slice(0, DEPTH_MAX[depth]);
224
+ const extra = [];
225
+ const errors = [];
226
+ let config;
227
+ try {
228
+ config = resolveNodeLlmConfig(opts.config);
229
+ }
230
+ catch (err) {
231
+ return { findings: opts.staticFindings, filesReviewed: 0, filesPlanned: files.length, errors: [err instanceof Error ? err.message : String(err)] };
232
+ }
233
+ const root = realpathSync(opts.projectDir);
234
+ let filesReviewed = 0;
235
+ for (const [index, rel] of files.entries()) {
236
+ opts.progress?.(`LLM review ${index + 1}/${files.length}: ${rel}`);
237
+ try {
238
+ const abs = realpathSync(resolve(root, rel));
239
+ const inside = relative(root, abs);
240
+ if (inside === '..' || inside.startsWith(`..${process.platform === 'win32' ? '\\' : '/'}`) || isAbsolute(inside)) {
241
+ throw new Error('source path is outside project');
242
+ }
243
+ if (!statSync(abs).isFile() || statSync(abs).size > 512 * 1024)
244
+ throw new Error('source file exceeds review limit');
245
+ const source = readFileSync(abs, 'utf8').slice(0, MAX_SOURCE_CHARS);
246
+ const signal = AbortSignal.timeout(Math.min(opts.timeoutMs ?? 30_000, 60_000));
247
+ const raw = await chatCompletion(config, [
248
+ { role: 'system', content: systemPrompt() },
249
+ { role: 'user', content: JSON.stringify(userPayload(rel, source, opts.staticFindings)) },
250
+ ], opts.fetchImpl ?? fetch, signal);
251
+ const lines = source.split(/\r?\n/);
252
+ for (const hit of parseHits(raw, rel)) {
253
+ if (!hit.start_line || hit.start_line > lines.length)
254
+ continue;
255
+ if (!['compute.cpu', 'memory.host', 'network.io', 'external_api', 'storage.io'].includes(hit.primary_domain))
256
+ continue;
257
+ if (!hit.evidence?.trim() || !source.includes(hit.evidence) || !lines[hit.start_line - 1]?.includes(hit.evidence.split('\n')[0]))
258
+ continue;
259
+ const duplicate = [...opts.staticFindings, ...extra].some(f => f.mechanism_id === hit.mechanism_id && f.scope.source_locations.some(l => l.path === rel && l.start_line === hit.start_line));
260
+ if (duplicate)
261
+ continue;
262
+ const candidate = toResourceFindingCandidate({
263
+ mechanism_id: hit.mechanism_id,
264
+ primary_domain: hit.primary_domain,
265
+ admission_domain: hit.primary_domain,
266
+ title: hit.title, rel_path: rel, start_line: hit.start_line, end_line: hit.start_line,
267
+ language: /\.tsx?$/.test(rel) ? 'typescript' : 'javascript',
268
+ detector_id: 'js.llm.resource_review', detector_version: '0.1.0',
269
+ }, opts.projectId);
270
+ if (candidate) {
271
+ if (candidate.optimization && hit.recommended_fix)
272
+ candidate.optimization.summary = hit.recommended_fix;
273
+ extra.push(candidate);
274
+ }
275
+ }
276
+ filesReviewed += 1;
277
+ }
278
+ catch (err) {
279
+ errors.push(`${rel}: ${err instanceof Error ? err.message : String(err)}`);
280
+ // Authentication, provider errors and timeouts should not repeat for every file.
281
+ break;
282
+ }
283
+ }
284
+ return { findings: filterValidResourceFindings([...opts.staticFindings, ...extra]), filesReviewed, filesPlanned: files.length, errors };
285
+ }
@@ -0,0 +1,6 @@
1
+ export declare function loadAnalyzeRepositoryConfig(projectDir: string): {
2
+ projectId?: string;
3
+ orgSlug?: string;
4
+ apiUrl?: string;
5
+ environment?: string;
6
+ };
@@ -0,0 +1,49 @@
1
+ /** Python-compatible Analyze repository identity and API configuration. */
2
+ import { existsSync, readFileSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+ import { parse } from 'yaml';
5
+ export function loadAnalyzeRepositoryConfig(projectDir) {
6
+ const documents = {};
7
+ for (const name of ['project', 'config']) {
8
+ const path = ['yaml', 'yml', 'json'].map(ext => join(projectDir, '.netgreener', `${name}.${ext}`))
9
+ .find(candidate => existsSync(candidate));
10
+ if (!path)
11
+ continue;
12
+ let data;
13
+ try {
14
+ const raw = readFileSync(path, 'utf8');
15
+ data = path.endsWith('.json') ? JSON.parse(raw) : parse(raw);
16
+ }
17
+ catch {
18
+ throw new Error(`Cannot read Analyze repository configuration: ${path}`);
19
+ }
20
+ if (!data || typeof data !== 'object' || Array.isArray(data)) {
21
+ throw new Error(`Analyze repository configuration must be an object: ${path}`);
22
+ }
23
+ documents[name] = data;
24
+ }
25
+ const projectId = documents.project?.project_id;
26
+ if (projectId != null && (typeof projectId !== 'string' && typeof projectId !== 'number'
27
+ || !/^\d+$/.test(String(projectId).trim()) || !Number.isSafeInteger(Number(projectId)) || Number(projectId) <= 0)) {
28
+ throw new Error('Repository project_id must be a positive integer.');
29
+ }
30
+ let apiUrl = typeof documents.config?.api_url === 'string' ? documents.config.api_url.trim() : undefined;
31
+ if (!apiUrl) {
32
+ for (const name of ['.env', '.env.local']) {
33
+ const path = join(projectDir, name);
34
+ if (!existsSync(path))
35
+ continue;
36
+ const match = readFileSync(path, 'utf8').match(/^\s*NETGREENER_API_URL\s*=\s*(.*?)\s*$/m);
37
+ if (match) {
38
+ apiUrl = match[1].replace(/^["']|["']$/g, '').trim();
39
+ break;
40
+ }
41
+ }
42
+ }
43
+ return {
44
+ projectId: projectId == null ? undefined : String(Number(projectId)),
45
+ orgSlug: typeof documents.project?.org_slug === 'string' ? documents.project.org_slug.trim().toLowerCase() : undefined,
46
+ apiUrl: apiUrl?.replace(/\/+$/, ''),
47
+ environment: typeof documents.config?.environment === 'string' ? documents.config.environment.trim().toLowerCase() : undefined,
48
+ };
49
+ }
package/dist/auto.js CHANGED
@@ -3,6 +3,10 @@
3
3
  * Starts NetGreener with runtime mode ``auto`` (persistent vs ephemeral).
4
4
  */
5
5
  import { NetGreener } from './init.js';
6
- NetGreener.init({ runtime: 'auto' });
6
+ import { installMcpClientInstrumentation } from './mcpToolMeter.js';
7
+ const runtime = NetGreener.init({ runtime: 'auto' });
8
+ // Complete optional ESM/CJS patching before dependent application code starts.
9
+ if (runtime.enabled)
10
+ await installMcpClientInstrumentation();
7
11
  export { NetGreener };
8
12
  export default NetGreener;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Shared CLI help / version copy for the Node ``netgreener`` CLI.
3
3
  */
4
- export declare const CLI_VERSION = "0.1.8";
4
+ export declare const CLI_VERSION: string;
5
5
  export declare function formatRootHelp(): string;
6
6
  export declare function formatAnalyzeHelp(): string;
7
7
  /** @deprecated Prefer formatOptimizeHelp / formatRunHelp. Kept for older tests. */
@@ -1,7 +1,11 @@
1
1
  /**
2
2
  * Shared CLI help / version copy for the Node ``netgreener`` CLI.
3
3
  */
4
- export const CLI_VERSION = '0.1.8';
4
+ import { existsSync, readFileSync } from 'node:fs';
5
+ // The extension bundles this module beside its own package metadata.
6
+ const bundledPackage = new URL('./package.json', import.meta.url);
7
+ const packageUrl = existsSync(bundledPackage) ? bundledPackage : new URL('../../package.json', import.meta.url);
8
+ export const CLI_VERSION = JSON.parse(readFileSync(packageUrl, 'utf8')).version;
5
9
  export function formatRootHelp() {
6
10
  return `NetGreener Node CLI ${CLI_VERSION}
7
11
 
@@ -33,7 +37,7 @@ Examples:
33
37
  netgreener-analyze ./my-app
34
38
 
35
39
  Notes:
36
- • Analyze is local only (upload=never).
40
+ • Analyze runs static checks + LLM review when logged in/BYOK configured; --no-llm stays offline.
37
41
  • run meters via process runtime; default EXPORT_MODE stays direct.
38
42
  • optimize emits proposals; --apply only for auto_apply_safe + VerificationProvider.
39
43
  • --llm uses shared api_server /optimize/function; --apply-verified for VP-gated writes.
@@ -45,24 +49,29 @@ Analyze: netgreener help analyze
45
49
  `;
46
50
  }
47
51
  export function formatAnalyzeHelp() {
48
- return `Usage: netgreener analyze <projectDir> [options]
49
- (alias) netgreener-analyze <projectDir> [options]
52
+ return `Usage: netgreener analyze [projectDir] [options]
53
+ (alias) netgreener-analyze [projectDir] [options]
50
54
 
51
- Print NetGreener Analyze scaffold output:
55
+ Analyze JavaScript/TypeScript with static checks and bounded LLM review:
52
56
  service_manifest_v0 + resource_finding_v1 candidates (JSON on stdout).
53
57
 
54
- Does not upload and does not modify RunSession / metering pipelines.
58
+ LLM review sends selected source files to your configured provider. Findings are not stored.
59
+ Does not modify RunSession / metering pipelines.
55
60
 
56
61
  Options:
57
- --project-id <id> Stamp project_id on candidates (default: local)
62
+ --project-id <id> Override env/repository project ID (default: local)
58
63
  --paths a,b Explicit repo-relative source paths (comma-separated)
59
64
  --findings-only Emit findings JSON array only
60
65
  --manifest-only Emit service_manifest_v0 JSON only
61
66
  --json-summary Emit netgreener_analyze_gate_summary_v0 only (CI)
62
67
  --fail-on any|<mechanism>[,…] Fail (exit 1) when matching candidates exceed cap
63
68
  --max-at-or-above <n> With --fail-on: allow up to N matches (default 0)
64
- --depth quick|standard|thorough Python-parity LLM budget (default standard; static scan ignores)
65
- --quiet No stderr status lines
69
+ --depth quick|standard|thorough LLM budget: 1 / 5 / 10 files (default standard)
70
+ --llm Require LLM review (managed login or BYOK)
71
+ --no-llm Static analysis only; no network calls
72
+ --output-file <path> Save full local report and notify the editor on completion
73
+ --no-upload Accepted; findings are never uploaded
74
+ --quiet No stderr progress (errors still reported)
66
75
  -h, --help Show this help
67
76
 
68
77
  Examples:
@@ -74,6 +83,14 @@ Examples:
74
83
  netgreener analyze ./app --paths src/server.ts --findings-only
75
84
  netgreener-analyze ./app --manifest-only
76
85
 
86
+ Configuration:
87
+ Project directory defaults to the current folder, like Python Analyze.
88
+ Identity: --project-id > NETGREENER_PROJECT_ID > .netgreener/project.yaml|yml|json.
89
+ API URL: NETGREENER_API_URL > .netgreener/config.yaml|yml|json > project .env > global config.
90
+ Managed login or BYOK: NETGREENER_LLM_PROVIDER, MODEL, KEY, ENDPOINT.
91
+ Providers: openai, azure_openai, anthropic, ollama, custom.
92
+ Ollama/custom endpoints may run without a key. LLM calls time out after 30s.
93
+
77
94
  CI tip: pair --json-summary with --fail-on for gate annotations without upload.
78
95
  `;
79
96
  }
@@ -240,7 +240,7 @@ export async function runRunCli(argv) {
240
240
  }
241
241
  if (!flags.noAnalyze) {
242
242
  process.stderr.write('netgreener run: local analyze (upload=never)…\n');
243
- const analyzeCode = runAnalyzeCli([projectAbs, '--quiet']);
243
+ const analyzeCode = await runAnalyzeCli([projectAbs, '--quiet', '--no-llm']);
244
244
  if (analyzeCode !== 0) {
245
245
  process.stderr.write(`netgreener run: analyze exited ${analyzeCode}\n`);
246
246
  }
@@ -7,8 +7,8 @@
7
7
  *
8
8
  * The counter is this process only. Warn is the default. Block is opt-in and
9
9
  * does not invoke the tool. A configured estimate is not added to API cost.
10
- * The official TypeScript MCP client is not patched. Calls are recorded only
11
- * when this process calls beginToolCall. A missing client hook is not monitoring.
10
+ * Await installMcpClientInstrumentation before using the supported MCP client.
11
+ * Both ESM and CommonJS SDK clients use the existing ledger and call context.
12
12
  *
13
13
  * Raw tool arguments, results, and HTTP bodies are not stored.
14
14
  */
@@ -92,10 +92,9 @@ export declare function httpDurationMs(externalV0: Record<string, unknown> | nul
92
92
  export declare function mcpDurationMs(mcpV0: Record<string, unknown> | null | undefined, failedOnly?: boolean): number;
93
93
  export declare function buildMcpLatencyV0(mcpV0: Record<string, unknown> | null | undefined, externalV0: Record<string, unknown> | null | undefined, wallSeconds: number | null): Record<string, unknown> | null;
94
94
  export declare function clearMcpToolLedger(): void;
95
- /**
96
- * Official MCP client patching stays off until one SDK version is installed
97
- * and its callTool signature is tested. A command basename is not a server name.
98
- * This returns false so a caller cannot treat the hook as active.
95
+ /** Optional SDK hook. Await before the first call; false means no active hook.
96
+ * Arguments, results, errors, transport URLs and connection secrets are not stored.
97
+ * Server identity is the negotiated name, or unknown before initialization.
99
98
  */
100
- export declare function installMcpClientInstrumentation(): boolean;
99
+ export declare function installMcpClientInstrumentation(): Promise<boolean>;
101
100
  export declare function _resetMcpToolMeterForTests(): void;
@@ -7,8 +7,8 @@
7
7
  *
8
8
  * The counter is this process only. Warn is the default. Block is opt-in and
9
9
  * does not invoke the tool. A configured estimate is not added to API cost.
10
- * The official TypeScript MCP client is not patched. Calls are recorded only
11
- * when this process calls beginToolCall. A missing client hook is not monitoring.
10
+ * Await installMcpClientInstrumentation before using the supported MCP client.
11
+ * Both ESM and CommonJS SDK clients use the existing ledger and call context.
12
12
  *
13
13
  * Raw tool arguments, results, and HTTP bodies are not stored.
14
14
  */
@@ -16,6 +16,9 @@ import { meteringAllowed } from './runtimeControls.js';
16
16
  import { AsyncLocalStorage } from 'node:async_hooks';
17
17
  import { randomUUID } from 'node:crypto';
18
18
  import { connect } from 'node:net';
19
+ import { createRequire } from 'node:module';
20
+ import { readFileSync } from 'node:fs';
21
+ import { pathToFileURL } from 'node:url';
19
22
  import { sendCollectorEvent } from './collectorIpc.js';
20
23
  import { getTenantContext } from './tenantContext.js';
21
24
  const MAX_STORED = 200;
@@ -488,13 +491,61 @@ export function buildMcpLatencyV0(mcpV0, externalV0, wallSeconds) {
488
491
  export function clearMcpToolLedger() {
489
492
  ledger.clear();
490
493
  }
491
- /**
492
- * Official MCP client patching stays off until one SDK version is installed
493
- * and its callTool signature is tested. A command basename is not a server name.
494
- * This returns false so a caller cannot treat the hook as active.
494
+ const requireMcp = createRequire(import.meta.url);
495
+ const mcpWrapped = Symbol.for('netgreener.mcp.callTool.wrapped');
496
+ /** Optional SDK hook. Await before the first call; false means no active hook.
497
+ * Arguments, results, errors, transport URLs and connection secrets are not stored.
498
+ * Server identity is the negotiated name, or unknown before initialization.
495
499
  */
496
- export function installMcpClientInstrumentation() {
497
- return false;
500
+ export async function installMcpClientInstrumentation() {
501
+ try {
502
+ // Do not require the SDK at runtime for applications that do not use MCP.
503
+ const specifier = '@modelcontextprotocol/sdk/client/index.js';
504
+ const packageUrl = new URL('../../../package.json', pathToFileURL(requireMcp.resolve(specifier)));
505
+ const sdkPackage = JSON.parse(readFileSync(packageUrl, 'utf8'));
506
+ // Broaden only after real-client acceptance for another SDK version.
507
+ if (sdkPackage.version !== '1.32.2')
508
+ return false;
509
+ const modules = [await import(specifier), requireMcp(specifier)];
510
+ if (modules.some(({ Client }) => typeof Client?.prototype?.callTool !== 'function'
511
+ || typeof Client.prototype.getServerVersion !== 'function'))
512
+ return false;
513
+ for (const { Client } of modules) {
514
+ const prototype = Client?.prototype;
515
+ const original = prototype?.callTool;
516
+ if (original[mcpWrapped])
517
+ continue;
518
+ const wrapped = async function (...args) {
519
+ if (!meteringAllowed())
520
+ return original.apply(this, args);
521
+ const params = args[0];
522
+ const decision = beginToolCall(this.getServerVersion()?.name || 'unknown', typeof params?.name === 'string' ? params.name : 'unknown');
523
+ const startedAt = new Date().toISOString();
524
+ if (decision.action === 'block') {
525
+ finishToolCall(decision, { success: false, durationMs: 0, startedAt });
526
+ throw new McpToolBudgetBlocked(decision.reason || 'MCP tool call blocked');
527
+ }
528
+ const start = performance.now();
529
+ return runWithMcpToolCall(decision.callId, async () => {
530
+ let success = false;
531
+ try {
532
+ const result = await original.apply(this, args);
533
+ success = !result?.isError;
534
+ return result;
535
+ }
536
+ finally {
537
+ finishToolCall(decision, { success, durationMs: performance.now() - start, startedAt });
538
+ }
539
+ });
540
+ };
541
+ Object.defineProperty(wrapped, mcpWrapped, { value: true });
542
+ prototype.callTool = wrapped;
543
+ }
544
+ return true;
545
+ }
546
+ catch {
547
+ return false;
548
+ }
498
549
  }
499
550
  export function _resetMcpToolMeterForTests() {
500
551
  ledger.clear();
@@ -92,6 +92,8 @@ export declare class NetGreenerClient {
92
92
  }): Promise<ProjectRow[]>;
93
93
  createProject(payload: Record<string, unknown>): Promise<Record<string, unknown>>;
94
94
  updateProject(projectId: number, payload: Record<string, unknown>): Promise<Record<string, unknown>>;
95
+ /** Managed analysis credentials; shared Python/editor endpoint. */
96
+ getLlmConfig(): Promise<Record<string, unknown>>;
95
97
  getProject(projectId: number): Promise<Record<string, unknown>>;
96
98
  private ensureUserId;
97
99
  private maybeSyncProject;
@@ -172,6 +172,10 @@ export class NetGreenerClient {
172
172
  async updateProject(projectId, payload) {
173
173
  return this.request('PUT', `/projects/${projectId}`, payload);
174
174
  }
175
+ /** Managed analysis credentials; shared Python/editor endpoint. */
176
+ async getLlmConfig() {
177
+ return this.request('GET', '/config/llm/');
178
+ }
175
179
  async getProject(projectId) {
176
180
  return this.request('GET', `/projects/${projectId}`);
177
181
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netgreener/runtime",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "NetGreener Node.js Runtime — Express/Fastify/Nest/Next/process adapter uploading RunSessions to api_server.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -142,6 +142,7 @@
142
142
  }
143
143
  },
144
144
  "devDependencies": {
145
+ "@modelcontextprotocol/sdk": "1.32.2",
145
146
  "@types/express": "^4.17.21",
146
147
  "@types/node": "^22.10.0",
147
148
  "axios": "^1.20.0",
@@ -152,7 +153,8 @@
152
153
  },
153
154
  "dependencies": {
154
155
  "ajv": "^8.20.0",
155
- "ajv-formats": "^3.0.1"
156
+ "ajv-formats": "^3.0.1",
157
+ "yaml": "^2.9.0"
156
158
  },
157
159
  "publishConfig": {
158
160
  "access": "public"