@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.
- package/CHANGELOG.md +1527 -1493
- package/README.md +550 -538
- package/SECURITY.md +82 -82
- package/bin/cli.js +2562 -2558
- package/docs/api-reference.md +356 -356
- package/docs/audit-fix.md +109 -0
- package/docs/autofix.md +3 -62
- package/docs/getting-started.md +1 -1
- package/docs/index.html +592 -592
- package/docs/integration-contracts.md +287 -287
- package/docs/maintenance.md +128 -128
- package/docs/new-platform-guide.md +202 -202
- package/docs/release-process.md +63 -0
- package/docs/shallow-risk.md +244 -244
- package/docs/why-nerviq.md +82 -82
- package/package.json +67 -67
- package/src/aider/activity.js +226 -226
- package/src/aider/context.js +162 -162
- package/src/aider/freshness.js +123 -123
- package/src/aider/techniques.js +3465 -3465
- package/src/audit/layers.js +180 -180
- package/src/audit.js +1032 -1032
- package/src/benchmark.js +299 -299
- package/src/codex/activity.js +324 -324
- package/src/codex/freshness.js +142 -142
- package/src/codex/techniques.js +4895 -4895
- package/src/context.js +326 -326
- package/src/continuous-ops.js +11 -1
- package/src/convert.js +340 -340
- package/src/copilot/config-parser.js +280 -280
- package/src/copilot/context.js +218 -218
- package/src/copilot/freshness.js +177 -177
- package/src/copilot/patch.js +238 -238
- package/src/copilot/techniques.js +3578 -3578
- package/src/cursor/freshness.js +194 -194
- package/src/cursor/patch.js +243 -243
- package/src/cursor/techniques.js +3735 -3735
- package/src/doctor.js +201 -201
- package/src/fix-engine.js +511 -8
- package/src/formatters/csv.js +86 -86
- package/src/formatters/junit.js +123 -123
- package/src/formatters/markdown.js +164 -164
- package/src/formatters/otel.js +151 -151
- package/src/freshness.js +156 -156
- package/src/gemini/activity.js +402 -402
- package/src/gemini/context.js +290 -290
- package/src/gemini/freshness.js +183 -183
- package/src/gemini/patch.js +229 -229
- package/src/gemini/techniques.js +3811 -3811
- package/src/governance.js +533 -533
- package/src/harmony/audit.js +306 -306
- package/src/i18n.js +63 -63
- package/src/insights.js +119 -119
- package/src/integrations.js +134 -134
- package/src/locales/en.json +33 -33
- package/src/locales/es.json +33 -33
- package/src/migrate.js +354 -354
- package/src/opencode/activity.js +286 -286
- package/src/opencode/freshness.js +137 -137
- package/src/opencode/techniques.js +3450 -3450
- package/src/setup/analysis.js +12 -12
- package/src/setup.js +7 -6
- package/src/shallow-risk/index.js +56 -56
- package/src/shallow-risk/patterns/agent-config-cross-platform-drift.js +50 -50
- package/src/shallow-risk/patterns/agent-config-dangerous-autoapprove.js +46 -46
- package/src/shallow-risk/patterns/agent-config-deprecated-keys.js +46 -46
- package/src/shallow-risk/patterns/agent-config-missing-file.js +317 -317
- package/src/shallow-risk/patterns/agent-config-secret-literal.js +49 -49
- package/src/shallow-risk/patterns/agent-config-stack-contradiction.js +34 -34
- package/src/shallow-risk/patterns/hook-script-missing.js +70 -70
- package/src/shallow-risk/patterns/mcp-server-no-allowlist.js +52 -52
- package/src/shallow-risk/shared.js +648 -648
- package/src/source-urls.js +295 -295
- package/src/state-paths.js +85 -85
- package/src/supplemental-checks.js +805 -805
- package/src/telemetry.js +160 -160
- package/src/windsurf/context.js +359 -359
- package/src/windsurf/freshness.js +194 -194
- package/src/windsurf/patch.js +231 -231
- package/src/windsurf/techniques.js +3779 -3779
package/src/cursor/patch.js
CHANGED
|
@@ -1,243 +1,243 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cursor Patch Intelligence
|
|
3
|
-
*
|
|
4
|
-
* Safe patching of existing Cursor files using managed blocks.
|
|
5
|
-
* Supports .cursor/rules/*.mdc (MDC comment blocks) and
|
|
6
|
-
* .cursor/mcp.json + .cursor/environment.json (JSON merge).
|
|
7
|
-
*
|
|
8
|
-
* Managed blocks are sections that nerviq controls.
|
|
9
|
-
* Hand-authored content outside managed blocks is preserved.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const fs = require('fs');
|
|
13
|
-
const path = require('path');
|
|
14
|
-
const { writeRollbackArtifact, writeActivityArtifact } = require('../activity');
|
|
15
|
-
|
|
16
|
-
// Managed block markers for MDC files (HTML comments work in Markdown body)
|
|
17
|
-
const MANAGED_START_MDC = '<!-- nerviq:managed:start -->';
|
|
18
|
-
const MANAGED_END_MDC = '<!-- nerviq:managed:end -->';
|
|
19
|
-
const MANAGED_JSON_KEY = '_nerviq_managed';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Extract managed blocks from a file.
|
|
23
|
-
*/
|
|
24
|
-
function extractManagedBlock(content, startMarker, endMarker) {
|
|
25
|
-
const startIdx = content.indexOf(startMarker);
|
|
26
|
-
const endIdx = content.indexOf(endMarker);
|
|
27
|
-
|
|
28
|
-
if (startIdx === -1 || endIdx === -1 || endIdx <= startIdx) {
|
|
29
|
-
return { before: content, managed: null, after: '' };
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
before: content.substring(0, startIdx),
|
|
34
|
-
managed: content.substring(startIdx + startMarker.length, endIdx).trim(),
|
|
35
|
-
after: content.substring(endIdx + endMarker.length),
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Replace or insert a managed block in a file.
|
|
41
|
-
*/
|
|
42
|
-
function upsertManagedBlock(content, newManaged, startMarker, endMarker) {
|
|
43
|
-
const { before, managed, after } = extractManagedBlock(content, startMarker, endMarker);
|
|
44
|
-
|
|
45
|
-
if (managed !== null) {
|
|
46
|
-
return `${before}${startMarker}\n${newManaged}\n${endMarker}${after}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const separator = content.endsWith('\n') ? '\n' : '\n\n';
|
|
50
|
-
return `${content}${separator}${startMarker}\n${newManaged}\n${endMarker}\n`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Patch .cursor/rules/*.mdc with managed sections.
|
|
55
|
-
* Preserves MDC frontmatter and hand-authored content.
|
|
56
|
-
*/
|
|
57
|
-
function patchCursorRuleMdc(existingContent, managedSections) {
|
|
58
|
-
const newManaged = Object.entries(managedSections)
|
|
59
|
-
.map(([section, content]) => `## ${section}\n${content}`)
|
|
60
|
-
.join('\n\n');
|
|
61
|
-
|
|
62
|
-
return upsertManagedBlock(existingContent, newManaged, MANAGED_START_MDC, MANAGED_END_MDC);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Patch .cursor/mcp.json by safely merging new servers.
|
|
67
|
-
* Cursor MCP uses the "mcpServers" wrapper format.
|
|
68
|
-
*/
|
|
69
|
-
function patchMcpJson(existingContent, newServers) {
|
|
70
|
-
let existing;
|
|
71
|
-
try {
|
|
72
|
-
existing = JSON.parse(existingContent);
|
|
73
|
-
} catch {
|
|
74
|
-
existing = {};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (!existing.mcpServers) existing.mcpServers = {};
|
|
78
|
-
|
|
79
|
-
const merged = { ...existing };
|
|
80
|
-
for (const [serverName, config] of Object.entries(newServers)) {
|
|
81
|
-
if (!(serverName in merged.mcpServers)) {
|
|
82
|
-
merged.mcpServers[serverName] = config;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (!merged[MANAGED_JSON_KEY]) merged[MANAGED_JSON_KEY] = {};
|
|
87
|
-
merged[MANAGED_JSON_KEY]._updatedAt = new Date().toISOString();
|
|
88
|
-
merged[MANAGED_JSON_KEY]._generator = nerviq;
|
|
89
|
-
merged[MANAGED_JSON_KEY]._platform = 'cursor';
|
|
90
|
-
|
|
91
|
-
return JSON.stringify(merged, null, 2) + '\n';
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Patch .cursor/environment.json by safely merging new fields.
|
|
96
|
-
*/
|
|
97
|
-
function patchEnvironmentJson(existingContent, newFields) {
|
|
98
|
-
let existing;
|
|
99
|
-
try {
|
|
100
|
-
existing = JSON.parse(existingContent);
|
|
101
|
-
} catch {
|
|
102
|
-
existing = {};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const merged = { ...existing };
|
|
106
|
-
|
|
107
|
-
for (const [key, value] of Object.entries(newFields)) {
|
|
108
|
-
if (key === MANAGED_JSON_KEY) {
|
|
109
|
-
merged[MANAGED_JSON_KEY] = { ...(existing[MANAGED_JSON_KEY] || {}), ...value };
|
|
110
|
-
} else if (!(key in existing)) {
|
|
111
|
-
merged[key] = value;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (!merged[MANAGED_JSON_KEY]) merged[MANAGED_JSON_KEY] = {};
|
|
116
|
-
merged[MANAGED_JSON_KEY]._updatedAt = new Date().toISOString();
|
|
117
|
-
merged[MANAGED_JSON_KEY]._generator = nerviq;
|
|
118
|
-
merged[MANAGED_JSON_KEY]._platform = 'cursor';
|
|
119
|
-
|
|
120
|
-
return JSON.stringify(merged, null, 2) + '\n';
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Detect if a repo has multiple agent surfaces (Cursor + Claude + Codex + Gemini + Copilot coexistence).
|
|
125
|
-
*/
|
|
126
|
-
function detectMixedAgentRepo(dir) {
|
|
127
|
-
const hasClaude = fs.existsSync(path.join(dir, 'CLAUDE.md')) || fs.existsSync(path.join(dir, '.claude'));
|
|
128
|
-
const hasCodex = fs.existsSync(path.join(dir, 'AGENTS.md')) || fs.existsSync(path.join(dir, '.codex'));
|
|
129
|
-
const hasGemini = fs.existsSync(path.join(dir, 'GEMINI.md')) || fs.existsSync(path.join(dir, '.gemini'));
|
|
130
|
-
const hasCopilot = fs.existsSync(path.join(dir, '.github', 'copilot-instructions.md')) ||
|
|
131
|
-
fs.existsSync(path.join(dir, '.vscode', 'mcp.json'));
|
|
132
|
-
const hasCursor = fs.existsSync(path.join(dir, '.cursor')) ||
|
|
133
|
-
fs.existsSync(path.join(dir, '.cursorrules'));
|
|
134
|
-
|
|
135
|
-
const platforms = [];
|
|
136
|
-
if (hasClaude) platforms.push('claude');
|
|
137
|
-
if (hasCodex) platforms.push('codex');
|
|
138
|
-
if (hasGemini) platforms.push('gemini');
|
|
139
|
-
if (hasCopilot) platforms.push('copilot');
|
|
140
|
-
if (hasCursor) platforms.push('cursor');
|
|
141
|
-
|
|
142
|
-
return {
|
|
143
|
-
isMixed: platforms.length >= 2,
|
|
144
|
-
hasClaude,
|
|
145
|
-
hasCodex,
|
|
146
|
-
hasGemini,
|
|
147
|
-
hasCopilot,
|
|
148
|
-
hasCursor,
|
|
149
|
-
platforms,
|
|
150
|
-
guidance: platforms.length >= 2
|
|
151
|
-
? `This is a mixed-agent repo (${platforms.join(', ')}). Keep each platform's config in its own directory (.claude/, .cursor/, .gemini/, .github/). Do not merge them.`
|
|
152
|
-
: null,
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Generate a diff preview for a patch operation.
|
|
158
|
-
*/
|
|
159
|
-
function generatePatchPreview(originalContent, patchedContent, filePath) {
|
|
160
|
-
const origLines = originalContent.split('\n');
|
|
161
|
-
const patchLines = patchedContent.split('\n');
|
|
162
|
-
const lines = [`--- ${filePath} (original)`, `+++ ${filePath} (patched)`];
|
|
163
|
-
|
|
164
|
-
let inChange = false;
|
|
165
|
-
for (let i = 0; i < Math.max(origLines.length, patchLines.length); i++) {
|
|
166
|
-
const orig = origLines[i] || '';
|
|
167
|
-
const patched = patchLines[i] || '';
|
|
168
|
-
if (orig !== patched) {
|
|
169
|
-
if (!inChange) { lines.push(`@@ line ${i + 1} @@`); inChange = true; }
|
|
170
|
-
if (i < origLines.length) lines.push(`-${orig}`);
|
|
171
|
-
if (i < patchLines.length) lines.push(`+${patched}`);
|
|
172
|
-
} else {
|
|
173
|
-
inChange = false;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return lines.join('\n');
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Apply a patch to a file with backup and rollback support.
|
|
182
|
-
*/
|
|
183
|
-
function applyPatch(dir, filePath, patchFn, options = {}) {
|
|
184
|
-
const fullPath = path.join(dir, filePath);
|
|
185
|
-
const dryRun = options.dryRun === true;
|
|
186
|
-
|
|
187
|
-
if (!fs.existsSync(fullPath)) {
|
|
188
|
-
return { success: false, reason: `${filePath} does not exist`, preview: null };
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const original = fs.readFileSync(fullPath, 'utf8');
|
|
192
|
-
const patched = patchFn(original);
|
|
193
|
-
|
|
194
|
-
if (patched === original) {
|
|
195
|
-
return { success: true, reason: 'no changes needed', preview: null, unchanged: true };
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const preview = generatePatchPreview(original, patched, filePath);
|
|
199
|
-
|
|
200
|
-
if (dryRun) {
|
|
201
|
-
return { success: true, reason: 'dry run', preview, unchanged: false };
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const backupPath = fullPath + '.nerviq-backup';
|
|
205
|
-
fs.writeFileSync(backupPath, original, 'utf8');
|
|
206
|
-
fs.writeFileSync(fullPath, patched, 'utf8');
|
|
207
|
-
|
|
208
|
-
const rollback = writeRollbackArtifact(dir, {
|
|
209
|
-
sourcePlan: 'cursor-patch',
|
|
210
|
-
patchedFiles: [filePath],
|
|
211
|
-
backupFiles: [{ original: filePath, backup: path.relative(dir, backupPath) }],
|
|
212
|
-
rollbackInstructions: [`Restore ${filePath} from ${path.relative(dir, backupPath)}`],
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
const activity = writeActivityArtifact(dir, 'cursor-patch', {
|
|
216
|
-
platform: 'cursor',
|
|
217
|
-
patchedFiles: [filePath],
|
|
218
|
-
rollbackArtifact: rollback.relativePath,
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
return {
|
|
222
|
-
success: true,
|
|
223
|
-
reason: 'patched',
|
|
224
|
-
preview,
|
|
225
|
-
unchanged: false,
|
|
226
|
-
rollbackArtifact: rollback.relativePath,
|
|
227
|
-
activityArtifact: activity.relativePath,
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
module.exports = {
|
|
232
|
-
MANAGED_START_MDC,
|
|
233
|
-
MANAGED_END_MDC,
|
|
234
|
-
MANAGED_JSON_KEY,
|
|
235
|
-
extractManagedBlock,
|
|
236
|
-
upsertManagedBlock,
|
|
237
|
-
patchCursorRuleMdc,
|
|
238
|
-
patchMcpJson,
|
|
239
|
-
patchEnvironmentJson,
|
|
240
|
-
detectMixedAgentRepo,
|
|
241
|
-
generatePatchPreview,
|
|
242
|
-
applyPatch,
|
|
243
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Cursor Patch Intelligence
|
|
3
|
+
*
|
|
4
|
+
* Safe patching of existing Cursor files using managed blocks.
|
|
5
|
+
* Supports .cursor/rules/*.mdc (MDC comment blocks) and
|
|
6
|
+
* .cursor/mcp.json + .cursor/environment.json (JSON merge).
|
|
7
|
+
*
|
|
8
|
+
* Managed blocks are sections that nerviq controls.
|
|
9
|
+
* Hand-authored content outside managed blocks is preserved.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
const path = require('path');
|
|
14
|
+
const { writeRollbackArtifact, writeActivityArtifact } = require('../activity');
|
|
15
|
+
|
|
16
|
+
// Managed block markers for MDC files (HTML comments work in Markdown body)
|
|
17
|
+
const MANAGED_START_MDC = '<!-- nerviq:managed:start -->';
|
|
18
|
+
const MANAGED_END_MDC = '<!-- nerviq:managed:end -->';
|
|
19
|
+
const MANAGED_JSON_KEY = '_nerviq_managed';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Extract managed blocks from a file.
|
|
23
|
+
*/
|
|
24
|
+
function extractManagedBlock(content, startMarker, endMarker) {
|
|
25
|
+
const startIdx = content.indexOf(startMarker);
|
|
26
|
+
const endIdx = content.indexOf(endMarker);
|
|
27
|
+
|
|
28
|
+
if (startIdx === -1 || endIdx === -1 || endIdx <= startIdx) {
|
|
29
|
+
return { before: content, managed: null, after: '' };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
before: content.substring(0, startIdx),
|
|
34
|
+
managed: content.substring(startIdx + startMarker.length, endIdx).trim(),
|
|
35
|
+
after: content.substring(endIdx + endMarker.length),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Replace or insert a managed block in a file.
|
|
41
|
+
*/
|
|
42
|
+
function upsertManagedBlock(content, newManaged, startMarker, endMarker) {
|
|
43
|
+
const { before, managed, after } = extractManagedBlock(content, startMarker, endMarker);
|
|
44
|
+
|
|
45
|
+
if (managed !== null) {
|
|
46
|
+
return `${before}${startMarker}\n${newManaged}\n${endMarker}${after}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const separator = content.endsWith('\n') ? '\n' : '\n\n';
|
|
50
|
+
return `${content}${separator}${startMarker}\n${newManaged}\n${endMarker}\n`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Patch .cursor/rules/*.mdc with managed sections.
|
|
55
|
+
* Preserves MDC frontmatter and hand-authored content.
|
|
56
|
+
*/
|
|
57
|
+
function patchCursorRuleMdc(existingContent, managedSections) {
|
|
58
|
+
const newManaged = Object.entries(managedSections)
|
|
59
|
+
.map(([section, content]) => `## ${section}\n${content}`)
|
|
60
|
+
.join('\n\n');
|
|
61
|
+
|
|
62
|
+
return upsertManagedBlock(existingContent, newManaged, MANAGED_START_MDC, MANAGED_END_MDC);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Patch .cursor/mcp.json by safely merging new servers.
|
|
67
|
+
* Cursor MCP uses the "mcpServers" wrapper format.
|
|
68
|
+
*/
|
|
69
|
+
function patchMcpJson(existingContent, newServers) {
|
|
70
|
+
let existing;
|
|
71
|
+
try {
|
|
72
|
+
existing = JSON.parse(existingContent);
|
|
73
|
+
} catch {
|
|
74
|
+
existing = {};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!existing.mcpServers) existing.mcpServers = {};
|
|
78
|
+
|
|
79
|
+
const merged = { ...existing };
|
|
80
|
+
for (const [serverName, config] of Object.entries(newServers)) {
|
|
81
|
+
if (!(serverName in merged.mcpServers)) {
|
|
82
|
+
merged.mcpServers[serverName] = config;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!merged[MANAGED_JSON_KEY]) merged[MANAGED_JSON_KEY] = {};
|
|
87
|
+
merged[MANAGED_JSON_KEY]._updatedAt = new Date().toISOString();
|
|
88
|
+
merged[MANAGED_JSON_KEY]._generator = nerviq;
|
|
89
|
+
merged[MANAGED_JSON_KEY]._platform = 'cursor';
|
|
90
|
+
|
|
91
|
+
return JSON.stringify(merged, null, 2) + '\n';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Patch .cursor/environment.json by safely merging new fields.
|
|
96
|
+
*/
|
|
97
|
+
function patchEnvironmentJson(existingContent, newFields) {
|
|
98
|
+
let existing;
|
|
99
|
+
try {
|
|
100
|
+
existing = JSON.parse(existingContent);
|
|
101
|
+
} catch {
|
|
102
|
+
existing = {};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const merged = { ...existing };
|
|
106
|
+
|
|
107
|
+
for (const [key, value] of Object.entries(newFields)) {
|
|
108
|
+
if (key === MANAGED_JSON_KEY) {
|
|
109
|
+
merged[MANAGED_JSON_KEY] = { ...(existing[MANAGED_JSON_KEY] || {}), ...value };
|
|
110
|
+
} else if (!(key in existing)) {
|
|
111
|
+
merged[key] = value;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!merged[MANAGED_JSON_KEY]) merged[MANAGED_JSON_KEY] = {};
|
|
116
|
+
merged[MANAGED_JSON_KEY]._updatedAt = new Date().toISOString();
|
|
117
|
+
merged[MANAGED_JSON_KEY]._generator = nerviq;
|
|
118
|
+
merged[MANAGED_JSON_KEY]._platform = 'cursor';
|
|
119
|
+
|
|
120
|
+
return JSON.stringify(merged, null, 2) + '\n';
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Detect if a repo has multiple agent surfaces (Cursor + Claude + Codex + Gemini + Copilot coexistence).
|
|
125
|
+
*/
|
|
126
|
+
function detectMixedAgentRepo(dir) {
|
|
127
|
+
const hasClaude = fs.existsSync(path.join(dir, 'CLAUDE.md')) || fs.existsSync(path.join(dir, '.claude'));
|
|
128
|
+
const hasCodex = fs.existsSync(path.join(dir, 'AGENTS.md')) || fs.existsSync(path.join(dir, '.codex'));
|
|
129
|
+
const hasGemini = fs.existsSync(path.join(dir, 'GEMINI.md')) || fs.existsSync(path.join(dir, '.gemini'));
|
|
130
|
+
const hasCopilot = fs.existsSync(path.join(dir, '.github', 'copilot-instructions.md')) ||
|
|
131
|
+
fs.existsSync(path.join(dir, '.vscode', 'mcp.json'));
|
|
132
|
+
const hasCursor = fs.existsSync(path.join(dir, '.cursor')) ||
|
|
133
|
+
fs.existsSync(path.join(dir, '.cursorrules'));
|
|
134
|
+
|
|
135
|
+
const platforms = [];
|
|
136
|
+
if (hasClaude) platforms.push('claude');
|
|
137
|
+
if (hasCodex) platforms.push('codex');
|
|
138
|
+
if (hasGemini) platforms.push('gemini');
|
|
139
|
+
if (hasCopilot) platforms.push('copilot');
|
|
140
|
+
if (hasCursor) platforms.push('cursor');
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
isMixed: platforms.length >= 2,
|
|
144
|
+
hasClaude,
|
|
145
|
+
hasCodex,
|
|
146
|
+
hasGemini,
|
|
147
|
+
hasCopilot,
|
|
148
|
+
hasCursor,
|
|
149
|
+
platforms,
|
|
150
|
+
guidance: platforms.length >= 2
|
|
151
|
+
? `This is a mixed-agent repo (${platforms.join(', ')}). Keep each platform's config in its own directory (.claude/, .cursor/, .gemini/, .github/). Do not merge them.`
|
|
152
|
+
: null,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Generate a diff preview for a patch operation.
|
|
158
|
+
*/
|
|
159
|
+
function generatePatchPreview(originalContent, patchedContent, filePath) {
|
|
160
|
+
const origLines = originalContent.split('\n');
|
|
161
|
+
const patchLines = patchedContent.split('\n');
|
|
162
|
+
const lines = [`--- ${filePath} (original)`, `+++ ${filePath} (patched)`];
|
|
163
|
+
|
|
164
|
+
let inChange = false;
|
|
165
|
+
for (let i = 0; i < Math.max(origLines.length, patchLines.length); i++) {
|
|
166
|
+
const orig = origLines[i] || '';
|
|
167
|
+
const patched = patchLines[i] || '';
|
|
168
|
+
if (orig !== patched) {
|
|
169
|
+
if (!inChange) { lines.push(`@@ line ${i + 1} @@`); inChange = true; }
|
|
170
|
+
if (i < origLines.length) lines.push(`-${orig}`);
|
|
171
|
+
if (i < patchLines.length) lines.push(`+${patched}`);
|
|
172
|
+
} else {
|
|
173
|
+
inChange = false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return lines.join('\n');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Apply a patch to a file with backup and rollback support.
|
|
182
|
+
*/
|
|
183
|
+
function applyPatch(dir, filePath, patchFn, options = {}) {
|
|
184
|
+
const fullPath = path.join(dir, filePath);
|
|
185
|
+
const dryRun = options.dryRun === true;
|
|
186
|
+
|
|
187
|
+
if (!fs.existsSync(fullPath)) {
|
|
188
|
+
return { success: false, reason: `${filePath} does not exist`, preview: null };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const original = fs.readFileSync(fullPath, 'utf8');
|
|
192
|
+
const patched = patchFn(original);
|
|
193
|
+
|
|
194
|
+
if (patched === original) {
|
|
195
|
+
return { success: true, reason: 'no changes needed', preview: null, unchanged: true };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const preview = generatePatchPreview(original, patched, filePath);
|
|
199
|
+
|
|
200
|
+
if (dryRun) {
|
|
201
|
+
return { success: true, reason: 'dry run', preview, unchanged: false };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const backupPath = fullPath + '.nerviq-backup';
|
|
205
|
+
fs.writeFileSync(backupPath, original, 'utf8');
|
|
206
|
+
fs.writeFileSync(fullPath, patched, 'utf8');
|
|
207
|
+
|
|
208
|
+
const rollback = writeRollbackArtifact(dir, {
|
|
209
|
+
sourcePlan: 'cursor-patch',
|
|
210
|
+
patchedFiles: [filePath],
|
|
211
|
+
backupFiles: [{ original: filePath, backup: path.relative(dir, backupPath) }],
|
|
212
|
+
rollbackInstructions: [`Restore ${filePath} from ${path.relative(dir, backupPath)}`],
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const activity = writeActivityArtifact(dir, 'cursor-patch', {
|
|
216
|
+
platform: 'cursor',
|
|
217
|
+
patchedFiles: [filePath],
|
|
218
|
+
rollbackArtifact: rollback.relativePath,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
success: true,
|
|
223
|
+
reason: 'patched',
|
|
224
|
+
preview,
|
|
225
|
+
unchanged: false,
|
|
226
|
+
rollbackArtifact: rollback.relativePath,
|
|
227
|
+
activityArtifact: activity.relativePath,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
module.exports = {
|
|
232
|
+
MANAGED_START_MDC,
|
|
233
|
+
MANAGED_END_MDC,
|
|
234
|
+
MANAGED_JSON_KEY,
|
|
235
|
+
extractManagedBlock,
|
|
236
|
+
upsertManagedBlock,
|
|
237
|
+
patchCursorRuleMdc,
|
|
238
|
+
patchMcpJson,
|
|
239
|
+
patchEnvironmentJson,
|
|
240
|
+
detectMixedAgentRepo,
|
|
241
|
+
generatePatchPreview,
|
|
242
|
+
applyPatch,
|
|
243
|
+
};
|