@netgreener/runtime 0.1.14 → 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.
package/TUTORIAL.md CHANGED
@@ -227,7 +227,7 @@ Pass = scripts exit 0; payloads validate; no live upload required.
227
227
 
228
228
  1. Deploy / start the API with live env (§4.3).
229
229
  2. Call 2–3 real routes (health + one business route).
230
- 3. Stop the process (or wait for `NETGREENER_RUNTIME_FLUSH_MINUTES`, default 5).
230
+ 3. Stop the process (or wait for `NETGREENER_RUNTIME_FLUSH_MINUTES`, default 1).
231
231
  4. Dashboard → **Project `<PROJECT_ID>`** → newest **Run** → **Runtime Ops** / API Runtime.
232
232
  5. Pass = bounded route templates listed (e.g. `GET /health`), not raw customer paths.
233
233
  6. Keep a sanitized evidence record (revision, env, run id) — see
@@ -16,9 +16,11 @@ export declare class ServiceRuntimeAggregator {
16
16
  cpuTimeMs?: number;
17
17
  /** Measured RSS KiB at sample end. */
18
18
  peakRssKb?: number;
19
+ completed?: boolean;
20
+ completionDurationMs?: number;
19
21
  }): void;
20
22
  private recordIntoMap;
21
- /** Peak RSS (KiB) across units with calls — for crude window RAM estimate. */
23
+ /** Peak RSS (KiB) across units with calls/activity — for the window RAM estimate. */
22
24
  maxPeakRssKb(): number;
23
25
  private buildUnitsList;
24
26
  hasData(): boolean;
@@ -57,6 +57,7 @@ export class ServiceRuntimeAggregator {
57
57
  calls: 0,
58
58
  errors: 0,
59
59
  durationMsTotal: 0,
60
+ completedDurationMsTotal: 0,
60
61
  durationMsMax: 0,
61
62
  cpuMsTotal: 0,
62
63
  cpuMsMeasured: false,
@@ -65,13 +66,18 @@ export class ServiceRuntimeAggregator {
65
66
  target.set(key, unit);
66
67
  }
67
68
  }
68
- unit.calls += 1;
69
- if (sample.error)
69
+ if (sample.completed !== false)
70
+ unit.calls += 1;
71
+ if (sample.completed !== false && sample.error)
70
72
  unit.errors += 1;
71
73
  // Math.max(0, NaN) === NaN — must reject non-finite before accumulate.
72
74
  const ms = nonNegativeFinite(sample.durationMs);
73
75
  unit.durationMsTotal += ms;
74
- unit.durationMsMax = Math.max(unit.durationMsMax, ms);
76
+ if (sample.completed !== false) {
77
+ const completedMs = nonNegativeFinite(sample.completionDurationMs ?? sample.durationMs);
78
+ unit.completedDurationMsTotal += completedMs;
79
+ unit.durationMsMax = Math.max(unit.durationMsMax, completedMs);
80
+ }
75
81
  if (sample.cpuTimeMs !== undefined) {
76
82
  unit.cpuMsTotal += nonNegativeFinite(sample.cpuTimeMs);
77
83
  unit.cpuMsMeasured = true;
@@ -82,17 +88,17 @@ export class ServiceRuntimeAggregator {
82
88
  unit.peakRssKbMax = Math.max(unit.peakRssKbMax, rss);
83
89
  }
84
90
  }
85
- /** Peak RSS (KiB) across units with calls — for crude window RAM estimate. */
91
+ /** Peak RSS (KiB) across units with calls/activity — for the window RAM estimate. */
86
92
  maxPeakRssKb() {
87
93
  let max = 0;
88
94
  for (const u of this.units.values()) {
89
- if (u.calls > 0 && u.peakRssKbMax > max)
95
+ if ((u.calls > 0 || u.durationMsTotal > 0) && u.peakRssKbMax > max)
90
96
  max = u.peakRssKbMax;
91
97
  }
92
98
  return max;
93
99
  }
94
100
  buildUnitsList(source, windowEnergyKwh, equalShare, totalDuration, occByUnit, alloc) {
95
- const list = [...source.values()].filter((u) => u.calls > 0);
101
+ const list = [...source.values()].filter((u) => (u.calls > 0 || u.durationMsTotal > 0));
96
102
  if (!list.length)
97
103
  return [];
98
104
  const energyBudget = nonNegativeFinite(windowEnergyKwh);
@@ -104,9 +110,9 @@ export class ServiceRuntimeAggregator {
104
110
  const sorted = list.sort((a, b) => b.durationMsTotal - a.durationMsTotal);
105
111
  const totalCalls = list.reduce((sum, unit) => sum + unit.calls, 0);
106
112
  const units = sorted.map((u) => {
107
- const share = equalShare ? u.calls / totalCalls : u.durationMsTotal / totalDuration;
113
+ const share = equalShare ? (totalCalls > 0 ? u.calls / totalCalls : 1 / list.length) : u.durationMsTotal / totalDuration;
108
114
  const energy = energyBudget * share;
109
- const avg = u.calls ? u.durationMsTotal / u.calls : 0;
115
+ const avg = u.calls ? u.completedDurationMsTotal / u.calls : 0;
110
116
  const cpuSeconds = alloc?.windowCpuSeconds != null
111
117
  ? nonNegativeFinite(alloc.windowCpuSeconds) * share
112
118
  : u.cpuMsMeasured
@@ -153,7 +159,7 @@ export class ServiceRuntimeAggregator {
153
159
  }
154
160
  hasData() {
155
161
  for (const u of this.units.values()) {
156
- if (u.calls > 0)
162
+ if ((u.calls > 0 || u.durationMsTotal > 0))
157
163
  return true;
158
164
  }
159
165
  return false;
@@ -161,7 +167,7 @@ export class ServiceRuntimeAggregator {
161
167
  totalDurationMs() {
162
168
  let total = 0;
163
169
  for (const u of this.units.values()) {
164
- if (u.calls > 0)
170
+ if ((u.calls > 0 || u.durationMsTotal > 0))
165
171
  total += u.durationMsTotal;
166
172
  }
167
173
  return total;
@@ -170,7 +176,7 @@ export class ServiceRuntimeAggregator {
170
176
  totalMeasuredCpuMs() {
171
177
  let total = 0;
172
178
  for (const u of this.units.values()) {
173
- if (u.calls > 0 && u.cpuMsMeasured)
179
+ if ((u.calls > 0 || u.durationMsTotal > 0) && u.cpuMsMeasured)
174
180
  total += u.cpuMsTotal;
175
181
  }
176
182
  return total;
@@ -178,7 +184,7 @@ export class ServiceRuntimeAggregator {
178
184
  /** True when any unit recorded measured process CPU (not duration proxy). */
179
185
  hasMeasuredCpu() {
180
186
  for (const u of this.units.values()) {
181
- if (u.calls > 0 && u.cpuMsMeasured)
187
+ if ((u.calls > 0 || u.durationMsTotal > 0) && u.cpuMsMeasured)
182
188
  return true;
183
189
  }
184
190
  return false;
@@ -186,7 +192,7 @@ export class ServiceRuntimeAggregator {
186
192
  /** True when any unit recorded peak RSS. */
187
193
  hasMeasuredRss() {
188
194
  for (const u of this.units.values()) {
189
- if (u.calls > 0 && u.peakRssKbMax > 0)
195
+ if ((u.calls > 0 || u.durationMsTotal > 0) && u.peakRssKbMax > 0)
190
196
  return true;
191
197
  }
192
198
  return false;
@@ -197,7 +203,7 @@ export class ServiceRuntimeAggregator {
197
203
  * Occupancy is window-scaled (Python twin) so coverage reconciles to ``window_seconds``.
198
204
  */
199
205
  buildV0(opts) {
200
- const list = [...this.units.values()].filter((u) => u.calls > 0);
206
+ const list = [...this.units.values()].filter((u) => (u.calls > 0 || u.durationMsTotal > 0));
201
207
  if (!list.length)
202
208
  return null;
203
209
  const windowEnergyKwh = nonNegativeFinite(opts.windowEnergyKwh);
@@ -263,12 +269,12 @@ export class ServiceRuntimeAggregator {
263
269
  const totalBusy = list.reduce((s, u) => s + u.durationMsTotal, 0);
264
270
  const totalCalls = list.reduce((s, u) => s + u.calls, 0);
265
271
  for (const [tenantId, tenantMap] of this.byTenant.entries()) {
266
- const tenantList = [...tenantMap.values()].filter((u) => u.calls > 0);
272
+ const tenantList = [...tenantMap.values()].filter((u) => (u.calls > 0 || u.durationMsTotal > 0));
267
273
  if (!tenantList.length)
268
274
  continue;
269
275
  const tenantBusy = tenantList.reduce((s, u) => s + u.durationMsTotal, 0);
270
276
  const tenantShare = totalBusy > 0 ? tenantBusy / totalBusy
271
- : tenantList.reduce((sum, unit) => sum + unit.calls, 0) / totalCalls;
277
+ : totalCalls > 0 ? tenantList.reduce((sum, unit) => sum + unit.calls, 0) / totalCalls : 0;
272
278
  const tenantEnergy = totalBusy > 0
273
279
  ? windowEnergyKwh * (tenantBusy / totalBusy)
274
280
  : windowEnergyKwh * tenantShare;
@@ -411,6 +417,7 @@ export class ServiceRuntimeAggregator {
411
417
  dst.calls += src.calls;
412
418
  dst.errors += src.errors;
413
419
  dst.durationMsTotal += src.durationMsTotal;
420
+ dst.completedDurationMsTotal += src.completedDurationMsTotal;
414
421
  dst.durationMsMax = Math.max(dst.durationMsMax, src.durationMsMax);
415
422
  dst.cpuMsTotal += src.cpuMsTotal;
416
423
  dst.cpuMsMeasured = dst.cpuMsMeasured || src.cpuMsMeasured;
@@ -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';