@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/windsurf/patch.js
CHANGED
|
@@ -1,231 +1,231 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Windsurf Patch Intelligence
|
|
3
|
-
*
|
|
4
|
-
* Safe patching of existing Windsurf files using managed blocks.
|
|
5
|
-
* Supports .windsurf/rules/*.md (HTML comment blocks) and
|
|
6
|
-
* .windsurf/mcp.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 Markdown rule files (HTML comments work in Markdown body)
|
|
17
|
-
const MANAGED_START_MD = '<!-- nerviq:managed:start -->';
|
|
18
|
-
const MANAGED_END_MD = '<!-- 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 .windsurf/rules/*.md with managed sections.
|
|
55
|
-
* Preserves YAML frontmatter and hand-authored content.
|
|
56
|
-
*/
|
|
57
|
-
function patchWindsurfRuleMd(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_MD, MANAGED_END_MD);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Patch .windsurf/mcp.json by safely merging new servers.
|
|
67
|
-
*/
|
|
68
|
-
function patchMcpJson(existingContent, newServers) {
|
|
69
|
-
let existing;
|
|
70
|
-
try {
|
|
71
|
-
existing = JSON.parse(existingContent);
|
|
72
|
-
} catch {
|
|
73
|
-
existing = {};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (!existing.mcpServers) existing.mcpServers = {};
|
|
77
|
-
|
|
78
|
-
const merged = { ...existing };
|
|
79
|
-
for (const [serverName, config] of Object.entries(newServers)) {
|
|
80
|
-
if (!(serverName in merged.mcpServers)) {
|
|
81
|
-
merged.mcpServers[serverName] = config;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!merged[MANAGED_JSON_KEY]) merged[MANAGED_JSON_KEY] = {};
|
|
86
|
-
merged[MANAGED_JSON_KEY]._updatedAt = new Date().toISOString();
|
|
87
|
-
merged[MANAGED_JSON_KEY]._generator = 'nerviq';
|
|
88
|
-
merged[MANAGED_JSON_KEY]._platform = 'windsurf';
|
|
89
|
-
|
|
90
|
-
return JSON.stringify(merged, null, 2) + '\n';
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Patch .cascadeignore by appending new patterns.
|
|
95
|
-
*/
|
|
96
|
-
function patchCascadeignore(existingContent, newPatterns) {
|
|
97
|
-
const existing = existingContent || '';
|
|
98
|
-
const existingLines = new Set(existing.split('\n').map(l => l.trim()).filter(l => l && !l.startsWith('#')));
|
|
99
|
-
|
|
100
|
-
const toAdd = newPatterns.filter(p => !existingLines.has(p.trim()));
|
|
101
|
-
if (toAdd.length === 0) return existing;
|
|
102
|
-
|
|
103
|
-
const separator = existing.endsWith('\n') ? '' : '\n';
|
|
104
|
-
return `${existing}${separator}\n# Added by nerviq\n${toAdd.join('\n')}\n`;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Detect if a repo has multiple agent surfaces (Windsurf + Claude + Cursor + Codex + Copilot coexistence).
|
|
109
|
-
*/
|
|
110
|
-
function detectMixedAgentRepo(dir) {
|
|
111
|
-
const hasClaude = fs.existsSync(path.join(dir, 'CLAUDE.md')) || fs.existsSync(path.join(dir, '.claude'));
|
|
112
|
-
const hasCodex = fs.existsSync(path.join(dir, 'AGENTS.md')) || fs.existsSync(path.join(dir, '.codex'));
|
|
113
|
-
const hasGemini = fs.existsSync(path.join(dir, 'GEMINI.md')) || fs.existsSync(path.join(dir, '.gemini'));
|
|
114
|
-
const hasCopilot = fs.existsSync(path.join(dir, '.github', 'copilot-instructions.md')) ||
|
|
115
|
-
fs.existsSync(path.join(dir, '.vscode', 'mcp.json'));
|
|
116
|
-
const hasCursor = fs.existsSync(path.join(dir, '.cursor')) ||
|
|
117
|
-
fs.existsSync(path.join(dir, '.cursorrules'));
|
|
118
|
-
const hasWindsurf = fs.existsSync(path.join(dir, '.windsurf')) ||
|
|
119
|
-
fs.existsSync(path.join(dir, '.windsurfrules'));
|
|
120
|
-
|
|
121
|
-
const platforms = [];
|
|
122
|
-
if (hasClaude) platforms.push('claude');
|
|
123
|
-
if (hasCodex) platforms.push('codex');
|
|
124
|
-
if (hasGemini) platforms.push('gemini');
|
|
125
|
-
if (hasCopilot) platforms.push('copilot');
|
|
126
|
-
if (hasCursor) platforms.push('cursor');
|
|
127
|
-
if (hasWindsurf) platforms.push('windsurf');
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
isMixed: platforms.length >= 2,
|
|
131
|
-
hasClaude,
|
|
132
|
-
hasCodex,
|
|
133
|
-
hasGemini,
|
|
134
|
-
hasCopilot,
|
|
135
|
-
hasCursor,
|
|
136
|
-
hasWindsurf,
|
|
137
|
-
platforms,
|
|
138
|
-
guidance: platforms.length >= 2
|
|
139
|
-
? `This is a mixed-agent repo (${platforms.join(', ')}). Keep each platform's config in its own directory (.claude/, .cursor/, .windsurf/, .gemini/, .github/). Do not merge them.`
|
|
140
|
-
: null,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Generate a diff preview for a patch operation.
|
|
146
|
-
*/
|
|
147
|
-
function generatePatchPreview(originalContent, patchedContent, filePath) {
|
|
148
|
-
const origLines = originalContent.split('\n');
|
|
149
|
-
const patchLines = patchedContent.split('\n');
|
|
150
|
-
const lines = [`--- ${filePath} (original)`, `+++ ${filePath} (patched)`];
|
|
151
|
-
|
|
152
|
-
let inChange = false;
|
|
153
|
-
for (let i = 0; i < Math.max(origLines.length, patchLines.length); i++) {
|
|
154
|
-
const orig = origLines[i] || '';
|
|
155
|
-
const patched = patchLines[i] || '';
|
|
156
|
-
if (orig !== patched) {
|
|
157
|
-
if (!inChange) { lines.push(`@@ line ${i + 1} @@`); inChange = true; }
|
|
158
|
-
if (i < origLines.length) lines.push(`-${orig}`);
|
|
159
|
-
if (i < patchLines.length) lines.push(`+${patched}`);
|
|
160
|
-
} else {
|
|
161
|
-
inChange = false;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return lines.join('\n');
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Apply a patch to a file with backup and rollback support.
|
|
170
|
-
*/
|
|
171
|
-
function applyPatch(dir, filePath, patchFn, options = {}) {
|
|
172
|
-
const fullPath = path.join(dir, filePath);
|
|
173
|
-
const dryRun = options.dryRun === true;
|
|
174
|
-
|
|
175
|
-
if (!fs.existsSync(fullPath)) {
|
|
176
|
-
return { success: false, reason: `${filePath} does not exist`, preview: null };
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const original = fs.readFileSync(fullPath, 'utf8');
|
|
180
|
-
const patched = patchFn(original);
|
|
181
|
-
|
|
182
|
-
if (patched === original) {
|
|
183
|
-
return { success: true, reason: 'no changes needed', preview: null, unchanged: true };
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const preview = generatePatchPreview(original, patched, filePath);
|
|
187
|
-
|
|
188
|
-
if (dryRun) {
|
|
189
|
-
return { success: true, reason: 'dry run', preview, unchanged: false };
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const backupPath = fullPath + '.nerviq-backup';
|
|
193
|
-
fs.writeFileSync(backupPath, original, 'utf8');
|
|
194
|
-
fs.writeFileSync(fullPath, patched, 'utf8');
|
|
195
|
-
|
|
196
|
-
const rollback = writeRollbackArtifact(dir, {
|
|
197
|
-
sourcePlan: 'windsurf-patch',
|
|
198
|
-
patchedFiles: [filePath],
|
|
199
|
-
backupFiles: [{ original: filePath, backup: path.relative(dir, backupPath) }],
|
|
200
|
-
rollbackInstructions: [`Restore ${filePath} from ${path.relative(dir, backupPath)}`],
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
const activity = writeActivityArtifact(dir, 'windsurf-patch', {
|
|
204
|
-
platform: 'windsurf',
|
|
205
|
-
patchedFiles: [filePath],
|
|
206
|
-
rollbackArtifact: rollback.relativePath,
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
return {
|
|
210
|
-
success: true,
|
|
211
|
-
reason: 'patched',
|
|
212
|
-
preview,
|
|
213
|
-
unchanged: false,
|
|
214
|
-
rollbackArtifact: rollback.relativePath,
|
|
215
|
-
activityArtifact: activity.relativePath,
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
module.exports = {
|
|
220
|
-
MANAGED_START_MD,
|
|
221
|
-
MANAGED_END_MD,
|
|
222
|
-
MANAGED_JSON_KEY,
|
|
223
|
-
extractManagedBlock,
|
|
224
|
-
upsertManagedBlock,
|
|
225
|
-
patchWindsurfRuleMd,
|
|
226
|
-
patchMcpJson,
|
|
227
|
-
patchCascadeignore,
|
|
228
|
-
detectMixedAgentRepo,
|
|
229
|
-
generatePatchPreview,
|
|
230
|
-
applyPatch,
|
|
231
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Windsurf Patch Intelligence
|
|
3
|
+
*
|
|
4
|
+
* Safe patching of existing Windsurf files using managed blocks.
|
|
5
|
+
* Supports .windsurf/rules/*.md (HTML comment blocks) and
|
|
6
|
+
* .windsurf/mcp.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 Markdown rule files (HTML comments work in Markdown body)
|
|
17
|
+
const MANAGED_START_MD = '<!-- nerviq:managed:start -->';
|
|
18
|
+
const MANAGED_END_MD = '<!-- 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 .windsurf/rules/*.md with managed sections.
|
|
55
|
+
* Preserves YAML frontmatter and hand-authored content.
|
|
56
|
+
*/
|
|
57
|
+
function patchWindsurfRuleMd(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_MD, MANAGED_END_MD);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Patch .windsurf/mcp.json by safely merging new servers.
|
|
67
|
+
*/
|
|
68
|
+
function patchMcpJson(existingContent, newServers) {
|
|
69
|
+
let existing;
|
|
70
|
+
try {
|
|
71
|
+
existing = JSON.parse(existingContent);
|
|
72
|
+
} catch {
|
|
73
|
+
existing = {};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!existing.mcpServers) existing.mcpServers = {};
|
|
77
|
+
|
|
78
|
+
const merged = { ...existing };
|
|
79
|
+
for (const [serverName, config] of Object.entries(newServers)) {
|
|
80
|
+
if (!(serverName in merged.mcpServers)) {
|
|
81
|
+
merged.mcpServers[serverName] = config;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!merged[MANAGED_JSON_KEY]) merged[MANAGED_JSON_KEY] = {};
|
|
86
|
+
merged[MANAGED_JSON_KEY]._updatedAt = new Date().toISOString();
|
|
87
|
+
merged[MANAGED_JSON_KEY]._generator = 'nerviq';
|
|
88
|
+
merged[MANAGED_JSON_KEY]._platform = 'windsurf';
|
|
89
|
+
|
|
90
|
+
return JSON.stringify(merged, null, 2) + '\n';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Patch .cascadeignore by appending new patterns.
|
|
95
|
+
*/
|
|
96
|
+
function patchCascadeignore(existingContent, newPatterns) {
|
|
97
|
+
const existing = existingContent || '';
|
|
98
|
+
const existingLines = new Set(existing.split('\n').map(l => l.trim()).filter(l => l && !l.startsWith('#')));
|
|
99
|
+
|
|
100
|
+
const toAdd = newPatterns.filter(p => !existingLines.has(p.trim()));
|
|
101
|
+
if (toAdd.length === 0) return existing;
|
|
102
|
+
|
|
103
|
+
const separator = existing.endsWith('\n') ? '' : '\n';
|
|
104
|
+
return `${existing}${separator}\n# Added by nerviq\n${toAdd.join('\n')}\n`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Detect if a repo has multiple agent surfaces (Windsurf + Claude + Cursor + Codex + Copilot coexistence).
|
|
109
|
+
*/
|
|
110
|
+
function detectMixedAgentRepo(dir) {
|
|
111
|
+
const hasClaude = fs.existsSync(path.join(dir, 'CLAUDE.md')) || fs.existsSync(path.join(dir, '.claude'));
|
|
112
|
+
const hasCodex = fs.existsSync(path.join(dir, 'AGENTS.md')) || fs.existsSync(path.join(dir, '.codex'));
|
|
113
|
+
const hasGemini = fs.existsSync(path.join(dir, 'GEMINI.md')) || fs.existsSync(path.join(dir, '.gemini'));
|
|
114
|
+
const hasCopilot = fs.existsSync(path.join(dir, '.github', 'copilot-instructions.md')) ||
|
|
115
|
+
fs.existsSync(path.join(dir, '.vscode', 'mcp.json'));
|
|
116
|
+
const hasCursor = fs.existsSync(path.join(dir, '.cursor')) ||
|
|
117
|
+
fs.existsSync(path.join(dir, '.cursorrules'));
|
|
118
|
+
const hasWindsurf = fs.existsSync(path.join(dir, '.windsurf')) ||
|
|
119
|
+
fs.existsSync(path.join(dir, '.windsurfrules'));
|
|
120
|
+
|
|
121
|
+
const platforms = [];
|
|
122
|
+
if (hasClaude) platforms.push('claude');
|
|
123
|
+
if (hasCodex) platforms.push('codex');
|
|
124
|
+
if (hasGemini) platforms.push('gemini');
|
|
125
|
+
if (hasCopilot) platforms.push('copilot');
|
|
126
|
+
if (hasCursor) platforms.push('cursor');
|
|
127
|
+
if (hasWindsurf) platforms.push('windsurf');
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
isMixed: platforms.length >= 2,
|
|
131
|
+
hasClaude,
|
|
132
|
+
hasCodex,
|
|
133
|
+
hasGemini,
|
|
134
|
+
hasCopilot,
|
|
135
|
+
hasCursor,
|
|
136
|
+
hasWindsurf,
|
|
137
|
+
platforms,
|
|
138
|
+
guidance: platforms.length >= 2
|
|
139
|
+
? `This is a mixed-agent repo (${platforms.join(', ')}). Keep each platform's config in its own directory (.claude/, .cursor/, .windsurf/, .gemini/, .github/). Do not merge them.`
|
|
140
|
+
: null,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Generate a diff preview for a patch operation.
|
|
146
|
+
*/
|
|
147
|
+
function generatePatchPreview(originalContent, patchedContent, filePath) {
|
|
148
|
+
const origLines = originalContent.split('\n');
|
|
149
|
+
const patchLines = patchedContent.split('\n');
|
|
150
|
+
const lines = [`--- ${filePath} (original)`, `+++ ${filePath} (patched)`];
|
|
151
|
+
|
|
152
|
+
let inChange = false;
|
|
153
|
+
for (let i = 0; i < Math.max(origLines.length, patchLines.length); i++) {
|
|
154
|
+
const orig = origLines[i] || '';
|
|
155
|
+
const patched = patchLines[i] || '';
|
|
156
|
+
if (orig !== patched) {
|
|
157
|
+
if (!inChange) { lines.push(`@@ line ${i + 1} @@`); inChange = true; }
|
|
158
|
+
if (i < origLines.length) lines.push(`-${orig}`);
|
|
159
|
+
if (i < patchLines.length) lines.push(`+${patched}`);
|
|
160
|
+
} else {
|
|
161
|
+
inChange = false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return lines.join('\n');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Apply a patch to a file with backup and rollback support.
|
|
170
|
+
*/
|
|
171
|
+
function applyPatch(dir, filePath, patchFn, options = {}) {
|
|
172
|
+
const fullPath = path.join(dir, filePath);
|
|
173
|
+
const dryRun = options.dryRun === true;
|
|
174
|
+
|
|
175
|
+
if (!fs.existsSync(fullPath)) {
|
|
176
|
+
return { success: false, reason: `${filePath} does not exist`, preview: null };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const original = fs.readFileSync(fullPath, 'utf8');
|
|
180
|
+
const patched = patchFn(original);
|
|
181
|
+
|
|
182
|
+
if (patched === original) {
|
|
183
|
+
return { success: true, reason: 'no changes needed', preview: null, unchanged: true };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const preview = generatePatchPreview(original, patched, filePath);
|
|
187
|
+
|
|
188
|
+
if (dryRun) {
|
|
189
|
+
return { success: true, reason: 'dry run', preview, unchanged: false };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const backupPath = fullPath + '.nerviq-backup';
|
|
193
|
+
fs.writeFileSync(backupPath, original, 'utf8');
|
|
194
|
+
fs.writeFileSync(fullPath, patched, 'utf8');
|
|
195
|
+
|
|
196
|
+
const rollback = writeRollbackArtifact(dir, {
|
|
197
|
+
sourcePlan: 'windsurf-patch',
|
|
198
|
+
patchedFiles: [filePath],
|
|
199
|
+
backupFiles: [{ original: filePath, backup: path.relative(dir, backupPath) }],
|
|
200
|
+
rollbackInstructions: [`Restore ${filePath} from ${path.relative(dir, backupPath)}`],
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const activity = writeActivityArtifact(dir, 'windsurf-patch', {
|
|
204
|
+
platform: 'windsurf',
|
|
205
|
+
patchedFiles: [filePath],
|
|
206
|
+
rollbackArtifact: rollback.relativePath,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
success: true,
|
|
211
|
+
reason: 'patched',
|
|
212
|
+
preview,
|
|
213
|
+
unchanged: false,
|
|
214
|
+
rollbackArtifact: rollback.relativePath,
|
|
215
|
+
activityArtifact: activity.relativePath,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
module.exports = {
|
|
220
|
+
MANAGED_START_MD,
|
|
221
|
+
MANAGED_END_MD,
|
|
222
|
+
MANAGED_JSON_KEY,
|
|
223
|
+
extractManagedBlock,
|
|
224
|
+
upsertManagedBlock,
|
|
225
|
+
patchWindsurfRuleMd,
|
|
226
|
+
patchMcpJson,
|
|
227
|
+
patchCascadeignore,
|
|
228
|
+
detectMixedAgentRepo,
|
|
229
|
+
generatePatchPreview,
|
|
230
|
+
applyPatch,
|
|
231
|
+
};
|