@nerviq/cli 1.8.6 → 1.8.8
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/README.md +349 -362
- package/bin/cli.js +131 -34
- package/package.json +1 -1
- package/src/aider/activity.js +226 -226
- package/src/audit.js +1443 -1443
- package/src/benchmark.js +346 -346
- package/src/codex/activity.js +324 -324
- package/src/context.js +27 -1
- package/src/convert.js +6 -2
- package/src/copilot/patch.js +238 -238
- package/src/cursor/patch.js +243 -243
- package/src/gemini/activity.js +402 -402
- package/src/gemini/patch.js +229 -229
- package/src/governance.js +583 -583
- package/src/harmony/audit.js +306 -306
- package/src/insights.js +119 -119
- package/src/{claudex-sync.json → nerviq-sync.json} +1 -1
- package/src/opencode/activity.js +286 -286
- package/src/setup.js +47 -9
- package/src/state-paths.js +85 -85
- package/src/techniques.js +5498 -5494
- package/src/windsurf/patch.js +231 -231
package/src/context.js
CHANGED
|
@@ -185,12 +185,38 @@ class ProjectContext {
|
|
|
185
185
|
return deps;
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
/**
|
|
189
|
+
* Recursively check if a file or directory name exists anywhere under a given base directory.
|
|
190
|
+
* Searches up to maxDepth levels deep.
|
|
191
|
+
*/
|
|
192
|
+
_findInSubdirs(name, baseDir, maxDepth = 3) {
|
|
193
|
+
if (maxDepth <= 0) return false;
|
|
194
|
+
try {
|
|
195
|
+
const entries = fs.readdirSync(baseDir, { withFileTypes: true });
|
|
196
|
+
for (const entry of entries) {
|
|
197
|
+
if (entry.name === 'node_modules' || entry.name === '__pycache__' || entry.name === '.git') continue;
|
|
198
|
+
if (entry.name === name || entry.name.endsWith(name)) return true;
|
|
199
|
+
if (entry.isDirectory()) {
|
|
200
|
+
if (this._findInSubdirs(name, path.join(baseDir, entry.name), maxDepth - 1)) return true;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
} catch {
|
|
204
|
+
// directory not readable
|
|
205
|
+
}
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
|
|
188
209
|
detectStacks(STACKS) {
|
|
189
210
|
const detected = [];
|
|
190
211
|
for (const [key, stack] of Object.entries(STACKS)) {
|
|
191
|
-
|
|
212
|
+
// Check root-level files first (fast path)
|
|
213
|
+
let hasFile = stack.files.some(f => {
|
|
192
214
|
return this.files.some(pf => pf.startsWith(f));
|
|
193
215
|
});
|
|
216
|
+
// If not found at root, search subdirectories (up to 3 levels deep)
|
|
217
|
+
if (!hasFile) {
|
|
218
|
+
hasFile = stack.files.some(f => this._findInSubdirs(f, this.dir));
|
|
219
|
+
}
|
|
194
220
|
if (!hasFile) continue;
|
|
195
221
|
|
|
196
222
|
let contentMatch = true;
|
package/src/convert.js
CHANGED
|
@@ -98,7 +98,7 @@ function readSourceConfig(dir, from) {
|
|
|
98
98
|
if (descMatch) desc = descMatch[1].trim();
|
|
99
99
|
}
|
|
100
100
|
canonical.rules.push({
|
|
101
|
-
name: file.replace(
|
|
101
|
+
name: file.replace(/\.(mdc|md|txt)$/i, ''),
|
|
102
102
|
content,
|
|
103
103
|
alwaysOn,
|
|
104
104
|
glob,
|
|
@@ -165,7 +165,11 @@ function readSourceConfig(dir, from) {
|
|
|
165
165
|
|
|
166
166
|
function buildTargetOutput(canonical, to, { dryRun = false } = {}) {
|
|
167
167
|
const outputs = []; // Array of { path, content }
|
|
168
|
-
|
|
168
|
+
// Strip MDC frontmatter from rule content for non-cursor targets to prevent leaking
|
|
169
|
+
const stripFrontmatter = (text) => text.replace(/^---[\s\S]*?---\n/m, '').trim();
|
|
170
|
+
const combinedContent = to === 'cursor'
|
|
171
|
+
? canonical.rules.map(r => r.content).join('\n\n')
|
|
172
|
+
: canonical.rules.map(r => stripFrontmatter(r.content)).join('\n\n');
|
|
169
173
|
|
|
170
174
|
if (to === 'claude') {
|
|
171
175
|
// Extract or create CLAUDE.md from combined rules
|
package/src/copilot/patch.js
CHANGED
|
@@ -1,238 +1,238 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copilot Patch Intelligence
|
|
3
|
-
*
|
|
4
|
-
* Safe patching of existing Copilot files using managed blocks.
|
|
5
|
-
* Supports copilot-instructions.md (HTML comment blocks) and
|
|
6
|
-
* .vscode/settings.json + .vscode/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
|
|
17
|
-
const MANAGED_START_MD = '<!-- nerviq:managed:start -->';
|
|
18
|
-
const MANAGED_END_MD = '<!-- nerviq:managed:end -->';
|
|
19
|
-
const MANAGED_JSON_KEY = '
|
|
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 copilot-instructions.md with managed sections.
|
|
55
|
-
*/
|
|
56
|
-
function patchCopilotInstructionsMd(existingContent, managedSections) {
|
|
57
|
-
const newManaged = Object.entries(managedSections)
|
|
58
|
-
.map(([section, content]) => `## ${section}\n${content}`)
|
|
59
|
-
.join('\n\n');
|
|
60
|
-
|
|
61
|
-
return upsertManagedBlock(existingContent, newManaged, MANAGED_START_MD, MANAGED_END_MD);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Patch .vscode/settings.json by safely merging new keys.
|
|
66
|
-
* Only adds new keys or updates the
|
|
67
|
-
*/
|
|
68
|
-
function patchVscodeSettingsJson(existingContent, newKeys) {
|
|
69
|
-
let existing;
|
|
70
|
-
try {
|
|
71
|
-
existing = JSON.parse(existingContent);
|
|
72
|
-
} catch {
|
|
73
|
-
existing = {};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const merged = { ...existing };
|
|
77
|
-
|
|
78
|
-
for (const [key, value] of Object.entries(newKeys)) {
|
|
79
|
-
if (key === MANAGED_JSON_KEY) {
|
|
80
|
-
merged[MANAGED_JSON_KEY] = { ...(existing[MANAGED_JSON_KEY] || {}), ...value };
|
|
81
|
-
} else if (!(key in existing)) {
|
|
82
|
-
merged[key] = value;
|
|
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 = 'copilot';
|
|
90
|
-
|
|
91
|
-
return JSON.stringify(merged, null, 2) + '\n';
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Patch .vscode/mcp.json by safely merging new servers.
|
|
96
|
-
* Copilot MCP uses the "servers" wrapper format.
|
|
97
|
-
*/
|
|
98
|
-
function patchMcpJson(existingContent, newServers) {
|
|
99
|
-
let existing;
|
|
100
|
-
try {
|
|
101
|
-
existing = JSON.parse(existingContent);
|
|
102
|
-
} catch {
|
|
103
|
-
existing = {};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (!existing.servers) existing.servers = {};
|
|
107
|
-
|
|
108
|
-
const merged = { ...existing };
|
|
109
|
-
for (const [serverName, config] of Object.entries(newServers)) {
|
|
110
|
-
if (!(serverName in merged.servers)) {
|
|
111
|
-
merged.servers[serverName] = config;
|
|
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
|
-
|
|
119
|
-
return JSON.stringify(merged, null, 2) + '\n';
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Detect if a repo has multiple agent surfaces (Copilot + Claude + Codex + Gemini coexistence).
|
|
124
|
-
*/
|
|
125
|
-
function detectMixedAgentRepo(dir) {
|
|
126
|
-
const hasClaude = fs.existsSync(path.join(dir, 'CLAUDE.md')) || fs.existsSync(path.join(dir, '.claude'));
|
|
127
|
-
const hasCodex = fs.existsSync(path.join(dir, 'AGENTS.md')) || fs.existsSync(path.join(dir, '.codex'));
|
|
128
|
-
const hasGemini = fs.existsSync(path.join(dir, 'GEMINI.md')) || fs.existsSync(path.join(dir, '.gemini'));
|
|
129
|
-
const hasCopilot = fs.existsSync(path.join(dir, '.github', 'copilot-instructions.md')) ||
|
|
130
|
-
fs.existsSync(path.join(dir, '.vscode', 'mcp.json'));
|
|
131
|
-
|
|
132
|
-
const platforms = [];
|
|
133
|
-
if (hasClaude) platforms.push('claude');
|
|
134
|
-
if (hasCodex) platforms.push('codex');
|
|
135
|
-
if (hasGemini) platforms.push('gemini');
|
|
136
|
-
if (hasCopilot) platforms.push('copilot');
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
isMixed: platforms.length >= 2,
|
|
140
|
-
hasClaude,
|
|
141
|
-
hasCodex,
|
|
142
|
-
hasGemini,
|
|
143
|
-
hasCopilot,
|
|
144
|
-
platforms,
|
|
145
|
-
guidance: platforms.length >= 2
|
|
146
|
-
? `This is a mixed-agent repo (${platforms.join(', ')}). Keep each platform's instructions in its own file (CLAUDE.md, AGENTS.md, GEMINI.md, copilot-instructions.md). Do not merge them.`
|
|
147
|
-
: null,
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Generate a diff preview for a patch operation.
|
|
153
|
-
*/
|
|
154
|
-
function generatePatchPreview(originalContent, patchedContent, filePath) {
|
|
155
|
-
const origLines = originalContent.split('\n');
|
|
156
|
-
const patchLines = patchedContent.split('\n');
|
|
157
|
-
const lines = [`--- ${filePath} (original)`, `+++ ${filePath} (patched)`];
|
|
158
|
-
|
|
159
|
-
let inChange = false;
|
|
160
|
-
for (let i = 0; i < Math.max(origLines.length, patchLines.length); i++) {
|
|
161
|
-
const orig = origLines[i] || '';
|
|
162
|
-
const patched = patchLines[i] || '';
|
|
163
|
-
if (orig !== patched) {
|
|
164
|
-
if (!inChange) { lines.push(`@@ line ${i + 1} @@`); inChange = true; }
|
|
165
|
-
if (i < origLines.length) lines.push(`-${orig}`);
|
|
166
|
-
if (i < patchLines.length) lines.push(`+${patched}`);
|
|
167
|
-
} else {
|
|
168
|
-
inChange = false;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return lines.join('\n');
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Apply a patch to a file with backup and rollback support.
|
|
177
|
-
*/
|
|
178
|
-
function applyPatch(dir, filePath, patchFn, options = {}) {
|
|
179
|
-
const fullPath = path.join(dir, filePath);
|
|
180
|
-
const dryRun = options.dryRun === true;
|
|
181
|
-
|
|
182
|
-
if (!fs.existsSync(fullPath)) {
|
|
183
|
-
return { success: false, reason: `${filePath} does not exist`, preview: null };
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const original = fs.readFileSync(fullPath, 'utf8');
|
|
187
|
-
const patched = patchFn(original);
|
|
188
|
-
|
|
189
|
-
if (patched === original) {
|
|
190
|
-
return { success: true, reason: 'no changes needed', preview: null, unchanged: true };
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const preview = generatePatchPreview(original, patched, filePath);
|
|
194
|
-
|
|
195
|
-
if (dryRun) {
|
|
196
|
-
return { success: true, reason: 'dry run', preview, unchanged: false };
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const backupPath = fullPath + '.nerviq-backup';
|
|
200
|
-
fs.writeFileSync(backupPath, original, 'utf8');
|
|
201
|
-
fs.writeFileSync(fullPath, patched, 'utf8');
|
|
202
|
-
|
|
203
|
-
const rollback = writeRollbackArtifact(dir, {
|
|
204
|
-
sourcePlan: 'copilot-patch',
|
|
205
|
-
patchedFiles: [filePath],
|
|
206
|
-
backupFiles: [{ original: filePath, backup: path.relative(dir, backupPath) }],
|
|
207
|
-
rollbackInstructions: [`Restore ${filePath} from ${path.relative(dir, backupPath)}`],
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
const activity = writeActivityArtifact(dir, 'copilot-patch', {
|
|
211
|
-
platform: 'copilot',
|
|
212
|
-
patchedFiles: [filePath],
|
|
213
|
-
rollbackArtifact: rollback.relativePath,
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
return {
|
|
217
|
-
success: true,
|
|
218
|
-
reason: 'patched',
|
|
219
|
-
preview,
|
|
220
|
-
unchanged: false,
|
|
221
|
-
rollbackArtifact: rollback.relativePath,
|
|
222
|
-
activityArtifact: activity.relativePath,
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
module.exports = {
|
|
227
|
-
MANAGED_START_MD,
|
|
228
|
-
MANAGED_END_MD,
|
|
229
|
-
MANAGED_JSON_KEY,
|
|
230
|
-
extractManagedBlock,
|
|
231
|
-
upsertManagedBlock,
|
|
232
|
-
patchCopilotInstructionsMd,
|
|
233
|
-
patchVscodeSettingsJson,
|
|
234
|
-
patchMcpJson,
|
|
235
|
-
detectMixedAgentRepo,
|
|
236
|
-
generatePatchPreview,
|
|
237
|
-
applyPatch,
|
|
238
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Copilot Patch Intelligence
|
|
3
|
+
*
|
|
4
|
+
* Safe patching of existing Copilot files using managed blocks.
|
|
5
|
+
* Supports copilot-instructions.md (HTML comment blocks) and
|
|
6
|
+
* .vscode/settings.json + .vscode/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
|
|
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 copilot-instructions.md with managed sections.
|
|
55
|
+
*/
|
|
56
|
+
function patchCopilotInstructionsMd(existingContent, managedSections) {
|
|
57
|
+
const newManaged = Object.entries(managedSections)
|
|
58
|
+
.map(([section, content]) => `## ${section}\n${content}`)
|
|
59
|
+
.join('\n\n');
|
|
60
|
+
|
|
61
|
+
return upsertManagedBlock(existingContent, newManaged, MANAGED_START_MD, MANAGED_END_MD);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Patch .vscode/settings.json by safely merging new keys.
|
|
66
|
+
* Only adds new keys or updates the _nerviq_managed namespace.
|
|
67
|
+
*/
|
|
68
|
+
function patchVscodeSettingsJson(existingContent, newKeys) {
|
|
69
|
+
let existing;
|
|
70
|
+
try {
|
|
71
|
+
existing = JSON.parse(existingContent);
|
|
72
|
+
} catch {
|
|
73
|
+
existing = {};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const merged = { ...existing };
|
|
77
|
+
|
|
78
|
+
for (const [key, value] of Object.entries(newKeys)) {
|
|
79
|
+
if (key === MANAGED_JSON_KEY) {
|
|
80
|
+
merged[MANAGED_JSON_KEY] = { ...(existing[MANAGED_JSON_KEY] || {}), ...value };
|
|
81
|
+
} else if (!(key in existing)) {
|
|
82
|
+
merged[key] = value;
|
|
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 = 'copilot';
|
|
90
|
+
|
|
91
|
+
return JSON.stringify(merged, null, 2) + '\n';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Patch .vscode/mcp.json by safely merging new servers.
|
|
96
|
+
* Copilot MCP uses the "servers" wrapper format.
|
|
97
|
+
*/
|
|
98
|
+
function patchMcpJson(existingContent, newServers) {
|
|
99
|
+
let existing;
|
|
100
|
+
try {
|
|
101
|
+
existing = JSON.parse(existingContent);
|
|
102
|
+
} catch {
|
|
103
|
+
existing = {};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!existing.servers) existing.servers = {};
|
|
107
|
+
|
|
108
|
+
const merged = { ...existing };
|
|
109
|
+
for (const [serverName, config] of Object.entries(newServers)) {
|
|
110
|
+
if (!(serverName in merged.servers)) {
|
|
111
|
+
merged.servers[serverName] = config;
|
|
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
|
+
|
|
119
|
+
return JSON.stringify(merged, null, 2) + '\n';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Detect if a repo has multiple agent surfaces (Copilot + Claude + Codex + Gemini coexistence).
|
|
124
|
+
*/
|
|
125
|
+
function detectMixedAgentRepo(dir) {
|
|
126
|
+
const hasClaude = fs.existsSync(path.join(dir, 'CLAUDE.md')) || fs.existsSync(path.join(dir, '.claude'));
|
|
127
|
+
const hasCodex = fs.existsSync(path.join(dir, 'AGENTS.md')) || fs.existsSync(path.join(dir, '.codex'));
|
|
128
|
+
const hasGemini = fs.existsSync(path.join(dir, 'GEMINI.md')) || fs.existsSync(path.join(dir, '.gemini'));
|
|
129
|
+
const hasCopilot = fs.existsSync(path.join(dir, '.github', 'copilot-instructions.md')) ||
|
|
130
|
+
fs.existsSync(path.join(dir, '.vscode', 'mcp.json'));
|
|
131
|
+
|
|
132
|
+
const platforms = [];
|
|
133
|
+
if (hasClaude) platforms.push('claude');
|
|
134
|
+
if (hasCodex) platforms.push('codex');
|
|
135
|
+
if (hasGemini) platforms.push('gemini');
|
|
136
|
+
if (hasCopilot) platforms.push('copilot');
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
isMixed: platforms.length >= 2,
|
|
140
|
+
hasClaude,
|
|
141
|
+
hasCodex,
|
|
142
|
+
hasGemini,
|
|
143
|
+
hasCopilot,
|
|
144
|
+
platforms,
|
|
145
|
+
guidance: platforms.length >= 2
|
|
146
|
+
? `This is a mixed-agent repo (${platforms.join(', ')}). Keep each platform's instructions in its own file (CLAUDE.md, AGENTS.md, GEMINI.md, copilot-instructions.md). Do not merge them.`
|
|
147
|
+
: null,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Generate a diff preview for a patch operation.
|
|
153
|
+
*/
|
|
154
|
+
function generatePatchPreview(originalContent, patchedContent, filePath) {
|
|
155
|
+
const origLines = originalContent.split('\n');
|
|
156
|
+
const patchLines = patchedContent.split('\n');
|
|
157
|
+
const lines = [`--- ${filePath} (original)`, `+++ ${filePath} (patched)`];
|
|
158
|
+
|
|
159
|
+
let inChange = false;
|
|
160
|
+
for (let i = 0; i < Math.max(origLines.length, patchLines.length); i++) {
|
|
161
|
+
const orig = origLines[i] || '';
|
|
162
|
+
const patched = patchLines[i] || '';
|
|
163
|
+
if (orig !== patched) {
|
|
164
|
+
if (!inChange) { lines.push(`@@ line ${i + 1} @@`); inChange = true; }
|
|
165
|
+
if (i < origLines.length) lines.push(`-${orig}`);
|
|
166
|
+
if (i < patchLines.length) lines.push(`+${patched}`);
|
|
167
|
+
} else {
|
|
168
|
+
inChange = false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return lines.join('\n');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Apply a patch to a file with backup and rollback support.
|
|
177
|
+
*/
|
|
178
|
+
function applyPatch(dir, filePath, patchFn, options = {}) {
|
|
179
|
+
const fullPath = path.join(dir, filePath);
|
|
180
|
+
const dryRun = options.dryRun === true;
|
|
181
|
+
|
|
182
|
+
if (!fs.existsSync(fullPath)) {
|
|
183
|
+
return { success: false, reason: `${filePath} does not exist`, preview: null };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const original = fs.readFileSync(fullPath, 'utf8');
|
|
187
|
+
const patched = patchFn(original);
|
|
188
|
+
|
|
189
|
+
if (patched === original) {
|
|
190
|
+
return { success: true, reason: 'no changes needed', preview: null, unchanged: true };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const preview = generatePatchPreview(original, patched, filePath);
|
|
194
|
+
|
|
195
|
+
if (dryRun) {
|
|
196
|
+
return { success: true, reason: 'dry run', preview, unchanged: false };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const backupPath = fullPath + '.nerviq-backup';
|
|
200
|
+
fs.writeFileSync(backupPath, original, 'utf8');
|
|
201
|
+
fs.writeFileSync(fullPath, patched, 'utf8');
|
|
202
|
+
|
|
203
|
+
const rollback = writeRollbackArtifact(dir, {
|
|
204
|
+
sourcePlan: 'copilot-patch',
|
|
205
|
+
patchedFiles: [filePath],
|
|
206
|
+
backupFiles: [{ original: filePath, backup: path.relative(dir, backupPath) }],
|
|
207
|
+
rollbackInstructions: [`Restore ${filePath} from ${path.relative(dir, backupPath)}`],
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const activity = writeActivityArtifact(dir, 'copilot-patch', {
|
|
211
|
+
platform: 'copilot',
|
|
212
|
+
patchedFiles: [filePath],
|
|
213
|
+
rollbackArtifact: rollback.relativePath,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
success: true,
|
|
218
|
+
reason: 'patched',
|
|
219
|
+
preview,
|
|
220
|
+
unchanged: false,
|
|
221
|
+
rollbackArtifact: rollback.relativePath,
|
|
222
|
+
activityArtifact: activity.relativePath,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
module.exports = {
|
|
227
|
+
MANAGED_START_MD,
|
|
228
|
+
MANAGED_END_MD,
|
|
229
|
+
MANAGED_JSON_KEY,
|
|
230
|
+
extractManagedBlock,
|
|
231
|
+
upsertManagedBlock,
|
|
232
|
+
patchCopilotInstructionsMd,
|
|
233
|
+
patchVscodeSettingsJson,
|
|
234
|
+
patchMcpJson,
|
|
235
|
+
detectMixedAgentRepo,
|
|
236
|
+
generatePatchPreview,
|
|
237
|
+
applyPatch,
|
|
238
|
+
};
|