@nerviq/cli 1.20.0 → 1.21.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 (185) hide show
  1. package/LICENSE +23 -23
  2. package/README.md +2 -2
  3. package/bin/cli.js +1 -0
  4. package/package.json +2 -1
  5. package/src/activity.js +1039 -1039
  6. package/src/adoption-advisor.js +299 -299
  7. package/src/aider/config-parser.js +166 -166
  8. package/src/aider/context.js +6 -2
  9. package/src/aider/deep-review.js +316 -316
  10. package/src/aider/domain-packs.js +303 -303
  11. package/src/aider/freshness.js +93 -93
  12. package/src/aider/governance.js +253 -253
  13. package/src/aider/interactive.js +334 -334
  14. package/src/aider/mcp-packs.js +329 -329
  15. package/src/aider/patch.js +214 -214
  16. package/src/aider/plans.js +186 -186
  17. package/src/aider/premium.js +360 -360
  18. package/src/aider/setup.js +404 -404
  19. package/src/aider/techniques.js +312 -67
  20. package/src/analyze.js +951 -951
  21. package/src/anti-patterns.js +485 -485
  22. package/src/audit/instruction-files.js +180 -180
  23. package/src/audit/recommendations.js +577 -577
  24. package/src/auto-suggest.js +154 -154
  25. package/src/badge.js +13 -13
  26. package/src/behavioral-drift.js +801 -801
  27. package/src/benchmark.js +67 -67
  28. package/src/catalog.js +103 -103
  29. package/src/certification.js +128 -128
  30. package/src/codex/config-parser.js +183 -183
  31. package/src/codex/context.js +223 -223
  32. package/src/codex/deep-review.js +493 -493
  33. package/src/codex/domain-packs.js +394 -394
  34. package/src/codex/freshness.js +84 -84
  35. package/src/codex/governance.js +192 -192
  36. package/src/codex/interactive.js +618 -618
  37. package/src/codex/mcp-packs.js +914 -914
  38. package/src/codex/patch.js +209 -209
  39. package/src/codex/plans.js +251 -251
  40. package/src/codex/premium.js +614 -614
  41. package/src/codex/setup.js +591 -591
  42. package/src/context.js +10 -4
  43. package/src/continuous-ops.js +681 -681
  44. package/src/copilot/activity.js +309 -309
  45. package/src/copilot/deep-review.js +346 -346
  46. package/src/copilot/domain-packs.js +372 -372
  47. package/src/copilot/freshness.js +57 -57
  48. package/src/copilot/governance.js +222 -222
  49. package/src/copilot/interactive.js +406 -406
  50. package/src/copilot/mcp-packs.js +826 -826
  51. package/src/copilot/plans.js +253 -253
  52. package/src/copilot/premium.js +451 -451
  53. package/src/copilot/setup.js +488 -488
  54. package/src/cost-tracking.js +61 -61
  55. package/src/cursor/activity.js +301 -301
  56. package/src/cursor/config-parser.js +265 -265
  57. package/src/cursor/context.js +256 -256
  58. package/src/cursor/deep-review.js +334 -334
  59. package/src/cursor/domain-packs.js +368 -368
  60. package/src/cursor/freshness.js +65 -65
  61. package/src/cursor/governance.js +229 -229
  62. package/src/cursor/interactive.js +391 -391
  63. package/src/cursor/mcp-packs.js +828 -828
  64. package/src/cursor/plans.js +254 -254
  65. package/src/cursor/premium.js +469 -469
  66. package/src/cursor/setup.js +488 -488
  67. package/src/dashboard.js +493 -493
  68. package/src/deep-review.js +428 -428
  69. package/src/deprecation.js +98 -98
  70. package/src/diff-only.js +280 -280
  71. package/src/doctor.js +119 -119
  72. package/src/domain-pack-expansion.js +1033 -1033
  73. package/src/domain-packs.js +387 -387
  74. package/src/feedback.js +178 -178
  75. package/src/fix-engine.js +783 -783
  76. package/src/fix-prompts.js +122 -122
  77. package/src/formatters/sarif.js +115 -115
  78. package/src/freshness.js +74 -74
  79. package/src/gemini/config-parser.js +275 -275
  80. package/src/gemini/deep-review.js +559 -559
  81. package/src/gemini/domain-packs.js +393 -393
  82. package/src/gemini/freshness.js +66 -66
  83. package/src/gemini/governance.js +201 -201
  84. package/src/gemini/interactive.js +860 -860
  85. package/src/gemini/mcp-packs.js +915 -915
  86. package/src/gemini/plans.js +269 -269
  87. package/src/gemini/premium.js +760 -760
  88. package/src/gemini/setup.js +692 -692
  89. package/src/governance.js +72 -72
  90. package/src/harmony/add.js +68 -68
  91. package/src/harmony/advisor.js +333 -333
  92. package/src/harmony/canon.js +565 -565
  93. package/src/harmony/cli.js +591 -591
  94. package/src/harmony/drift.js +401 -401
  95. package/src/harmony/governance.js +313 -313
  96. package/src/harmony/memory.js +239 -239
  97. package/src/harmony/sync.js +475 -475
  98. package/src/harmony/watch.js +370 -370
  99. package/src/hook-validation.js +342 -342
  100. package/src/index.js +271 -271
  101. package/src/init.js +184 -184
  102. package/src/instruction-surfaces.js +185 -185
  103. package/src/integrations.js +144 -144
  104. package/src/interactive.js +118 -118
  105. package/src/locales/en.json +1 -1
  106. package/src/locales/es.json +1 -1
  107. package/src/mcp-packs.js +830 -830
  108. package/src/mcp-server.js +726 -726
  109. package/src/mcp-validation.js +337 -337
  110. package/src/nerviq-sync.json +7 -7
  111. package/src/opencode/config-parser.js +109 -109
  112. package/src/opencode/context.js +247 -247
  113. package/src/opencode/deep-review.js +313 -313
  114. package/src/opencode/domain-packs.js +262 -262
  115. package/src/opencode/freshness.js +66 -66
  116. package/src/opencode/governance.js +159 -159
  117. package/src/opencode/interactive.js +392 -392
  118. package/src/opencode/mcp-packs.js +705 -705
  119. package/src/opencode/patch.js +184 -184
  120. package/src/opencode/plans.js +231 -231
  121. package/src/opencode/premium.js +413 -413
  122. package/src/opencode/setup.js +449 -449
  123. package/src/opencode/techniques.js +27 -27
  124. package/src/operating-profile.js +574 -574
  125. package/src/org.js +152 -152
  126. package/src/permission-rules.js +218 -218
  127. package/src/plans.js +839 -839
  128. package/src/platform-change-manifest.js +86 -86
  129. package/src/plugins.js +110 -110
  130. package/src/policy-layers.js +210 -210
  131. package/src/profiles.js +124 -124
  132. package/src/prompt-injection.js +74 -74
  133. package/src/public-api.js +173 -173
  134. package/src/recommendation-rules.js +84 -84
  135. package/src/repo-archetype.js +386 -386
  136. package/src/secret-patterns.js +39 -39
  137. package/src/server.js +527 -527
  138. package/src/setup/analysis.js +607 -607
  139. package/src/setup/runtime.js +172 -172
  140. package/src/setup.js +677 -677
  141. package/src/shared/capabilities.js +194 -194
  142. package/src/source-urls.js +132 -132
  143. package/src/stack-checks.js +565 -565
  144. package/src/supplemental-checks.js +13 -13
  145. package/src/synergy/adaptive.js +261 -261
  146. package/src/synergy/compensation.js +137 -137
  147. package/src/synergy/evidence.js +193 -193
  148. package/src/synergy/learning.js +199 -199
  149. package/src/synergy/patterns.js +227 -227
  150. package/src/synergy/ranking.js +83 -83
  151. package/src/synergy/report.js +165 -165
  152. package/src/synergy/routing.js +146 -146
  153. package/src/techniques/api.js +407 -407
  154. package/src/techniques/automation.js +316 -316
  155. package/src/techniques/compliance.js +257 -257
  156. package/src/techniques/hygiene.js +294 -294
  157. package/src/techniques/instructions.js +243 -243
  158. package/src/techniques/observability.js +226 -226
  159. package/src/techniques/optimization.js +142 -142
  160. package/src/techniques/quality.js +318 -318
  161. package/src/techniques/security.js +237 -237
  162. package/src/techniques/shared.js +443 -443
  163. package/src/techniques/stacks.js +2294 -2294
  164. package/src/techniques/tools.js +106 -106
  165. package/src/techniques/workflow.js +413 -413
  166. package/src/techniques.js +81 -81
  167. package/src/terminology.js +73 -73
  168. package/src/token-estimate.js +35 -35
  169. package/src/usage-patterns.js +99 -99
  170. package/src/verification-metadata.js +145 -145
  171. package/src/watch.js +247 -247
  172. package/src/windsurf/activity.js +302 -302
  173. package/src/windsurf/config-parser.js +267 -267
  174. package/src/windsurf/context.js +120 -10
  175. package/src/windsurf/deep-review.js +337 -337
  176. package/src/windsurf/domain-packs.js +370 -370
  177. package/src/windsurf/freshness.js +36 -36
  178. package/src/windsurf/governance.js +231 -231
  179. package/src/windsurf/interactive.js +388 -388
  180. package/src/windsurf/mcp-packs.js +792 -792
  181. package/src/windsurf/plans.js +247 -247
  182. package/src/windsurf/premium.js +468 -468
  183. package/src/windsurf/setup.js +471 -471
  184. package/src/windsurf/techniques.js +155 -33
  185. package/src/workspace.js +375 -375
@@ -1,565 +1,565 @@
1
- /**
2
- * Harmony Canon — Canonical Project Intelligence Model
3
- *
4
- * Reads ALL platform config files from a single project and builds a unified
5
- * understanding of instructions, MCP servers, trust posture, and governance
6
- * across Claude, Codex, Gemini, Copilot, Cursor, Windsurf, Aider, and OpenCode.
7
- */
8
-
9
- const fs = require('fs');
10
- const path = require('path');
11
- const { ProjectContext } = require('../context');
12
- const { CodexProjectContext } = require('../codex/context');
13
- const { GeminiProjectContext } = require('../gemini/context');
14
- const { CopilotProjectContext } = require('../copilot/context');
15
- const { CursorProjectContext } = require('../cursor/context');
16
- const { WindsurfProjectContext } = require('../windsurf/context');
17
- const { AiderProjectContext } = require('../aider/context');
18
- const { OpenCodeProjectContext } = require('../opencode/context');
19
- const { getCodexGovernanceSummary } = require('../codex/governance');
20
- const { getGeminiGovernanceSummary } = require('../gemini/governance');
21
- const { getCopilotGovernanceSummary } = require('../copilot/governance');
22
- const { getCursorGovernanceSummary } = require('../cursor/governance');
23
- const { getWindsurfGovernanceSummary } = require('../windsurf/governance');
24
- const { getAiderGovernanceSummary } = require('../aider/governance');
25
- const { getOpenCodeGovernanceSummary } = require('../opencode/governance');
26
- const { tryParseJsonc } = require('../opencode/config-parser');
27
-
28
- // ─── Platform detection signatures ──────────────────────────────────────────
29
-
30
- const PLATFORM_SIGNATURES = {
31
- claude: {
32
- label: 'Claude',
33
- detect: (dir) => {
34
- try {
35
- if (fs.existsSync(path.join(dir, 'CLAUDE.md'))) return true;
36
- if (fs.existsSync(path.join(dir, '.claude'))) return true;
37
- return false;
38
- } catch { return false; }
39
- },
40
- instructionFiles: ['CLAUDE.md', '.claude/CLAUDE.md'],
41
- configFiles: ['.claude/settings.json', '.claude/settings.local.json'],
42
- mcpFiles: ['.claude/settings.json'],
43
- rulesDir: '.claude/rules',
44
- hooksDir: '.claude/hooks',
45
- },
46
- codex: {
47
- label: 'Codex',
48
- detect: (dir) => {
49
- try {
50
- if (fs.existsSync(path.join(dir, 'AGENTS.md'))) return true;
51
- if (fs.existsSync(path.join(dir, '.codex'))) return true;
52
- return false;
53
- } catch { return false; }
54
- },
55
- instructionFiles: ['AGENTS.md'],
56
- configFiles: ['.codex/config.toml'],
57
- mcpFiles: [],
58
- rulesDir: null,
59
- hooksDir: null,
60
- },
61
- gemini: {
62
- label: 'Gemini CLI',
63
- detect: (dir) => {
64
- try {
65
- if (fs.existsSync(path.join(dir, 'GEMINI.md'))) return true;
66
- if (fs.existsSync(path.join(dir, '.gemini'))) return true;
67
- return false;
68
- } catch { return false; }
69
- },
70
- instructionFiles: ['GEMINI.md', '.gemini/GEMINI.md'],
71
- configFiles: ['.gemini/settings.json'],
72
- mcpFiles: ['.gemini/settings.json'],
73
- rulesDir: null,
74
- hooksDir: null,
75
- },
76
- copilot: {
77
- label: 'GitHub Copilot',
78
- detect: (dir) => {
79
- try {
80
- if (fs.existsSync(path.join(dir, '.github', 'copilot-instructions.md'))) return true;
81
- if (fs.existsSync(path.join(dir, '.vscode', 'mcp.json'))) return true;
82
- return false;
83
- } catch { return false; }
84
- },
85
- instructionFiles: ['.github/copilot-instructions.md'],
86
- configFiles: ['.vscode/settings.json'],
87
- mcpFiles: ['.vscode/mcp.json'],
88
- rulesDir: '.github/instructions',
89
- hooksDir: null,
90
- },
91
- cursor: {
92
- label: 'Cursor',
93
- detect: (dir) => {
94
- try {
95
- if (fs.existsSync(path.join(dir, '.cursor'))) return true;
96
- if (fs.existsSync(path.join(dir, '.cursorrules'))) return true;
97
- return false;
98
- } catch { return false; }
99
- },
100
- instructionFiles: ['.cursorrules'],
101
- configFiles: ['.cursor/mcp.json', '.cursor/environment.json'],
102
- mcpFiles: ['.cursor/mcp.json'],
103
- rulesDir: '.cursor/rules',
104
- hooksDir: null,
105
- },
106
- windsurf: {
107
- label: 'Windsurf',
108
- detect: (dir) => {
109
- try {
110
- if (fs.existsSync(path.join(dir, '.windsurfrules'))) return true;
111
- if (fs.existsSync(path.join(dir, '.windsurf'))) return true;
112
- return false;
113
- } catch { return false; }
114
- },
115
- instructionFiles: ['.windsurfrules'],
116
- configFiles: ['.windsurfrules', '.windsurf/mcp.json', '.cascadeignore'],
117
- mcpFiles: ['.windsurf/mcp.json'],
118
- rulesDir: '.windsurf/rules',
119
- hooksDir: '.windsurf/workflows',
120
- },
121
- aider: {
122
- label: 'Aider',
123
- detect: (dir) => {
124
- try {
125
- if (fs.existsSync(path.join(dir, '.aider.conf.yml'))) return true;
126
- if (fs.existsSync(path.join(dir, '.aiderignore'))) return true;
127
- return false;
128
- } catch { return false; }
129
- },
130
- instructionFiles: ['CONVENTIONS.md', '.aider.conventions.md'],
131
- configFiles: ['.aider.conf.yml', '.aider.model.settings.yml', '.aiderignore'],
132
- mcpFiles: [],
133
- rulesDir: null,
134
- hooksDir: null,
135
- },
136
- opencode: {
137
- label: 'OpenCode',
138
- detect: (dir) => {
139
- try {
140
- if (fs.existsSync(path.join(dir, 'opencode.json'))) return true;
141
- if (fs.existsSync(path.join(dir, 'opencode.jsonc'))) return true;
142
- if (fs.existsSync(path.join(dir, '.opencode'))) return true;
143
- return false;
144
- } catch { return false; }
145
- },
146
- instructionFiles: ['AGENTS.md', 'CLAUDE.md'],
147
- configFiles: ['opencode.json', 'opencode.jsonc'],
148
- mcpFiles: ['opencode.json', 'opencode.jsonc'],
149
- rulesDir: null,
150
- hooksDir: null,
151
- },
152
- };
153
-
154
- // ─── Context builders per platform ──────────────────────────────────────────
155
-
156
- const CONTEXT_BUILDERS = {
157
- claude: (dir) => new ProjectContext(dir),
158
- codex: (dir) => new CodexProjectContext(dir),
159
- gemini: (dir) => new GeminiProjectContext(dir),
160
- copilot: (dir) => new CopilotProjectContext(dir),
161
- cursor: (dir) => new CursorProjectContext(dir),
162
- windsurf: (dir) => new WindsurfProjectContext(dir),
163
- aider: (dir) => new AiderProjectContext(dir),
164
- opencode: (dir) => new OpenCodeProjectContext(dir),
165
- };
166
-
167
- const GOVERNANCE_GETTERS = {
168
- codex: getCodexGovernanceSummary,
169
- gemini: getGeminiGovernanceSummary,
170
- copilot: getCopilotGovernanceSummary,
171
- cursor: getCursorGovernanceSummary,
172
- windsurf: getWindsurfGovernanceSummary,
173
- aider: getAiderGovernanceSummary,
174
- opencode: getOpenCodeGovernanceSummary,
175
- };
176
-
177
- // ─── Helpers ────────────────────────────────────────────────────────────────
178
-
179
- function safeReadFile(filePath) {
180
- try {
181
- return fs.readFileSync(filePath, 'utf8');
182
- } catch {
183
- return null;
184
- }
185
- }
186
-
187
- function safeParseJson(content) {
188
- try {
189
- return JSON.parse(content);
190
- } catch {
191
- return null;
192
- }
193
- }
194
-
195
- function safeParseJsonc(content) {
196
- const parsed = tryParseJsonc(content);
197
- return parsed.ok ? parsed.data : null;
198
- }
199
-
200
- /**
201
- * Extract instruction text from a platform's instruction files.
202
- * Returns array of { file, content } for each found file.
203
- */
204
- function readInstructionFiles(dir, files) {
205
- const found = [];
206
- for (const file of files) {
207
- const content = safeReadFile(path.join(dir, file));
208
- if (content) {
209
- found.push({ file, content });
210
- }
211
- }
212
- return found;
213
- }
214
-
215
- /**
216
- * Extract MCP server names from Claude settings.json format.
217
- */
218
- function extractMcpFromClaudeSettings(content) {
219
- const json = safeParseJson(content);
220
- if (!json) return [];
221
- // Claude stores MCP in mcpServers key
222
- const servers = json.mcpServers || {};
223
- return Object.keys(servers).map(name => ({
224
- name,
225
- command: servers[name].command || null,
226
- args: servers[name].args || [],
227
- }));
228
- }
229
-
230
- /**
231
- * Extract MCP server names from Cursor/Copilot mcp.json format.
232
- */
233
- function extractMcpFromMcpJson(content) {
234
- const json = safeParseJson(content);
235
- if (!json) return [];
236
- const servers = json.mcpServers || json.servers || {};
237
- return Object.keys(servers).map(name => ({
238
- name,
239
- command: servers[name].command || null,
240
- args: servers[name].args || [],
241
- }));
242
- }
243
-
244
- function extractMcpFromOpenCodeConfig(content) {
245
- const json = safeParseJsonc(content);
246
- if (!json) return [];
247
- const servers = json.mcpServers || json.servers || {};
248
- return Object.keys(servers).map(name => ({
249
- name,
250
- command: servers[name].command || null,
251
- args: servers[name].args || [],
252
- }));
253
- }
254
-
255
- /**
256
- * Extract MCP server names from Gemini settings.json.
257
- */
258
- function extractMcpFromGeminiSettings(content) {
259
- const json = safeParseJson(content);
260
- if (!json) return [];
261
- const servers = json.mcpServers || {};
262
- return Object.keys(servers).map(name => ({
263
- name,
264
- command: servers[name].command || null,
265
- args: servers[name].args || [],
266
- }));
267
- }
268
-
269
- /**
270
- * Read MCP servers for a platform from its MCP config files.
271
- */
272
- function readMcpServers(dir, platform, mcpFiles) {
273
- const servers = [];
274
- for (const file of mcpFiles) {
275
- const content = safeReadFile(path.join(dir, file));
276
- if (!content) continue;
277
-
278
- let extracted = [];
279
- if (platform === 'claude') {
280
- extracted = extractMcpFromClaudeSettings(content);
281
- } else if (platform === 'gemini') {
282
- extracted = extractMcpFromGeminiSettings(content);
283
- } else if (platform === 'opencode') {
284
- extracted = extractMcpFromOpenCodeConfig(content);
285
- } else {
286
- extracted = extractMcpFromMcpJson(content);
287
- }
288
-
289
- for (const server of extracted) {
290
- servers.push({ ...server, sourceFile: file });
291
- }
292
- }
293
- return servers;
294
- }
295
-
296
- /**
297
- * Count rule files in a rules directory.
298
- */
299
- function countRuleFiles(dir, rulesDir) {
300
- if (!rulesDir) return { count: 0, files: [] };
301
- const fullPath = path.join(dir, rulesDir);
302
- try {
303
- const entries = fs.readdirSync(fullPath).filter(f => !f.startsWith('.'));
304
- return { count: entries.length, files: entries };
305
- } catch {
306
- return { count: 0, files: [] };
307
- }
308
- }
309
-
310
- /**
311
- * Detect trust posture for a platform based on its context.
312
- */
313
- function detectTrustPosture(platform, ctx) {
314
- if (platform === 'claude') {
315
- const settings = ctx.jsonFile('.claude/settings.json');
316
- if (!settings) return 'unknown';
317
- if (settings.bypassPermissions === true) return 'bypass';
318
- if (settings.permissions && settings.permissions.deny && settings.permissions.deny.length > 0) {
319
- return 'safe-write';
320
- }
321
- return 'default';
322
- }
323
-
324
- if (platform === 'codex') {
325
- const config = ctx.fileContent('.codex/config.toml') || '';
326
- if (config.includes('approval_policy') && config.includes('never')) return 'full-auto';
327
- if (config.includes('sandbox') && config.includes('read-only')) return 'locked-down';
328
- return 'standard';
329
- }
330
-
331
- if (platform === 'gemini') {
332
- const settings = ctx.jsonFile('.gemini/settings.json');
333
- if (!settings) return 'unknown';
334
- if (settings.sandboxMode === 'none') return 'unrestricted';
335
- return settings.sandboxMode || 'default';
336
- }
337
-
338
- if (platform === 'copilot') {
339
- // Copilot trust is primarily controlled by VS Code settings
340
- return 'default';
341
- }
342
-
343
- if (platform === 'cursor') {
344
- // Cursor has no sandbox equivalent
345
- return 'no-sandbox';
346
- }
347
-
348
- if (platform === 'windsurf') {
349
- if (ctx.fileContent('.cascadeignore')) return 'guarded';
350
- if (ctx.fileContent('.windsurf/mcp.json')) return 'team-managed';
351
- return 'foreground';
352
- }
353
-
354
- if (platform === 'aider') {
355
- const config = ctx.configContent ? (ctx.configContent() || '') : '';
356
- if (/^\s*(yes|yes-always)\s*:\s*true\b/m.test(config)) return 'full-auto';
357
- if (/^\s*auto-commits\s*:\s*true\b/m.test(config)) return 'git-guarded';
358
- return 'manual-review';
359
- }
360
-
361
- if (platform === 'opencode') {
362
- const config = ctx.configContent ? (ctx.configContent() || '') : '';
363
- if (/"\*"\s*:\s*"allow"/.test(config)) return 'unrestricted';
364
- if (/"(?:bash|edit|task|external_directory)"\s*:\s*"deny"/.test(config)) return 'locked-down';
365
- if (/"(?:bash|edit|task|external_directory)"\s*:\s*"ask"/.test(config)) return 'prompted';
366
- return 'standard';
367
- }
368
-
369
- return 'unknown';
370
- }
371
-
372
- // ─── Core functions ─────────────────────────────────────────────────────────
373
-
374
- /**
375
- * Detect which AI coding platforms are active in the given directory.
376
- * Returns array of { platform, label, detected: true }.
377
- */
378
- function detectActivePlatforms(dir) {
379
- const active = [];
380
- for (const [platform, sig] of Object.entries(PLATFORM_SIGNATURES)) {
381
- if (sig.detect(dir)) {
382
- active.push({
383
- platform,
384
- label: sig.label,
385
- detected: true,
386
- });
387
- }
388
- }
389
- return active;
390
- }
391
-
392
- /**
393
- * Build a canonical model of the project's AI platform configuration.
394
- *
395
- * Reads all platform config files from the given directory and produces a
396
- * unified view: active platforms, shared instructions, conflicting instructions,
397
- * MCP servers, trust posture, and governance summaries.
398
- *
399
- * @param {string} dir - Project root directory
400
- * @returns {object} Canonical model
401
- */
402
- function buildCanonicalModel(dir) {
403
- const activePlatforms = detectActivePlatforms(dir);
404
- const platformKeys = activePlatforms.map(p => p.platform);
405
-
406
- // Build per-platform details
407
- const platformDetails = {};
408
- for (const { platform } of activePlatforms) {
409
- const sig = PLATFORM_SIGNATURES[platform];
410
- const ctx = CONTEXT_BUILDERS[platform](dir);
411
- const instructions = readInstructionFiles(dir, sig.instructionFiles);
412
- const mcpServers = readMcpServers(dir, platform, sig.mcpFiles);
413
- const rules = countRuleFiles(dir, sig.rulesDir);
414
- const trust = detectTrustPosture(platform, ctx);
415
-
416
- let governance = null;
417
- if (GOVERNANCE_GETTERS[platform]) {
418
- try {
419
- governance = GOVERNANCE_GETTERS[platform]();
420
- } catch {
421
- governance = null;
422
- }
423
- }
424
-
425
- platformDetails[platform] = {
426
- platform,
427
- label: sig.label,
428
- instructionFiles: instructions,
429
- instructionContent: instructions.map(i => i.content).join('\n'),
430
- configFiles: sig.configFiles.filter(f => safeReadFile(path.join(dir, f)) !== null),
431
- mcpServers,
432
- rules,
433
- trustPosture: trust,
434
- governance,
435
- };
436
- }
437
-
438
- // Detect project name and stacks via Claude context (shared base)
439
- const baseCtx = new ProjectContext(dir);
440
- const { STACKS } = require('../techniques');
441
- const stacks = baseCtx.detectStacks(STACKS);
442
- const pkg = baseCtx.jsonFile('package.json');
443
- const projectName = (pkg && pkg.name) || path.basename(dir);
444
-
445
- // Detect shared vs conflicting instructions
446
- const sharedInstructions = [];
447
- const conflictingInstructions = [];
448
-
449
- if (platformKeys.length >= 2) {
450
- // Extract instruction lines per platform (non-empty, trimmed)
451
- const instructionSets = {};
452
- for (const key of platformKeys) {
453
- const content = platformDetails[key].instructionContent || '';
454
- const lines = content
455
- .split(/\r?\n/)
456
- .map(l => l.trim())
457
- .filter(l => l.length > 0 && !l.startsWith('#') && !l.startsWith('<!--'));
458
- instructionSets[key] = new Set(lines);
459
- }
460
-
461
- // Find lines shared across ALL platforms
462
- const allSets = Object.values(instructionSets);
463
- if (allSets.length >= 2) {
464
- const first = allSets[0];
465
- for (const line of first) {
466
- if (allSets.every(s => s.has(line))) {
467
- sharedInstructions.push(line);
468
- }
469
- }
470
- }
471
-
472
- // Detect known conflict patterns between instruction files
473
- const trustLevels = {};
474
- for (const key of platformKeys) {
475
- trustLevels[key] = platformDetails[key].trustPosture;
476
- }
477
- const uniqueTrust = new Set(Object.values(trustLevels));
478
- if (uniqueTrust.size > 1) {
479
- conflictingInstructions.push({
480
- type: 'trust-posture',
481
- description: 'Trust posture differs across platforms',
482
- details: trustLevels,
483
- });
484
- }
485
- }
486
-
487
- // Build unified MCP server list (union across all platforms)
488
- const mcpUnion = {};
489
- for (const key of platformKeys) {
490
- for (const server of platformDetails[key].mcpServers) {
491
- if (!mcpUnion[server.name]) {
492
- mcpUnion[server.name] = {
493
- name: server.name,
494
- command: server.command,
495
- args: server.args,
496
- platforms: [key],
497
- };
498
- } else {
499
- mcpUnion[server.name].platforms.push(key);
500
- }
501
- }
502
- }
503
-
504
- // Build trust posture summary
505
- const trustPosture = {};
506
- for (const key of platformKeys) {
507
- trustPosture[key] = platformDetails[key].trustPosture;
508
- }
509
-
510
- // Build governance summary
511
- const governanceSummary = {};
512
- for (const key of platformKeys) {
513
- governanceSummary[key] = platformDetails[key].governance;
514
- }
515
-
516
- // SD2: Adaptive project signals — infrastructure & tooling detection
517
- const projectSignals = {};
518
- const signalChecks = [
519
- { key: 'docker', label: 'Docker', files: ['Dockerfile', 'docker-compose.yml', 'docker-compose.yaml', '.dockerignore'] },
520
- { key: 'terraform', label: 'Terraform', files: ['main.tf', 'terraform.tf', '.terraform.lock.hcl'] },
521
- { key: 'kubernetes', label: 'Kubernetes', files: ['k8s/', 'kubernetes/', 'helm/', 'Chart.yaml'] },
522
- { key: 'ci-github', label: 'GitHub Actions', files: ['.github/workflows/'] },
523
- { key: 'ci-gitlab', label: 'GitLab CI', files: ['.gitlab-ci.yml'] },
524
- { key: 'pytest', label: 'pytest', files: ['pytest.ini', 'conftest.py', 'pyproject.toml'] },
525
- { key: 'jest', label: 'Jest', files: ['jest.config.js', 'jest.config.ts', 'jest.config.mjs'] },
526
- { key: 'migrations', label: 'DB Migrations', files: ['migrations/', 'alembic/', 'prisma/migrations/', 'db/migrate/'] },
527
- { key: 'monorepo', label: 'Monorepo', files: ['pnpm-workspace.yaml', 'lerna.json', 'nx.json', 'turbo.json'] },
528
- { key: 'openapi', label: 'OpenAPI', files: ['openapi.yaml', 'openapi.json', 'swagger.yaml', 'swagger.json'] },
529
- ];
530
- for (const signal of signalChecks) {
531
- const detected = signal.files.some(f => {
532
- const full = path.join(dir, f);
533
- try { return fs.existsSync(full); } catch { return false; }
534
- });
535
- if (detected) projectSignals[signal.key] = signal.label;
536
- }
537
-
538
- return {
539
- projectName,
540
- dir,
541
- stacks: stacks.map(s => s.key),
542
- projectSignals,
543
- activePlatforms: platformKeys.map(key => ({
544
- platform: key,
545
- label: platformDetails[key].label,
546
- instructionFiles: platformDetails[key].instructionFiles.map(i => i.file),
547
- configFiles: platformDetails[key].configFiles,
548
- mcpServerCount: platformDetails[key].mcpServers.length,
549
- ruleCount: platformDetails[key].rules.count,
550
- trustPosture: platformDetails[key].trustPosture,
551
- })),
552
- platformDetails,
553
- sharedInstructions,
554
- conflictingInstructions,
555
- mcpServers: mcpUnion,
556
- trustPosture,
557
- governanceSummary,
558
- };
559
- }
560
-
561
- module.exports = {
562
- buildCanonicalModel,
563
- detectActivePlatforms,
564
- PLATFORM_SIGNATURES,
565
- };
1
+ /**
2
+ * Harmony Canon — Canonical Project Intelligence Model
3
+ *
4
+ * Reads ALL platform config files from a single project and builds a unified
5
+ * understanding of instructions, MCP servers, trust posture, and governance
6
+ * across Claude, Codex, Gemini, Copilot, Cursor, Windsurf, Aider, and OpenCode.
7
+ */
8
+
9
+ const fs = require('fs');
10
+ const path = require('path');
11
+ const { ProjectContext } = require('../context');
12
+ const { CodexProjectContext } = require('../codex/context');
13
+ const { GeminiProjectContext } = require('../gemini/context');
14
+ const { CopilotProjectContext } = require('../copilot/context');
15
+ const { CursorProjectContext } = require('../cursor/context');
16
+ const { WindsurfProjectContext } = require('../windsurf/context');
17
+ const { AiderProjectContext } = require('../aider/context');
18
+ const { OpenCodeProjectContext } = require('../opencode/context');
19
+ const { getCodexGovernanceSummary } = require('../codex/governance');
20
+ const { getGeminiGovernanceSummary } = require('../gemini/governance');
21
+ const { getCopilotGovernanceSummary } = require('../copilot/governance');
22
+ const { getCursorGovernanceSummary } = require('../cursor/governance');
23
+ const { getWindsurfGovernanceSummary } = require('../windsurf/governance');
24
+ const { getAiderGovernanceSummary } = require('../aider/governance');
25
+ const { getOpenCodeGovernanceSummary } = require('../opencode/governance');
26
+ const { tryParseJsonc } = require('../opencode/config-parser');
27
+
28
+ // ─── Platform detection signatures ──────────────────────────────────────────
29
+
30
+ const PLATFORM_SIGNATURES = {
31
+ claude: {
32
+ label: 'Claude',
33
+ detect: (dir) => {
34
+ try {
35
+ if (fs.existsSync(path.join(dir, 'CLAUDE.md'))) return true;
36
+ if (fs.existsSync(path.join(dir, '.claude'))) return true;
37
+ return false;
38
+ } catch { return false; }
39
+ },
40
+ instructionFiles: ['CLAUDE.md', '.claude/CLAUDE.md'],
41
+ configFiles: ['.claude/settings.json', '.claude/settings.local.json'],
42
+ mcpFiles: ['.claude/settings.json'],
43
+ rulesDir: '.claude/rules',
44
+ hooksDir: '.claude/hooks',
45
+ },
46
+ codex: {
47
+ label: 'Codex',
48
+ detect: (dir) => {
49
+ try {
50
+ if (fs.existsSync(path.join(dir, 'AGENTS.md'))) return true;
51
+ if (fs.existsSync(path.join(dir, '.codex'))) return true;
52
+ return false;
53
+ } catch { return false; }
54
+ },
55
+ instructionFiles: ['AGENTS.md'],
56
+ configFiles: ['.codex/config.toml'],
57
+ mcpFiles: [],
58
+ rulesDir: null,
59
+ hooksDir: null,
60
+ },
61
+ gemini: {
62
+ label: 'Gemini CLI',
63
+ detect: (dir) => {
64
+ try {
65
+ if (fs.existsSync(path.join(dir, 'GEMINI.md'))) return true;
66
+ if (fs.existsSync(path.join(dir, '.gemini'))) return true;
67
+ return false;
68
+ } catch { return false; }
69
+ },
70
+ instructionFiles: ['GEMINI.md', '.gemini/GEMINI.md'],
71
+ configFiles: ['.gemini/settings.json'],
72
+ mcpFiles: ['.gemini/settings.json'],
73
+ rulesDir: null,
74
+ hooksDir: null,
75
+ },
76
+ copilot: {
77
+ label: 'GitHub Copilot',
78
+ detect: (dir) => {
79
+ try {
80
+ if (fs.existsSync(path.join(dir, '.github', 'copilot-instructions.md'))) return true;
81
+ if (fs.existsSync(path.join(dir, '.vscode', 'mcp.json'))) return true;
82
+ return false;
83
+ } catch { return false; }
84
+ },
85
+ instructionFiles: ['.github/copilot-instructions.md'],
86
+ configFiles: ['.vscode/settings.json'],
87
+ mcpFiles: ['.vscode/mcp.json'],
88
+ rulesDir: '.github/instructions',
89
+ hooksDir: null,
90
+ },
91
+ cursor: {
92
+ label: 'Cursor',
93
+ detect: (dir) => {
94
+ try {
95
+ if (fs.existsSync(path.join(dir, '.cursor'))) return true;
96
+ if (fs.existsSync(path.join(dir, '.cursorrules'))) return true;
97
+ return false;
98
+ } catch { return false; }
99
+ },
100
+ instructionFiles: ['.cursorrules'],
101
+ configFiles: ['.cursor/mcp.json', '.cursor/environment.json'],
102
+ mcpFiles: ['.cursor/mcp.json'],
103
+ rulesDir: '.cursor/rules',
104
+ hooksDir: null,
105
+ },
106
+ windsurf: {
107
+ label: 'Windsurf',
108
+ detect: (dir) => {
109
+ try {
110
+ if (fs.existsSync(path.join(dir, '.windsurfrules'))) return true;
111
+ if (fs.existsSync(path.join(dir, '.windsurf'))) return true;
112
+ return false;
113
+ } catch { return false; }
114
+ },
115
+ instructionFiles: ['.windsurfrules'],
116
+ configFiles: ['.windsurfrules', '.windsurf/mcp.json', '.cascadeignore'],
117
+ mcpFiles: ['.windsurf/mcp.json'],
118
+ rulesDir: '.windsurf/rules',
119
+ hooksDir: '.windsurf/workflows',
120
+ },
121
+ aider: {
122
+ label: 'Aider',
123
+ detect: (dir) => {
124
+ try {
125
+ if (fs.existsSync(path.join(dir, '.aider.conf.yml'))) return true;
126
+ if (fs.existsSync(path.join(dir, '.aiderignore'))) return true;
127
+ return false;
128
+ } catch { return false; }
129
+ },
130
+ instructionFiles: ['CONVENTIONS.md', '.aider.conventions.md'],
131
+ configFiles: ['.aider.conf.yml', '.aider.model.settings.yml', '.aiderignore'],
132
+ mcpFiles: [],
133
+ rulesDir: null,
134
+ hooksDir: null,
135
+ },
136
+ opencode: {
137
+ label: 'OpenCode',
138
+ detect: (dir) => {
139
+ try {
140
+ if (fs.existsSync(path.join(dir, 'opencode.json'))) return true;
141
+ if (fs.existsSync(path.join(dir, 'opencode.jsonc'))) return true;
142
+ if (fs.existsSync(path.join(dir, '.opencode'))) return true;
143
+ return false;
144
+ } catch { return false; }
145
+ },
146
+ instructionFiles: ['AGENTS.md', 'CLAUDE.md'],
147
+ configFiles: ['opencode.json', 'opencode.jsonc'],
148
+ mcpFiles: ['opencode.json', 'opencode.jsonc'],
149
+ rulesDir: null,
150
+ hooksDir: null,
151
+ },
152
+ };
153
+
154
+ // ─── Context builders per platform ──────────────────────────────────────────
155
+
156
+ const CONTEXT_BUILDERS = {
157
+ claude: (dir) => new ProjectContext(dir),
158
+ codex: (dir) => new CodexProjectContext(dir),
159
+ gemini: (dir) => new GeminiProjectContext(dir),
160
+ copilot: (dir) => new CopilotProjectContext(dir),
161
+ cursor: (dir) => new CursorProjectContext(dir),
162
+ windsurf: (dir) => new WindsurfProjectContext(dir),
163
+ aider: (dir) => new AiderProjectContext(dir),
164
+ opencode: (dir) => new OpenCodeProjectContext(dir),
165
+ };
166
+
167
+ const GOVERNANCE_GETTERS = {
168
+ codex: getCodexGovernanceSummary,
169
+ gemini: getGeminiGovernanceSummary,
170
+ copilot: getCopilotGovernanceSummary,
171
+ cursor: getCursorGovernanceSummary,
172
+ windsurf: getWindsurfGovernanceSummary,
173
+ aider: getAiderGovernanceSummary,
174
+ opencode: getOpenCodeGovernanceSummary,
175
+ };
176
+
177
+ // ─── Helpers ────────────────────────────────────────────────────────────────
178
+
179
+ function safeReadFile(filePath) {
180
+ try {
181
+ return fs.readFileSync(filePath, 'utf8');
182
+ } catch {
183
+ return null;
184
+ }
185
+ }
186
+
187
+ function safeParseJson(content) {
188
+ try {
189
+ return JSON.parse(content);
190
+ } catch {
191
+ return null;
192
+ }
193
+ }
194
+
195
+ function safeParseJsonc(content) {
196
+ const parsed = tryParseJsonc(content);
197
+ return parsed.ok ? parsed.data : null;
198
+ }
199
+
200
+ /**
201
+ * Extract instruction text from a platform's instruction files.
202
+ * Returns array of { file, content } for each found file.
203
+ */
204
+ function readInstructionFiles(dir, files) {
205
+ const found = [];
206
+ for (const file of files) {
207
+ const content = safeReadFile(path.join(dir, file));
208
+ if (content) {
209
+ found.push({ file, content });
210
+ }
211
+ }
212
+ return found;
213
+ }
214
+
215
+ /**
216
+ * Extract MCP server names from Claude settings.json format.
217
+ */
218
+ function extractMcpFromClaudeSettings(content) {
219
+ const json = safeParseJson(content);
220
+ if (!json) return [];
221
+ // Claude stores MCP in mcpServers key
222
+ const servers = json.mcpServers || {};
223
+ return Object.keys(servers).map(name => ({
224
+ name,
225
+ command: servers[name].command || null,
226
+ args: servers[name].args || [],
227
+ }));
228
+ }
229
+
230
+ /**
231
+ * Extract MCP server names from Cursor/Copilot mcp.json format.
232
+ */
233
+ function extractMcpFromMcpJson(content) {
234
+ const json = safeParseJson(content);
235
+ if (!json) return [];
236
+ const servers = json.mcpServers || json.servers || {};
237
+ return Object.keys(servers).map(name => ({
238
+ name,
239
+ command: servers[name].command || null,
240
+ args: servers[name].args || [],
241
+ }));
242
+ }
243
+
244
+ function extractMcpFromOpenCodeConfig(content) {
245
+ const json = safeParseJsonc(content);
246
+ if (!json) return [];
247
+ const servers = json.mcpServers || json.servers || {};
248
+ return Object.keys(servers).map(name => ({
249
+ name,
250
+ command: servers[name].command || null,
251
+ args: servers[name].args || [],
252
+ }));
253
+ }
254
+
255
+ /**
256
+ * Extract MCP server names from Gemini settings.json.
257
+ */
258
+ function extractMcpFromGeminiSettings(content) {
259
+ const json = safeParseJson(content);
260
+ if (!json) return [];
261
+ const servers = json.mcpServers || {};
262
+ return Object.keys(servers).map(name => ({
263
+ name,
264
+ command: servers[name].command || null,
265
+ args: servers[name].args || [],
266
+ }));
267
+ }
268
+
269
+ /**
270
+ * Read MCP servers for a platform from its MCP config files.
271
+ */
272
+ function readMcpServers(dir, platform, mcpFiles) {
273
+ const servers = [];
274
+ for (const file of mcpFiles) {
275
+ const content = safeReadFile(path.join(dir, file));
276
+ if (!content) continue;
277
+
278
+ let extracted = [];
279
+ if (platform === 'claude') {
280
+ extracted = extractMcpFromClaudeSettings(content);
281
+ } else if (platform === 'gemini') {
282
+ extracted = extractMcpFromGeminiSettings(content);
283
+ } else if (platform === 'opencode') {
284
+ extracted = extractMcpFromOpenCodeConfig(content);
285
+ } else {
286
+ extracted = extractMcpFromMcpJson(content);
287
+ }
288
+
289
+ for (const server of extracted) {
290
+ servers.push({ ...server, sourceFile: file });
291
+ }
292
+ }
293
+ return servers;
294
+ }
295
+
296
+ /**
297
+ * Count rule files in a rules directory.
298
+ */
299
+ function countRuleFiles(dir, rulesDir) {
300
+ if (!rulesDir) return { count: 0, files: [] };
301
+ const fullPath = path.join(dir, rulesDir);
302
+ try {
303
+ const entries = fs.readdirSync(fullPath).filter(f => !f.startsWith('.'));
304
+ return { count: entries.length, files: entries };
305
+ } catch {
306
+ return { count: 0, files: [] };
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Detect trust posture for a platform based on its context.
312
+ */
313
+ function detectTrustPosture(platform, ctx) {
314
+ if (platform === 'claude') {
315
+ const settings = ctx.jsonFile('.claude/settings.json');
316
+ if (!settings) return 'unknown';
317
+ if (settings.bypassPermissions === true) return 'bypass';
318
+ if (settings.permissions && settings.permissions.deny && settings.permissions.deny.length > 0) {
319
+ return 'safe-write';
320
+ }
321
+ return 'default';
322
+ }
323
+
324
+ if (platform === 'codex') {
325
+ const config = ctx.fileContent('.codex/config.toml') || '';
326
+ if (config.includes('approval_policy') && config.includes('never')) return 'full-auto';
327
+ if (config.includes('sandbox') && config.includes('read-only')) return 'locked-down';
328
+ return 'standard';
329
+ }
330
+
331
+ if (platform === 'gemini') {
332
+ const settings = ctx.jsonFile('.gemini/settings.json');
333
+ if (!settings) return 'unknown';
334
+ if (settings.sandboxMode === 'none') return 'unrestricted';
335
+ return settings.sandboxMode || 'default';
336
+ }
337
+
338
+ if (platform === 'copilot') {
339
+ // Copilot trust is primarily controlled by VS Code settings
340
+ return 'default';
341
+ }
342
+
343
+ if (platform === 'cursor') {
344
+ // Cursor has no sandbox equivalent
345
+ return 'no-sandbox';
346
+ }
347
+
348
+ if (platform === 'windsurf') {
349
+ if (ctx.fileContent('.cascadeignore')) return 'guarded';
350
+ if (ctx.fileContent('.windsurf/mcp.json')) return 'team-managed';
351
+ return 'foreground';
352
+ }
353
+
354
+ if (platform === 'aider') {
355
+ const config = ctx.configContent ? (ctx.configContent() || '') : '';
356
+ if (/^\s*(yes|yes-always)\s*:\s*true\b/m.test(config)) return 'full-auto';
357
+ if (/^\s*auto-commits\s*:\s*true\b/m.test(config)) return 'git-guarded';
358
+ return 'manual-review';
359
+ }
360
+
361
+ if (platform === 'opencode') {
362
+ const config = ctx.configContent ? (ctx.configContent() || '') : '';
363
+ if (/"\*"\s*:\s*"allow"/.test(config)) return 'unrestricted';
364
+ if (/"(?:bash|edit|task|external_directory)"\s*:\s*"deny"/.test(config)) return 'locked-down';
365
+ if (/"(?:bash|edit|task|external_directory)"\s*:\s*"ask"/.test(config)) return 'prompted';
366
+ return 'standard';
367
+ }
368
+
369
+ return 'unknown';
370
+ }
371
+
372
+ // ─── Core functions ─────────────────────────────────────────────────────────
373
+
374
+ /**
375
+ * Detect which AI coding platforms are active in the given directory.
376
+ * Returns array of { platform, label, detected: true }.
377
+ */
378
+ function detectActivePlatforms(dir) {
379
+ const active = [];
380
+ for (const [platform, sig] of Object.entries(PLATFORM_SIGNATURES)) {
381
+ if (sig.detect(dir)) {
382
+ active.push({
383
+ platform,
384
+ label: sig.label,
385
+ detected: true,
386
+ });
387
+ }
388
+ }
389
+ return active;
390
+ }
391
+
392
+ /**
393
+ * Build a canonical model of the project's AI platform configuration.
394
+ *
395
+ * Reads all platform config files from the given directory and produces a
396
+ * unified view: active platforms, shared instructions, conflicting instructions,
397
+ * MCP servers, trust posture, and governance summaries.
398
+ *
399
+ * @param {string} dir - Project root directory
400
+ * @returns {object} Canonical model
401
+ */
402
+ function buildCanonicalModel(dir) {
403
+ const activePlatforms = detectActivePlatforms(dir);
404
+ const platformKeys = activePlatforms.map(p => p.platform);
405
+
406
+ // Build per-platform details
407
+ const platformDetails = {};
408
+ for (const { platform } of activePlatforms) {
409
+ const sig = PLATFORM_SIGNATURES[platform];
410
+ const ctx = CONTEXT_BUILDERS[platform](dir);
411
+ const instructions = readInstructionFiles(dir, sig.instructionFiles);
412
+ const mcpServers = readMcpServers(dir, platform, sig.mcpFiles);
413
+ const rules = countRuleFiles(dir, sig.rulesDir);
414
+ const trust = detectTrustPosture(platform, ctx);
415
+
416
+ let governance = null;
417
+ if (GOVERNANCE_GETTERS[platform]) {
418
+ try {
419
+ governance = GOVERNANCE_GETTERS[platform]();
420
+ } catch {
421
+ governance = null;
422
+ }
423
+ }
424
+
425
+ platformDetails[platform] = {
426
+ platform,
427
+ label: sig.label,
428
+ instructionFiles: instructions,
429
+ instructionContent: instructions.map(i => i.content).join('\n'),
430
+ configFiles: sig.configFiles.filter(f => safeReadFile(path.join(dir, f)) !== null),
431
+ mcpServers,
432
+ rules,
433
+ trustPosture: trust,
434
+ governance,
435
+ };
436
+ }
437
+
438
+ // Detect project name and stacks via Claude context (shared base)
439
+ const baseCtx = new ProjectContext(dir);
440
+ const { STACKS } = require('../techniques');
441
+ const stacks = baseCtx.detectStacks(STACKS);
442
+ const pkg = baseCtx.jsonFile('package.json');
443
+ const projectName = (pkg && pkg.name) || path.basename(dir);
444
+
445
+ // Detect shared vs conflicting instructions
446
+ const sharedInstructions = [];
447
+ const conflictingInstructions = [];
448
+
449
+ if (platformKeys.length >= 2) {
450
+ // Extract instruction lines per platform (non-empty, trimmed)
451
+ const instructionSets = {};
452
+ for (const key of platformKeys) {
453
+ const content = platformDetails[key].instructionContent || '';
454
+ const lines = content
455
+ .split(/\r?\n/)
456
+ .map(l => l.trim())
457
+ .filter(l => l.length > 0 && !l.startsWith('#') && !l.startsWith('<!--'));
458
+ instructionSets[key] = new Set(lines);
459
+ }
460
+
461
+ // Find lines shared across ALL platforms
462
+ const allSets = Object.values(instructionSets);
463
+ if (allSets.length >= 2) {
464
+ const first = allSets[0];
465
+ for (const line of first) {
466
+ if (allSets.every(s => s.has(line))) {
467
+ sharedInstructions.push(line);
468
+ }
469
+ }
470
+ }
471
+
472
+ // Detect known conflict patterns between instruction files
473
+ const trustLevels = {};
474
+ for (const key of platformKeys) {
475
+ trustLevels[key] = platformDetails[key].trustPosture;
476
+ }
477
+ const uniqueTrust = new Set(Object.values(trustLevels));
478
+ if (uniqueTrust.size > 1) {
479
+ conflictingInstructions.push({
480
+ type: 'trust-posture',
481
+ description: 'Trust posture differs across platforms',
482
+ details: trustLevels,
483
+ });
484
+ }
485
+ }
486
+
487
+ // Build unified MCP server list (union across all platforms)
488
+ const mcpUnion = {};
489
+ for (const key of platformKeys) {
490
+ for (const server of platformDetails[key].mcpServers) {
491
+ if (!mcpUnion[server.name]) {
492
+ mcpUnion[server.name] = {
493
+ name: server.name,
494
+ command: server.command,
495
+ args: server.args,
496
+ platforms: [key],
497
+ };
498
+ } else {
499
+ mcpUnion[server.name].platforms.push(key);
500
+ }
501
+ }
502
+ }
503
+
504
+ // Build trust posture summary
505
+ const trustPosture = {};
506
+ for (const key of platformKeys) {
507
+ trustPosture[key] = platformDetails[key].trustPosture;
508
+ }
509
+
510
+ // Build governance summary
511
+ const governanceSummary = {};
512
+ for (const key of platformKeys) {
513
+ governanceSummary[key] = platformDetails[key].governance;
514
+ }
515
+
516
+ // SD2: Adaptive project signals — infrastructure & tooling detection
517
+ const projectSignals = {};
518
+ const signalChecks = [
519
+ { key: 'docker', label: 'Docker', files: ['Dockerfile', 'docker-compose.yml', 'docker-compose.yaml', '.dockerignore'] },
520
+ { key: 'terraform', label: 'Terraform', files: ['main.tf', 'terraform.tf', '.terraform.lock.hcl'] },
521
+ { key: 'kubernetes', label: 'Kubernetes', files: ['k8s/', 'kubernetes/', 'helm/', 'Chart.yaml'] },
522
+ { key: 'ci-github', label: 'GitHub Actions', files: ['.github/workflows/'] },
523
+ { key: 'ci-gitlab', label: 'GitLab CI', files: ['.gitlab-ci.yml'] },
524
+ { key: 'pytest', label: 'pytest', files: ['pytest.ini', 'conftest.py', 'pyproject.toml'] },
525
+ { key: 'jest', label: 'Jest', files: ['jest.config.js', 'jest.config.ts', 'jest.config.mjs'] },
526
+ { key: 'migrations', label: 'DB Migrations', files: ['migrations/', 'alembic/', 'prisma/migrations/', 'db/migrate/'] },
527
+ { key: 'monorepo', label: 'Monorepo', files: ['pnpm-workspace.yaml', 'lerna.json', 'nx.json', 'turbo.json'] },
528
+ { key: 'openapi', label: 'OpenAPI', files: ['openapi.yaml', 'openapi.json', 'swagger.yaml', 'swagger.json'] },
529
+ ];
530
+ for (const signal of signalChecks) {
531
+ const detected = signal.files.some(f => {
532
+ const full = path.join(dir, f);
533
+ try { return fs.existsSync(full); } catch { return false; }
534
+ });
535
+ if (detected) projectSignals[signal.key] = signal.label;
536
+ }
537
+
538
+ return {
539
+ projectName,
540
+ dir,
541
+ stacks: stacks.map(s => s.key),
542
+ projectSignals,
543
+ activePlatforms: platformKeys.map(key => ({
544
+ platform: key,
545
+ label: platformDetails[key].label,
546
+ instructionFiles: platformDetails[key].instructionFiles.map(i => i.file),
547
+ configFiles: platformDetails[key].configFiles,
548
+ mcpServerCount: platformDetails[key].mcpServers.length,
549
+ ruleCount: platformDetails[key].rules.count,
550
+ trustPosture: platformDetails[key].trustPosture,
551
+ })),
552
+ platformDetails,
553
+ sharedInstructions,
554
+ conflictingInstructions,
555
+ mcpServers: mcpUnion,
556
+ trustPosture,
557
+ governanceSummary,
558
+ };
559
+ }
560
+
561
+ module.exports = {
562
+ buildCanonicalModel,
563
+ detectActivePlatforms,
564
+ PLATFORM_SIGNATURES,
565
+ };