@nerviq/cli 1.29.0 → 1.29.1

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 (80) hide show
  1. package/CHANGELOG.md +1527 -1493
  2. package/README.md +550 -538
  3. package/SECURITY.md +82 -82
  4. package/bin/cli.js +2562 -2558
  5. package/docs/api-reference.md +356 -356
  6. package/docs/audit-fix.md +109 -0
  7. package/docs/autofix.md +3 -62
  8. package/docs/getting-started.md +1 -1
  9. package/docs/index.html +592 -592
  10. package/docs/integration-contracts.md +287 -287
  11. package/docs/maintenance.md +128 -128
  12. package/docs/new-platform-guide.md +202 -202
  13. package/docs/release-process.md +63 -0
  14. package/docs/shallow-risk.md +244 -244
  15. package/docs/why-nerviq.md +82 -82
  16. package/package.json +67 -67
  17. package/src/aider/activity.js +226 -226
  18. package/src/aider/context.js +162 -162
  19. package/src/aider/freshness.js +123 -123
  20. package/src/aider/techniques.js +3465 -3465
  21. package/src/audit/layers.js +180 -180
  22. package/src/audit.js +1032 -1032
  23. package/src/benchmark.js +299 -299
  24. package/src/codex/activity.js +324 -324
  25. package/src/codex/freshness.js +142 -142
  26. package/src/codex/techniques.js +4895 -4895
  27. package/src/context.js +326 -326
  28. package/src/continuous-ops.js +11 -1
  29. package/src/convert.js +340 -340
  30. package/src/copilot/config-parser.js +280 -280
  31. package/src/copilot/context.js +218 -218
  32. package/src/copilot/freshness.js +177 -177
  33. package/src/copilot/patch.js +238 -238
  34. package/src/copilot/techniques.js +3578 -3578
  35. package/src/cursor/freshness.js +194 -194
  36. package/src/cursor/patch.js +243 -243
  37. package/src/cursor/techniques.js +3735 -3735
  38. package/src/doctor.js +201 -201
  39. package/src/fix-engine.js +511 -8
  40. package/src/formatters/csv.js +86 -86
  41. package/src/formatters/junit.js +123 -123
  42. package/src/formatters/markdown.js +164 -164
  43. package/src/formatters/otel.js +151 -151
  44. package/src/freshness.js +156 -156
  45. package/src/gemini/activity.js +402 -402
  46. package/src/gemini/context.js +290 -290
  47. package/src/gemini/freshness.js +183 -183
  48. package/src/gemini/patch.js +229 -229
  49. package/src/gemini/techniques.js +3811 -3811
  50. package/src/governance.js +533 -533
  51. package/src/harmony/audit.js +306 -306
  52. package/src/i18n.js +63 -63
  53. package/src/insights.js +119 -119
  54. package/src/integrations.js +134 -134
  55. package/src/locales/en.json +33 -33
  56. package/src/locales/es.json +33 -33
  57. package/src/migrate.js +354 -354
  58. package/src/opencode/activity.js +286 -286
  59. package/src/opencode/freshness.js +137 -137
  60. package/src/opencode/techniques.js +3450 -3450
  61. package/src/setup/analysis.js +12 -12
  62. package/src/setup.js +7 -6
  63. package/src/shallow-risk/index.js +56 -56
  64. package/src/shallow-risk/patterns/agent-config-cross-platform-drift.js +50 -50
  65. package/src/shallow-risk/patterns/agent-config-dangerous-autoapprove.js +46 -46
  66. package/src/shallow-risk/patterns/agent-config-deprecated-keys.js +46 -46
  67. package/src/shallow-risk/patterns/agent-config-missing-file.js +317 -317
  68. package/src/shallow-risk/patterns/agent-config-secret-literal.js +49 -49
  69. package/src/shallow-risk/patterns/agent-config-stack-contradiction.js +34 -34
  70. package/src/shallow-risk/patterns/hook-script-missing.js +70 -70
  71. package/src/shallow-risk/patterns/mcp-server-no-allowlist.js +52 -52
  72. package/src/shallow-risk/shared.js +648 -648
  73. package/src/source-urls.js +295 -295
  74. package/src/state-paths.js +85 -85
  75. package/src/supplemental-checks.js +805 -805
  76. package/src/telemetry.js +160 -160
  77. package/src/windsurf/context.js +359 -359
  78. package/src/windsurf/freshness.js +194 -194
  79. package/src/windsurf/patch.js +231 -231
  80. package/src/windsurf/techniques.js +3779 -3779
@@ -1,324 +1,324 @@
1
- /**
2
- * Codex Repeat-Usage Surfaces — CP-06
3
- *
4
- * Adapts the shared activity/snapshot backend for Codex platform.
5
- * Provides: history, compare, trend, watch, feedback, insights.
6
- *
7
- * Codex snapshots are stored alongside Claude snapshots in
8
- * .nerviq/snapshots/ (legacy: .claude/nerviq-cli/snapshots/) but filtered by platform='codex'.
9
- */
10
-
11
- const path = require('path');
12
- const {
13
- getHistory: getSharedHistory,
14
- compareLatest: sharedCompareLatest,
15
- readSnapshotIndex,
16
- writeSnapshotArtifact,
17
- exportTrendReport: sharedExportTrendReport,
18
- recordRecommendationOutcome,
19
- readOutcomeIndex,
20
- summarizeOutcomeEntries,
21
- } = require('../activity');
22
- const { version } = require('../../package.json');
23
-
24
- // --- History ---
25
-
26
- /**
27
- * Get Codex audit history from snapshots.
28
- * Filters to platform='codex' snapshots only.
29
- */
30
- function getCodexHistory(dir, limit = 20) {
31
- const entries = readSnapshotIndex(dir);
32
- return entries
33
- .filter(e => e.snapshotKind === 'audit' && (e.platform === 'codex' || e.summary?.platform === 'codex'))
34
- .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
35
- .slice(0, limit);
36
- }
37
-
38
- function formatCodexHistory(dir) {
39
- const history = getCodexHistory(dir, 10);
40
- if (history.length === 0) {
41
- return 'No Codex snapshots found. Run `npx nerviq --platform codex --snapshot` to save one.';
42
- }
43
-
44
- const lines = ['Codex Score History (most recent first):', ''];
45
- for (const entry of history) {
46
- const date = entry.createdAt?.split('T')[0] || 'unknown';
47
- const score = entry.summary?.score ?? '?';
48
- const passed = entry.summary?.passed ?? '?';
49
- const total = entry.summary?.checkCount ?? '?';
50
- lines.push(` ${date} ${score}/100 (${passed}/${total} passing)`);
51
- }
52
-
53
- const comparison = compareCodexLatest(dir);
54
- if (comparison) {
55
- lines.push('');
56
- const sign = comparison.delta.score >= 0 ? '+' : '';
57
- lines.push(` Trend: ${comparison.trend} (${sign}${comparison.delta.score} since previous)`);
58
- if (comparison.improvements.length > 0) {
59
- lines.push(` Fixed: ${comparison.improvements.join(', ')}`);
60
- }
61
- if (comparison.regressions.length > 0) {
62
- lines.push(` New gaps: ${comparison.regressions.join(', ')}`);
63
- }
64
- }
65
-
66
- return lines.join('\n');
67
- }
68
-
69
- // --- Compare ---
70
-
71
- /**
72
- * Compare the two most recent Codex audit snapshots.
73
- */
74
- function compareCodexLatest(dir) {
75
- const audits = getCodexHistory(dir, 2);
76
- if (audits.length < 2) return null;
77
-
78
- const current = audits[0];
79
- const previous = audits[1];
80
-
81
- const delta = {
82
- score: (current.summary?.score || 0) - (previous.summary?.score || 0),
83
- organic: (current.summary?.organicScore || 0) - (previous.summary?.organicScore || 0),
84
- passed: (current.summary?.passed || 0) - (previous.summary?.passed || 0),
85
- };
86
-
87
- const regressions = [];
88
- const improvements = [];
89
-
90
- const prevKeys = new Set(previous.summary?.topActionKeys || []);
91
- const currKeys = new Set(current.summary?.topActionKeys || []);
92
-
93
- for (const key of currKeys) {
94
- if (!prevKeys.has(key)) regressions.push(key);
95
- }
96
- for (const key of prevKeys) {
97
- if (!currKeys.has(key)) improvements.push(key);
98
- }
99
-
100
- return {
101
- platform: 'codex',
102
- current: { date: current.createdAt, score: current.summary?.score, passed: current.summary?.passed },
103
- previous: { date: previous.createdAt, score: previous.summary?.score, passed: previous.summary?.passed },
104
- delta,
105
- regressions,
106
- improvements,
107
- trend: delta.score > 0 ? 'improving' : delta.score < 0 ? 'regressing' : 'stable',
108
- };
109
- }
110
-
111
- // --- Trend ---
112
-
113
- /**
114
- * Export Codex trend report as markdown.
115
- */
116
- function exportCodexTrendReport(dir) {
117
- const history = getCodexHistory(dir, 50);
118
- if (history.length === 0) return null;
119
-
120
- const comparison = compareCodexLatest(dir);
121
- const lines = [
122
- '# Codex Setup Trend Report',
123
- '',
124
- `**Project:** ${path.basename(dir)}`,
125
- `**Platform:** Codex`,
126
- `**Generated:** ${new Date().toISOString().split('T')[0]}`,
127
- `**Snapshots:** ${history.length}`,
128
- '',
129
- '## Score History',
130
- '',
131
- '| Date | Score | Passed | Checks |',
132
- '|------|-------|--------|--------|',
133
- ];
134
-
135
- for (const entry of history) {
136
- const date = entry.createdAt?.split('T')[0] || '?';
137
- lines.push(`| ${date} | ${entry.summary?.score ?? '?'}/100 | ${entry.summary?.passed ?? '?'} | ${entry.summary?.checkCount ?? '?'} |`);
138
- }
139
-
140
- if (comparison) {
141
- lines.push('');
142
- lines.push('## Latest Comparison');
143
- lines.push('');
144
- lines.push(`- **Previous:** ${comparison.previous.score}/100 (${comparison.previous.date?.split('T')[0]})`);
145
- lines.push(`- **Current:** ${comparison.current.score}/100 (${comparison.current.date?.split('T')[0]})`);
146
- lines.push(`- **Delta:** ${comparison.delta.score >= 0 ? '+' : ''}${comparison.delta.score} points`);
147
- lines.push(`- **Trend:** ${comparison.trend}`);
148
- if (comparison.improvements.length > 0) lines.push(`- **Fixed:** ${comparison.improvements.join(', ')}`);
149
- if (comparison.regressions.length > 0) lines.push(`- **New gaps:** ${comparison.regressions.join(', ')}`);
150
- }
151
-
152
- // ASCII trend chart
153
- if (history.length >= 3) {
154
- lines.push('');
155
- lines.push('## Trend Chart');
156
- lines.push('');
157
- lines.push('```');
158
- const scores = history.slice().reverse().map(e => e.summary?.score ?? 0);
159
- const max = Math.max(...scores, 100);
160
- const chartHeight = 10;
161
- for (let row = chartHeight; row >= 0; row--) {
162
- const threshold = (row / chartHeight) * max;
163
- const rowLabel = String(Math.round(threshold)).padStart(3);
164
- const bar = scores.map(s => s >= threshold ? '#' : ' ').join('');
165
- lines.push(`${rowLabel} |${bar}`);
166
- }
167
- lines.push(` +${'─'.repeat(scores.length)}`);
168
- lines.push('```');
169
- }
170
-
171
- lines.push('');
172
- lines.push(`---`);
173
- lines.push(`*Generated by nerviq v${version} for Codex*`);
174
- return lines.join('\n');
175
- }
176
-
177
- // --- Feedback ---
178
-
179
- /**
180
- * Record feedback on a Codex recommendation.
181
- */
182
- function recordCodexFeedback(dir, payload) {
183
- return recordRecommendationOutcome(dir, {
184
- ...payload,
185
- source: payload.source || 'codex-cli',
186
- platform: 'codex',
187
- });
188
- }
189
-
190
- /**
191
- * Get Codex feedback summary.
192
- */
193
- function getCodexFeedbackSummary(dir) {
194
- const entries = readOutcomeIndex(dir)
195
- .filter(e => e.source === 'codex-cli' || e.platform === 'codex');
196
- return summarizeOutcomeEntries(entries);
197
- }
198
-
199
- function formatCodexFeedback(dir) {
200
- const summary = getCodexFeedbackSummary(dir);
201
- if (!summary || Object.keys(summary).length === 0) {
202
- return 'No Codex feedback recorded yet. Use `npx nerviq --platform codex feedback` to rate recommendations.';
203
- }
204
-
205
- const lines = ['Codex Recommendation Feedback:', ''];
206
- const entries = Array.isArray(summary) ? summary : Object.values(summary);
207
- for (const entry of entries) {
208
- const key = entry.key || 'unknown';
209
- const accepted = entry.accepted || 0;
210
- const rejected = entry.rejected || 0;
211
- const total = entry.total || 0;
212
- lines.push(` ${key}: ${accepted} accepted, ${rejected} rejected (${total} total)`);
213
- }
214
- return lines.join('\n');
215
- }
216
-
217
- // --- Insights ---
218
-
219
- /**
220
- * Generate Codex-specific insights from audit history and feedback.
221
- */
222
- function generateCodexInsights(dir) {
223
- const history = getCodexHistory(dir, 50);
224
- const feedback = getCodexFeedbackSummary(dir);
225
- const insights = [];
226
-
227
- // Pattern 1: Persistent failures
228
- if (history.length >= 3) {
229
- const recentFailKeys = new Map();
230
- for (const entry of history.slice(0, 5)) {
231
- for (const key of (entry.summary?.topActionKeys || [])) {
232
- recentFailKeys.set(key, (recentFailKeys.get(key) || 0) + 1);
233
- }
234
- }
235
- for (const [key, count] of recentFailKeys) {
236
- if (count >= 3) {
237
- insights.push({
238
- type: 'persistent-failure',
239
- severity: 'high',
240
- key,
241
- message: `Check ${key} has failed in ${count} of the last ${Math.min(history.length, 5)} audits. Consider addressing it or marking it as intentionally skipped.`,
242
- });
243
- }
244
- }
245
- }
246
-
247
- // Pattern 2: Score regression
248
- if (history.length >= 2) {
249
- const scores = history.map(e => e.summary?.score ?? 0);
250
- if (scores[0] < scores[1]) {
251
- insights.push({
252
- type: 'regression-pattern',
253
- severity: 'medium',
254
- message: `Score dropped from ${scores[1]} to ${scores[0]} in the most recent audit. Review recent changes.`,
255
- delta: scores[0] - scores[1],
256
- });
257
- }
258
- }
259
-
260
- // Pattern 3: Improvement velocity stall
261
- if (history.length >= 5) {
262
- const recentScores = history.slice(0, 5).map(e => e.summary?.score ?? 0);
263
- const range = Math.max(...recentScores) - Math.min(...recentScores);
264
- if (range <= 2) {
265
- insights.push({
266
- type: 'velocity-stall',
267
- severity: 'low',
268
- message: `Score has been flat (range: ${range} points) over the last 5 audits. Consider addressing lower-priority checks.`,
269
- });
270
- }
271
- }
272
-
273
- // Pattern 4: Feedback signals
274
- const feedbackEntries = Array.isArray(feedback) ? feedback : Object.values(feedback || {});
275
- for (const entry of feedbackEntries) {
276
- if (entry.rejected > entry.accepted && entry.total >= 2) {
277
- insights.push({
278
- type: 'feedback-signal',
279
- severity: 'medium',
280
- key: entry.key,
281
- message: `Recommendation ${entry.key} has been rejected more than accepted (${entry.rejected}/${entry.total}). Consider adjusting or removing this recommendation.`,
282
- });
283
- }
284
- }
285
-
286
- return {
287
- platform: 'codex',
288
- generatedAt: new Date().toISOString(),
289
- snapshotCount: history.length,
290
- feedbackCount: feedbackEntries.length,
291
- insights,
292
- summary: insights.length === 0
293
- ? 'No actionable insights detected. Keep running audits to build pattern data.'
294
- : `${insights.length} insight(s) detected across ${history.length} snapshots.`,
295
- };
296
- }
297
-
298
- function formatCodexInsights(dir) {
299
- const result = generateCodexInsights(dir);
300
- if (result.insights.length === 0) {
301
- return result.summary;
302
- }
303
-
304
- const lines = ['Codex Insights:', ''];
305
- for (const insight of result.insights) {
306
- const severity = insight.severity.toUpperCase();
307
- lines.push(` [${severity}] ${insight.message}`);
308
- }
309
- lines.push('');
310
- lines.push(result.summary);
311
- return lines.join('\n');
312
- }
313
-
314
- module.exports = {
315
- getCodexHistory,
316
- formatCodexHistory,
317
- compareCodexLatest,
318
- exportCodexTrendReport,
319
- recordCodexFeedback,
320
- getCodexFeedbackSummary,
321
- formatCodexFeedback,
322
- generateCodexInsights,
323
- formatCodexInsights,
324
- };
1
+ /**
2
+ * Codex Repeat-Usage Surfaces — CP-06
3
+ *
4
+ * Adapts the shared activity/snapshot backend for Codex platform.
5
+ * Provides: history, compare, trend, watch, feedback, insights.
6
+ *
7
+ * Codex snapshots are stored alongside Claude snapshots in
8
+ * .nerviq/snapshots/ (legacy: .claude/nerviq-cli/snapshots/) but filtered by platform='codex'.
9
+ */
10
+
11
+ const path = require('path');
12
+ const {
13
+ getHistory: getSharedHistory,
14
+ compareLatest: sharedCompareLatest,
15
+ readSnapshotIndex,
16
+ writeSnapshotArtifact,
17
+ exportTrendReport: sharedExportTrendReport,
18
+ recordRecommendationOutcome,
19
+ readOutcomeIndex,
20
+ summarizeOutcomeEntries,
21
+ } = require('../activity');
22
+ const { version } = require('../../package.json');
23
+
24
+ // --- History ---
25
+
26
+ /**
27
+ * Get Codex audit history from snapshots.
28
+ * Filters to platform='codex' snapshots only.
29
+ */
30
+ function getCodexHistory(dir, limit = 20) {
31
+ const entries = readSnapshotIndex(dir);
32
+ return entries
33
+ .filter(e => e.snapshotKind === 'audit' && (e.platform === 'codex' || e.summary?.platform === 'codex'))
34
+ .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
35
+ .slice(0, limit);
36
+ }
37
+
38
+ function formatCodexHistory(dir) {
39
+ const history = getCodexHistory(dir, 10);
40
+ if (history.length === 0) {
41
+ return 'No Codex snapshots found. Run `npx nerviq --platform codex --snapshot` to save one.';
42
+ }
43
+
44
+ const lines = ['Codex Score History (most recent first):', ''];
45
+ for (const entry of history) {
46
+ const date = entry.createdAt?.split('T')[0] || 'unknown';
47
+ const score = entry.summary?.score ?? '?';
48
+ const passed = entry.summary?.passed ?? '?';
49
+ const total = entry.summary?.checkCount ?? '?';
50
+ lines.push(` ${date} ${score}/100 (${passed}/${total} passing)`);
51
+ }
52
+
53
+ const comparison = compareCodexLatest(dir);
54
+ if (comparison) {
55
+ lines.push('');
56
+ const sign = comparison.delta.score >= 0 ? '+' : '';
57
+ lines.push(` Trend: ${comparison.trend} (${sign}${comparison.delta.score} since previous)`);
58
+ if (comparison.improvements.length > 0) {
59
+ lines.push(` Fixed: ${comparison.improvements.join(', ')}`);
60
+ }
61
+ if (comparison.regressions.length > 0) {
62
+ lines.push(` New gaps: ${comparison.regressions.join(', ')}`);
63
+ }
64
+ }
65
+
66
+ return lines.join('\n');
67
+ }
68
+
69
+ // --- Compare ---
70
+
71
+ /**
72
+ * Compare the two most recent Codex audit snapshots.
73
+ */
74
+ function compareCodexLatest(dir) {
75
+ const audits = getCodexHistory(dir, 2);
76
+ if (audits.length < 2) return null;
77
+
78
+ const current = audits[0];
79
+ const previous = audits[1];
80
+
81
+ const delta = {
82
+ score: (current.summary?.score || 0) - (previous.summary?.score || 0),
83
+ organic: (current.summary?.organicScore || 0) - (previous.summary?.organicScore || 0),
84
+ passed: (current.summary?.passed || 0) - (previous.summary?.passed || 0),
85
+ };
86
+
87
+ const regressions = [];
88
+ const improvements = [];
89
+
90
+ const prevKeys = new Set(previous.summary?.topActionKeys || []);
91
+ const currKeys = new Set(current.summary?.topActionKeys || []);
92
+
93
+ for (const key of currKeys) {
94
+ if (!prevKeys.has(key)) regressions.push(key);
95
+ }
96
+ for (const key of prevKeys) {
97
+ if (!currKeys.has(key)) improvements.push(key);
98
+ }
99
+
100
+ return {
101
+ platform: 'codex',
102
+ current: { date: current.createdAt, score: current.summary?.score, passed: current.summary?.passed },
103
+ previous: { date: previous.createdAt, score: previous.summary?.score, passed: previous.summary?.passed },
104
+ delta,
105
+ regressions,
106
+ improvements,
107
+ trend: delta.score > 0 ? 'improving' : delta.score < 0 ? 'regressing' : 'stable',
108
+ };
109
+ }
110
+
111
+ // --- Trend ---
112
+
113
+ /**
114
+ * Export Codex trend report as markdown.
115
+ */
116
+ function exportCodexTrendReport(dir) {
117
+ const history = getCodexHistory(dir, 50);
118
+ if (history.length === 0) return null;
119
+
120
+ const comparison = compareCodexLatest(dir);
121
+ const lines = [
122
+ '# Codex Setup Trend Report',
123
+ '',
124
+ `**Project:** ${path.basename(dir)}`,
125
+ `**Platform:** Codex`,
126
+ `**Generated:** ${new Date().toISOString().split('T')[0]}`,
127
+ `**Snapshots:** ${history.length}`,
128
+ '',
129
+ '## Score History',
130
+ '',
131
+ '| Date | Score | Passed | Checks |',
132
+ '|------|-------|--------|--------|',
133
+ ];
134
+
135
+ for (const entry of history) {
136
+ const date = entry.createdAt?.split('T')[0] || '?';
137
+ lines.push(`| ${date} | ${entry.summary?.score ?? '?'}/100 | ${entry.summary?.passed ?? '?'} | ${entry.summary?.checkCount ?? '?'} |`);
138
+ }
139
+
140
+ if (comparison) {
141
+ lines.push('');
142
+ lines.push('## Latest Comparison');
143
+ lines.push('');
144
+ lines.push(`- **Previous:** ${comparison.previous.score}/100 (${comparison.previous.date?.split('T')[0]})`);
145
+ lines.push(`- **Current:** ${comparison.current.score}/100 (${comparison.current.date?.split('T')[0]})`);
146
+ lines.push(`- **Delta:** ${comparison.delta.score >= 0 ? '+' : ''}${comparison.delta.score} points`);
147
+ lines.push(`- **Trend:** ${comparison.trend}`);
148
+ if (comparison.improvements.length > 0) lines.push(`- **Fixed:** ${comparison.improvements.join(', ')}`);
149
+ if (comparison.regressions.length > 0) lines.push(`- **New gaps:** ${comparison.regressions.join(', ')}`);
150
+ }
151
+
152
+ // ASCII trend chart
153
+ if (history.length >= 3) {
154
+ lines.push('');
155
+ lines.push('## Trend Chart');
156
+ lines.push('');
157
+ lines.push('```');
158
+ const scores = history.slice().reverse().map(e => e.summary?.score ?? 0);
159
+ const max = Math.max(...scores, 100);
160
+ const chartHeight = 10;
161
+ for (let row = chartHeight; row >= 0; row--) {
162
+ const threshold = (row / chartHeight) * max;
163
+ const rowLabel = String(Math.round(threshold)).padStart(3);
164
+ const bar = scores.map(s => s >= threshold ? '#' : ' ').join('');
165
+ lines.push(`${rowLabel} |${bar}`);
166
+ }
167
+ lines.push(` +${'─'.repeat(scores.length)}`);
168
+ lines.push('```');
169
+ }
170
+
171
+ lines.push('');
172
+ lines.push(`---`);
173
+ lines.push(`*Generated by nerviq v${version} for Codex*`);
174
+ return lines.join('\n');
175
+ }
176
+
177
+ // --- Feedback ---
178
+
179
+ /**
180
+ * Record feedback on a Codex recommendation.
181
+ */
182
+ function recordCodexFeedback(dir, payload) {
183
+ return recordRecommendationOutcome(dir, {
184
+ ...payload,
185
+ source: payload.source || 'codex-cli',
186
+ platform: 'codex',
187
+ });
188
+ }
189
+
190
+ /**
191
+ * Get Codex feedback summary.
192
+ */
193
+ function getCodexFeedbackSummary(dir) {
194
+ const entries = readOutcomeIndex(dir)
195
+ .filter(e => e.source === 'codex-cli' || e.platform === 'codex');
196
+ return summarizeOutcomeEntries(entries);
197
+ }
198
+
199
+ function formatCodexFeedback(dir) {
200
+ const summary = getCodexFeedbackSummary(dir);
201
+ if (!summary || Object.keys(summary).length === 0) {
202
+ return 'No Codex feedback recorded yet. Use `npx nerviq --platform codex feedback` to rate recommendations.';
203
+ }
204
+
205
+ const lines = ['Codex Recommendation Feedback:', ''];
206
+ const entries = Array.isArray(summary) ? summary : Object.values(summary);
207
+ for (const entry of entries) {
208
+ const key = entry.key || 'unknown';
209
+ const accepted = entry.accepted || 0;
210
+ const rejected = entry.rejected || 0;
211
+ const total = entry.total || 0;
212
+ lines.push(` ${key}: ${accepted} accepted, ${rejected} rejected (${total} total)`);
213
+ }
214
+ return lines.join('\n');
215
+ }
216
+
217
+ // --- Insights ---
218
+
219
+ /**
220
+ * Generate Codex-specific insights from audit history and feedback.
221
+ */
222
+ function generateCodexInsights(dir) {
223
+ const history = getCodexHistory(dir, 50);
224
+ const feedback = getCodexFeedbackSummary(dir);
225
+ const insights = [];
226
+
227
+ // Pattern 1: Persistent failures
228
+ if (history.length >= 3) {
229
+ const recentFailKeys = new Map();
230
+ for (const entry of history.slice(0, 5)) {
231
+ for (const key of (entry.summary?.topActionKeys || [])) {
232
+ recentFailKeys.set(key, (recentFailKeys.get(key) || 0) + 1);
233
+ }
234
+ }
235
+ for (const [key, count] of recentFailKeys) {
236
+ if (count >= 3) {
237
+ insights.push({
238
+ type: 'persistent-failure',
239
+ severity: 'high',
240
+ key,
241
+ message: `Check ${key} has failed in ${count} of the last ${Math.min(history.length, 5)} audits. Consider addressing it or marking it as intentionally skipped.`,
242
+ });
243
+ }
244
+ }
245
+ }
246
+
247
+ // Pattern 2: Score regression
248
+ if (history.length >= 2) {
249
+ const scores = history.map(e => e.summary?.score ?? 0);
250
+ if (scores[0] < scores[1]) {
251
+ insights.push({
252
+ type: 'regression-pattern',
253
+ severity: 'medium',
254
+ message: `Score dropped from ${scores[1]} to ${scores[0]} in the most recent audit. Review recent changes.`,
255
+ delta: scores[0] - scores[1],
256
+ });
257
+ }
258
+ }
259
+
260
+ // Pattern 3: Improvement velocity stall
261
+ if (history.length >= 5) {
262
+ const recentScores = history.slice(0, 5).map(e => e.summary?.score ?? 0);
263
+ const range = Math.max(...recentScores) - Math.min(...recentScores);
264
+ if (range <= 2) {
265
+ insights.push({
266
+ type: 'velocity-stall',
267
+ severity: 'low',
268
+ message: `Score has been flat (range: ${range} points) over the last 5 audits. Consider addressing lower-priority checks.`,
269
+ });
270
+ }
271
+ }
272
+
273
+ // Pattern 4: Feedback signals
274
+ const feedbackEntries = Array.isArray(feedback) ? feedback : Object.values(feedback || {});
275
+ for (const entry of feedbackEntries) {
276
+ if (entry.rejected > entry.accepted && entry.total >= 2) {
277
+ insights.push({
278
+ type: 'feedback-signal',
279
+ severity: 'medium',
280
+ key: entry.key,
281
+ message: `Recommendation ${entry.key} has been rejected more than accepted (${entry.rejected}/${entry.total}). Consider adjusting or removing this recommendation.`,
282
+ });
283
+ }
284
+ }
285
+
286
+ return {
287
+ platform: 'codex',
288
+ generatedAt: new Date().toISOString(),
289
+ snapshotCount: history.length,
290
+ feedbackCount: feedbackEntries.length,
291
+ insights,
292
+ summary: insights.length === 0
293
+ ? 'No actionable insights detected. Keep running audits to build pattern data.'
294
+ : `${insights.length} insight(s) detected across ${history.length} snapshots.`,
295
+ };
296
+ }
297
+
298
+ function formatCodexInsights(dir) {
299
+ const result = generateCodexInsights(dir);
300
+ if (result.insights.length === 0) {
301
+ return result.summary;
302
+ }
303
+
304
+ const lines = ['Codex Insights:', ''];
305
+ for (const insight of result.insights) {
306
+ const severity = insight.severity.toUpperCase();
307
+ lines.push(` [${severity}] ${insight.message}`);
308
+ }
309
+ lines.push('');
310
+ lines.push(result.summary);
311
+ return lines.join('\n');
312
+ }
313
+
314
+ module.exports = {
315
+ getCodexHistory,
316
+ formatCodexHistory,
317
+ compareCodexLatest,
318
+ exportCodexTrendReport,
319
+ recordCodexFeedback,
320
+ getCodexFeedbackSummary,
321
+ formatCodexFeedback,
322
+ generateCodexInsights,
323
+ formatCodexInsights,
324
+ };