@netgreener/runtime 0.1.8 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/TUTORIAL.md +120 -29
- package/dist/aggregator.d.ts +12 -2
- package/dist/aggregator.js +135 -8
- package/dist/analyze/buildResourceFindings.d.ts +4 -0
- package/dist/analyze/buildResourceFindings.js +17 -0
- package/dist/analyze/cli.d.ts +1 -0
- package/dist/analyze/cli.js +22 -1
- package/dist/analyze/declaredExternalApis.d.ts +31 -0
- package/dist/analyze/declaredExternalApis.js +174 -0
- package/dist/analyze/index.d.ts +1 -0
- package/dist/analyze/index.js +1 -0
- package/dist/auto.d.ts +7 -0
- package/dist/auto.js +8 -0
- package/dist/bullmq.d.ts +2 -2
- package/dist/bullmq.js +28 -58
- package/dist/carbonLedger.d.ts +28 -0
- package/dist/carbonLedger.js +52 -0
- package/dist/cgroupV2.d.ts +44 -0
- package/dist/cgroupV2.js +174 -0
- package/dist/cli/helpText.d.ts +3 -3
- package/dist/cli/helpText.js +42 -35
- package/dist/cli/netgreener.d.ts +7 -7
- package/dist/cli/netgreener.js +23 -16
- package/dist/cli/runCli.d.ts +20 -0
- package/dist/cli/runCli.js +249 -0
- package/dist/cli/runPreload.d.ts +5 -0
- package/dist/cli/runPreload.js +43 -0
- package/dist/collectorMetadata.d.ts +1 -1
- package/dist/collectorMetadata.js +2 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +6 -2
- package/dist/exporter.d.ts +7 -2
- package/dist/exporter.js +12 -6
- package/dist/express.js +17 -6
- package/dist/externalApiMeter.d.ts +51 -2
- package/dist/externalApiMeter.js +379 -26
- package/dist/fastify.js +17 -6
- package/dist/index.d.ts +12 -2
- package/dist/index.js +11 -1
- package/dist/init.d.ts +30 -0
- package/dist/init.js +31 -0
- package/dist/lifecycle.d.ts +45 -0
- package/dist/lifecycle.js +112 -0
- package/dist/managedCollectorLauncher.d.ts +37 -0
- package/dist/managedCollectorLauncher.js +162 -0
- package/dist/mcpToolMeter.d.ts +101 -0
- package/dist/mcpToolMeter.js +496 -0
- package/dist/n4/deploymentMatrix.d.ts +9 -5
- package/dist/n4/deploymentMatrix.js +13 -9
- package/dist/n4/missingnessInventory.js +3 -3
- package/dist/next.d.ts +89 -0
- package/dist/next.js +328 -0
- package/dist/nextConfig.d.ts +34 -0
- package/dist/nextConfig.js +73 -0
- package/dist/nextEntryTransform.d.ts +9 -0
- package/dist/nextEntryTransform.js +181 -0
- package/dist/occupancyTracker.d.ts +27 -0
- package/dist/occupancyTracker.js +87 -0
- package/dist/optimize/apply.d.ts +38 -0
- package/dist/optimize/apply.js +173 -0
- package/dist/optimize/cli.d.ts +26 -0
- package/dist/optimize/cli.js +323 -0
- package/dist/optimize/index.d.ts +6 -0
- package/dist/optimize/index.js +5 -0
- package/dist/optimize/llmApply.d.ts +22 -0
- package/dist/optimize/llmApply.js +80 -0
- package/dist/optimize/llmSession.d.ts +88 -0
- package/dist/optimize/llmSession.js +135 -0
- package/dist/optimize/provider.d.ts +17 -0
- package/dist/optimize/provider.js +119 -0
- package/dist/optimize/safePatches.d.ts +18 -0
- package/dist/optimize/safePatches.js +247 -0
- package/dist/optimize/types.d.ts +79 -0
- package/dist/optimize/types.js +6 -0
- package/dist/optimize/verification.d.ts +16 -0
- package/dist/optimize/verification.js +165 -0
- package/dist/processResources.d.ts +5 -0
- package/dist/processResources.js +28 -4
- package/dist/processRuntime.js +7 -28
- package/dist/processTree.d.ts +47 -0
- package/dist/processTree.js +148 -0
- package/dist/runContextEnrichment.d.ts +11 -0
- package/dist/runContextEnrichment.js +92 -0
- package/dist/runtime.d.ts +36 -1
- package/dist/runtime.js +140 -17
- package/dist/sdk/client.d.ts +152 -0
- package/dist/sdk/client.js +396 -0
- package/dist/sdk/gitUrl.d.ts +6 -0
- package/dist/sdk/gitUrl.js +87 -0
- package/dist/sdk/index.d.ts +5 -0
- package/dist/sdk/index.js +5 -0
- package/dist/serverlessHints.d.ts +9 -6
- package/dist/serverlessHints.js +10 -7
- package/dist/taskMeter.d.ts +14 -0
- package/dist/taskMeter.js +53 -0
- package/dist/types.d.ts +43 -0
- package/examples/aca-container-gate/Dockerfile +20 -0
- package/examples/aca-container-gate/azure-container-app.yaml +27 -0
- package/examples/aca-container-gate/gate.env +7 -0
- package/examples/aca-container-gate/server.mjs +117 -0
- package/examples/azure-functions-gate/.funcignore +6 -0
- package/examples/azure-functions-gate/README.md +25 -0
- package/examples/azure-functions-gate/host.json +15 -0
- package/examples/azure-functions-gate/package-lock.json +161 -0
- package/examples/azure-functions-gate/package.json +14 -0
- package/examples/azure-functions-gate/src/functions/health.js +27 -0
- package/examples/express-local-live-smoke.mjs +127 -0
- package/examples/extractRunId.mjs +11 -0
- package/examples/next-pilot-dry-run.mjs +32 -0
- package/examples/serverless-thin-gate.mjs +89 -0
- package/fixtures/run-smoke/hello.mjs +4 -0
- package/loaders/next-entry-loader.cjs +14 -0
- package/package.json +25 -2
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concurrency-normalized request occupancy (Python OccupancyTracker twin).
|
|
3
|
+
*
|
|
4
|
+
* Accrues ``dt / N`` to each active request id while N > 0. On end, share moves
|
|
5
|
+
* to the service unit. Window snapshots leave in-flight as unattributed.
|
|
6
|
+
* Additive for anomaly evidence — does not change EXPORT_MODE.
|
|
7
|
+
*/
|
|
8
|
+
export class OccupancyTracker {
|
|
9
|
+
activeIds = new Set();
|
|
10
|
+
occById = new Map();
|
|
11
|
+
occByUnit = new Map();
|
|
12
|
+
idleSeconds = 0;
|
|
13
|
+
lastMono = null;
|
|
14
|
+
begin(requestId) {
|
|
15
|
+
if (!requestId)
|
|
16
|
+
return;
|
|
17
|
+
this.accrue(nowMono());
|
|
18
|
+
this.activeIds.add(requestId);
|
|
19
|
+
if (!this.occById.has(requestId))
|
|
20
|
+
this.occById.set(requestId, 0);
|
|
21
|
+
}
|
|
22
|
+
end(requestId, serviceUnit) {
|
|
23
|
+
if (!requestId)
|
|
24
|
+
return;
|
|
25
|
+
this.accrue(nowMono());
|
|
26
|
+
this.activeIds.delete(requestId);
|
|
27
|
+
const share = this.occById.get(requestId) || 0;
|
|
28
|
+
this.occById.delete(requestId);
|
|
29
|
+
const unit = String(serviceUnit || '').trim();
|
|
30
|
+
if (unit && share > 0) {
|
|
31
|
+
this.occByUnit.set(unit, (this.occByUnit.get(unit) || 0) + share);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
accrue(now) {
|
|
35
|
+
if (this.lastMono == null) {
|
|
36
|
+
this.lastMono = now;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const dt = Math.max(0, now - this.lastMono);
|
|
40
|
+
this.lastMono = now;
|
|
41
|
+
if (dt <= 0)
|
|
42
|
+
return;
|
|
43
|
+
const n = this.activeIds.size;
|
|
44
|
+
if (n <= 0) {
|
|
45
|
+
this.idleSeconds += dt;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const per = dt / n;
|
|
49
|
+
for (const rid of this.activeIds) {
|
|
50
|
+
this.occById.set(rid, (this.occById.get(rid) || 0) + per);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** Snapshot closed-window occupancy; keep in-flight ids active with zeroed accrual. */
|
|
54
|
+
takeWindowSnapshot() {
|
|
55
|
+
this.accrue(nowMono());
|
|
56
|
+
const by_unit = {};
|
|
57
|
+
for (const [k, v] of this.occByUnit) {
|
|
58
|
+
if (v > 0)
|
|
59
|
+
by_unit[k] = Math.round(v * 1e6) / 1e6;
|
|
60
|
+
}
|
|
61
|
+
let inFlight = 0;
|
|
62
|
+
for (const v of this.occById.values())
|
|
63
|
+
inFlight += v;
|
|
64
|
+
const idle = this.idleSeconds;
|
|
65
|
+
this.occByUnit.clear();
|
|
66
|
+
for (const rid of this.occById.keys())
|
|
67
|
+
this.occById.set(rid, 0);
|
|
68
|
+
this.idleSeconds = 0;
|
|
69
|
+
return {
|
|
70
|
+
by_unit,
|
|
71
|
+
in_flight_seconds: Math.round(inFlight * 1e6) / 1e6,
|
|
72
|
+
idle_seconds: Math.round(idle * 1e6) / 1e6,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function nowMono() {
|
|
77
|
+
const t = process.hrtime.bigint();
|
|
78
|
+
return Number(t) / 1e9;
|
|
79
|
+
}
|
|
80
|
+
let globalTracker = new OccupancyTracker();
|
|
81
|
+
export function getOccupancyTracker() {
|
|
82
|
+
return globalTracker;
|
|
83
|
+
}
|
|
84
|
+
/** Test helper — swap in a fresh tracker. */
|
|
85
|
+
export function _resetOccupancyTrackerForTests() {
|
|
86
|
+
globalTracker = new OccupancyTracker();
|
|
87
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Apply verified full-file patches with VerificationProvider gates + rollback.
|
|
3
|
+
*
|
|
4
|
+
* Used by:
|
|
5
|
+
* - ``--apply`` (local auto_apply_safe only)
|
|
6
|
+
* - ``--llm --apply-verified`` (owner-OK LLM splice; not marked auto_apply_safe)
|
|
7
|
+
*
|
|
8
|
+
* Never uploads. Does not flip EXPORT_MODE. Does not invent savings.
|
|
9
|
+
*/
|
|
10
|
+
import type { OptimizeApplyResultV0, OptimizeProposalV0, VerificationResultV0 } from './types.js';
|
|
11
|
+
export type VerifiedPatchWrite = {
|
|
12
|
+
proposal_id: string;
|
|
13
|
+
path: string;
|
|
14
|
+
new_content: string;
|
|
15
|
+
rule_id: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Verify → backup → write → verify-after → rollback on fail.
|
|
19
|
+
* Callers decide which patches are eligible (safe rules vs LLM-gated).
|
|
20
|
+
*/
|
|
21
|
+
export declare function applyVerifiedPatches(projectDir: string, patches: VerifiedPatchWrite[], opts?: {
|
|
22
|
+
skipTest?: boolean;
|
|
23
|
+
skipBuild?: boolean;
|
|
24
|
+
/** Already-skipped rows to include in the result. */
|
|
25
|
+
preSkipped?: OptimizeApplyResultV0['skipped'];
|
|
26
|
+
}): {
|
|
27
|
+
apply: OptimizeApplyResultV0;
|
|
28
|
+
verification_before: VerificationResultV0;
|
|
29
|
+
verification_after: VerificationResultV0 | null;
|
|
30
|
+
};
|
|
31
|
+
export declare function applySafeProposals(projectDir: string, proposals: OptimizeProposalV0[], opts?: {
|
|
32
|
+
skipTest?: boolean;
|
|
33
|
+
skipBuild?: boolean;
|
|
34
|
+
}): {
|
|
35
|
+
apply: OptimizeApplyResultV0;
|
|
36
|
+
verification_before: VerificationResultV0;
|
|
37
|
+
verification_after: VerificationResultV0 | null;
|
|
38
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Apply verified full-file patches with VerificationProvider gates + rollback.
|
|
3
|
+
*
|
|
4
|
+
* Used by:
|
|
5
|
+
* - ``--apply`` (local auto_apply_safe only)
|
|
6
|
+
* - ``--llm --apply-verified`` (owner-OK LLM splice; not marked auto_apply_safe)
|
|
7
|
+
*
|
|
8
|
+
* Never uploads. Does not flip EXPORT_MODE. Does not invent savings.
|
|
9
|
+
*/
|
|
10
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { dirname, join } from 'node:path';
|
|
12
|
+
import { createLocalJsTsVerificationProvider } from './verification.js';
|
|
13
|
+
function backupRoot(projectDir) {
|
|
14
|
+
return join(projectDir, '.netgreener', 'optimize-backup');
|
|
15
|
+
}
|
|
16
|
+
function writeWithDirs(abs, content) {
|
|
17
|
+
mkdirSync(dirname(abs), { recursive: true });
|
|
18
|
+
writeFileSync(abs, content, 'utf8');
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Verify → backup → write → verify-after → rollback on fail.
|
|
22
|
+
* Callers decide which patches are eligible (safe rules vs LLM-gated).
|
|
23
|
+
*/
|
|
24
|
+
export function applyVerifiedPatches(projectDir, patches, opts) {
|
|
25
|
+
const verifier = createLocalJsTsVerificationProvider();
|
|
26
|
+
const skipped = [...(opts?.preSkipped || [])];
|
|
27
|
+
const touched = [...new Set(patches.map((p) => p.path))];
|
|
28
|
+
const verification_before = verifier.verify(projectDir, {
|
|
29
|
+
paths: touched,
|
|
30
|
+
skipTest: opts?.skipTest,
|
|
31
|
+
skipBuild: opts?.skipBuild ?? true,
|
|
32
|
+
});
|
|
33
|
+
const applied = [];
|
|
34
|
+
if (!verification_before.ok) {
|
|
35
|
+
return {
|
|
36
|
+
apply: {
|
|
37
|
+
schema: 'optimize_apply_result_v0',
|
|
38
|
+
applied: [],
|
|
39
|
+
skipped: [
|
|
40
|
+
...skipped,
|
|
41
|
+
...patches.map((p) => ({
|
|
42
|
+
proposal_id: p.proposal_id,
|
|
43
|
+
reason: 'verification_before failed',
|
|
44
|
+
})),
|
|
45
|
+
],
|
|
46
|
+
rolled_back: false,
|
|
47
|
+
verification_before_ok: false,
|
|
48
|
+
verification_after_ok: null,
|
|
49
|
+
},
|
|
50
|
+
verification_before,
|
|
51
|
+
verification_after: null,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if (patches.length === 0) {
|
|
55
|
+
return {
|
|
56
|
+
apply: {
|
|
57
|
+
schema: 'optimize_apply_result_v0',
|
|
58
|
+
applied: [],
|
|
59
|
+
skipped,
|
|
60
|
+
rolled_back: false,
|
|
61
|
+
verification_before_ok: true,
|
|
62
|
+
verification_after_ok: null,
|
|
63
|
+
},
|
|
64
|
+
verification_before,
|
|
65
|
+
verification_after: null,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const bakDir = backupRoot(projectDir);
|
|
69
|
+
mkdirSync(bakDir, { recursive: true });
|
|
70
|
+
const backups = [];
|
|
71
|
+
try {
|
|
72
|
+
// One file may appear in multiple patches — last write wins by path.
|
|
73
|
+
const byPath = new Map();
|
|
74
|
+
for (const p of patches) {
|
|
75
|
+
byPath.set(p.path, p);
|
|
76
|
+
}
|
|
77
|
+
for (const p of byPath.values()) {
|
|
78
|
+
const rel = p.path;
|
|
79
|
+
const abs = join(projectDir, rel);
|
|
80
|
+
const bak = join(bakDir, rel.replace(/[\\/]/g, '__'));
|
|
81
|
+
const prev = readFileSync(abs, 'utf8');
|
|
82
|
+
writeWithDirs(bak, prev);
|
|
83
|
+
backups.push({ abs, bak });
|
|
84
|
+
writeFileSync(abs, p.new_content, 'utf8');
|
|
85
|
+
applied.push({
|
|
86
|
+
proposal_id: p.proposal_id,
|
|
87
|
+
path: rel,
|
|
88
|
+
rule_id: p.rule_id,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const verification_after = verifier.verify(projectDir, {
|
|
92
|
+
paths: touched,
|
|
93
|
+
skipTest: opts?.skipTest,
|
|
94
|
+
skipBuild: opts?.skipBuild ?? true,
|
|
95
|
+
});
|
|
96
|
+
if (!verification_after.ok) {
|
|
97
|
+
for (const { abs, bak } of backups) {
|
|
98
|
+
renameSync(bak, abs);
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
rmSync(bakDir, { recursive: true, force: true });
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
/* ignore */
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
apply: {
|
|
108
|
+
schema: 'optimize_apply_result_v0',
|
|
109
|
+
applied: [],
|
|
110
|
+
skipped: [
|
|
111
|
+
...skipped,
|
|
112
|
+
...applied.map((a) => ({
|
|
113
|
+
proposal_id: a.proposal_id,
|
|
114
|
+
reason: 'rolled back: verification_after failed',
|
|
115
|
+
})),
|
|
116
|
+
],
|
|
117
|
+
rolled_back: true,
|
|
118
|
+
verification_before_ok: true,
|
|
119
|
+
verification_after_ok: false,
|
|
120
|
+
},
|
|
121
|
+
verification_before,
|
|
122
|
+
verification_after,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
rmSync(bakDir, { recursive: true, force: true });
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
/* ignore */
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
apply: {
|
|
133
|
+
schema: 'optimize_apply_result_v0',
|
|
134
|
+
applied,
|
|
135
|
+
skipped,
|
|
136
|
+
rolled_back: false,
|
|
137
|
+
verification_before_ok: true,
|
|
138
|
+
verification_after_ok: true,
|
|
139
|
+
},
|
|
140
|
+
verification_before,
|
|
141
|
+
verification_after,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
for (const { abs, bak } of backups) {
|
|
146
|
+
try {
|
|
147
|
+
renameSync(bak, abs);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
/* ignore */
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
throw err;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
export function applySafeProposals(projectDir, proposals, opts) {
|
|
157
|
+
const safe = proposals.filter((p) => p.auto_apply_safe && p.patch);
|
|
158
|
+
const preSkipped = [];
|
|
159
|
+
for (const p of proposals) {
|
|
160
|
+
if (!p.auto_apply_safe || !p.patch) {
|
|
161
|
+
preSkipped.push({
|
|
162
|
+
proposal_id: p.proposal_id,
|
|
163
|
+
reason: 'not auto_apply_safe (guidance-only)',
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return applyVerifiedPatches(projectDir, safe.map((p) => ({
|
|
168
|
+
proposal_id: p.proposal_id,
|
|
169
|
+
path: p.patch.path,
|
|
170
|
+
new_content: p.patch.new_content,
|
|
171
|
+
rule_id: p.patch.rule_id,
|
|
172
|
+
})), { ...opts, preSkipped });
|
|
173
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ``netgreener optimize <projectDir>`` — local OptimizerProvider + VerificationProvider.
|
|
3
|
+
*
|
|
4
|
+
* Default: local proposals (no LLM).
|
|
5
|
+
* ``--llm``: opt-in shared api_server ``POST /optimize/function`` (owner OK 2026-10-02).
|
|
6
|
+
* ``--apply``: only auto_apply_safe local patches (not LLM results).
|
|
7
|
+
* ``--llm --apply-verified``: LLM splice writes gated by VerificationProvider (owner OK).
|
|
8
|
+
* Does not invent measured savings. Does not flip EXPORT_MODE.
|
|
9
|
+
*/
|
|
10
|
+
export type OptimizeCliFlags = {
|
|
11
|
+
projectDir: string;
|
|
12
|
+
projectId: string;
|
|
13
|
+
paths: string[] | null;
|
|
14
|
+
apply: boolean;
|
|
15
|
+
/** LLM-only: write reviewed splices via VerificationProvider. */
|
|
16
|
+
applyVerified: boolean;
|
|
17
|
+
verifyOnly: boolean;
|
|
18
|
+
skipTest: boolean;
|
|
19
|
+
quiet: boolean;
|
|
20
|
+
help: boolean;
|
|
21
|
+
llm: boolean;
|
|
22
|
+
maxLlm: number;
|
|
23
|
+
};
|
|
24
|
+
export declare function formatOptimizeHelp(): string;
|
|
25
|
+
export declare function parseOptimizeArgs(argv: string[]): OptimizeCliFlags;
|
|
26
|
+
export declare function runOptimizeCli(argv: string[]): number | Promise<number>;
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ``netgreener optimize <projectDir>`` — local OptimizerProvider + VerificationProvider.
|
|
3
|
+
*
|
|
4
|
+
* Default: local proposals (no LLM).
|
|
5
|
+
* ``--llm``: opt-in shared api_server ``POST /optimize/function`` (owner OK 2026-10-02).
|
|
6
|
+
* ``--apply``: only auto_apply_safe local patches (not LLM results).
|
|
7
|
+
* ``--llm --apply-verified``: LLM splice writes gated by VerificationProvider (owner OK).
|
|
8
|
+
* Does not invent measured savings. Does not flip EXPORT_MODE.
|
|
9
|
+
*/
|
|
10
|
+
import { accessSync, constants, statSync } from 'node:fs';
|
|
11
|
+
import { resolve } from 'node:path';
|
|
12
|
+
import { buildResourceAnalyzeResultFromPaths } from '../analyze/buildResourceFindings.js';
|
|
13
|
+
import { applySafeProposals, applyVerifiedPatches } from './apply.js';
|
|
14
|
+
import { buildVerifiedPatchesFromLlmSession } from './llmApply.js';
|
|
15
|
+
import { runLlmOptimizeSession } from './llmSession.js';
|
|
16
|
+
import { createLocalJsTsOptimizerProvider } from './provider.js';
|
|
17
|
+
import { createLocalJsTsVerificationProvider } from './verification.js';
|
|
18
|
+
export function formatOptimizeHelp() {
|
|
19
|
+
return `Usage: netgreener optimize <projectDir> [options]
|
|
20
|
+
|
|
21
|
+
Local JS/TS OptimizerProvider + VerificationProvider (default):
|
|
22
|
+
1. Run Analyze (upload=never) for resource_finding_v1 candidates
|
|
23
|
+
2. Emit optimize_result_v0 proposals (guidance; some may include safe patches)
|
|
24
|
+
3. Optional --apply: write only auto_apply_safe patches after verify gates
|
|
25
|
+
|
|
26
|
+
LLM session (opt-in, same api_server brain as Python/VSCE):
|
|
27
|
+
--llm Call POST /optimize/function for top candidates
|
|
28
|
+
Requires NETGREENER_TOKEN + numeric --project-id
|
|
29
|
+
--llm --apply-verified
|
|
30
|
+
After LLM session: splice reviewed code, VerificationProvider
|
|
31
|
+
before+after, rollback on fail (NOT the same as --apply)
|
|
32
|
+
|
|
33
|
+
Does NOT:
|
|
34
|
+
• invent measured energy/cost savings
|
|
35
|
+
• flip NETGREENER_EXPORT_MODE
|
|
36
|
+
• apply LLM patches via --apply (use --apply-verified with --llm)
|
|
37
|
+
|
|
38
|
+
Options:
|
|
39
|
+
--project-id <id> Stamp project_id (default: local; required numeric for --llm)
|
|
40
|
+
--paths a,b Explicit repo-relative paths (skip auto-walk)
|
|
41
|
+
--apply Apply auto_apply_safe patches (verify before+after; rollback on fail)
|
|
42
|
+
--apply-verified With --llm only: apply LLM splices via VerificationProvider
|
|
43
|
+
--verify Run VerificationProvider only (no proposals apply)
|
|
44
|
+
--llm Shared LLM optimize session (api_server)
|
|
45
|
+
--max-llm <n> Max findings for --llm (default 5, max 20)
|
|
46
|
+
--skip-test Do not run npm test during verification
|
|
47
|
+
--quiet Suppress stderr status lines
|
|
48
|
+
-h, --help Show this help
|
|
49
|
+
|
|
50
|
+
Env (--llm):
|
|
51
|
+
NETGREENER_TOKEN / NETGREENER_SERVICE_TOKEN
|
|
52
|
+
NETGREENER_API_URL (optional)
|
|
53
|
+
NETGREENER_PROJECT_ID (optional alias for --project-id)
|
|
54
|
+
|
|
55
|
+
Examples:
|
|
56
|
+
netgreener optimize ./fixtures/analyze-sample
|
|
57
|
+
netgreener optimize ./my-app --verify
|
|
58
|
+
netgreener optimize ./my-app --apply --skip-test
|
|
59
|
+
netgreener optimize ./my-app --llm --project-id 97
|
|
60
|
+
netgreener optimize ./my-app --llm --apply-verified --project-id 97 --skip-test
|
|
61
|
+
|
|
62
|
+
See OPTIMIZE_STUB.md for MP4 boundaries.
|
|
63
|
+
`;
|
|
64
|
+
}
|
|
65
|
+
export function parseOptimizeArgs(argv) {
|
|
66
|
+
const flags = {
|
|
67
|
+
projectDir: '',
|
|
68
|
+
projectId: 'local',
|
|
69
|
+
paths: null,
|
|
70
|
+
apply: false,
|
|
71
|
+
applyVerified: false,
|
|
72
|
+
verifyOnly: false,
|
|
73
|
+
skipTest: false,
|
|
74
|
+
quiet: false,
|
|
75
|
+
help: false,
|
|
76
|
+
llm: false,
|
|
77
|
+
maxLlm: 5,
|
|
78
|
+
};
|
|
79
|
+
const positionals = [];
|
|
80
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
81
|
+
const arg = argv[i] ?? '';
|
|
82
|
+
if (arg === '-h' || arg === '--help') {
|
|
83
|
+
flags.help = true;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (arg === '--quiet') {
|
|
87
|
+
flags.quiet = true;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (arg === '--apply') {
|
|
91
|
+
flags.apply = true;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (arg === '--apply-verified') {
|
|
95
|
+
flags.applyVerified = true;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (arg === '--verify') {
|
|
99
|
+
flags.verifyOnly = true;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (arg === '--llm') {
|
|
103
|
+
flags.llm = true;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (arg === '--skip-test') {
|
|
107
|
+
flags.skipTest = true;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (arg === '--max-llm') {
|
|
111
|
+
const n = Number(argv[++i] || '5');
|
|
112
|
+
flags.maxLlm = Number.isFinite(n) ? n : 5;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (arg.startsWith('--max-llm=')) {
|
|
116
|
+
const n = Number(arg.slice('--max-llm='.length) || '5');
|
|
117
|
+
flags.maxLlm = Number.isFinite(n) ? n : 5;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (arg === '--project-id') {
|
|
121
|
+
flags.projectId = String(argv[++i] || 'local');
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (arg.startsWith('--project-id=')) {
|
|
125
|
+
flags.projectId = arg.slice('--project-id='.length) || 'local';
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (arg === '--paths') {
|
|
129
|
+
const raw = String(argv[++i] || '');
|
|
130
|
+
flags.paths = raw
|
|
131
|
+
.split(',')
|
|
132
|
+
.map((p) => p.trim())
|
|
133
|
+
.filter(Boolean);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (arg.startsWith('--paths=')) {
|
|
137
|
+
flags.paths = arg
|
|
138
|
+
.slice('--paths='.length)
|
|
139
|
+
.split(',')
|
|
140
|
+
.map((p) => p.trim())
|
|
141
|
+
.filter(Boolean);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (arg.startsWith('-')) {
|
|
145
|
+
throw new Error(`Unknown optimize option: ${arg}`);
|
|
146
|
+
}
|
|
147
|
+
positionals.push(arg);
|
|
148
|
+
}
|
|
149
|
+
flags.projectDir = positionals[0] || '';
|
|
150
|
+
return flags;
|
|
151
|
+
}
|
|
152
|
+
export function runOptimizeCli(argv) {
|
|
153
|
+
let flags;
|
|
154
|
+
try {
|
|
155
|
+
flags = parseOptimizeArgs(argv);
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
|
|
159
|
+
process.stderr.write(formatOptimizeHelp());
|
|
160
|
+
return 2;
|
|
161
|
+
}
|
|
162
|
+
if (flags.help || !flags.projectDir) {
|
|
163
|
+
process.stdout.write(formatOptimizeHelp());
|
|
164
|
+
return flags.help ? 0 : 2;
|
|
165
|
+
}
|
|
166
|
+
if (flags.llm && flags.apply) {
|
|
167
|
+
process.stderr.write('netgreener optimize: --llm cannot be combined with --apply ' +
|
|
168
|
+
'(local safe rules only). Use --llm --apply-verified for VerificationProvider LLM writes.\n');
|
|
169
|
+
return 2;
|
|
170
|
+
}
|
|
171
|
+
if (flags.applyVerified && !flags.llm) {
|
|
172
|
+
process.stderr.write('netgreener optimize: --apply-verified requires --llm ' +
|
|
173
|
+
'(for local safe rules use --apply).\n');
|
|
174
|
+
return 2;
|
|
175
|
+
}
|
|
176
|
+
const projectDir = resolve(flags.projectDir);
|
|
177
|
+
try {
|
|
178
|
+
accessSync(projectDir, constants.R_OK);
|
|
179
|
+
if (!statSync(projectDir).isDirectory()) {
|
|
180
|
+
process.stderr.write(`Not a directory: ${projectDir}\n`);
|
|
181
|
+
return 2;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
process.stderr.write(`Cannot read projectDir: ${projectDir}\n`);
|
|
186
|
+
return 2;
|
|
187
|
+
}
|
|
188
|
+
if (flags.verifyOnly) {
|
|
189
|
+
if (!flags.quiet) {
|
|
190
|
+
process.stderr.write(`netgreener optimize --verify: ${projectDir}\n`);
|
|
191
|
+
}
|
|
192
|
+
const verification = createLocalJsTsVerificationProvider().verify(projectDir, {
|
|
193
|
+
skipTest: flags.skipTest,
|
|
194
|
+
skipBuild: true,
|
|
195
|
+
});
|
|
196
|
+
process.stdout.write(`${JSON.stringify(verification, null, 2)}\n`);
|
|
197
|
+
return verification.ok ? 0 : 1;
|
|
198
|
+
}
|
|
199
|
+
if (flags.llm) {
|
|
200
|
+
return runLlmOptimizeCli(projectDir, flags);
|
|
201
|
+
}
|
|
202
|
+
if (!flags.quiet) {
|
|
203
|
+
process.stderr.write(`netgreener optimize: Analyze + local proposals for ${projectDir} (upload=never)…\n`);
|
|
204
|
+
}
|
|
205
|
+
const analyze = buildResourceAnalyzeResultFromPaths(projectDir, flags.paths, { projectId: flags.projectId });
|
|
206
|
+
const provider = createLocalJsTsOptimizerProvider();
|
|
207
|
+
const result = provider.proposeFromFindings(analyze.findings, {
|
|
208
|
+
projectId: flags.projectId,
|
|
209
|
+
projectDir,
|
|
210
|
+
});
|
|
211
|
+
if (flags.apply) {
|
|
212
|
+
if (!flags.quiet) {
|
|
213
|
+
process.stderr.write(`netgreener optimize --apply: ${result.stats.auto_apply_safe} safe patch(es); verifying…\n`);
|
|
214
|
+
}
|
|
215
|
+
const { apply, verification_before, verification_after } = applySafeProposals(projectDir, result.proposals, { skipTest: flags.skipTest, skipBuild: true });
|
|
216
|
+
result.verification = verification_after || verification_before;
|
|
217
|
+
result.notes = [
|
|
218
|
+
...result.notes,
|
|
219
|
+
apply.rolled_back
|
|
220
|
+
? 'Apply rolled back after verification_after failure.'
|
|
221
|
+
: apply.applied.length
|
|
222
|
+
? `Applied ${apply.applied.length} safe patch(es).`
|
|
223
|
+
: 'No safe patches applied.',
|
|
224
|
+
];
|
|
225
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
226
|
+
if (!flags.quiet) {
|
|
227
|
+
process.stderr.write(`netgreener optimize --apply: applied=${apply.applied.length} ` +
|
|
228
|
+
`rolled_back=${apply.rolled_back} verify_ok=${result.verification?.ok}\n`);
|
|
229
|
+
}
|
|
230
|
+
if (!apply.verification_before_ok)
|
|
231
|
+
return 1;
|
|
232
|
+
if (apply.rolled_back)
|
|
233
|
+
return 1;
|
|
234
|
+
return 0;
|
|
235
|
+
}
|
|
236
|
+
result.verification = createLocalJsTsVerificationProvider().verify(projectDir, {
|
|
237
|
+
skipTest: true,
|
|
238
|
+
skipBuild: true,
|
|
239
|
+
});
|
|
240
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
241
|
+
if (!flags.quiet) {
|
|
242
|
+
process.stderr.write(`netgreener optimize: ${result.stats.proposals} proposal(s), ` +
|
|
243
|
+
`${result.stats.auto_apply_safe} auto_apply_safe — no patches applied ` +
|
|
244
|
+
`(use --apply).\n`);
|
|
245
|
+
}
|
|
246
|
+
return 0;
|
|
247
|
+
}
|
|
248
|
+
async function runLlmOptimizeCli(projectDir, flags) {
|
|
249
|
+
const token = (process.env.NETGREENER_TOKEN ||
|
|
250
|
+
process.env.NETGREENER_SERVICE_TOKEN ||
|
|
251
|
+
'').trim();
|
|
252
|
+
if (!token) {
|
|
253
|
+
process.stderr.write('netgreener optimize --llm: set NETGREENER_TOKEN (or NETGREENER_SERVICE_TOKEN).\n');
|
|
254
|
+
return 2;
|
|
255
|
+
}
|
|
256
|
+
const projectIdRaw = flags.projectId !== 'local'
|
|
257
|
+
? flags.projectId
|
|
258
|
+
: (process.env.NETGREENER_PROJECT_ID || '').trim();
|
|
259
|
+
const projectId = Number(projectIdRaw);
|
|
260
|
+
if (!Number.isSafeInteger(projectId) || projectId <= 0) {
|
|
261
|
+
process.stderr.write('netgreener optimize --llm: require numeric --project-id or NETGREENER_PROJECT_ID.\n');
|
|
262
|
+
return 2;
|
|
263
|
+
}
|
|
264
|
+
if (!flags.quiet) {
|
|
265
|
+
process.stderr.write(`netgreener optimize --llm: Analyze + api_server /optimize/function ` +
|
|
266
|
+
`(project=${projectId}, max=${flags.maxLlm})…\n`);
|
|
267
|
+
}
|
|
268
|
+
const analyze = buildResourceAnalyzeResultFromPaths(projectDir, flags.paths, {
|
|
269
|
+
projectId: String(projectId),
|
|
270
|
+
});
|
|
271
|
+
const apiUrl = (process.env.NETGREENER_API_URL || '').trim() || undefined;
|
|
272
|
+
const session = await runLlmOptimizeSession({
|
|
273
|
+
projectDir,
|
|
274
|
+
projectId,
|
|
275
|
+
findings: analyze.findings,
|
|
276
|
+
token,
|
|
277
|
+
apiUrl,
|
|
278
|
+
maxFindings: flags.maxLlm,
|
|
279
|
+
byok: {
|
|
280
|
+
provider: process.env.NETGREENER_LLM_PROVIDER || undefined,
|
|
281
|
+
key: process.env.NETGREENER_LLM_KEY || undefined,
|
|
282
|
+
endpoint: process.env.NETGREENER_LLM_ENDPOINT || undefined,
|
|
283
|
+
model: process.env.NETGREENER_LLM_MODEL || undefined,
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
if (!flags.applyVerified) {
|
|
287
|
+
process.stdout.write(`${JSON.stringify(session, null, 2)}\n`);
|
|
288
|
+
const okCount = session.items.filter((i) => i.ok).length;
|
|
289
|
+
if (!flags.quiet) {
|
|
290
|
+
process.stderr.write(`netgreener optimize --llm: ${okCount}/${session.items.length} ok ` +
|
|
291
|
+
`(of ${session.candidates_considered} candidates). Review JSON; ` +
|
|
292
|
+
`or re-run with --apply-verified.\n`);
|
|
293
|
+
}
|
|
294
|
+
return session.items.length === 0 || okCount > 0 ? 0 : 1;
|
|
295
|
+
}
|
|
296
|
+
if (!flags.quiet) {
|
|
297
|
+
process.stderr.write('netgreener optimize --llm --apply-verified: splicing reviewed items; verifying…\n');
|
|
298
|
+
}
|
|
299
|
+
const { patches, skipped } = buildVerifiedPatchesFromLlmSession(projectDir, session);
|
|
300
|
+
const { apply, verification_before, verification_after } = applyVerifiedPatches(projectDir, patches, { skipTest: flags.skipTest, skipBuild: true, preSkipped: skipped });
|
|
301
|
+
const out = {
|
|
302
|
+
schema: 'optimize_llm_apply_verified_v0',
|
|
303
|
+
session,
|
|
304
|
+
apply,
|
|
305
|
+
verification_before,
|
|
306
|
+
verification_after,
|
|
307
|
+
notes: [
|
|
308
|
+
'LLM splices applied only when review_passed/apply_recommended allow.',
|
|
309
|
+
'VerificationProvider before+after; rollback on fail.',
|
|
310
|
+
'Not auto_apply_safe. No measured savings claimed.',
|
|
311
|
+
],
|
|
312
|
+
};
|
|
313
|
+
process.stdout.write(`${JSON.stringify(out, null, 2)}\n`);
|
|
314
|
+
if (!flags.quiet) {
|
|
315
|
+
process.stderr.write(`netgreener optimize --llm --apply-verified: applied=${apply.applied.length} ` +
|
|
316
|
+
`skipped=${apply.skipped.length} rolled_back=${apply.rolled_back}\n`);
|
|
317
|
+
}
|
|
318
|
+
if (!apply.verification_before_ok)
|
|
319
|
+
return 1;
|
|
320
|
+
if (apply.rolled_back)
|
|
321
|
+
return 1;
|
|
322
|
+
return apply.applied.length > 0 || patches.length === 0 ? 0 : 1;
|
|
323
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { OptimizeActionKind, OptimizeApplyResultV0, OptimizePatchV0, OptimizeProposalV0, OptimizeResultV0, VerificationResultV0, VerifyGateId, VerifyGateOutcome, } from './types.js';
|
|
2
|
+
export { createLocalJsTsOptimizerProvider, type OptimizerProvider, } from './provider.js';
|
|
3
|
+
export { createLocalJsTsVerificationProvider, type VerificationProvider, } from './verification.js';
|
|
4
|
+
export { tryBuildSafePatch, trySafePatchRulesOnSource, type SafePatchMatch } from './safePatches.js';
|
|
5
|
+
export { applySafeProposals } from './apply.js';
|
|
6
|
+
export { formatOptimizeHelp, parseOptimizeArgs, runOptimizeCli, type OptimizeCliFlags, } from './cli.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createLocalJsTsOptimizerProvider, } from './provider.js';
|
|
2
|
+
export { createLocalJsTsVerificationProvider, } from './verification.js';
|
|
3
|
+
export { tryBuildSafePatch, trySafePatchRulesOnSource } from './safePatches.js';
|
|
4
|
+
export { applySafeProposals } from './apply.js';
|
|
5
|
+
export { formatOptimizeHelp, parseOptimizeArgs, runOptimizeCli, } from './cli.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build VerificationProvider-gated writes from an LLM optimize session.
|
|
3
|
+
*
|
|
4
|
+
* Owner OK 2026-10-02 (LLM) + apply-verified OK (chat).
|
|
5
|
+
* Does NOT mark patches auto_apply_safe. Does not invent savings.
|
|
6
|
+
*/
|
|
7
|
+
import type { VerifiedPatchWrite } from './apply.js';
|
|
8
|
+
import type { LlmOptimizeSessionItem, LlmOptimizeSessionV0 } from './llmSession.js';
|
|
9
|
+
export declare function spliceSnippetIntoFile(fullText: string, lineStart: number, lineEnd: number, newSnippet: string): string;
|
|
10
|
+
export declare function llmItemEligibleForVerifiedApply(item: LlmOptimizeSessionItem): {
|
|
11
|
+
ok: true;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
reason: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function buildVerifiedPatchesFromLlmSession(projectDir: string, session: LlmOptimizeSessionV0): {
|
|
17
|
+
patches: VerifiedPatchWrite[];
|
|
18
|
+
skipped: Array<{
|
|
19
|
+
proposal_id: string;
|
|
20
|
+
reason: string;
|
|
21
|
+
}>;
|
|
22
|
+
};
|