@nerviq/cli 1.29.0 → 1.30.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 (93) hide show
  1. package/CHANGELOG.md +1764 -1493
  2. package/README.md +568 -538
  3. package/SECURITY.md +78 -82
  4. package/bin/cli.js +2838 -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 +75 -67
  17. package/sdk/README.md +12 -3
  18. package/sdk/examples/langchain-integration.md +128 -0
  19. package/sdk/examples/self-governing-agent.js +135 -0
  20. package/sdk/index.d.ts +115 -0
  21. package/sdk/index.js +94 -0
  22. package/sdk/package.json +11 -0
  23. package/src/activity.js +13 -0
  24. package/src/aider/activity.js +226 -226
  25. package/src/aider/context.js +162 -162
  26. package/src/aider/freshness.js +123 -123
  27. package/src/aider/techniques.js +3465 -3465
  28. package/src/audit/layers.js +180 -180
  29. package/src/audit.js +1133 -1032
  30. package/src/auto-suggest.js +9 -2
  31. package/src/behavioral-drift.js +37 -2
  32. package/src/benchmark.js +299 -299
  33. package/src/codex/activity.js +324 -324
  34. package/src/codex/freshness.js +149 -142
  35. package/src/codex/techniques.js +4895 -4895
  36. package/src/context.js +326 -326
  37. package/src/continuous-ops.js +11 -1
  38. package/src/convert.js +340 -340
  39. package/src/copilot/config-parser.js +280 -280
  40. package/src/copilot/context.js +218 -218
  41. package/src/copilot/freshness.js +184 -177
  42. package/src/copilot/patch.js +238 -238
  43. package/src/copilot/techniques.js +3578 -3578
  44. package/src/cursor/freshness.js +194 -194
  45. package/src/cursor/patch.js +243 -243
  46. package/src/cursor/techniques.js +3735 -3735
  47. package/src/doctor.js +201 -201
  48. package/src/fix-engine.js +511 -8
  49. package/src/formatters/csv.js +86 -86
  50. package/src/formatters/junit.js +123 -123
  51. package/src/formatters/markdown.js +164 -164
  52. package/src/formatters/otel.js +151 -151
  53. package/src/freshness.js +163 -156
  54. package/src/gemini/activity.js +402 -402
  55. package/src/gemini/context.js +290 -290
  56. package/src/gemini/freshness.js +188 -188
  57. package/src/gemini/patch.js +229 -229
  58. package/src/gemini/techniques.js +3811 -3811
  59. package/src/governance.js +533 -533
  60. package/src/harmony/audit.js +306 -306
  61. package/src/i18n.js +63 -63
  62. package/src/insights.js +119 -119
  63. package/src/integrations.js +134 -134
  64. package/src/locales/en.json +33 -33
  65. package/src/locales/es.json +33 -33
  66. package/src/migrate.js +354 -354
  67. package/src/opencode/activity.js +286 -286
  68. package/src/opencode/freshness.js +137 -137
  69. package/src/opencode/techniques.js +3450 -3450
  70. package/src/safe-glyph.js +97 -0
  71. package/src/setup/analysis.js +12 -12
  72. package/src/setup.js +13 -6
  73. package/src/shallow-risk/index.js +113 -56
  74. package/src/shallow-risk/patterns/agent-config-cross-platform-drift.js +51 -50
  75. package/src/shallow-risk/patterns/agent-config-dangerous-autoapprove.js +47 -46
  76. package/src/shallow-risk/patterns/agent-config-deprecated-keys.js +47 -46
  77. package/src/shallow-risk/patterns/agent-config-framework-version-mismatch.js +138 -0
  78. package/src/shallow-risk/patterns/agent-config-missing-file.js +318 -317
  79. package/src/shallow-risk/patterns/agent-config-script-not-in-package-json.js +108 -0
  80. package/src/shallow-risk/patterns/agent-config-secret-literal.js +52 -49
  81. package/src/shallow-risk/patterns/agent-config-stack-contradiction.js +35 -34
  82. package/src/shallow-risk/patterns/hook-script-missing.js +71 -70
  83. package/src/shallow-risk/patterns/mcp-server-no-allowlist.js +53 -52
  84. package/src/shallow-risk/shared.js +653 -648
  85. package/src/source-urls.js +295 -295
  86. package/src/state-paths.js +85 -85
  87. package/src/supplemental-checks.js +805 -805
  88. package/src/telemetry.js +160 -160
  89. package/src/watch.js +46 -0
  90. package/src/windsurf/context.js +359 -359
  91. package/src/windsurf/freshness.js +194 -194
  92. package/src/windsurf/patch.js +231 -231
  93. package/src/windsurf/techniques.js +3779 -3779
package/src/migrate.js CHANGED
@@ -1,354 +1,354 @@
1
- /**
2
- * Nerviq Migrate
3
- *
4
- * Helps migrate configuration files from one version/format of a platform
5
- * to a newer version, applying safe in-place transformations.
6
- *
7
- * Supports:
8
- * cursor v2 → v3 (legacy .cursorrules → .cursor/rules/*.mdc)
9
- * codex v1 → v2 (AGENTS.md additions, .codex/ bootstrap)
10
- * aider v0 → v1 (basic .aider.conf.yml creation from CLI pattern)
11
- * gemini v1 → v2 (.gemini/ settings.json schema update)
12
- */
13
-
14
- 'use strict';
15
-
16
- const fs = require('fs');
17
- const path = require('path');
18
-
19
- const COLORS = {
20
- reset: '\x1b[0m',
21
- bold: '\x1b[1m',
22
- dim: '\x1b[2m',
23
- red: '\x1b[31m',
24
- green: '\x1b[32m',
25
- yellow: '\x1b[33m',
26
- blue: '\x1b[36m',
27
- };
28
-
29
- function c(text, color) {
30
- return `${COLORS[color] || ''}${text}${COLORS.reset}`;
31
- }
32
-
33
- // ─── Migration definitions ───────────────────────────────────────────────────
34
-
35
- const MIGRATIONS = {
36
- cursor: {
37
- 'v2→v3': {
38
- description: 'Migrate legacy .cursorrules to .cursor/rules/*.mdc with proper frontmatter',
39
- from: 'v2',
40
- to: 'v3',
41
- detect: (dir) => {
42
- const hasLegacy = fs.existsSync(path.join(dir, '.cursorrules'));
43
- const hasMdc = fs.existsSync(path.join(dir, '.cursor', 'rules'));
44
- return { applicable: hasLegacy, notes: hasLegacy ? (hasMdc ? 'Legacy .cursorrules present alongside .cursor/rules/ (coexistence)' : 'Legacy .cursorrules present, no .cursor/rules/ yet') : 'No legacy .cursorrules found' };
45
- },
46
- apply: (dir, { dryRun = false } = {}) => {
47
- const legacyPath = path.join(dir, '.cursorrules');
48
- if (!fs.existsSync(legacyPath)) return { changes: [], warnings: ['No .cursorrules found'] };
49
-
50
- const content = fs.readFileSync(legacyPath, 'utf8').trim();
51
- const targetDir = path.join(dir, '.cursor', 'rules');
52
- const targetFile = path.join(targetDir, 'core.mdc');
53
-
54
- const mdcContent = [
55
- '---',
56
- 'alwaysApply: true',
57
- '---',
58
- '',
59
- content,
60
- '',
61
- ].join('\n');
62
-
63
- const changes = [];
64
- const warnings = [];
65
-
66
- if (fs.existsSync(targetFile)) {
67
- warnings.push('.cursor/rules/core.mdc already exists — skipped to avoid overwrite');
68
- } else {
69
- if (!dryRun) {
70
- fs.mkdirSync(targetDir, { recursive: true });
71
- fs.writeFileSync(targetFile, mdcContent, 'utf8');
72
- }
73
- changes.push({ action: dryRun ? 'would-create' : 'created', file: '.cursor/rules/core.mdc', detail: 'Converted from .cursorrules with alwaysApply: true' });
74
- }
75
-
76
- // Optionally rename .cursorrules → .cursorrules.migrated
77
- const migratedPath = path.join(dir, '.cursorrules.migrated');
78
- if (!fs.existsSync(migratedPath) && !dryRun) {
79
- fs.renameSync(legacyPath, migratedPath);
80
- changes.push({ action: 'renamed', file: '.cursorrules → .cursorrules.migrated', detail: 'Preserved as backup' });
81
- } else if (dryRun) {
82
- changes.push({ action: 'would-rename', file: '.cursorrules → .cursorrules.migrated', detail: 'Backup rename' });
83
- }
84
-
85
- return { changes, warnings };
86
- },
87
- },
88
- },
89
-
90
- codex: {
91
- 'v1→v2': {
92
- description: 'Bootstrap .codex/ directory with config.toml and update AGENTS.md',
93
- from: 'v1',
94
- to: 'v2',
95
- detect: (dir) => {
96
- const hasAgents = fs.existsSync(path.join(dir, 'AGENTS.md'));
97
- const hasCodexDir = fs.existsSync(path.join(dir, '.codex'));
98
- return {
99
- applicable: hasAgents && !hasCodexDir,
100
- notes: !hasAgents
101
- ? 'No AGENTS.md found'
102
- : hasCodexDir
103
- ? '.codex/ already exists'
104
- : 'AGENTS.md found, .codex/ missing — upgrade applicable',
105
- };
106
- },
107
- apply: (dir, { dryRun = false } = {}) => {
108
- const changes = [];
109
- const warnings = [];
110
- const codexDir = path.join(dir, '.codex');
111
-
112
- if (!fs.existsSync(codexDir)) {
113
- const configToml = [
114
- '# Codex configuration — generated by nerviq migrate',
115
- '[agent]',
116
- 'auto_approve = false',
117
- '',
118
- '[context]',
119
- 'files = ["AGENTS.md"]',
120
- '',
121
- ].join('\n');
122
-
123
- if (!dryRun) {
124
- fs.mkdirSync(codexDir, { recursive: true });
125
- fs.writeFileSync(path.join(codexDir, 'config.toml'), configToml, 'utf8');
126
- }
127
- changes.push({ action: dryRun ? 'would-create' : 'created', file: '.codex/config.toml', detail: 'Codex config with safe defaults' });
128
- } else {
129
- warnings.push('.codex/ already exists');
130
- }
131
-
132
- return { changes, warnings };
133
- },
134
- },
135
- },
136
-
137
- aider: {
138
- 'v0→v1': {
139
- description: 'Create .aider.conf.yml if missing and add .aider* to .gitignore',
140
- from: 'v0',
141
- to: 'v1',
142
- detect: (dir) => {
143
- const hasConf = fs.existsSync(path.join(dir, '.aider.conf.yml'));
144
- return {
145
- applicable: !hasConf,
146
- notes: hasConf ? '.aider.conf.yml already exists' : 'No .aider.conf.yml found — migration applicable',
147
- };
148
- },
149
- apply: (dir, { dryRun = false } = {}) => {
150
- const changes = [];
151
- const warnings = [];
152
-
153
- const confPath = path.join(dir, '.aider.conf.yml');
154
- if (!fs.existsSync(confPath)) {
155
- const conf = [
156
- '# Aider configuration — generated by nerviq migrate',
157
- 'auto-commits: true',
158
- 'auto-lint: false',
159
- 'auto-test: false',
160
- '# model: gpt-4o',
161
- '# weak-model: gpt-4o-mini',
162
- '# edit-format: diff',
163
- ].join('\n') + '\n';
164
- if (!dryRun) fs.writeFileSync(confPath, conf, 'utf8');
165
- changes.push({ action: dryRun ? 'would-create' : 'created', file: '.aider.conf.yml', detail: 'Safe defaults with auto-commits enabled' });
166
- }
167
-
168
- const gitignorePath = path.join(dir, '.gitignore');
169
- if (fs.existsSync(gitignorePath)) {
170
- const gi = fs.readFileSync(gitignorePath, 'utf8');
171
- if (!/\.aider/i.test(gi)) {
172
- if (!dryRun) fs.appendFileSync(gitignorePath, '\n# Aider artifacts\n.aider*\n');
173
- changes.push({ action: dryRun ? 'would-append' : 'appended', file: '.gitignore', detail: 'Added .aider* pattern' });
174
- } else {
175
- warnings.push('.gitignore already has .aider* pattern');
176
- }
177
- }
178
-
179
- return { changes, warnings };
180
- },
181
- },
182
- },
183
-
184
- gemini: {
185
- 'v1→v2': {
186
- description: 'Update .gemini/settings.json schema (model field: string → object)',
187
- from: 'v1',
188
- to: 'v2',
189
- detect: (dir) => {
190
- const settingsPath = path.join(dir, '.gemini', 'settings.json');
191
- if (!fs.existsSync(settingsPath)) {
192
- return { applicable: false, notes: 'No .gemini/settings.json found' };
193
- }
194
- try {
195
- const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
196
- const needsMigration = typeof settings.model === 'string';
197
- return {
198
- applicable: needsMigration,
199
- notes: needsMigration
200
- ? `model field is a string "${settings.model}" — needs to be an object`
201
- : 'model field already uses object format',
202
- };
203
- } catch {
204
- return { applicable: false, notes: 'settings.json is invalid JSON' };
205
- }
206
- },
207
- apply: (dir, { dryRun = false } = {}) => {
208
- const changes = [];
209
- const warnings = [];
210
- const settingsPath = path.join(dir, '.gemini', 'settings.json');
211
-
212
- if (!fs.existsSync(settingsPath)) {
213
- warnings.push('.gemini/settings.json not found');
214
- return { changes, warnings };
215
- }
216
-
217
- let settings;
218
- try {
219
- settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
220
- } catch {
221
- warnings.push('settings.json is invalid JSON — skipped');
222
- return { changes, warnings };
223
- }
224
-
225
- if (typeof settings.model === 'string') {
226
- const oldModel = settings.model;
227
- settings.model = { name: oldModel };
228
- if (!dryRun) fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf8');
229
- changes.push({
230
- action: dryRun ? 'would-update' : 'updated',
231
- file: '.gemini/settings.json',
232
- detail: `model: "${oldModel}" → { name: "${oldModel}" }`,
233
- });
234
- } else {
235
- warnings.push('model field already in correct format');
236
- }
237
-
238
- return { changes, warnings };
239
- },
240
- },
241
- },
242
- };
243
-
244
- // ─── Main migrate function ────────────────────────────────────────────────────
245
-
246
- async function runMigrate({ dir = process.cwd(), platform, from: fromVersion, to: toVersion, dryRun = false, json = false } = {}) {
247
- if (!platform) throw new Error('--platform is required. Example: nerviq migrate --platform cursor --from v2 --to v3');
248
-
249
- const platformMigrations = MIGRATIONS[platform];
250
- if (!platformMigrations) {
251
- throw new Error(`No migrations available for platform '${platform}'. Supported: ${Object.keys(MIGRATIONS).join(', ')}.\nUsage: nerviq migrate --platform <platform> [--from <version> --to <version>]`);
252
- }
253
-
254
- // Determine migration key
255
- let migrationKey = null;
256
- if (fromVersion && toVersion) {
257
- migrationKey = `${fromVersion}→${toVersion}`;
258
- } else {
259
- // Auto-detect applicable migration
260
- for (const [key, migration] of Object.entries(platformMigrations)) {
261
- const detection = migration.detect(dir);
262
- if (detection.applicable) {
263
- migrationKey = key;
264
- break;
265
- }
266
- }
267
- }
268
-
269
- if (!migrationKey) {
270
- // Show available migrations
271
- const availableKeys = Object.keys(platformMigrations);
272
- const detectionResults = availableKeys.map(key => {
273
- const detection = platformMigrations[key].detect(dir);
274
- return { key, ...detection };
275
- });
276
-
277
- if (json) {
278
- return JSON.stringify({ platform, status: 'no-migration-applicable', detectionResults }, null, 2);
279
- }
280
-
281
- const lines = [''];
282
- lines.push(c(` nerviq migrate — ${platform}`, 'bold'));
283
- lines.push('');
284
- lines.push(c(' No applicable migration found.', 'yellow'));
285
- lines.push('');
286
- lines.push(' Available migrations:');
287
- for (const r of detectionResults) {
288
- const icon = r.applicable ? c('✓', 'green') : c('-', 'dim');
289
- lines.push(` ${icon} ${r.key.padEnd(10)} ${r.notes}`);
290
- }
291
- lines.push('');
292
- return lines.join('\n');
293
- }
294
-
295
- const migration = platformMigrations[migrationKey];
296
- if (!migration) {
297
- throw new Error(`Migration '${migrationKey}' not found for platform '${platform}'`);
298
- }
299
-
300
- const detection = migration.detect(dir);
301
- const { changes, warnings } = migration.apply(dir, { dryRun });
302
-
303
- const result = {
304
- platform,
305
- migration: migrationKey,
306
- description: migration.description,
307
- dryRun,
308
- applicable: detection.applicable,
309
- detectionNotes: detection.notes,
310
- changes,
311
- warnings,
312
- };
313
-
314
- if (json) return JSON.stringify(result, null, 2);
315
-
316
- const lines = [''];
317
- lines.push(c(` nerviq migrate ${platform} ${migrationKey}`, 'bold'));
318
- lines.push(c(' ═══════════════════════════════════════', 'dim'));
319
- lines.push('');
320
- lines.push(` ${migration.description}`);
321
- lines.push(` Directory: ${dir}`);
322
- lines.push('');
323
-
324
- if (dryRun) lines.push(c(' Dry run — no files modified', 'yellow'));
325
-
326
- if (changes.length > 0) {
327
- lines.push(' Changes:');
328
- for (const ch of changes) {
329
- const icon = ch.action.startsWith('would') ? c('→', 'blue') : c('✓', 'green');
330
- lines.push(` ${icon} [${ch.action}] ${ch.file}`);
331
- if (ch.detail) lines.push(c(` ${ch.detail}`, 'dim'));
332
- }
333
- } else {
334
- lines.push(c(' No changes made.', 'dim'));
335
- }
336
-
337
- if (warnings.length > 0) {
338
- lines.push('');
339
- lines.push(' Warnings:');
340
- for (const w of warnings) lines.push(` ${c('⚠', 'yellow')} ${w}`);
341
- }
342
-
343
- lines.push('');
344
- if (!dryRun && changes.length > 0) {
345
- lines.push(c(` ✓ Migration complete. Run \`nerviq audit --platform ${platform}\` to verify.`, 'green'));
346
- } else if (dryRun) {
347
- lines.push(c(' Run without --dry-run to apply changes.', 'dim'));
348
- }
349
- lines.push('');
350
-
351
- return lines.join('\n');
352
- }
353
-
354
- module.exports = { runMigrate, MIGRATIONS };
1
+ /**
2
+ * Nerviq Migrate
3
+ *
4
+ * Helps migrate configuration files from one version/format of a platform
5
+ * to a newer version, applying safe in-place transformations.
6
+ *
7
+ * Supports:
8
+ * cursor v2 → v3 (legacy .cursorrules → .cursor/rules/*.mdc)
9
+ * codex v1 → v2 (AGENTS.md additions, .codex/ bootstrap)
10
+ * aider v0 → v1 (basic .aider.conf.yml creation from CLI pattern)
11
+ * gemini v1 → v2 (.gemini/ settings.json schema update)
12
+ */
13
+
14
+ 'use strict';
15
+
16
+ const fs = require('fs');
17
+ const path = require('path');
18
+
19
+ const COLORS = {
20
+ reset: '\x1b[0m',
21
+ bold: '\x1b[1m',
22
+ dim: '\x1b[2m',
23
+ red: '\x1b[31m',
24
+ green: '\x1b[32m',
25
+ yellow: '\x1b[33m',
26
+ blue: '\x1b[36m',
27
+ };
28
+
29
+ function c(text, color) {
30
+ return `${COLORS[color] || ''}${text}${COLORS.reset}`;
31
+ }
32
+
33
+ // ─── Migration definitions ───────────────────────────────────────────────────
34
+
35
+ const MIGRATIONS = {
36
+ cursor: {
37
+ 'v2→v3': {
38
+ description: 'Migrate legacy .cursorrules to .cursor/rules/*.mdc with proper frontmatter',
39
+ from: 'v2',
40
+ to: 'v3',
41
+ detect: (dir) => {
42
+ const hasLegacy = fs.existsSync(path.join(dir, '.cursorrules'));
43
+ const hasMdc = fs.existsSync(path.join(dir, '.cursor', 'rules'));
44
+ return { applicable: hasLegacy, notes: hasLegacy ? (hasMdc ? 'Legacy .cursorrules present alongside .cursor/rules/ (coexistence)' : 'Legacy .cursorrules present, no .cursor/rules/ yet') : 'No legacy .cursorrules found' };
45
+ },
46
+ apply: (dir, { dryRun = false } = {}) => {
47
+ const legacyPath = path.join(dir, '.cursorrules');
48
+ if (!fs.existsSync(legacyPath)) return { changes: [], warnings: ['No .cursorrules found'] };
49
+
50
+ const content = fs.readFileSync(legacyPath, 'utf8').trim();
51
+ const targetDir = path.join(dir, '.cursor', 'rules');
52
+ const targetFile = path.join(targetDir, 'core.mdc');
53
+
54
+ const mdcContent = [
55
+ '---',
56
+ 'alwaysApply: true',
57
+ '---',
58
+ '',
59
+ content,
60
+ '',
61
+ ].join('\n');
62
+
63
+ const changes = [];
64
+ const warnings = [];
65
+
66
+ if (fs.existsSync(targetFile)) {
67
+ warnings.push('.cursor/rules/core.mdc already exists — skipped to avoid overwrite');
68
+ } else {
69
+ if (!dryRun) {
70
+ fs.mkdirSync(targetDir, { recursive: true });
71
+ fs.writeFileSync(targetFile, mdcContent, 'utf8');
72
+ }
73
+ changes.push({ action: dryRun ? 'would-create' : 'created', file: '.cursor/rules/core.mdc', detail: 'Converted from .cursorrules with alwaysApply: true' });
74
+ }
75
+
76
+ // Optionally rename .cursorrules → .cursorrules.migrated
77
+ const migratedPath = path.join(dir, '.cursorrules.migrated');
78
+ if (!fs.existsSync(migratedPath) && !dryRun) {
79
+ fs.renameSync(legacyPath, migratedPath);
80
+ changes.push({ action: 'renamed', file: '.cursorrules → .cursorrules.migrated', detail: 'Preserved as backup' });
81
+ } else if (dryRun) {
82
+ changes.push({ action: 'would-rename', file: '.cursorrules → .cursorrules.migrated', detail: 'Backup rename' });
83
+ }
84
+
85
+ return { changes, warnings };
86
+ },
87
+ },
88
+ },
89
+
90
+ codex: {
91
+ 'v1→v2': {
92
+ description: 'Bootstrap .codex/ directory with config.toml and update AGENTS.md',
93
+ from: 'v1',
94
+ to: 'v2',
95
+ detect: (dir) => {
96
+ const hasAgents = fs.existsSync(path.join(dir, 'AGENTS.md'));
97
+ const hasCodexDir = fs.existsSync(path.join(dir, '.codex'));
98
+ return {
99
+ applicable: hasAgents && !hasCodexDir,
100
+ notes: !hasAgents
101
+ ? 'No AGENTS.md found'
102
+ : hasCodexDir
103
+ ? '.codex/ already exists'
104
+ : 'AGENTS.md found, .codex/ missing — upgrade applicable',
105
+ };
106
+ },
107
+ apply: (dir, { dryRun = false } = {}) => {
108
+ const changes = [];
109
+ const warnings = [];
110
+ const codexDir = path.join(dir, '.codex');
111
+
112
+ if (!fs.existsSync(codexDir)) {
113
+ const configToml = [
114
+ '# Codex configuration — generated by nerviq migrate',
115
+ '[agent]',
116
+ 'auto_approve = false',
117
+ '',
118
+ '[context]',
119
+ 'files = ["AGENTS.md"]',
120
+ '',
121
+ ].join('\n');
122
+
123
+ if (!dryRun) {
124
+ fs.mkdirSync(codexDir, { recursive: true });
125
+ fs.writeFileSync(path.join(codexDir, 'config.toml'), configToml, 'utf8');
126
+ }
127
+ changes.push({ action: dryRun ? 'would-create' : 'created', file: '.codex/config.toml', detail: 'Codex config with safe defaults' });
128
+ } else {
129
+ warnings.push('.codex/ already exists');
130
+ }
131
+
132
+ return { changes, warnings };
133
+ },
134
+ },
135
+ },
136
+
137
+ aider: {
138
+ 'v0→v1': {
139
+ description: 'Create .aider.conf.yml if missing and add .aider* to .gitignore',
140
+ from: 'v0',
141
+ to: 'v1',
142
+ detect: (dir) => {
143
+ const hasConf = fs.existsSync(path.join(dir, '.aider.conf.yml'));
144
+ return {
145
+ applicable: !hasConf,
146
+ notes: hasConf ? '.aider.conf.yml already exists' : 'No .aider.conf.yml found — migration applicable',
147
+ };
148
+ },
149
+ apply: (dir, { dryRun = false } = {}) => {
150
+ const changes = [];
151
+ const warnings = [];
152
+
153
+ const confPath = path.join(dir, '.aider.conf.yml');
154
+ if (!fs.existsSync(confPath)) {
155
+ const conf = [
156
+ '# Aider configuration — generated by nerviq migrate',
157
+ 'auto-commits: true',
158
+ 'auto-lint: false',
159
+ 'auto-test: false',
160
+ '# model: gpt-4o',
161
+ '# weak-model: gpt-4o-mini',
162
+ '# edit-format: diff',
163
+ ].join('\n') + '\n';
164
+ if (!dryRun) fs.writeFileSync(confPath, conf, 'utf8');
165
+ changes.push({ action: dryRun ? 'would-create' : 'created', file: '.aider.conf.yml', detail: 'Safe defaults with auto-commits enabled' });
166
+ }
167
+
168
+ const gitignorePath = path.join(dir, '.gitignore');
169
+ if (fs.existsSync(gitignorePath)) {
170
+ const gi = fs.readFileSync(gitignorePath, 'utf8');
171
+ if (!/\.aider/i.test(gi)) {
172
+ if (!dryRun) fs.appendFileSync(gitignorePath, '\n# Aider artifacts\n.aider*\n');
173
+ changes.push({ action: dryRun ? 'would-append' : 'appended', file: '.gitignore', detail: 'Added .aider* pattern' });
174
+ } else {
175
+ warnings.push('.gitignore already has .aider* pattern');
176
+ }
177
+ }
178
+
179
+ return { changes, warnings };
180
+ },
181
+ },
182
+ },
183
+
184
+ gemini: {
185
+ 'v1→v2': {
186
+ description: 'Update .gemini/settings.json schema (model field: string → object)',
187
+ from: 'v1',
188
+ to: 'v2',
189
+ detect: (dir) => {
190
+ const settingsPath = path.join(dir, '.gemini', 'settings.json');
191
+ if (!fs.existsSync(settingsPath)) {
192
+ return { applicable: false, notes: 'No .gemini/settings.json found' };
193
+ }
194
+ try {
195
+ const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
196
+ const needsMigration = typeof settings.model === 'string';
197
+ return {
198
+ applicable: needsMigration,
199
+ notes: needsMigration
200
+ ? `model field is a string "${settings.model}" — needs to be an object`
201
+ : 'model field already uses object format',
202
+ };
203
+ } catch {
204
+ return { applicable: false, notes: 'settings.json is invalid JSON' };
205
+ }
206
+ },
207
+ apply: (dir, { dryRun = false } = {}) => {
208
+ const changes = [];
209
+ const warnings = [];
210
+ const settingsPath = path.join(dir, '.gemini', 'settings.json');
211
+
212
+ if (!fs.existsSync(settingsPath)) {
213
+ warnings.push('.gemini/settings.json not found');
214
+ return { changes, warnings };
215
+ }
216
+
217
+ let settings;
218
+ try {
219
+ settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
220
+ } catch {
221
+ warnings.push('settings.json is invalid JSON — skipped');
222
+ return { changes, warnings };
223
+ }
224
+
225
+ if (typeof settings.model === 'string') {
226
+ const oldModel = settings.model;
227
+ settings.model = { name: oldModel };
228
+ if (!dryRun) fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf8');
229
+ changes.push({
230
+ action: dryRun ? 'would-update' : 'updated',
231
+ file: '.gemini/settings.json',
232
+ detail: `model: "${oldModel}" → { name: "${oldModel}" }`,
233
+ });
234
+ } else {
235
+ warnings.push('model field already in correct format');
236
+ }
237
+
238
+ return { changes, warnings };
239
+ },
240
+ },
241
+ },
242
+ };
243
+
244
+ // ─── Main migrate function ────────────────────────────────────────────────────
245
+
246
+ async function runMigrate({ dir = process.cwd(), platform, from: fromVersion, to: toVersion, dryRun = false, json = false } = {}) {
247
+ if (!platform) throw new Error('--platform is required. Example: nerviq migrate --platform cursor --from v2 --to v3');
248
+
249
+ const platformMigrations = MIGRATIONS[platform];
250
+ if (!platformMigrations) {
251
+ throw new Error(`No migrations available for platform '${platform}'. Supported: ${Object.keys(MIGRATIONS).join(', ')}.\nUsage: nerviq migrate --platform <platform> [--from <version> --to <version>]`);
252
+ }
253
+
254
+ // Determine migration key
255
+ let migrationKey = null;
256
+ if (fromVersion && toVersion) {
257
+ migrationKey = `${fromVersion}→${toVersion}`;
258
+ } else {
259
+ // Auto-detect applicable migration
260
+ for (const [key, migration] of Object.entries(platformMigrations)) {
261
+ const detection = migration.detect(dir);
262
+ if (detection.applicable) {
263
+ migrationKey = key;
264
+ break;
265
+ }
266
+ }
267
+ }
268
+
269
+ if (!migrationKey) {
270
+ // Show available migrations
271
+ const availableKeys = Object.keys(platformMigrations);
272
+ const detectionResults = availableKeys.map(key => {
273
+ const detection = platformMigrations[key].detect(dir);
274
+ return { key, ...detection };
275
+ });
276
+
277
+ if (json) {
278
+ return JSON.stringify({ platform, status: 'no-migration-applicable', detectionResults }, null, 2);
279
+ }
280
+
281
+ const lines = [''];
282
+ lines.push(c(` nerviq migrate — ${platform}`, 'bold'));
283
+ lines.push('');
284
+ lines.push(c(' No applicable migration found.', 'yellow'));
285
+ lines.push('');
286
+ lines.push(' Available migrations:');
287
+ for (const r of detectionResults) {
288
+ const icon = r.applicable ? c('✓', 'green') : c('-', 'dim');
289
+ lines.push(` ${icon} ${r.key.padEnd(10)} ${r.notes}`);
290
+ }
291
+ lines.push('');
292
+ return lines.join('\n');
293
+ }
294
+
295
+ const migration = platformMigrations[migrationKey];
296
+ if (!migration) {
297
+ throw new Error(`Migration '${migrationKey}' not found for platform '${platform}'`);
298
+ }
299
+
300
+ const detection = migration.detect(dir);
301
+ const { changes, warnings } = migration.apply(dir, { dryRun });
302
+
303
+ const result = {
304
+ platform,
305
+ migration: migrationKey,
306
+ description: migration.description,
307
+ dryRun,
308
+ applicable: detection.applicable,
309
+ detectionNotes: detection.notes,
310
+ changes,
311
+ warnings,
312
+ };
313
+
314
+ if (json) return JSON.stringify(result, null, 2);
315
+
316
+ const lines = [''];
317
+ lines.push(c(` nerviq migrate ${platform} ${migrationKey}`, 'bold'));
318
+ lines.push(c(' ═══════════════════════════════════════', 'dim'));
319
+ lines.push('');
320
+ lines.push(` ${migration.description}`);
321
+ lines.push(` Directory: ${dir}`);
322
+ lines.push('');
323
+
324
+ if (dryRun) lines.push(c(' Dry run — no files modified', 'yellow'));
325
+
326
+ if (changes.length > 0) {
327
+ lines.push(' Changes:');
328
+ for (const ch of changes) {
329
+ const icon = ch.action.startsWith('would') ? c('→', 'blue') : c('✓', 'green');
330
+ lines.push(` ${icon} [${ch.action}] ${ch.file}`);
331
+ if (ch.detail) lines.push(c(` ${ch.detail}`, 'dim'));
332
+ }
333
+ } else {
334
+ lines.push(c(' No changes made.', 'dim'));
335
+ }
336
+
337
+ if (warnings.length > 0) {
338
+ lines.push('');
339
+ lines.push(' Warnings:');
340
+ for (const w of warnings) lines.push(` ${c('⚠', 'yellow')} ${w}`);
341
+ }
342
+
343
+ lines.push('');
344
+ if (!dryRun && changes.length > 0) {
345
+ lines.push(c(` ✓ Migration complete. Run \`nerviq audit --platform ${platform}\` to verify.`, 'green'));
346
+ } else if (dryRun) {
347
+ lines.push(c(' Run without --dry-run to apply changes.', 'dim'));
348
+ }
349
+ lines.push('');
350
+
351
+ return lines.join('\n');
352
+ }
353
+
354
+ module.exports = { runMigrate, MIGRATIONS };