@nerviq/cli 1.18.0 → 1.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/LICENSE +23 -23
  2. package/README.md +2 -2
  3. package/bin/cli.js +131 -130
  4. package/package.json +2 -1
  5. package/src/activity.js +1039 -1039
  6. package/src/adoption-advisor.js +299 -299
  7. package/src/aider/config-parser.js +166 -166
  8. package/src/aider/context.js +158 -158
  9. package/src/aider/deep-review.js +316 -316
  10. package/src/aider/domain-packs.js +303 -303
  11. package/src/aider/freshness.js +93 -93
  12. package/src/aider/governance.js +253 -253
  13. package/src/aider/interactive.js +334 -334
  14. package/src/aider/mcp-packs.js +329 -329
  15. package/src/aider/patch.js +214 -214
  16. package/src/aider/plans.js +186 -186
  17. package/src/aider/premium.js +360 -360
  18. package/src/aider/setup.js +404 -404
  19. package/src/aider/techniques.js +16 -16
  20. package/src/analyze.js +951 -951
  21. package/src/anti-patterns.js +485 -485
  22. package/src/audit/instruction-files.js +180 -180
  23. package/src/audit/recommendations.js +577 -577
  24. package/src/auto-suggest.js +154 -154
  25. package/src/badge.js +13 -13
  26. package/src/behavioral-drift.js +801 -801
  27. package/src/benchmark.js +67 -67
  28. package/src/catalog.js +103 -103
  29. package/src/certification.js +128 -128
  30. package/src/codex/config-parser.js +183 -183
  31. package/src/codex/context.js +223 -223
  32. package/src/codex/deep-review.js +493 -493
  33. package/src/codex/domain-packs.js +394 -394
  34. package/src/codex/freshness.js +84 -84
  35. package/src/codex/governance.js +192 -192
  36. package/src/codex/interactive.js +618 -618
  37. package/src/codex/mcp-packs.js +914 -914
  38. package/src/codex/patch.js +209 -209
  39. package/src/codex/plans.js +251 -251
  40. package/src/codex/premium.js +614 -614
  41. package/src/codex/setup.js +591 -591
  42. package/src/context.js +320 -320
  43. package/src/continuous-ops.js +681 -681
  44. package/src/copilot/activity.js +309 -309
  45. package/src/copilot/deep-review.js +346 -346
  46. package/src/copilot/domain-packs.js +372 -372
  47. package/src/copilot/freshness.js +57 -57
  48. package/src/copilot/governance.js +222 -222
  49. package/src/copilot/interactive.js +406 -406
  50. package/src/copilot/mcp-packs.js +826 -826
  51. package/src/copilot/plans.js +253 -253
  52. package/src/copilot/premium.js +451 -451
  53. package/src/copilot/setup.js +488 -488
  54. package/src/cost-tracking.js +61 -61
  55. package/src/cursor/activity.js +301 -301
  56. package/src/cursor/config-parser.js +265 -265
  57. package/src/cursor/context.js +256 -256
  58. package/src/cursor/deep-review.js +334 -334
  59. package/src/cursor/domain-packs.js +368 -368
  60. package/src/cursor/freshness.js +65 -65
  61. package/src/cursor/governance.js +229 -229
  62. package/src/cursor/interactive.js +391 -391
  63. package/src/cursor/mcp-packs.js +828 -828
  64. package/src/cursor/plans.js +254 -254
  65. package/src/cursor/premium.js +469 -469
  66. package/src/cursor/setup.js +488 -488
  67. package/src/dashboard.js +493 -493
  68. package/src/deep-review.js +428 -428
  69. package/src/deprecation.js +98 -98
  70. package/src/diff-only.js +280 -280
  71. package/src/doctor.js +119 -119
  72. package/src/domain-pack-expansion.js +1033 -1033
  73. package/src/domain-packs.js +387 -387
  74. package/src/feedback.js +178 -178
  75. package/src/fix-engine.js +783 -783
  76. package/src/fix-prompts.js +122 -122
  77. package/src/formatters/sarif.js +115 -115
  78. package/src/freshness.js +74 -74
  79. package/src/gemini/config-parser.js +275 -275
  80. package/src/gemini/context.js +290 -221
  81. package/src/gemini/deep-review.js +559 -559
  82. package/src/gemini/domain-packs.js +393 -393
  83. package/src/gemini/freshness.js +66 -66
  84. package/src/gemini/governance.js +201 -201
  85. package/src/gemini/interactive.js +860 -860
  86. package/src/gemini/mcp-packs.js +915 -915
  87. package/src/gemini/plans.js +269 -269
  88. package/src/gemini/premium.js +760 -760
  89. package/src/gemini/setup.js +692 -692
  90. package/src/gemini/techniques.js +105 -33
  91. package/src/governance.js +72 -72
  92. package/src/harmony/add.js +68 -68
  93. package/src/harmony/advisor.js +333 -333
  94. package/src/harmony/canon.js +565 -565
  95. package/src/harmony/cli.js +591 -591
  96. package/src/harmony/drift.js +401 -401
  97. package/src/harmony/governance.js +313 -313
  98. package/src/harmony/memory.js +239 -239
  99. package/src/harmony/sync.js +475 -475
  100. package/src/harmony/watch.js +370 -370
  101. package/src/hook-validation.js +342 -342
  102. package/src/index.js +271 -271
  103. package/src/init.js +184 -184
  104. package/src/instruction-surfaces.js +185 -185
  105. package/src/integrations.js +144 -144
  106. package/src/interactive.js +118 -118
  107. package/src/locales/en.json +1 -1
  108. package/src/locales/es.json +1 -1
  109. package/src/mcp-packs.js +830 -830
  110. package/src/mcp-server.js +726 -726
  111. package/src/mcp-validation.js +337 -337
  112. package/src/nerviq-sync.json +7 -7
  113. package/src/opencode/config-parser.js +109 -109
  114. package/src/opencode/context.js +247 -247
  115. package/src/opencode/deep-review.js +313 -313
  116. package/src/opencode/domain-packs.js +262 -262
  117. package/src/opencode/freshness.js +66 -66
  118. package/src/opencode/governance.js +159 -159
  119. package/src/opencode/interactive.js +392 -392
  120. package/src/opencode/mcp-packs.js +705 -705
  121. package/src/opencode/patch.js +184 -184
  122. package/src/opencode/plans.js +231 -231
  123. package/src/opencode/premium.js +413 -413
  124. package/src/opencode/setup.js +449 -449
  125. package/src/opencode/techniques.js +27 -27
  126. package/src/operating-profile.js +574 -574
  127. package/src/org.js +152 -152
  128. package/src/permission-rules.js +218 -218
  129. package/src/plans.js +839 -839
  130. package/src/platform-change-manifest.js +86 -86
  131. package/src/plugins.js +110 -110
  132. package/src/policy-layers.js +210 -210
  133. package/src/profiles.js +124 -124
  134. package/src/prompt-injection.js +74 -74
  135. package/src/public-api.js +173 -173
  136. package/src/recommendation-rules.js +84 -84
  137. package/src/repo-archetype.js +386 -386
  138. package/src/secret-patterns.js +39 -39
  139. package/src/server.js +527 -527
  140. package/src/setup/analysis.js +607 -607
  141. package/src/setup/runtime.js +172 -172
  142. package/src/setup.js +677 -677
  143. package/src/shared/capabilities.js +194 -194
  144. package/src/source-urls.js +132 -132
  145. package/src/stack-checks.js +565 -565
  146. package/src/supplemental-checks.js +13 -13
  147. package/src/synergy/adaptive.js +261 -261
  148. package/src/synergy/compensation.js +137 -137
  149. package/src/synergy/evidence.js +193 -193
  150. package/src/synergy/learning.js +199 -199
  151. package/src/synergy/patterns.js +227 -227
  152. package/src/synergy/ranking.js +83 -83
  153. package/src/synergy/report.js +165 -165
  154. package/src/synergy/routing.js +146 -146
  155. package/src/techniques/api.js +407 -407
  156. package/src/techniques/automation.js +316 -316
  157. package/src/techniques/compliance.js +257 -257
  158. package/src/techniques/hygiene.js +294 -294
  159. package/src/techniques/instructions.js +243 -243
  160. package/src/techniques/observability.js +226 -226
  161. package/src/techniques/optimization.js +142 -142
  162. package/src/techniques/quality.js +318 -318
  163. package/src/techniques/security.js +237 -237
  164. package/src/techniques/shared.js +443 -443
  165. package/src/techniques/stacks.js +2294 -2294
  166. package/src/techniques/tools.js +106 -106
  167. package/src/techniques/workflow.js +413 -413
  168. package/src/techniques.js +81 -81
  169. package/src/terminology.js +73 -73
  170. package/src/token-estimate.js +35 -35
  171. package/src/usage-patterns.js +99 -99
  172. package/src/verification-metadata.js +145 -145
  173. package/src/watch.js +247 -247
  174. package/src/windsurf/activity.js +302 -302
  175. package/src/windsurf/config-parser.js +267 -267
  176. package/src/windsurf/context.js +249 -249
  177. package/src/windsurf/deep-review.js +337 -337
  178. package/src/windsurf/domain-packs.js +370 -370
  179. package/src/windsurf/freshness.js +36 -36
  180. package/src/windsurf/governance.js +231 -231
  181. package/src/windsurf/interactive.js +388 -388
  182. package/src/windsurf/mcp-packs.js +792 -792
  183. package/src/windsurf/plans.js +247 -247
  184. package/src/windsurf/premium.js +468 -468
  185. package/src/windsurf/setup.js +471 -471
  186. package/src/windsurf/techniques.js +17 -17
  187. package/src/workspace.js +375 -375
package/src/activity.js CHANGED
@@ -1,1039 +1,1039 @@
1
- const fs = require('fs');
2
- const os = require('os');
3
- const path = require('path');
4
- const { version } = require('../package.json');
5
- const {
6
- resolveProjectStateReadPath,
7
- ensureProjectStateDir,
8
- } = require('./state-paths');
9
-
10
- /**
11
- * Generate a machine-level user identity for audit tracking.
12
- * Format: hostname:username — no PII, just machine identity.
13
- * @returns {string}
14
- */
15
- function getUserId() {
16
- try {
17
- const hostname = os.hostname();
18
- const username = os.userInfo().username;
19
- return `${hostname}:${username}`;
20
- } catch {
21
- return 'unknown:unknown';
22
- }
23
- }
24
-
25
- let _lastTimestamp = '';
26
- let _counter = 0;
27
-
28
- const SNAPSHOT_MILESTONES = ['baseline', 'post-fix', 'pre-upgrade', 'release'];
29
-
30
- function timestampId() {
31
- const ts = new Date().toISOString().replace(/[:.]/g, '-');
32
- if (ts === _lastTimestamp) {
33
- _counter++;
34
- return `${ts}-${_counter}`;
35
- }
36
- _lastTimestamp = ts;
37
- _counter = 0;
38
- return ts;
39
- }
40
-
41
- function ensureArtifactDirs(dir) {
42
- const root = ensureProjectStateDir(dir);
43
- const activityDir = ensureProjectStateDir(dir, 'activity');
44
- const rollbackDir = ensureProjectStateDir(dir, 'rollbacks');
45
- const snapshotDir = ensureProjectStateDir(dir, 'snapshots');
46
- const outcomesDir = ensureProjectStateDir(dir, 'outcomes');
47
- return { root, activityDir, rollbackDir, snapshotDir, outcomesDir };
48
- }
49
-
50
- function writeJson(filePath, payload) {
51
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
52
- fs.writeFileSync(filePath, JSON.stringify(payload, null, 2), 'utf8');
53
- }
54
-
55
- function writeActivityArtifact(dir, type, payload) {
56
- const id = timestampId();
57
- const { activityDir } = ensureArtifactDirs(dir);
58
- const filePath = path.join(activityDir, `${id}-${type}.json`);
59
- writeJson(filePath, {
60
- id,
61
- type,
62
- createdAt: new Date().toISOString(),
63
- userId: getUserId(),
64
- ...payload,
65
- });
66
- return {
67
- id,
68
- filePath,
69
- relativePath: path.relative(dir, filePath),
70
- };
71
- }
72
-
73
- function writeRollbackArtifact(dir, payload) {
74
- const id = timestampId();
75
- const { rollbackDir } = ensureArtifactDirs(dir);
76
- const filePath = path.join(rollbackDir, `${id}.json`);
77
- writeJson(filePath, {
78
- id,
79
- createdAt: new Date().toISOString(),
80
- userId: getUserId(),
81
- rollbackType: 'delete-created-files',
82
- ...payload,
83
- });
84
- return {
85
- id,
86
- filePath,
87
- relativePath: path.relative(dir, filePath),
88
- };
89
- }
90
-
91
- function summarizeSnapshot(snapshotKind, payload) {
92
- if (snapshotKind === 'audit') {
93
- return {
94
- score: payload.score,
95
- organicScore: payload.organicScore,
96
- passed: payload.passed,
97
- failed: payload.failed,
98
- checkCount: payload.checkCount,
99
- suggestedNextCommand: payload.suggestedNextCommand,
100
- topActionKeys: Array.isArray(payload.topNextActions)
101
- ? payload.topNextActions.slice(0, 3).map(item => item.key)
102
- : [],
103
- };
104
- }
105
-
106
- if (snapshotKind === 'augment' || snapshotKind === 'suggest-only') {
107
- return {
108
- score: payload.projectSummary?.score,
109
- organicScore: payload.projectSummary?.organicScore,
110
- maturity: payload.projectSummary?.maturity,
111
- domains: payload.projectSummary?.domains || [],
112
- topActionKeys: Array.isArray(payload.topNextActions)
113
- ? payload.topNextActions.slice(0, 3).map(item => item.key)
114
- : [],
115
- };
116
- }
117
-
118
- if (snapshotKind === 'benchmark') {
119
- return {
120
- beforeScore: payload.before?.score,
121
- afterScore: payload.after?.score,
122
- scoreDelta: payload.delta?.score,
123
- organicDelta: payload.delta?.organicScore,
124
- decisionGuidance: payload.executiveSummary?.decisionGuidance || null,
125
- };
126
- }
127
-
128
- if (snapshotKind === 'governance') {
129
- return {
130
- permissionProfiles: Array.isArray(payload.permissionProfiles) ? payload.permissionProfiles.length : 0,
131
- hooks: Array.isArray(payload.hookRegistry) ? payload.hookRegistry.length : 0,
132
- policyPacks: Array.isArray(payload.policyPacks) ? payload.policyPacks.length : 0,
133
- domainPacks: Array.isArray(payload.domainPacks) ? payload.domainPacks.length : 0,
134
- mcpPacks: Array.isArray(payload.mcpPacks) ? payload.mcpPacks.length : 0,
135
- };
136
- }
137
-
138
- if (snapshotKind === 'behavioral-drift') {
139
- return {
140
- score: payload.score,
141
- sourceFiles: payload.repoSummary?.sourceFiles ?? 0,
142
- findingCount: Array.isArray(payload.findings) ? payload.findings.length : 0,
143
- driftLabels: Array.isArray(payload.driftLabels) ? payload.driftLabels.slice(0, 5) : [],
144
- utilityShare: payload.structuralSignals?.utilityBalance?.utilityShare ?? null,
145
- layerBreaks: payload.structuralSignals?.layering?.count ?? 0,
146
- };
147
- }
148
-
149
- return {};
150
- }
151
-
152
- function normalizeSnapshotTags(input) {
153
- const values = Array.isArray(input) ? input : (input ? [input] : []);
154
- const seen = new Set();
155
- const tags = [];
156
-
157
- for (const value of values) {
158
- const parts = `${value || ''}`
159
- .split(',')
160
- .map((item) => item.replace(/\s+/g, ' ').trim())
161
- .filter(Boolean);
162
-
163
- for (const part of parts) {
164
- const key = part.toLowerCase();
165
- if (seen.has(key)) continue;
166
- seen.add(key);
167
- tags.push(part.slice(0, 48));
168
- if (tags.length >= 8) {
169
- return tags;
170
- }
171
- }
172
- }
173
-
174
- return tags;
175
- }
176
-
177
- function formatSnapshotTags(tags = []) {
178
- const normalized = normalizeSnapshotTags(tags);
179
- if (normalized.length === 0) return '';
180
- return ` [${normalized.join(', ')}]`;
181
- }
182
-
183
- function normalizeSnapshotMilestone(value) {
184
- if (value === null || value === undefined || value === '') return null;
185
- const normalized = `${value}`.trim().toLowerCase();
186
- if (!SNAPSHOT_MILESTONES.includes(normalized)) {
187
- throw new Error(`snapshot milestone must be one of: ${SNAPSHOT_MILESTONES.join(', ')}`);
188
- }
189
- return normalized;
190
- }
191
-
192
- function formatSnapshotMilestone(value) {
193
- const milestone = normalizeSnapshotMilestone(value);
194
- if (!milestone) return '';
195
- return ` (${milestone})`;
196
- }
197
-
198
- function updateSnapshotIndex(snapshotDir, record) {
199
- const indexPath = path.join(snapshotDir, 'index.json');
200
- let entries = [];
201
-
202
- if (fs.existsSync(indexPath)) {
203
- try {
204
- entries = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
205
- if (!Array.isArray(entries)) {
206
- entries = [];
207
- }
208
- } catch {
209
- entries = [];
210
- }
211
- }
212
-
213
- entries.push(record);
214
- // Prune to keep last 200 entries
215
- const MAX_INDEX_ENTRIES = 200;
216
- if (entries.length > MAX_INDEX_ENTRIES) {
217
- entries = entries.slice(entries.length - MAX_INDEX_ENTRIES);
218
- }
219
- fs.writeFileSync(indexPath, JSON.stringify(entries, null, 2), 'utf8');
220
- }
221
-
222
- /**
223
- * Write a normalized snapshot artifact to .nerviq/snapshots/ and update the index.
224
- * @param {string} dir - Project root directory.
225
- * @param {string} snapshotKind - Snapshot type ('audit', 'benchmark', 'governance', 'augment', 'suggest-only').
226
- * @param {Object} payload - Full result payload to persist.
227
- * @param {Object} [meta={}] - Optional metadata fields merged into the envelope.
228
- * @returns {Object} Artifact record with id, filePath, relativePath, indexPath, and summary.
229
- */
230
- function writeSnapshotArtifact(dir, snapshotKind, payload, meta = {}) {
231
- const id = timestampId();
232
- const { snapshotDir } = ensureArtifactDirs(dir);
233
- const filePath = path.join(snapshotDir, `${id}-${snapshotKind}.json`);
234
- const summary = summarizeSnapshot(snapshotKind, payload);
235
- const metaTags = normalizeSnapshotTags([
236
- ...(Array.isArray(meta.tags) ? meta.tags : (meta.tags ? [meta.tags] : [])),
237
- ...(meta.tag ? [meta.tag] : []),
238
- ]);
239
- const milestone = normalizeSnapshotMilestone(meta.milestone);
240
- const { tags: _ignoredTags, tag: _ignoredTag, ...restMeta } = meta;
241
- const envelope = {
242
- schemaVersion: 1,
243
- artifactType: 'snapshot',
244
- snapshotKind,
245
- id,
246
- createdAt: new Date().toISOString(),
247
- userId: getUserId(),
248
- generatedBy: `nerviq@${version}`,
249
- directory: dir,
250
- summary,
251
- tags: metaTags,
252
- milestone,
253
- ...restMeta,
254
- payload,
255
- };
256
-
257
- writeJson(filePath, envelope);
258
-
259
- const record = {
260
- id,
261
- snapshotKind,
262
- createdAt: envelope.createdAt,
263
- relativePath: path.relative(dir, filePath),
264
- tags: metaTags,
265
- milestone,
266
- summary,
267
- };
268
- updateSnapshotIndex(snapshotDir, record);
269
-
270
- return {
271
- id,
272
- filePath,
273
- relativePath: path.relative(dir, filePath),
274
- indexPath: path.relative(dir, path.join(snapshotDir, 'index.json')),
275
- summary,
276
- };
277
- }
278
-
279
- function readSnapshotIndex(dir) {
280
- const indexPath = resolveProjectStateReadPath(dir, 'snapshots', 'index.json');
281
- if (!fs.existsSync(indexPath)) return [];
282
- try {
283
- const entries = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
284
- return Array.isArray(entries) ? entries : [];
285
- } catch {
286
- return [];
287
- }
288
- }
289
-
290
- /**
291
- * Get the audit score history from saved snapshots, most recent first.
292
- * @param {string} dir - Project root directory.
293
- * @param {number} [limit=20] - Maximum number of entries to return.
294
- * @returns {Object[]} Array of snapshot index entries for audit snapshots.
295
- */
296
- function getHistory(dir, limit = 20) {
297
- const entries = readSnapshotIndex(dir);
298
- return entries
299
- .filter(e => e.snapshotKind === 'audit')
300
- .sort((a, b) => {
301
- const dateDiff = new Date(b.createdAt) - new Date(a.createdAt);
302
- if (dateDiff !== 0) return dateDiff;
303
- return (b.id || '').localeCompare(a.id || '');
304
- })
305
- .slice(0, limit);
306
- }
307
-
308
- function buildCheckDiffDetail(previousResult, currentResult) {
309
- const source = currentResult || previousResult || {};
310
- const previousState = previousResult ? previousResult.passed : undefined;
311
- const currentState = currentResult ? currentResult.passed : undefined;
312
- return {
313
- key: source.key,
314
- name: source.name || source.key,
315
- impact: source.impact || null,
316
- category: source.category || null,
317
- previousState,
318
- currentState,
319
- };
320
- }
321
-
322
- function collectCheckDiff(previousResults = [], currentResults = []) {
323
- const prevMap = new Map();
324
- const currMap = new Map();
325
-
326
- for (const result of previousResults) {
327
- if (result && result.key) prevMap.set(result.key, result);
328
- }
329
- for (const result of currentResults) {
330
- if (result && result.key) currMap.set(result.key, result);
331
- }
332
-
333
- const regressions = [];
334
- const improvements = [];
335
- const newlyApplicable = [];
336
- const noLongerApplicable = [];
337
- const newChecks = [];
338
- const removedChecks = [];
339
-
340
- const allKeys = [...new Set([...prevMap.keys(), ...currMap.keys()])].sort();
341
- for (const key of allKeys) {
342
- const previousResult = prevMap.get(key);
343
- const currentResult = currMap.get(key);
344
- const previousState = previousResult ? previousResult.passed : undefined;
345
- const currentState = currentResult ? currentResult.passed : undefined;
346
- const detail = buildCheckDiffDetail(previousResult, currentResult);
347
-
348
- if (!previousResult) {
349
- if (currentState !== undefined) {
350
- newChecks.push(detail);
351
- }
352
- continue;
353
- }
354
-
355
- if (!currentResult) {
356
- removedChecks.push(detail);
357
- continue;
358
- }
359
-
360
- if (previousState === true && currentState === false) {
361
- regressions.push(detail);
362
- } else if (previousState === false && currentState === true) {
363
- improvements.push(detail);
364
- } else if ((previousState === null || previousState === undefined) && (currentState === true || currentState === false)) {
365
- newlyApplicable.push(detail);
366
- } else if ((currentState === null || currentState === undefined) && (previousState === true || previousState === false)) {
367
- noLongerApplicable.push(detail);
368
- }
369
- }
370
-
371
- return {
372
- regressions,
373
- improvements,
374
- newlyApplicable,
375
- noLongerApplicable,
376
- newChecks,
377
- removedChecks,
378
- };
379
- }
380
-
381
- /**
382
- * Compare the two most recent audit snapshots and return the delta.
383
- * @param {string} dir - Project root directory.
384
- * @returns {Object|null} Comparison with current/previous scores, delta, regressions, improvements, and trend. Null if fewer than 2 snapshots.
385
- */
386
- function compareLatest(dir) {
387
- const audits = getHistory(dir, 2);
388
- if (audits.length < 2) return null;
389
-
390
- const current = audits[0];
391
- const previous = audits[1];
392
- const currentPayload = loadSnapshotPayload(dir, current);
393
- const previousPayload = loadSnapshotPayload(dir, previous);
394
-
395
- const delta = {
396
- score: (current.summary?.score || 0) - (previous.summary?.score || 0),
397
- organic: (current.summary?.organicScore || 0) - (previous.summary?.organicScore || 0),
398
- passed: (current.summary?.passed || 0) - (previous.summary?.passed || 0),
399
- };
400
-
401
- let regressionDetails = [];
402
- let improvementDetails = [];
403
- let newlyApplicableDetails = [];
404
- let noLongerApplicableDetails = [];
405
- let newChecks = [];
406
- let removedChecks = [];
407
- let regressions = [];
408
- let improvements = [];
409
- let detailedDiffAvailable = false;
410
-
411
- if (currentPayload && previousPayload && Array.isArray(currentPayload.results) && Array.isArray(previousPayload.results)) {
412
- const diff = collectCheckDiff(previousPayload.results, currentPayload.results);
413
- regressionDetails = diff.regressions;
414
- improvementDetails = diff.improvements;
415
- newlyApplicableDetails = diff.newlyApplicable;
416
- noLongerApplicableDetails = diff.noLongerApplicable;
417
- newChecks = diff.newChecks;
418
- removedChecks = diff.removedChecks;
419
- regressions = regressionDetails.map((item) => item.key);
420
- improvements = improvementDetails.map((item) => item.key);
421
- detailedDiffAvailable = true;
422
- } else {
423
- const prevKeys = new Set(previous.summary?.topActionKeys || []);
424
- const currKeys = new Set(current.summary?.topActionKeys || []);
425
- for (const key of currKeys) {
426
- if (!prevKeys.has(key)) regressions.push(key);
427
- }
428
- for (const key of prevKeys) {
429
- if (!currKeys.has(key)) improvements.push(key);
430
- }
431
- }
432
-
433
- return {
434
- scoreType: 'audit-snapshot-score',
435
- current: {
436
- date: current.createdAt,
437
- score: current.summary?.score,
438
- passed: current.summary?.passed,
439
- tags: current.tags || [],
440
- milestone: current.milestone || null,
441
- scoreType: 'audit-snapshot-score',
442
- },
443
- previous: {
444
- date: previous.createdAt,
445
- score: previous.summary?.score,
446
- passed: previous.summary?.passed,
447
- tags: previous.tags || [],
448
- milestone: previous.milestone || null,
449
- scoreType: 'audit-snapshot-score',
450
- },
451
- delta,
452
- regressions,
453
- improvements,
454
- regressionDetails,
455
- improvementDetails,
456
- newlyApplicableDetails,
457
- noLongerApplicableDetails,
458
- newChecks,
459
- removedChecks,
460
- detailedDiffAvailable,
461
- trend: delta.score > 0 ? 'improving' : delta.score < 0 ? 'regressing' : 'stable',
462
- };
463
- }
464
-
465
- function formatSnapshotBootstrap(dir, goal = 'history') {
466
- const snapshotCount = getHistory(dir, 50).length;
467
- const lines = [];
468
- const snapshotLabel = snapshotCount === 1
469
- ? '1 saved audit snapshot'
470
- : `${snapshotCount} saved audit snapshots`;
471
-
472
- if (goal === 'compare') {
473
- lines.push(snapshotCount === 0
474
- ? 'Compare needs 2 audit snapshots.'
475
- : 'Compare needs one more audit snapshot.');
476
- } else if (goal === 'trend') {
477
- lines.push(snapshotCount === 0
478
- ? 'Trend needs 2 audit snapshots to start.'
479
- : 'Trend needs one more audit snapshot to become meaningful.');
480
- } else {
481
- lines.push(snapshotCount === 0
482
- ? 'No audit snapshots found yet.'
483
- : 'History is initialized, but compare/trend still need one more snapshot.');
484
- }
485
-
486
- lines.push(` Current state: ${snapshotLabel}.`);
487
-
488
- if (snapshotCount === 0) {
489
- lines.push(' Bootstrap it with:');
490
- lines.push(' 1. Run `nerviq audit --snapshot --milestone baseline --tag "baseline"` to save the baseline.');
491
- lines.push(' 2. Make a meaningful repo change (`nerviq setup --auto` or `nerviq fix --all-critical --auto`).');
492
- lines.push(' 3. Run `nerviq audit --snapshot --milestone post-fix --tag "after-change"` to capture the next state.');
493
- } else {
494
- lines.push(' Next:');
495
- lines.push(' 1. Make a meaningful repo change (`nerviq setup --auto` or `nerviq fix --all-critical --auto`).');
496
- lines.push(' 2. Run `nerviq audit --snapshot --milestone post-fix --tag "after-change"` again.');
497
- }
498
-
499
- if (goal === 'compare') {
500
- lines.push(' Then rerun `nerviq compare`.');
501
- } else if (goal === 'trend') {
502
- lines.push(' Then rerun `nerviq trend`.');
503
- } else {
504
- lines.push(' Then rerun `nerviq history`, `nerviq compare`, or `nerviq trend`.');
505
- }
506
-
507
- return lines.join('\n');
508
- }
509
-
510
- function formatHistory(dir) {
511
- const history = getHistory(dir, 10);
512
- if (history.length === 0) return formatSnapshotBootstrap(dir, 'history');
513
-
514
- const lines = [
515
- 'Audit snapshot history (most recent first):',
516
- ' Score type: saved audit snapshot scores only (not live audits or benchmark projections).',
517
- '',
518
- ];
519
- for (const entry of history) {
520
- const dateStr = entry.createdAt || 'unknown';
521
- const date = dateStr.split('T')[0] || 'unknown';
522
- const time = dateStr.includes('T') ? dateStr.split('T')[1]?.substring(0, 5) || '' : '';
523
- const dateDisplay = time ? `${date} ${time}` : date;
524
- const score = entry.summary?.score ?? '?';
525
- const passed = entry.summary?.passed ?? '?';
526
- const total = entry.summary?.checkCount ?? '?';
527
- lines.push(` ${dateDisplay} snapshot${formatSnapshotMilestone(entry.milestone)}${formatSnapshotTags(entry.tags)} ${score}/100 (${passed}/${total} checks passing)`);
528
- }
529
-
530
- const comparison = compareLatest(dir);
531
- if (comparison) {
532
- lines.push('');
533
- const sign = comparison.delta.score >= 0 ? '+' : '';
534
- lines.push(` Latest snapshot trend: ${comparison.trend} (${sign}${comparison.delta.score} since previous snapshot)`);
535
- if ((comparison.previous.tags || []).length > 0 || (comparison.current.tags || []).length > 0) {
536
- lines.push(` Snapshot tags: previous${formatSnapshotTags(comparison.previous.tags)} -> current${formatSnapshotTags(comparison.current.tags)}`);
537
- }
538
- if (comparison.previous.milestone || comparison.current.milestone) {
539
- lines.push(` Lifecycle: previous${formatSnapshotMilestone(comparison.previous.milestone)} -> current${formatSnapshotMilestone(comparison.current.milestone)}`);
540
- }
541
- if (comparison.improvements.length > 0) {
542
- lines.push(` Fixed: ${comparison.improvements.join(', ')}`);
543
- }
544
- if (comparison.regressions.length > 0) {
545
- lines.push(` New gaps: ${comparison.regressions.join(', ')}`);
546
- }
547
- }
548
-
549
- if (history.length === 1) {
550
- lines.push('');
551
- lines.push(formatSnapshotBootstrap(dir, 'history'));
552
- }
553
-
554
- return lines.join('\n');
555
- }
556
-
557
- function exportTrendReport(dir) {
558
- const history = getHistory(dir, 50);
559
- if (history.length === 0) return null;
560
-
561
- const comparison = compareLatest(dir);
562
- const lines = [
563
- '# Nerviq Audit Snapshot Trend Report',
564
- '',
565
- `**Project:** ${path.basename(dir)}`,
566
- `**Generated:** ${new Date().toISOString().split('T')[0]}`,
567
- `**Audit snapshots:** ${history.length}`,
568
- '',
569
- '## Audit Snapshot History',
570
- '',
571
- '| Date | Milestone | Tags | Score | Passed | Checks |',
572
- '|------|-----------|------|-------|--------|--------|',
573
- ];
574
-
575
- for (const entry of history) {
576
- const date = entry.createdAt?.split('T')[0] || '?';
577
- const milestone = entry.milestone || '-';
578
- const tags = (entry.tags || []).length > 0 ? entry.tags.join(', ') : '-';
579
- lines.push(`| ${date} | ${milestone} | ${tags} | ${entry.summary?.score ?? '?'}/100 | ${entry.summary?.passed ?? '?'} | ${entry.summary?.checkCount ?? '?'} |`);
580
- }
581
-
582
- if (comparison) {
583
- lines.push('');
584
- lines.push('## Latest Comparison');
585
- lines.push('');
586
- lines.push(`- **Previous snapshot score:** ${comparison.previous.score}/100 (${comparison.previous.date?.split('T')[0]})${formatSnapshotMilestone(comparison.previous.milestone)}${formatSnapshotTags(comparison.previous.tags)}`);
587
- lines.push(`- **Current snapshot score:** ${comparison.current.score}/100 (${comparison.current.date?.split('T')[0]})${formatSnapshotMilestone(comparison.current.milestone)}${formatSnapshotTags(comparison.current.tags)}`);
588
- lines.push(`- **Snapshot delta:** ${comparison.delta.score >= 0 ? '+' : ''}${comparison.delta.score} points`);
589
- lines.push(`- **Trend:** ${comparison.trend}`);
590
- if (comparison.improvements.length > 0) lines.push(`- **Fixed:** ${comparison.improvements.join(', ')}`);
591
- if (comparison.regressions.length > 0) lines.push(`- **New gaps:** ${comparison.regressions.join(', ')}`);
592
- }
593
-
594
- lines.push('');
595
- lines.push(`---`);
596
- lines.push(`*Generated by nerviq v${version}*`);
597
- return lines.join('\n');
598
- }
599
-
600
- function readOutcomeIndex(dir) {
601
- const indexPath = resolveProjectStateReadPath(dir, 'outcomes', 'index.json');
602
- if (!fs.existsSync(indexPath)) return [];
603
- try {
604
- const entries = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
605
- return Array.isArray(entries) ? entries : [];
606
- } catch {
607
- return [];
608
- }
609
- }
610
-
611
- function updateOutcomeIndex(outcomesDir, record) {
612
- const indexPath = path.join(outcomesDir, 'index.json');
613
- let entries = [];
614
-
615
- if (fs.existsSync(indexPath)) {
616
- try {
617
- entries = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
618
- if (!Array.isArray(entries)) entries = [];
619
- } catch {
620
- entries = [];
621
- }
622
- }
623
-
624
- entries.push(record);
625
- const MAX_INDEX_ENTRIES = 500;
626
- if (entries.length > MAX_INDEX_ENTRIES) {
627
- entries = entries.slice(entries.length - MAX_INDEX_ENTRIES);
628
- }
629
- fs.writeFileSync(indexPath, JSON.stringify(entries, null, 2), 'utf8');
630
- }
631
-
632
- function normalizeOutcomeStatus(value) {
633
- const normalized = `${value || ''}`.trim().toLowerCase();
634
- if (!['accepted', 'rejected', 'deferred'].includes(normalized)) {
635
- throw new Error('feedback status must be one of: accepted, rejected, deferred');
636
- }
637
- return normalized;
638
- }
639
-
640
- function normalizeOutcomeEffect(value) {
641
- const normalized = `${value || ''}`.trim().toLowerCase();
642
- if (!['positive', 'neutral', 'negative'].includes(normalized)) {
643
- throw new Error('feedback effect must be one of: positive, neutral, negative');
644
- }
645
- return normalized;
646
- }
647
-
648
- function recordRecommendationOutcome(dir, payload) {
649
- const key = `${payload.key || ''}`.trim();
650
- if (!key) {
651
- throw new Error('feedback requires a recommendation key');
652
- }
653
-
654
- const status = normalizeOutcomeStatus(payload.status);
655
- const effect = normalizeOutcomeEffect(payload.effect || 'neutral');
656
- const scoreDelta = Number.isFinite(payload.scoreDelta) ? payload.scoreDelta : (
657
- payload.scoreDelta === null || payload.scoreDelta === undefined || payload.scoreDelta === ''
658
- ? null
659
- : Number(payload.scoreDelta)
660
- );
661
-
662
- if (scoreDelta !== null && !Number.isFinite(scoreDelta)) {
663
- throw new Error('feedback scoreDelta must be a number when provided');
664
- }
665
-
666
- const id = timestampId();
667
- const { outcomesDir } = ensureArtifactDirs(dir);
668
- const filePath = path.join(outcomesDir, `${id}.json`);
669
- const record = {
670
- id,
671
- createdAt: new Date().toISOString(),
672
- key,
673
- status,
674
- effect,
675
- source: `${payload.source || 'manual-cli'}`.trim() || 'manual-cli',
676
- notes: `${payload.notes || ''}`.trim(),
677
- scoreDelta,
678
- };
679
-
680
- writeJson(filePath, record);
681
- updateOutcomeIndex(outcomesDir, {
682
- ...record,
683
- relativePath: path.relative(dir, filePath),
684
- });
685
-
686
- return {
687
- id,
688
- filePath,
689
- relativePath: path.relative(dir, filePath),
690
- record,
691
- };
692
- }
693
-
694
- function summarizeOutcomeEntries(entries = []) {
695
- const byKey = {};
696
-
697
- for (const entry of entries) {
698
- if (!entry || !entry.key) continue;
699
- const bucket = byKey[entry.key] || {
700
- key: entry.key,
701
- total: 0,
702
- accepted: 0,
703
- rejected: 0,
704
- deferred: 0,
705
- positive: 0,
706
- neutral: 0,
707
- negative: 0,
708
- scoreDeltaTotal: 0,
709
- scoreDeltaCount: 0,
710
- latestAt: null,
711
- };
712
-
713
- bucket.total += 1;
714
- if (bucket[entry.status] !== undefined) bucket[entry.status] += 1;
715
- if (bucket[entry.effect] !== undefined) bucket[entry.effect] += 1;
716
- if (Number.isFinite(entry.scoreDelta)) {
717
- bucket.scoreDeltaTotal += entry.scoreDelta;
718
- bucket.scoreDeltaCount += 1;
719
- }
720
- if (!bucket.latestAt || new Date(entry.createdAt) > new Date(bucket.latestAt)) {
721
- bucket.latestAt = entry.createdAt;
722
- }
723
-
724
- byKey[entry.key] = bucket;
725
- }
726
-
727
- for (const bucket of Object.values(byKey)) {
728
- bucket.avgScoreDelta = bucket.scoreDeltaCount > 0
729
- ? Number((bucket.scoreDeltaTotal / bucket.scoreDeltaCount).toFixed(2))
730
- : null;
731
- bucket.evidenceClass = bucket.total > 0 ? 'measured' : 'estimated';
732
- }
733
-
734
- return {
735
- totalEntries: entries.length,
736
- byKey,
737
- keys: Object.keys(byKey).sort(),
738
- };
739
- }
740
-
741
- function getRecommendationOutcomeSummary(dir) {
742
- return summarizeOutcomeEntries(readOutcomeIndex(dir));
743
- }
744
-
745
- function getRecommendationAdjustment(summaryByKey, key) {
746
- const bucket = summaryByKey && summaryByKey[key];
747
- if (!bucket) return 0;
748
-
749
- let adjustment = 0;
750
- adjustment += bucket.accepted * 2;
751
- adjustment += bucket.positive * 3;
752
- adjustment -= bucket.rejected * 3;
753
- adjustment -= bucket.negative * 4;
754
-
755
- if (Number.isFinite(bucket.avgScoreDelta)) {
756
- if (bucket.avgScoreDelta > 0) adjustment += Math.min(4, Math.round(bucket.avgScoreDelta / 4));
757
- if (bucket.avgScoreDelta < 0) adjustment -= Math.min(4, Math.round(Math.abs(bucket.avgScoreDelta) / 4));
758
- }
759
-
760
- if (adjustment > 8) return 8;
761
- if (adjustment < -8) return -8;
762
- return adjustment;
763
- }
764
-
765
- function formatRecommendationOutcomeSummary(dir) {
766
- const summary = getRecommendationOutcomeSummary(dir);
767
- if (summary.totalEntries === 0) {
768
- return 'No recommendation outcomes recorded yet. Use `npx nerviq feedback --key permissionDeny --status accepted --effect positive` after a real run.';
769
- }
770
-
771
- const lines = [
772
- 'Recommendation outcome summary:',
773
- '',
774
- ];
775
-
776
- for (const key of summary.keys) {
777
- const bucket = summary.byKey[key];
778
- const avg = Number.isFinite(bucket.avgScoreDelta) ? ` | avg score delta ${bucket.avgScoreDelta >= 0 ? '+' : ''}${bucket.avgScoreDelta}` : '';
779
- const adjustment = getRecommendationAdjustment(summary.byKey, key);
780
- lines.push(` ${key}: total ${bucket.total} | accepted ${bucket.accepted} | rejected ${bucket.rejected} | deferred ${bucket.deferred} | positive ${bucket.positive} | negative ${bucket.negative}${avg} | ranking ${adjustment >= 0 ? '+' : ''}${adjustment}`);
781
- }
782
-
783
- return lines.join('\n');
784
- }
785
-
786
- /**
787
- * Load the full payload of a snapshot by its index entry.
788
- * @param {string} dir - Project root directory.
789
- * @param {Object} indexEntry - Snapshot index entry with relativePath.
790
- * @returns {Object|null} Full snapshot envelope, or null if unreadable.
791
- */
792
- function loadSnapshotPayload(dir, indexEntry) {
793
- if (!indexEntry || !indexEntry.relativePath) return null;
794
- const filePath = path.join(dir, indexEntry.relativePath);
795
- try {
796
- const envelope = JSON.parse(fs.readFileSync(filePath, 'utf8'));
797
- return envelope.payload || null;
798
- } catch {
799
- return null;
800
- }
801
- }
802
-
803
- /**
804
- * Analyze check health by comparing the two most recent audit snapshots.
805
- * Detects checks that regressed (passed → failed), improved (failed → passed),
806
- * and flags sudden drops that may indicate platform format changes.
807
- * When more than 2 snapshots exist, also computes per-check pass rates.
808
- *
809
- * @param {string} dir - Project root directory.
810
- * @returns {Object|null} Health report, or null if fewer than 2 audit snapshots exist.
811
- */
812
- function checkHealth(dir) {
813
- const history = getHistory(dir, 20);
814
- if (history.length < 2) return null;
815
-
816
- const currentPayload = loadSnapshotPayload(dir, history[0]);
817
- const previousPayload = loadSnapshotPayload(dir, history[1]);
818
- if (!currentPayload || !previousPayload) return null;
819
-
820
- const currentResults = currentPayload.results || [];
821
- const previousResults = previousPayload.results || [];
822
- const diff = collectCheckDiff(previousResults, currentResults);
823
- const regressions = diff.regressions;
824
- const improvements = diff.improvements;
825
- const newChecks = diff.newChecks;
826
- const removedChecks = diff.removedChecks;
827
-
828
- // Detect potential platform format changes:
829
- // If 3+ checks in the same category regressed, flag it
830
- const regressionsByCategory = {};
831
- for (const r of regressions) {
832
- if (!regressionsByCategory[r.category]) regressionsByCategory[r.category] = [];
833
- regressionsByCategory[r.category].push(r);
834
- }
835
- const platformAlerts = [];
836
- for (const [cat, items] of Object.entries(regressionsByCategory)) {
837
- if (items.length >= 3) {
838
- platformAlerts.push({
839
- category: cat,
840
- regressionCount: items.length,
841
- message: `${items.length} checks in '${cat}' regressed — possible platform format change`,
842
- checks: items.map(i => i.key),
843
- });
844
- }
845
- }
846
-
847
- // Per-check pass rates across all snapshots
848
- const passRates = computePassRates(dir, history);
849
-
850
- return {
851
- currentDate: history[0].createdAt,
852
- previousDate: history[1].createdAt,
853
- snapshotsAnalyzed: history.length,
854
- scoreDelta: (currentPayload.score || 0) - (previousPayload.score || 0),
855
- regressions,
856
- improvements,
857
- newChecks,
858
- removedChecks,
859
- platformAlerts,
860
- passRates,
861
- summary: {
862
- regressionsCount: regressions.length,
863
- improvementsCount: improvements.length,
864
- newChecksCount: newChecks.length,
865
- removedChecksCount: removedChecks.length,
866
- alertsCount: platformAlerts.length,
867
- },
868
- };
869
- }
870
-
871
- /**
872
- * Compute per-check pass rates across all snapshots.
873
- * Returns { declining, consistentlyFailing, consistentlyPassing, overallHealth }.
874
- */
875
- function computePassRates(dir, history) {
876
- // key → { passes, total, recentResults: [bool...] (newest first) }
877
- const stats = {};
878
- for (const entry of history) {
879
- const payload = loadSnapshotPayload(dir, entry);
880
- if (!payload || !payload.results) continue;
881
- for (const r of payload.results) {
882
- if (!r.key || r.passed === null || r.passed === undefined) continue;
883
- if (!stats[r.key]) stats[r.key] = { name: r.name, passes: 0, total: 0, recentResults: [] };
884
- stats[r.key].total++;
885
- if (r.passed) stats[r.key].passes++;
886
- stats[r.key].recentResults.push(!!r.passed);
887
- }
888
- }
889
-
890
- const declining = [];
891
- const consistentlyFailing = [];
892
- let consistentlyPassingCount = 0;
893
- let totalChecks = 0;
894
- let totalPasses = 0;
895
- let totalAppearances = 0;
896
-
897
- for (const [key, s] of Object.entries(stats)) {
898
- const rate = s.total > 0 ? s.passes / s.total : 0;
899
- totalChecks++;
900
- totalPasses += s.passes;
901
- totalAppearances += s.total;
902
-
903
- if (s.total >= 2 && rate === 0) {
904
- consistentlyFailing.push({ key, name: s.name, runs: s.total });
905
- } else if (rate === 1) {
906
- consistentlyPassingCount++;
907
- } else if (s.total >= 2) {
908
- // Check if declining: earlier results passed, recent ones failed
909
- const half = Math.ceil(s.recentResults.length / 2);
910
- const recentHalf = s.recentResults.slice(0, half);
911
- const olderHalf = s.recentResults.slice(half);
912
- const recentRate = recentHalf.filter(Boolean).length / recentHalf.length;
913
- const olderRate = olderHalf.length > 0 ? olderHalf.filter(Boolean).length / olderHalf.length : recentRate;
914
- if (olderRate > recentRate) {
915
- const failStreak = s.recentResults.findIndex(v => v === true);
916
- declining.push({
917
- key, name: s.name,
918
- oldRate: Math.round(olderRate * 100),
919
- newRate: Math.round(recentRate * 100),
920
- failingRuns: failStreak === -1 ? s.recentResults.length : failStreak,
921
- });
922
- }
923
- }
924
- }
925
-
926
- const overallHealth = totalAppearances > 0 ? Math.round((totalPasses / totalAppearances) * 100) : 100;
927
-
928
- return { declining, consistentlyFailing, consistentlyPassingCount, overallHealth };
929
- }
930
-
931
- /**
932
- * Format check-health report for CLI display.
933
- */
934
- function formatCheckHealth(healthReport) {
935
- if (!healthReport) return 'Need at least 2 audit snapshots. Run `nerviq audit --snapshot` twice.';
936
-
937
- const lines = [];
938
- const { scoreDelta, regressions, improvements, platformAlerts, newChecks, passRates } = healthReport;
939
- const sign = scoreDelta >= 0 ? '+' : '';
940
-
941
- lines.push(` Check Health Report`);
942
- lines.push(` ═══════════════════════════════════════`);
943
- lines.push(` Snapshots analyzed: ${healthReport.snapshotsAnalyzed}`);
944
- lines.push(` Period: ${healthReport.previousDate?.split('T')[0]} → ${healthReport.currentDate?.split('T')[0]}`);
945
- lines.push(` Score delta: ${sign}${scoreDelta}`);
946
- lines.push('');
947
-
948
- if (platformAlerts.length > 0) {
949
- lines.push(` ⚠️ PLATFORM ALERTS (${platformAlerts.length})`);
950
- for (const alert of platformAlerts) {
951
- lines.push(` ${alert.message}`);
952
- lines.push(` Checks: ${alert.checks.join(', ')}`);
953
- }
954
- lines.push('');
955
- }
956
-
957
- if (passRates && passRates.declining.length > 0) {
958
- lines.push(` Checks with declining pass rate:`);
959
- for (const d of passRates.declining) {
960
- const detail = d.failingRuns > 0 ? `(failing in last ${d.failingRuns} runs)` : '';
961
- lines.push(` ⚠ ${d.key.padEnd(22)} ${d.oldRate}% → ${d.newRate}% ${detail}`);
962
- }
963
- lines.push('');
964
- }
965
-
966
- if (passRates && passRates.consistentlyFailing.length > 0) {
967
- lines.push(` Consistently failing (0% pass rate):`);
968
- for (const f of passRates.consistentlyFailing) {
969
- lines.push(` ✗ ${f.key.padEnd(22)} 0/${f.runs} runs`);
970
- }
971
- lines.push('');
972
- }
973
-
974
- if (regressions.length > 0) {
975
- lines.push(` 🔴 Regressions (${regressions.length} checks now failing)`);
976
- for (const r of regressions) {
977
- lines.push(` ${r.name} [${r.impact}]`);
978
- }
979
- lines.push('');
980
- }
981
-
982
- if (improvements.length > 0) {
983
- lines.push(` ✅ Improvements (${improvements.length} checks now passing)`);
984
- for (const r of improvements) {
985
- lines.push(` ${r.name}`);
986
- }
987
- lines.push('');
988
- }
989
-
990
- if (newChecks.length > 0) {
991
- lines.push(` 🆕 New checks (${newChecks.length})`);
992
- lines.push('');
993
- }
994
-
995
- if (passRates && passRates.consistentlyPassingCount > 0) {
996
- lines.push(` Consistently passing (100%):`);
997
- lines.push(` ✓ ${passRates.consistentlyPassingCount} checks at 100% pass rate`);
998
- lines.push('');
999
- }
1000
-
1001
- if (regressions.length === 0 && platformAlerts.length === 0) {
1002
- lines.push(` ✅ All checks stable. No regressions detected.`);
1003
- lines.push('');
1004
- }
1005
-
1006
- if (passRates) {
1007
- lines.push(` Overall health: ${passRates.overallHealth}%`);
1008
- }
1009
-
1010
- return lines.join('\n');
1011
- }
1012
-
1013
- module.exports = {
1014
- getUserId,
1015
- ensureArtifactDirs,
1016
- writeActivityArtifact,
1017
- writeRollbackArtifact,
1018
- writeSnapshotArtifact,
1019
- normalizeSnapshotTags,
1020
- formatSnapshotTags,
1021
- normalizeSnapshotMilestone,
1022
- formatSnapshotMilestone,
1023
- SNAPSHOT_MILESTONES,
1024
- readSnapshotIndex,
1025
- getHistory,
1026
- compareLatest,
1027
- formatSnapshotBootstrap,
1028
- formatHistory,
1029
- exportTrendReport,
1030
- readOutcomeIndex,
1031
- recordRecommendationOutcome,
1032
- summarizeOutcomeEntries,
1033
- getRecommendationOutcomeSummary,
1034
- getRecommendationAdjustment,
1035
- formatRecommendationOutcomeSummary,
1036
- checkHealth,
1037
- formatCheckHealth,
1038
- loadSnapshotPayload,
1039
- };
1
+ const fs = require('fs');
2
+ const os = require('os');
3
+ const path = require('path');
4
+ const { version } = require('../package.json');
5
+ const {
6
+ resolveProjectStateReadPath,
7
+ ensureProjectStateDir,
8
+ } = require('./state-paths');
9
+
10
+ /**
11
+ * Generate a machine-level user identity for audit tracking.
12
+ * Format: hostname:username — no PII, just machine identity.
13
+ * @returns {string}
14
+ */
15
+ function getUserId() {
16
+ try {
17
+ const hostname = os.hostname();
18
+ const username = os.userInfo().username;
19
+ return `${hostname}:${username}`;
20
+ } catch {
21
+ return 'unknown:unknown';
22
+ }
23
+ }
24
+
25
+ let _lastTimestamp = '';
26
+ let _counter = 0;
27
+
28
+ const SNAPSHOT_MILESTONES = ['baseline', 'post-fix', 'pre-upgrade', 'release'];
29
+
30
+ function timestampId() {
31
+ const ts = new Date().toISOString().replace(/[:.]/g, '-');
32
+ if (ts === _lastTimestamp) {
33
+ _counter++;
34
+ return `${ts}-${_counter}`;
35
+ }
36
+ _lastTimestamp = ts;
37
+ _counter = 0;
38
+ return ts;
39
+ }
40
+
41
+ function ensureArtifactDirs(dir) {
42
+ const root = ensureProjectStateDir(dir);
43
+ const activityDir = ensureProjectStateDir(dir, 'activity');
44
+ const rollbackDir = ensureProjectStateDir(dir, 'rollbacks');
45
+ const snapshotDir = ensureProjectStateDir(dir, 'snapshots');
46
+ const outcomesDir = ensureProjectStateDir(dir, 'outcomes');
47
+ return { root, activityDir, rollbackDir, snapshotDir, outcomesDir };
48
+ }
49
+
50
+ function writeJson(filePath, payload) {
51
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
52
+ fs.writeFileSync(filePath, JSON.stringify(payload, null, 2), 'utf8');
53
+ }
54
+
55
+ function writeActivityArtifact(dir, type, payload) {
56
+ const id = timestampId();
57
+ const { activityDir } = ensureArtifactDirs(dir);
58
+ const filePath = path.join(activityDir, `${id}-${type}.json`);
59
+ writeJson(filePath, {
60
+ id,
61
+ type,
62
+ createdAt: new Date().toISOString(),
63
+ userId: getUserId(),
64
+ ...payload,
65
+ });
66
+ return {
67
+ id,
68
+ filePath,
69
+ relativePath: path.relative(dir, filePath),
70
+ };
71
+ }
72
+
73
+ function writeRollbackArtifact(dir, payload) {
74
+ const id = timestampId();
75
+ const { rollbackDir } = ensureArtifactDirs(dir);
76
+ const filePath = path.join(rollbackDir, `${id}.json`);
77
+ writeJson(filePath, {
78
+ id,
79
+ createdAt: new Date().toISOString(),
80
+ userId: getUserId(),
81
+ rollbackType: 'delete-created-files',
82
+ ...payload,
83
+ });
84
+ return {
85
+ id,
86
+ filePath,
87
+ relativePath: path.relative(dir, filePath),
88
+ };
89
+ }
90
+
91
+ function summarizeSnapshot(snapshotKind, payload) {
92
+ if (snapshotKind === 'audit') {
93
+ return {
94
+ score: payload.score,
95
+ organicScore: payload.organicScore,
96
+ passed: payload.passed,
97
+ failed: payload.failed,
98
+ checkCount: payload.checkCount,
99
+ suggestedNextCommand: payload.suggestedNextCommand,
100
+ topActionKeys: Array.isArray(payload.topNextActions)
101
+ ? payload.topNextActions.slice(0, 3).map(item => item.key)
102
+ : [],
103
+ };
104
+ }
105
+
106
+ if (snapshotKind === 'augment' || snapshotKind === 'suggest-only') {
107
+ return {
108
+ score: payload.projectSummary?.score,
109
+ organicScore: payload.projectSummary?.organicScore,
110
+ maturity: payload.projectSummary?.maturity,
111
+ domains: payload.projectSummary?.domains || [],
112
+ topActionKeys: Array.isArray(payload.topNextActions)
113
+ ? payload.topNextActions.slice(0, 3).map(item => item.key)
114
+ : [],
115
+ };
116
+ }
117
+
118
+ if (snapshotKind === 'benchmark') {
119
+ return {
120
+ beforeScore: payload.before?.score,
121
+ afterScore: payload.after?.score,
122
+ scoreDelta: payload.delta?.score,
123
+ organicDelta: payload.delta?.organicScore,
124
+ decisionGuidance: payload.executiveSummary?.decisionGuidance || null,
125
+ };
126
+ }
127
+
128
+ if (snapshotKind === 'governance') {
129
+ return {
130
+ permissionProfiles: Array.isArray(payload.permissionProfiles) ? payload.permissionProfiles.length : 0,
131
+ hooks: Array.isArray(payload.hookRegistry) ? payload.hookRegistry.length : 0,
132
+ policyPacks: Array.isArray(payload.policyPacks) ? payload.policyPacks.length : 0,
133
+ domainPacks: Array.isArray(payload.domainPacks) ? payload.domainPacks.length : 0,
134
+ mcpPacks: Array.isArray(payload.mcpPacks) ? payload.mcpPacks.length : 0,
135
+ };
136
+ }
137
+
138
+ if (snapshotKind === 'behavioral-drift') {
139
+ return {
140
+ score: payload.score,
141
+ sourceFiles: payload.repoSummary?.sourceFiles ?? 0,
142
+ findingCount: Array.isArray(payload.findings) ? payload.findings.length : 0,
143
+ driftLabels: Array.isArray(payload.driftLabels) ? payload.driftLabels.slice(0, 5) : [],
144
+ utilityShare: payload.structuralSignals?.utilityBalance?.utilityShare ?? null,
145
+ layerBreaks: payload.structuralSignals?.layering?.count ?? 0,
146
+ };
147
+ }
148
+
149
+ return {};
150
+ }
151
+
152
+ function normalizeSnapshotTags(input) {
153
+ const values = Array.isArray(input) ? input : (input ? [input] : []);
154
+ const seen = new Set();
155
+ const tags = [];
156
+
157
+ for (const value of values) {
158
+ const parts = `${value || ''}`
159
+ .split(',')
160
+ .map((item) => item.replace(/\s+/g, ' ').trim())
161
+ .filter(Boolean);
162
+
163
+ for (const part of parts) {
164
+ const key = part.toLowerCase();
165
+ if (seen.has(key)) continue;
166
+ seen.add(key);
167
+ tags.push(part.slice(0, 48));
168
+ if (tags.length >= 8) {
169
+ return tags;
170
+ }
171
+ }
172
+ }
173
+
174
+ return tags;
175
+ }
176
+
177
+ function formatSnapshotTags(tags = []) {
178
+ const normalized = normalizeSnapshotTags(tags);
179
+ if (normalized.length === 0) return '';
180
+ return ` [${normalized.join(', ')}]`;
181
+ }
182
+
183
+ function normalizeSnapshotMilestone(value) {
184
+ if (value === null || value === undefined || value === '') return null;
185
+ const normalized = `${value}`.trim().toLowerCase();
186
+ if (!SNAPSHOT_MILESTONES.includes(normalized)) {
187
+ throw new Error(`snapshot milestone must be one of: ${SNAPSHOT_MILESTONES.join(', ')}`);
188
+ }
189
+ return normalized;
190
+ }
191
+
192
+ function formatSnapshotMilestone(value) {
193
+ const milestone = normalizeSnapshotMilestone(value);
194
+ if (!milestone) return '';
195
+ return ` (${milestone})`;
196
+ }
197
+
198
+ function updateSnapshotIndex(snapshotDir, record) {
199
+ const indexPath = path.join(snapshotDir, 'index.json');
200
+ let entries = [];
201
+
202
+ if (fs.existsSync(indexPath)) {
203
+ try {
204
+ entries = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
205
+ if (!Array.isArray(entries)) {
206
+ entries = [];
207
+ }
208
+ } catch {
209
+ entries = [];
210
+ }
211
+ }
212
+
213
+ entries.push(record);
214
+ // Prune to keep last 200 entries
215
+ const MAX_INDEX_ENTRIES = 200;
216
+ if (entries.length > MAX_INDEX_ENTRIES) {
217
+ entries = entries.slice(entries.length - MAX_INDEX_ENTRIES);
218
+ }
219
+ fs.writeFileSync(indexPath, JSON.stringify(entries, null, 2), 'utf8');
220
+ }
221
+
222
+ /**
223
+ * Write a normalized snapshot artifact to .nerviq/snapshots/ and update the index.
224
+ * @param {string} dir - Project root directory.
225
+ * @param {string} snapshotKind - Snapshot type ('audit', 'benchmark', 'governance', 'augment', 'suggest-only').
226
+ * @param {Object} payload - Full result payload to persist.
227
+ * @param {Object} [meta={}] - Optional metadata fields merged into the envelope.
228
+ * @returns {Object} Artifact record with id, filePath, relativePath, indexPath, and summary.
229
+ */
230
+ function writeSnapshotArtifact(dir, snapshotKind, payload, meta = {}) {
231
+ const id = timestampId();
232
+ const { snapshotDir } = ensureArtifactDirs(dir);
233
+ const filePath = path.join(snapshotDir, `${id}-${snapshotKind}.json`);
234
+ const summary = summarizeSnapshot(snapshotKind, payload);
235
+ const metaTags = normalizeSnapshotTags([
236
+ ...(Array.isArray(meta.tags) ? meta.tags : (meta.tags ? [meta.tags] : [])),
237
+ ...(meta.tag ? [meta.tag] : []),
238
+ ]);
239
+ const milestone = normalizeSnapshotMilestone(meta.milestone);
240
+ const { tags: _ignoredTags, tag: _ignoredTag, ...restMeta } = meta;
241
+ const envelope = {
242
+ schemaVersion: 1,
243
+ artifactType: 'snapshot',
244
+ snapshotKind,
245
+ id,
246
+ createdAt: new Date().toISOString(),
247
+ userId: getUserId(),
248
+ generatedBy: `nerviq@${version}`,
249
+ directory: dir,
250
+ summary,
251
+ tags: metaTags,
252
+ milestone,
253
+ ...restMeta,
254
+ payload,
255
+ };
256
+
257
+ writeJson(filePath, envelope);
258
+
259
+ const record = {
260
+ id,
261
+ snapshotKind,
262
+ createdAt: envelope.createdAt,
263
+ relativePath: path.relative(dir, filePath),
264
+ tags: metaTags,
265
+ milestone,
266
+ summary,
267
+ };
268
+ updateSnapshotIndex(snapshotDir, record);
269
+
270
+ return {
271
+ id,
272
+ filePath,
273
+ relativePath: path.relative(dir, filePath),
274
+ indexPath: path.relative(dir, path.join(snapshotDir, 'index.json')),
275
+ summary,
276
+ };
277
+ }
278
+
279
+ function readSnapshotIndex(dir) {
280
+ const indexPath = resolveProjectStateReadPath(dir, 'snapshots', 'index.json');
281
+ if (!fs.existsSync(indexPath)) return [];
282
+ try {
283
+ const entries = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
284
+ return Array.isArray(entries) ? entries : [];
285
+ } catch {
286
+ return [];
287
+ }
288
+ }
289
+
290
+ /**
291
+ * Get the audit score history from saved snapshots, most recent first.
292
+ * @param {string} dir - Project root directory.
293
+ * @param {number} [limit=20] - Maximum number of entries to return.
294
+ * @returns {Object[]} Array of snapshot index entries for audit snapshots.
295
+ */
296
+ function getHistory(dir, limit = 20) {
297
+ const entries = readSnapshotIndex(dir);
298
+ return entries
299
+ .filter(e => e.snapshotKind === 'audit')
300
+ .sort((a, b) => {
301
+ const dateDiff = new Date(b.createdAt) - new Date(a.createdAt);
302
+ if (dateDiff !== 0) return dateDiff;
303
+ return (b.id || '').localeCompare(a.id || '');
304
+ })
305
+ .slice(0, limit);
306
+ }
307
+
308
+ function buildCheckDiffDetail(previousResult, currentResult) {
309
+ const source = currentResult || previousResult || {};
310
+ const previousState = previousResult ? previousResult.passed : undefined;
311
+ const currentState = currentResult ? currentResult.passed : undefined;
312
+ return {
313
+ key: source.key,
314
+ name: source.name || source.key,
315
+ impact: source.impact || null,
316
+ category: source.category || null,
317
+ previousState,
318
+ currentState,
319
+ };
320
+ }
321
+
322
+ function collectCheckDiff(previousResults = [], currentResults = []) {
323
+ const prevMap = new Map();
324
+ const currMap = new Map();
325
+
326
+ for (const result of previousResults) {
327
+ if (result && result.key) prevMap.set(result.key, result);
328
+ }
329
+ for (const result of currentResults) {
330
+ if (result && result.key) currMap.set(result.key, result);
331
+ }
332
+
333
+ const regressions = [];
334
+ const improvements = [];
335
+ const newlyApplicable = [];
336
+ const noLongerApplicable = [];
337
+ const newChecks = [];
338
+ const removedChecks = [];
339
+
340
+ const allKeys = [...new Set([...prevMap.keys(), ...currMap.keys()])].sort();
341
+ for (const key of allKeys) {
342
+ const previousResult = prevMap.get(key);
343
+ const currentResult = currMap.get(key);
344
+ const previousState = previousResult ? previousResult.passed : undefined;
345
+ const currentState = currentResult ? currentResult.passed : undefined;
346
+ const detail = buildCheckDiffDetail(previousResult, currentResult);
347
+
348
+ if (!previousResult) {
349
+ if (currentState !== undefined) {
350
+ newChecks.push(detail);
351
+ }
352
+ continue;
353
+ }
354
+
355
+ if (!currentResult) {
356
+ removedChecks.push(detail);
357
+ continue;
358
+ }
359
+
360
+ if (previousState === true && currentState === false) {
361
+ regressions.push(detail);
362
+ } else if (previousState === false && currentState === true) {
363
+ improvements.push(detail);
364
+ } else if ((previousState === null || previousState === undefined) && (currentState === true || currentState === false)) {
365
+ newlyApplicable.push(detail);
366
+ } else if ((currentState === null || currentState === undefined) && (previousState === true || previousState === false)) {
367
+ noLongerApplicable.push(detail);
368
+ }
369
+ }
370
+
371
+ return {
372
+ regressions,
373
+ improvements,
374
+ newlyApplicable,
375
+ noLongerApplicable,
376
+ newChecks,
377
+ removedChecks,
378
+ };
379
+ }
380
+
381
+ /**
382
+ * Compare the two most recent audit snapshots and return the delta.
383
+ * @param {string} dir - Project root directory.
384
+ * @returns {Object|null} Comparison with current/previous scores, delta, regressions, improvements, and trend. Null if fewer than 2 snapshots.
385
+ */
386
+ function compareLatest(dir) {
387
+ const audits = getHistory(dir, 2);
388
+ if (audits.length < 2) return null;
389
+
390
+ const current = audits[0];
391
+ const previous = audits[1];
392
+ const currentPayload = loadSnapshotPayload(dir, current);
393
+ const previousPayload = loadSnapshotPayload(dir, previous);
394
+
395
+ const delta = {
396
+ score: (current.summary?.score || 0) - (previous.summary?.score || 0),
397
+ organic: (current.summary?.organicScore || 0) - (previous.summary?.organicScore || 0),
398
+ passed: (current.summary?.passed || 0) - (previous.summary?.passed || 0),
399
+ };
400
+
401
+ let regressionDetails = [];
402
+ let improvementDetails = [];
403
+ let newlyApplicableDetails = [];
404
+ let noLongerApplicableDetails = [];
405
+ let newChecks = [];
406
+ let removedChecks = [];
407
+ let regressions = [];
408
+ let improvements = [];
409
+ let detailedDiffAvailable = false;
410
+
411
+ if (currentPayload && previousPayload && Array.isArray(currentPayload.results) && Array.isArray(previousPayload.results)) {
412
+ const diff = collectCheckDiff(previousPayload.results, currentPayload.results);
413
+ regressionDetails = diff.regressions;
414
+ improvementDetails = diff.improvements;
415
+ newlyApplicableDetails = diff.newlyApplicable;
416
+ noLongerApplicableDetails = diff.noLongerApplicable;
417
+ newChecks = diff.newChecks;
418
+ removedChecks = diff.removedChecks;
419
+ regressions = regressionDetails.map((item) => item.key);
420
+ improvements = improvementDetails.map((item) => item.key);
421
+ detailedDiffAvailable = true;
422
+ } else {
423
+ const prevKeys = new Set(previous.summary?.topActionKeys || []);
424
+ const currKeys = new Set(current.summary?.topActionKeys || []);
425
+ for (const key of currKeys) {
426
+ if (!prevKeys.has(key)) regressions.push(key);
427
+ }
428
+ for (const key of prevKeys) {
429
+ if (!currKeys.has(key)) improvements.push(key);
430
+ }
431
+ }
432
+
433
+ return {
434
+ scoreType: 'audit-snapshot-score',
435
+ current: {
436
+ date: current.createdAt,
437
+ score: current.summary?.score,
438
+ passed: current.summary?.passed,
439
+ tags: current.tags || [],
440
+ milestone: current.milestone || null,
441
+ scoreType: 'audit-snapshot-score',
442
+ },
443
+ previous: {
444
+ date: previous.createdAt,
445
+ score: previous.summary?.score,
446
+ passed: previous.summary?.passed,
447
+ tags: previous.tags || [],
448
+ milestone: previous.milestone || null,
449
+ scoreType: 'audit-snapshot-score',
450
+ },
451
+ delta,
452
+ regressions,
453
+ improvements,
454
+ regressionDetails,
455
+ improvementDetails,
456
+ newlyApplicableDetails,
457
+ noLongerApplicableDetails,
458
+ newChecks,
459
+ removedChecks,
460
+ detailedDiffAvailable,
461
+ trend: delta.score > 0 ? 'improving' : delta.score < 0 ? 'regressing' : 'stable',
462
+ };
463
+ }
464
+
465
+ function formatSnapshotBootstrap(dir, goal = 'history') {
466
+ const snapshotCount = getHistory(dir, 50).length;
467
+ const lines = [];
468
+ const snapshotLabel = snapshotCount === 1
469
+ ? '1 saved audit snapshot'
470
+ : `${snapshotCount} saved audit snapshots`;
471
+
472
+ if (goal === 'compare') {
473
+ lines.push(snapshotCount === 0
474
+ ? 'Compare needs 2 audit snapshots.'
475
+ : 'Compare needs one more audit snapshot.');
476
+ } else if (goal === 'trend') {
477
+ lines.push(snapshotCount === 0
478
+ ? 'Trend needs 2 audit snapshots to start.'
479
+ : 'Trend needs one more audit snapshot to become meaningful.');
480
+ } else {
481
+ lines.push(snapshotCount === 0
482
+ ? 'No audit snapshots found yet.'
483
+ : 'History is initialized, but compare/trend still need one more snapshot.');
484
+ }
485
+
486
+ lines.push(` Current state: ${snapshotLabel}.`);
487
+
488
+ if (snapshotCount === 0) {
489
+ lines.push(' Bootstrap it with:');
490
+ lines.push(' 1. Run `nerviq audit --snapshot --milestone baseline --tag "baseline"` to save the baseline.');
491
+ lines.push(' 2. Make a meaningful repo change (`nerviq setup --auto` or `nerviq fix --all-critical --auto`).');
492
+ lines.push(' 3. Run `nerviq audit --snapshot --milestone post-fix --tag "after-change"` to capture the next state.');
493
+ } else {
494
+ lines.push(' Next:');
495
+ lines.push(' 1. Make a meaningful repo change (`nerviq setup --auto` or `nerviq fix --all-critical --auto`).');
496
+ lines.push(' 2. Run `nerviq audit --snapshot --milestone post-fix --tag "after-change"` again.');
497
+ }
498
+
499
+ if (goal === 'compare') {
500
+ lines.push(' Then rerun `nerviq compare`.');
501
+ } else if (goal === 'trend') {
502
+ lines.push(' Then rerun `nerviq trend`.');
503
+ } else {
504
+ lines.push(' Then rerun `nerviq history`, `nerviq compare`, or `nerviq trend`.');
505
+ }
506
+
507
+ return lines.join('\n');
508
+ }
509
+
510
+ function formatHistory(dir) {
511
+ const history = getHistory(dir, 10);
512
+ if (history.length === 0) return formatSnapshotBootstrap(dir, 'history');
513
+
514
+ const lines = [
515
+ 'Audit snapshot history (most recent first):',
516
+ ' Score type: saved audit snapshot scores only (not live audits or benchmark projections).',
517
+ '',
518
+ ];
519
+ for (const entry of history) {
520
+ const dateStr = entry.createdAt || 'unknown';
521
+ const date = dateStr.split('T')[0] || 'unknown';
522
+ const time = dateStr.includes('T') ? dateStr.split('T')[1]?.substring(0, 5) || '' : '';
523
+ const dateDisplay = time ? `${date} ${time}` : date;
524
+ const score = entry.summary?.score ?? '?';
525
+ const passed = entry.summary?.passed ?? '?';
526
+ const total = entry.summary?.checkCount ?? '?';
527
+ lines.push(` ${dateDisplay} snapshot${formatSnapshotMilestone(entry.milestone)}${formatSnapshotTags(entry.tags)} ${score}/100 (${passed}/${total} checks passing)`);
528
+ }
529
+
530
+ const comparison = compareLatest(dir);
531
+ if (comparison) {
532
+ lines.push('');
533
+ const sign = comparison.delta.score >= 0 ? '+' : '';
534
+ lines.push(` Latest snapshot trend: ${comparison.trend} (${sign}${comparison.delta.score} since previous snapshot)`);
535
+ if ((comparison.previous.tags || []).length > 0 || (comparison.current.tags || []).length > 0) {
536
+ lines.push(` Snapshot tags: previous${formatSnapshotTags(comparison.previous.tags)} -> current${formatSnapshotTags(comparison.current.tags)}`);
537
+ }
538
+ if (comparison.previous.milestone || comparison.current.milestone) {
539
+ lines.push(` Lifecycle: previous${formatSnapshotMilestone(comparison.previous.milestone)} -> current${formatSnapshotMilestone(comparison.current.milestone)}`);
540
+ }
541
+ if (comparison.improvements.length > 0) {
542
+ lines.push(` Fixed: ${comparison.improvements.join(', ')}`);
543
+ }
544
+ if (comparison.regressions.length > 0) {
545
+ lines.push(` New gaps: ${comparison.regressions.join(', ')}`);
546
+ }
547
+ }
548
+
549
+ if (history.length === 1) {
550
+ lines.push('');
551
+ lines.push(formatSnapshotBootstrap(dir, 'history'));
552
+ }
553
+
554
+ return lines.join('\n');
555
+ }
556
+
557
+ function exportTrendReport(dir) {
558
+ const history = getHistory(dir, 50);
559
+ if (history.length === 0) return null;
560
+
561
+ const comparison = compareLatest(dir);
562
+ const lines = [
563
+ '# Nerviq Audit Snapshot Trend Report',
564
+ '',
565
+ `**Project:** ${path.basename(dir)}`,
566
+ `**Generated:** ${new Date().toISOString().split('T')[0]}`,
567
+ `**Audit snapshots:** ${history.length}`,
568
+ '',
569
+ '## Audit Snapshot History',
570
+ '',
571
+ '| Date | Milestone | Tags | Score | Passed | Checks |',
572
+ '|------|-----------|------|-------|--------|--------|',
573
+ ];
574
+
575
+ for (const entry of history) {
576
+ const date = entry.createdAt?.split('T')[0] || '?';
577
+ const milestone = entry.milestone || '-';
578
+ const tags = (entry.tags || []).length > 0 ? entry.tags.join(', ') : '-';
579
+ lines.push(`| ${date} | ${milestone} | ${tags} | ${entry.summary?.score ?? '?'}/100 | ${entry.summary?.passed ?? '?'} | ${entry.summary?.checkCount ?? '?'} |`);
580
+ }
581
+
582
+ if (comparison) {
583
+ lines.push('');
584
+ lines.push('## Latest Comparison');
585
+ lines.push('');
586
+ lines.push(`- **Previous snapshot score:** ${comparison.previous.score}/100 (${comparison.previous.date?.split('T')[0]})${formatSnapshotMilestone(comparison.previous.milestone)}${formatSnapshotTags(comparison.previous.tags)}`);
587
+ lines.push(`- **Current snapshot score:** ${comparison.current.score}/100 (${comparison.current.date?.split('T')[0]})${formatSnapshotMilestone(comparison.current.milestone)}${formatSnapshotTags(comparison.current.tags)}`);
588
+ lines.push(`- **Snapshot delta:** ${comparison.delta.score >= 0 ? '+' : ''}${comparison.delta.score} points`);
589
+ lines.push(`- **Trend:** ${comparison.trend}`);
590
+ if (comparison.improvements.length > 0) lines.push(`- **Fixed:** ${comparison.improvements.join(', ')}`);
591
+ if (comparison.regressions.length > 0) lines.push(`- **New gaps:** ${comparison.regressions.join(', ')}`);
592
+ }
593
+
594
+ lines.push('');
595
+ lines.push(`---`);
596
+ lines.push(`*Generated by nerviq v${version}*`);
597
+ return lines.join('\n');
598
+ }
599
+
600
+ function readOutcomeIndex(dir) {
601
+ const indexPath = resolveProjectStateReadPath(dir, 'outcomes', 'index.json');
602
+ if (!fs.existsSync(indexPath)) return [];
603
+ try {
604
+ const entries = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
605
+ return Array.isArray(entries) ? entries : [];
606
+ } catch {
607
+ return [];
608
+ }
609
+ }
610
+
611
+ function updateOutcomeIndex(outcomesDir, record) {
612
+ const indexPath = path.join(outcomesDir, 'index.json');
613
+ let entries = [];
614
+
615
+ if (fs.existsSync(indexPath)) {
616
+ try {
617
+ entries = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
618
+ if (!Array.isArray(entries)) entries = [];
619
+ } catch {
620
+ entries = [];
621
+ }
622
+ }
623
+
624
+ entries.push(record);
625
+ const MAX_INDEX_ENTRIES = 500;
626
+ if (entries.length > MAX_INDEX_ENTRIES) {
627
+ entries = entries.slice(entries.length - MAX_INDEX_ENTRIES);
628
+ }
629
+ fs.writeFileSync(indexPath, JSON.stringify(entries, null, 2), 'utf8');
630
+ }
631
+
632
+ function normalizeOutcomeStatus(value) {
633
+ const normalized = `${value || ''}`.trim().toLowerCase();
634
+ if (!['accepted', 'rejected', 'deferred'].includes(normalized)) {
635
+ throw new Error('feedback status must be one of: accepted, rejected, deferred');
636
+ }
637
+ return normalized;
638
+ }
639
+
640
+ function normalizeOutcomeEffect(value) {
641
+ const normalized = `${value || ''}`.trim().toLowerCase();
642
+ if (!['positive', 'neutral', 'negative'].includes(normalized)) {
643
+ throw new Error('feedback effect must be one of: positive, neutral, negative');
644
+ }
645
+ return normalized;
646
+ }
647
+
648
+ function recordRecommendationOutcome(dir, payload) {
649
+ const key = `${payload.key || ''}`.trim();
650
+ if (!key) {
651
+ throw new Error('feedback requires a recommendation key');
652
+ }
653
+
654
+ const status = normalizeOutcomeStatus(payload.status);
655
+ const effect = normalizeOutcomeEffect(payload.effect || 'neutral');
656
+ const scoreDelta = Number.isFinite(payload.scoreDelta) ? payload.scoreDelta : (
657
+ payload.scoreDelta === null || payload.scoreDelta === undefined || payload.scoreDelta === ''
658
+ ? null
659
+ : Number(payload.scoreDelta)
660
+ );
661
+
662
+ if (scoreDelta !== null && !Number.isFinite(scoreDelta)) {
663
+ throw new Error('feedback scoreDelta must be a number when provided');
664
+ }
665
+
666
+ const id = timestampId();
667
+ const { outcomesDir } = ensureArtifactDirs(dir);
668
+ const filePath = path.join(outcomesDir, `${id}.json`);
669
+ const record = {
670
+ id,
671
+ createdAt: new Date().toISOString(),
672
+ key,
673
+ status,
674
+ effect,
675
+ source: `${payload.source || 'manual-cli'}`.trim() || 'manual-cli',
676
+ notes: `${payload.notes || ''}`.trim(),
677
+ scoreDelta,
678
+ };
679
+
680
+ writeJson(filePath, record);
681
+ updateOutcomeIndex(outcomesDir, {
682
+ ...record,
683
+ relativePath: path.relative(dir, filePath),
684
+ });
685
+
686
+ return {
687
+ id,
688
+ filePath,
689
+ relativePath: path.relative(dir, filePath),
690
+ record,
691
+ };
692
+ }
693
+
694
+ function summarizeOutcomeEntries(entries = []) {
695
+ const byKey = {};
696
+
697
+ for (const entry of entries) {
698
+ if (!entry || !entry.key) continue;
699
+ const bucket = byKey[entry.key] || {
700
+ key: entry.key,
701
+ total: 0,
702
+ accepted: 0,
703
+ rejected: 0,
704
+ deferred: 0,
705
+ positive: 0,
706
+ neutral: 0,
707
+ negative: 0,
708
+ scoreDeltaTotal: 0,
709
+ scoreDeltaCount: 0,
710
+ latestAt: null,
711
+ };
712
+
713
+ bucket.total += 1;
714
+ if (bucket[entry.status] !== undefined) bucket[entry.status] += 1;
715
+ if (bucket[entry.effect] !== undefined) bucket[entry.effect] += 1;
716
+ if (Number.isFinite(entry.scoreDelta)) {
717
+ bucket.scoreDeltaTotal += entry.scoreDelta;
718
+ bucket.scoreDeltaCount += 1;
719
+ }
720
+ if (!bucket.latestAt || new Date(entry.createdAt) > new Date(bucket.latestAt)) {
721
+ bucket.latestAt = entry.createdAt;
722
+ }
723
+
724
+ byKey[entry.key] = bucket;
725
+ }
726
+
727
+ for (const bucket of Object.values(byKey)) {
728
+ bucket.avgScoreDelta = bucket.scoreDeltaCount > 0
729
+ ? Number((bucket.scoreDeltaTotal / bucket.scoreDeltaCount).toFixed(2))
730
+ : null;
731
+ bucket.evidenceClass = bucket.total > 0 ? 'measured' : 'estimated';
732
+ }
733
+
734
+ return {
735
+ totalEntries: entries.length,
736
+ byKey,
737
+ keys: Object.keys(byKey).sort(),
738
+ };
739
+ }
740
+
741
+ function getRecommendationOutcomeSummary(dir) {
742
+ return summarizeOutcomeEntries(readOutcomeIndex(dir));
743
+ }
744
+
745
+ function getRecommendationAdjustment(summaryByKey, key) {
746
+ const bucket = summaryByKey && summaryByKey[key];
747
+ if (!bucket) return 0;
748
+
749
+ let adjustment = 0;
750
+ adjustment += bucket.accepted * 2;
751
+ adjustment += bucket.positive * 3;
752
+ adjustment -= bucket.rejected * 3;
753
+ adjustment -= bucket.negative * 4;
754
+
755
+ if (Number.isFinite(bucket.avgScoreDelta)) {
756
+ if (bucket.avgScoreDelta > 0) adjustment += Math.min(4, Math.round(bucket.avgScoreDelta / 4));
757
+ if (bucket.avgScoreDelta < 0) adjustment -= Math.min(4, Math.round(Math.abs(bucket.avgScoreDelta) / 4));
758
+ }
759
+
760
+ if (adjustment > 8) return 8;
761
+ if (adjustment < -8) return -8;
762
+ return adjustment;
763
+ }
764
+
765
+ function formatRecommendationOutcomeSummary(dir) {
766
+ const summary = getRecommendationOutcomeSummary(dir);
767
+ if (summary.totalEntries === 0) {
768
+ return 'No recommendation outcomes recorded yet. Use `npx nerviq feedback --key permissionDeny --status accepted --effect positive` after a real run.';
769
+ }
770
+
771
+ const lines = [
772
+ 'Recommendation outcome summary:',
773
+ '',
774
+ ];
775
+
776
+ for (const key of summary.keys) {
777
+ const bucket = summary.byKey[key];
778
+ const avg = Number.isFinite(bucket.avgScoreDelta) ? ` | avg score delta ${bucket.avgScoreDelta >= 0 ? '+' : ''}${bucket.avgScoreDelta}` : '';
779
+ const adjustment = getRecommendationAdjustment(summary.byKey, key);
780
+ lines.push(` ${key}: total ${bucket.total} | accepted ${bucket.accepted} | rejected ${bucket.rejected} | deferred ${bucket.deferred} | positive ${bucket.positive} | negative ${bucket.negative}${avg} | ranking ${adjustment >= 0 ? '+' : ''}${adjustment}`);
781
+ }
782
+
783
+ return lines.join('\n');
784
+ }
785
+
786
+ /**
787
+ * Load the full payload of a snapshot by its index entry.
788
+ * @param {string} dir - Project root directory.
789
+ * @param {Object} indexEntry - Snapshot index entry with relativePath.
790
+ * @returns {Object|null} Full snapshot envelope, or null if unreadable.
791
+ */
792
+ function loadSnapshotPayload(dir, indexEntry) {
793
+ if (!indexEntry || !indexEntry.relativePath) return null;
794
+ const filePath = path.join(dir, indexEntry.relativePath);
795
+ try {
796
+ const envelope = JSON.parse(fs.readFileSync(filePath, 'utf8'));
797
+ return envelope.payload || null;
798
+ } catch {
799
+ return null;
800
+ }
801
+ }
802
+
803
+ /**
804
+ * Analyze check health by comparing the two most recent audit snapshots.
805
+ * Detects checks that regressed (passed → failed), improved (failed → passed),
806
+ * and flags sudden drops that may indicate platform format changes.
807
+ * When more than 2 snapshots exist, also computes per-check pass rates.
808
+ *
809
+ * @param {string} dir - Project root directory.
810
+ * @returns {Object|null} Health report, or null if fewer than 2 audit snapshots exist.
811
+ */
812
+ function checkHealth(dir) {
813
+ const history = getHistory(dir, 20);
814
+ if (history.length < 2) return null;
815
+
816
+ const currentPayload = loadSnapshotPayload(dir, history[0]);
817
+ const previousPayload = loadSnapshotPayload(dir, history[1]);
818
+ if (!currentPayload || !previousPayload) return null;
819
+
820
+ const currentResults = currentPayload.results || [];
821
+ const previousResults = previousPayload.results || [];
822
+ const diff = collectCheckDiff(previousResults, currentResults);
823
+ const regressions = diff.regressions;
824
+ const improvements = diff.improvements;
825
+ const newChecks = diff.newChecks;
826
+ const removedChecks = diff.removedChecks;
827
+
828
+ // Detect potential platform format changes:
829
+ // If 3+ checks in the same category regressed, flag it
830
+ const regressionsByCategory = {};
831
+ for (const r of regressions) {
832
+ if (!regressionsByCategory[r.category]) regressionsByCategory[r.category] = [];
833
+ regressionsByCategory[r.category].push(r);
834
+ }
835
+ const platformAlerts = [];
836
+ for (const [cat, items] of Object.entries(regressionsByCategory)) {
837
+ if (items.length >= 3) {
838
+ platformAlerts.push({
839
+ category: cat,
840
+ regressionCount: items.length,
841
+ message: `${items.length} checks in '${cat}' regressed — possible platform format change`,
842
+ checks: items.map(i => i.key),
843
+ });
844
+ }
845
+ }
846
+
847
+ // Per-check pass rates across all snapshots
848
+ const passRates = computePassRates(dir, history);
849
+
850
+ return {
851
+ currentDate: history[0].createdAt,
852
+ previousDate: history[1].createdAt,
853
+ snapshotsAnalyzed: history.length,
854
+ scoreDelta: (currentPayload.score || 0) - (previousPayload.score || 0),
855
+ regressions,
856
+ improvements,
857
+ newChecks,
858
+ removedChecks,
859
+ platformAlerts,
860
+ passRates,
861
+ summary: {
862
+ regressionsCount: regressions.length,
863
+ improvementsCount: improvements.length,
864
+ newChecksCount: newChecks.length,
865
+ removedChecksCount: removedChecks.length,
866
+ alertsCount: platformAlerts.length,
867
+ },
868
+ };
869
+ }
870
+
871
+ /**
872
+ * Compute per-check pass rates across all snapshots.
873
+ * Returns { declining, consistentlyFailing, consistentlyPassing, overallHealth }.
874
+ */
875
+ function computePassRates(dir, history) {
876
+ // key → { passes, total, recentResults: [bool...] (newest first) }
877
+ const stats = {};
878
+ for (const entry of history) {
879
+ const payload = loadSnapshotPayload(dir, entry);
880
+ if (!payload || !payload.results) continue;
881
+ for (const r of payload.results) {
882
+ if (!r.key || r.passed === null || r.passed === undefined) continue;
883
+ if (!stats[r.key]) stats[r.key] = { name: r.name, passes: 0, total: 0, recentResults: [] };
884
+ stats[r.key].total++;
885
+ if (r.passed) stats[r.key].passes++;
886
+ stats[r.key].recentResults.push(!!r.passed);
887
+ }
888
+ }
889
+
890
+ const declining = [];
891
+ const consistentlyFailing = [];
892
+ let consistentlyPassingCount = 0;
893
+ let totalChecks = 0;
894
+ let totalPasses = 0;
895
+ let totalAppearances = 0;
896
+
897
+ for (const [key, s] of Object.entries(stats)) {
898
+ const rate = s.total > 0 ? s.passes / s.total : 0;
899
+ totalChecks++;
900
+ totalPasses += s.passes;
901
+ totalAppearances += s.total;
902
+
903
+ if (s.total >= 2 && rate === 0) {
904
+ consistentlyFailing.push({ key, name: s.name, runs: s.total });
905
+ } else if (rate === 1) {
906
+ consistentlyPassingCount++;
907
+ } else if (s.total >= 2) {
908
+ // Check if declining: earlier results passed, recent ones failed
909
+ const half = Math.ceil(s.recentResults.length / 2);
910
+ const recentHalf = s.recentResults.slice(0, half);
911
+ const olderHalf = s.recentResults.slice(half);
912
+ const recentRate = recentHalf.filter(Boolean).length / recentHalf.length;
913
+ const olderRate = olderHalf.length > 0 ? olderHalf.filter(Boolean).length / olderHalf.length : recentRate;
914
+ if (olderRate > recentRate) {
915
+ const failStreak = s.recentResults.findIndex(v => v === true);
916
+ declining.push({
917
+ key, name: s.name,
918
+ oldRate: Math.round(olderRate * 100),
919
+ newRate: Math.round(recentRate * 100),
920
+ failingRuns: failStreak === -1 ? s.recentResults.length : failStreak,
921
+ });
922
+ }
923
+ }
924
+ }
925
+
926
+ const overallHealth = totalAppearances > 0 ? Math.round((totalPasses / totalAppearances) * 100) : 100;
927
+
928
+ return { declining, consistentlyFailing, consistentlyPassingCount, overallHealth };
929
+ }
930
+
931
+ /**
932
+ * Format check-health report for CLI display.
933
+ */
934
+ function formatCheckHealth(healthReport) {
935
+ if (!healthReport) return 'Need at least 2 audit snapshots. Run `nerviq audit --snapshot` twice.';
936
+
937
+ const lines = [];
938
+ const { scoreDelta, regressions, improvements, platformAlerts, newChecks, passRates } = healthReport;
939
+ const sign = scoreDelta >= 0 ? '+' : '';
940
+
941
+ lines.push(` Check Health Report`);
942
+ lines.push(` ═══════════════════════════════════════`);
943
+ lines.push(` Snapshots analyzed: ${healthReport.snapshotsAnalyzed}`);
944
+ lines.push(` Period: ${healthReport.previousDate?.split('T')[0]} → ${healthReport.currentDate?.split('T')[0]}`);
945
+ lines.push(` Score delta: ${sign}${scoreDelta}`);
946
+ lines.push('');
947
+
948
+ if (platformAlerts.length > 0) {
949
+ lines.push(` ⚠️ PLATFORM ALERTS (${platformAlerts.length})`);
950
+ for (const alert of platformAlerts) {
951
+ lines.push(` ${alert.message}`);
952
+ lines.push(` Checks: ${alert.checks.join(', ')}`);
953
+ }
954
+ lines.push('');
955
+ }
956
+
957
+ if (passRates && passRates.declining.length > 0) {
958
+ lines.push(` Checks with declining pass rate:`);
959
+ for (const d of passRates.declining) {
960
+ const detail = d.failingRuns > 0 ? `(failing in last ${d.failingRuns} runs)` : '';
961
+ lines.push(` ⚠ ${d.key.padEnd(22)} ${d.oldRate}% → ${d.newRate}% ${detail}`);
962
+ }
963
+ lines.push('');
964
+ }
965
+
966
+ if (passRates && passRates.consistentlyFailing.length > 0) {
967
+ lines.push(` Consistently failing (0% pass rate):`);
968
+ for (const f of passRates.consistentlyFailing) {
969
+ lines.push(` ✗ ${f.key.padEnd(22)} 0/${f.runs} runs`);
970
+ }
971
+ lines.push('');
972
+ }
973
+
974
+ if (regressions.length > 0) {
975
+ lines.push(` 🔴 Regressions (${regressions.length} checks now failing)`);
976
+ for (const r of regressions) {
977
+ lines.push(` ${r.name} [${r.impact}]`);
978
+ }
979
+ lines.push('');
980
+ }
981
+
982
+ if (improvements.length > 0) {
983
+ lines.push(` ✅ Improvements (${improvements.length} checks now passing)`);
984
+ for (const r of improvements) {
985
+ lines.push(` ${r.name}`);
986
+ }
987
+ lines.push('');
988
+ }
989
+
990
+ if (newChecks.length > 0) {
991
+ lines.push(` 🆕 New checks (${newChecks.length})`);
992
+ lines.push('');
993
+ }
994
+
995
+ if (passRates && passRates.consistentlyPassingCount > 0) {
996
+ lines.push(` Consistently passing (100%):`);
997
+ lines.push(` ✓ ${passRates.consistentlyPassingCount} checks at 100% pass rate`);
998
+ lines.push('');
999
+ }
1000
+
1001
+ if (regressions.length === 0 && platformAlerts.length === 0) {
1002
+ lines.push(` ✅ All checks stable. No regressions detected.`);
1003
+ lines.push('');
1004
+ }
1005
+
1006
+ if (passRates) {
1007
+ lines.push(` Overall health: ${passRates.overallHealth}%`);
1008
+ }
1009
+
1010
+ return lines.join('\n');
1011
+ }
1012
+
1013
+ module.exports = {
1014
+ getUserId,
1015
+ ensureArtifactDirs,
1016
+ writeActivityArtifact,
1017
+ writeRollbackArtifact,
1018
+ writeSnapshotArtifact,
1019
+ normalizeSnapshotTags,
1020
+ formatSnapshotTags,
1021
+ normalizeSnapshotMilestone,
1022
+ formatSnapshotMilestone,
1023
+ SNAPSHOT_MILESTONES,
1024
+ readSnapshotIndex,
1025
+ getHistory,
1026
+ compareLatest,
1027
+ formatSnapshotBootstrap,
1028
+ formatHistory,
1029
+ exportTrendReport,
1030
+ readOutcomeIndex,
1031
+ recordRecommendationOutcome,
1032
+ summarizeOutcomeEntries,
1033
+ getRecommendationOutcomeSummary,
1034
+ getRecommendationAdjustment,
1035
+ formatRecommendationOutcomeSummary,
1036
+ checkHealth,
1037
+ formatCheckHealth,
1038
+ loadSnapshotPayload,
1039
+ };