@nerviq/cli 1.11.0 → 1.12.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.
- package/README.md +97 -19
- package/bin/cli.js +618 -182
- package/package.json +2 -2
- package/src/activity.js +49 -9
- package/src/adoption-advisor.js +299 -0
- package/src/aider/techniques.js +16 -11
- package/src/analyze.js +128 -0
- package/src/anti-patterns.js +13 -0
- package/src/audit.js +97 -22
- package/src/behavioral-drift.js +801 -0
- package/src/continuous-ops.js +681 -0
- package/src/cost-tracking.js +61 -0
- package/src/cursor/techniques.js +17 -12
- package/src/deep-review.js +83 -0
- package/src/diff-only.js +280 -0
- package/src/doctor.js +118 -55
- package/src/governance.js +59 -43
- package/src/hook-validation.js +342 -0
- package/src/index.js +5 -0
- package/src/integrations.js +42 -5
- package/src/mcp-validation.js +337 -0
- package/src/opencode/techniques.js +12 -7
- package/src/operating-profile.js +574 -0
- package/src/org.js +97 -13
- package/src/plans.js +192 -8
- package/src/platform-change-manifest.js +86 -0
- package/src/policy-layers.js +210 -0
- package/src/profiles.js +4 -1
- package/src/prompt-injection.js +74 -0
- package/src/repo-archetype.js +386 -0
- package/src/setup.js +34 -0
- package/src/source-urls.js +132 -132
- package/src/supplemental-checks.js +13 -12
- package/src/techniques/api.js +407 -0
- package/src/techniques/automation.js +316 -0
- package/src/techniques/compliance.js +257 -0
- package/src/techniques/hygiene.js +294 -0
- package/src/techniques/instructions.js +243 -0
- package/src/techniques/observability.js +226 -0
- package/src/techniques/optimization.js +142 -0
- package/src/techniques/quality.js +317 -0
- package/src/techniques/security.js +237 -0
- package/src/techniques/shared.js +443 -0
- package/src/techniques/stacks.js +2294 -0
- package/src/techniques/tools.js +106 -0
- package/src/techniques/workflow.js +413 -0
- package/src/techniques.js +78 -5607
- package/src/watch.js +18 -0
- package/src/windsurf/techniques.js +17 -12
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { applyProfileToOptions } = require('./profiles');
|
|
4
|
+
|
|
5
|
+
const POLICY_FILES = {
|
|
6
|
+
org: path.join('.nerviq', 'org-policy.json'),
|
|
7
|
+
team: path.join('.nerviq', 'team-policy.json'),
|
|
8
|
+
repo: path.join('.nerviq', 'repo-policy.json'),
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
function normalizeArray(values) {
|
|
12
|
+
const seen = new Set();
|
|
13
|
+
const result = [];
|
|
14
|
+
for (const value of Array.isArray(values) ? values : []) {
|
|
15
|
+
const normalized = `${value || ''}`.trim();
|
|
16
|
+
if (!normalized || seen.has(normalized)) continue;
|
|
17
|
+
seen.add(normalized);
|
|
18
|
+
result.push(normalized);
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function readPolicyFile(filePath, layer) {
|
|
24
|
+
if (!filePath || !fs.existsSync(filePath)) return null;
|
|
25
|
+
|
|
26
|
+
let raw;
|
|
27
|
+
try {
|
|
28
|
+
raw = fs.readFileSync(filePath, 'utf8');
|
|
29
|
+
} catch {
|
|
30
|
+
return {
|
|
31
|
+
layer,
|
|
32
|
+
path: filePath,
|
|
33
|
+
valid: false,
|
|
34
|
+
error: 'could not read policy file',
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let parsed;
|
|
39
|
+
try {
|
|
40
|
+
parsed = JSON.parse(raw);
|
|
41
|
+
} catch {
|
|
42
|
+
return {
|
|
43
|
+
layer,
|
|
44
|
+
path: filePath,
|
|
45
|
+
valid: false,
|
|
46
|
+
error: 'invalid JSON',
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
layer,
|
|
52
|
+
path: filePath,
|
|
53
|
+
valid: true,
|
|
54
|
+
policy: {
|
|
55
|
+
name: parsed.name || `${layer} policy`,
|
|
56
|
+
description: parsed.description || '',
|
|
57
|
+
platforms: normalizeArray(parsed.platforms || []),
|
|
58
|
+
threshold: parsed.threshold != null ? Number(parsed.threshold) : null,
|
|
59
|
+
requireChecks: normalizeArray(parsed.requireChecks || []),
|
|
60
|
+
suppressedChecks: normalizeArray(parsed.suppressedChecks || []),
|
|
61
|
+
priorityBoosts: normalizeArray(parsed.priorityBoosts || []),
|
|
62
|
+
customWeights: parsed.customWeights && typeof parsed.customWeights === 'object' ? parsed.customWeights : {},
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function findAncestorOrgPolicy(dir) {
|
|
68
|
+
let current = path.resolve(dir);
|
|
69
|
+
let lastFound = null;
|
|
70
|
+
|
|
71
|
+
while (true) {
|
|
72
|
+
const candidate = path.join(current, POLICY_FILES.org);
|
|
73
|
+
if (fs.existsSync(candidate)) {
|
|
74
|
+
lastFound = candidate;
|
|
75
|
+
}
|
|
76
|
+
const parent = path.dirname(current);
|
|
77
|
+
if (parent === current) break;
|
|
78
|
+
current = parent;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return lastFound;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function resolvePolicyLayers(dir) {
|
|
85
|
+
const absoluteDir = path.resolve(dir);
|
|
86
|
+
const layers = [];
|
|
87
|
+
|
|
88
|
+
const orgLayer = readPolicyFile(findAncestorOrgPolicy(absoluteDir), 'org');
|
|
89
|
+
if (orgLayer) layers.push(orgLayer);
|
|
90
|
+
|
|
91
|
+
const teamLayer = readPolicyFile(path.join(absoluteDir, POLICY_FILES.team), 'team');
|
|
92
|
+
if (teamLayer) layers.push(teamLayer);
|
|
93
|
+
|
|
94
|
+
const repoLayer = readPolicyFile(path.join(absoluteDir, POLICY_FILES.repo), 'repo');
|
|
95
|
+
if (repoLayer) layers.push(repoLayer);
|
|
96
|
+
|
|
97
|
+
const resolved = {
|
|
98
|
+
platforms: [],
|
|
99
|
+
threshold: null,
|
|
100
|
+
requireChecks: [],
|
|
101
|
+
suppressedChecks: [],
|
|
102
|
+
priorityBoosts: [],
|
|
103
|
+
customWeights: {},
|
|
104
|
+
description: '',
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const fieldSources = {
|
|
108
|
+
platforms: [],
|
|
109
|
+
threshold: null,
|
|
110
|
+
requireChecks: [],
|
|
111
|
+
suppressedChecks: [],
|
|
112
|
+
priorityBoosts: [],
|
|
113
|
+
customWeights: [],
|
|
114
|
+
description: null,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
for (const layer of layers) {
|
|
118
|
+
if (!layer.valid || !layer.policy) continue;
|
|
119
|
+
const { policy } = layer;
|
|
120
|
+
|
|
121
|
+
if (policy.platforms.length > 0) {
|
|
122
|
+
resolved.platforms = [...policy.platforms];
|
|
123
|
+
fieldSources.platforms = [layer.layer];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (policy.threshold != null && Number.isFinite(policy.threshold)) {
|
|
127
|
+
resolved.threshold = policy.threshold;
|
|
128
|
+
fieldSources.threshold = layer.layer;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (policy.requireChecks.length > 0) {
|
|
132
|
+
resolved.requireChecks = normalizeArray([...resolved.requireChecks, ...policy.requireChecks]);
|
|
133
|
+
fieldSources.requireChecks = normalizeArray([...fieldSources.requireChecks, layer.layer]);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (policy.suppressedChecks.length > 0) {
|
|
137
|
+
resolved.suppressedChecks = normalizeArray([...resolved.suppressedChecks, ...policy.suppressedChecks]);
|
|
138
|
+
fieldSources.suppressedChecks = normalizeArray([...fieldSources.suppressedChecks, layer.layer]);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (policy.priorityBoosts.length > 0) {
|
|
142
|
+
resolved.priorityBoosts = normalizeArray([...resolved.priorityBoosts, ...policy.priorityBoosts]);
|
|
143
|
+
fieldSources.priorityBoosts = normalizeArray([...fieldSources.priorityBoosts, layer.layer]);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (Object.keys(policy.customWeights).length > 0) {
|
|
147
|
+
resolved.customWeights = { ...resolved.customWeights, ...policy.customWeights };
|
|
148
|
+
fieldSources.customWeights = normalizeArray([...fieldSources.customWeights, layer.layer]);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (policy.description) {
|
|
152
|
+
resolved.description = policy.description;
|
|
153
|
+
fieldSources.description = layer.layer;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
dir: absoluteDir,
|
|
159
|
+
overrideOrder: ['org', 'team', 'repo', 'explicit-cli'],
|
|
160
|
+
layers,
|
|
161
|
+
resolved,
|
|
162
|
+
fieldSources,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function applyPolicyLayersToOptions(contract, options) {
|
|
167
|
+
if (!contract || !contract.resolved) {
|
|
168
|
+
return { ...options };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return applyProfileToOptions(contract.resolved, options);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function formatPolicyContract(contract) {
|
|
175
|
+
if (!contract || !Array.isArray(contract.layers) || contract.layers.length === 0) {
|
|
176
|
+
return ' No org/team/repo policy layers found.';
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const lines = [
|
|
180
|
+
' Policy layers (override order: org -> team -> repo -> explicit CLI):',
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
for (const layer of contract.layers) {
|
|
184
|
+
if (!layer.valid) {
|
|
185
|
+
lines.push(` - ${layer.layer}: ${layer.path} [invalid: ${layer.error}]`);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const policy = layer.policy || {};
|
|
190
|
+
const details = [];
|
|
191
|
+
if (policy.platforms?.length > 0) details.push(`platforms=${policy.platforms.join(', ')}`);
|
|
192
|
+
if (policy.threshold != null) details.push(`threshold=${policy.threshold}`);
|
|
193
|
+
if (policy.requireChecks?.length > 0) details.push(`require=${policy.requireChecks.join(', ')}`);
|
|
194
|
+
lines.push(` - ${layer.layer}: ${path.relative(contract.dir, layer.path) || layer.path}${details.length > 0 ? ` (${details.join('; ')})` : ''}`);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const resolved = contract.resolved || {};
|
|
198
|
+
lines.push(' Resolved policy:');
|
|
199
|
+
lines.push(` - Platforms: ${resolved.platforms?.length > 0 ? resolved.platforms.join(', ') : 'default CLI platform'}`);
|
|
200
|
+
lines.push(` - Threshold: ${resolved.threshold != null ? resolved.threshold : 'default'}`);
|
|
201
|
+
lines.push(` - Required checks: ${resolved.requireChecks?.length > 0 ? resolved.requireChecks.join(', ') : 'none'}`);
|
|
202
|
+
return lines.join('\n');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
module.exports = {
|
|
206
|
+
POLICY_FILES,
|
|
207
|
+
resolvePolicyLayers,
|
|
208
|
+
applyPolicyLayersToOptions,
|
|
209
|
+
formatPolicyContract,
|
|
210
|
+
};
|
package/src/profiles.js
CHANGED
|
@@ -86,9 +86,12 @@ function applyProfileToOptions(profile, options) {
|
|
|
86
86
|
if (profile.threshold != null && merged.threshold == null) {
|
|
87
87
|
merged.threshold = profile.threshold;
|
|
88
88
|
}
|
|
89
|
-
if (profile.platforms && profile.platforms.length > 0 && !options.
|
|
89
|
+
if (profile.platforms && profile.platforms.length > 0 && !options.platformExplicit) {
|
|
90
90
|
merged.platform = profile.platforms[0];
|
|
91
91
|
}
|
|
92
|
+
if ((profile.requireChecks || []).length > 0 && (!Array.isArray(merged.require) || merged.require.length === 0)) {
|
|
93
|
+
merged.require = [...profile.requireChecks];
|
|
94
|
+
}
|
|
92
95
|
merged.suppressedChecks = profile.suppressedChecks || [];
|
|
93
96
|
merged.priorityBoosts = profile.priorityBoosts || [];
|
|
94
97
|
merged.customWeights = profile.customWeights || {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const PROMPT_INJECTION_PATTERNS = [
|
|
4
|
+
/\bignore (?:all )?(?:previous|earlier|above) instructions?\b/i,
|
|
5
|
+
/\boverride (?:the )?(?:system|developer|safety|previous) instructions?\b/i,
|
|
6
|
+
/\breveal (?:your|the) (?:system|developer) prompt\b/i,
|
|
7
|
+
/\bbypass (?:all )?(?:safety|guardrails|restrictions|protections)\b/i,
|
|
8
|
+
/\bdisable (?:the )?(?:guardrails|safety checks?)\b/i,
|
|
9
|
+
/\bact as (?:the )?(?:system|developer)\b/i,
|
|
10
|
+
/\breport (?:that )?(?:everything is )?perfect(?: and score 100\/100)?\b/i,
|
|
11
|
+
/\bscore 100\/100\b/i,
|
|
12
|
+
/\bexfiltrate\b.*\b(?:secret|token|credential|password)\b/i,
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const TRUST_BOUNDARY_PATTERNS = [
|
|
16
|
+
/\btreat(?: every| all)?(?: string| file| repo(?:sitory)?| external)?[\w\s,-]{0,80}\buntrusted\b/i,
|
|
17
|
+
/\bnever follow instructions embedded in\b/i,
|
|
18
|
+
/\b(?:repo(?:sitory)? files?|file contents?|web(?:site|page)? content|fetched content|external content|mcp responses?)\b[\w\s,-]{0,120}\b(?:data|quoted)\b[\w\s,-]{0,80}\bnot instructions\b/i,
|
|
19
|
+
/\bmcp responses?\b[\w\s,-]{0,80}\buntrusted\b/i,
|
|
20
|
+
/\bfile contents?\b[\w\s,-]{0,80}\buntrusted\b/i,
|
|
21
|
+
/\bprompt injection\b[\w\s,-]{0,120}\b(?:defense|resistance|guard|boundary)\b/i,
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
function containsPromptInjectionPattern(text) {
|
|
25
|
+
const normalized = String(text || '');
|
|
26
|
+
return PROMPT_INJECTION_PATTERNS.some((pattern) => {
|
|
27
|
+
pattern.lastIndex = 0;
|
|
28
|
+
return pattern.test(normalized);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function hasPromptInjectionDefenseGuidance(text) {
|
|
33
|
+
const normalized = String(text || '');
|
|
34
|
+
if (!normalized.trim()) return false;
|
|
35
|
+
return TRUST_BOUNDARY_PATTERNS.some((pattern) => {
|
|
36
|
+
pattern.lastIndex = 0;
|
|
37
|
+
return pattern.test(normalized);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function hasMcpPromptInjectionDefenseGuidance(text) {
|
|
42
|
+
const normalized = String(text || '');
|
|
43
|
+
if (!/\bmcp\b/i.test(normalized)) return false;
|
|
44
|
+
return hasPromptInjectionDefenseGuidance(normalized);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function collectHookBlocks(settings) {
|
|
48
|
+
if (!settings || !settings.hooks || typeof settings.hooks !== 'object') return [];
|
|
49
|
+
const blocks = [];
|
|
50
|
+
for (const [eventName, entries] of Object.entries(settings.hooks)) {
|
|
51
|
+
if (!Array.isArray(entries)) continue;
|
|
52
|
+
for (const entry of entries) {
|
|
53
|
+
blocks.push({ eventName, entry });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return blocks;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function hasInjectionDefenseHookConfigured(settings) {
|
|
60
|
+
return collectHookBlocks(settings).some(({ eventName, entry }) => {
|
|
61
|
+
if (eventName !== 'PostToolUse') return false;
|
|
62
|
+
const matcher = `${entry && entry.matcher ? entry.matcher : ''}`;
|
|
63
|
+
if (!/WebFetch|WebSearch|Read|Grep|Glob|mcp__/i.test(matcher)) return false;
|
|
64
|
+
const hooks = Array.isArray(entry && entry.hooks) ? entry.hooks : [];
|
|
65
|
+
return hooks.some((hook) => /injection|prompt|sanitize|untrusted/i.test(`${hook && hook.command ? hook.command : ''}`));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = {
|
|
70
|
+
containsPromptInjectionPattern,
|
|
71
|
+
hasPromptInjectionDefenseGuidance,
|
|
72
|
+
hasMcpPromptInjectionDefenseGuidance,
|
|
73
|
+
hasInjectionDefenseHookConfigured,
|
|
74
|
+
};
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function unique(values) {
|
|
4
|
+
return [...new Set((values || []).filter(Boolean))];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function hasAny(set, values) {
|
|
8
|
+
return values.some((value) => set.has(value));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function humanizeMaturity(maturity) {
|
|
12
|
+
const labels = {
|
|
13
|
+
none: 'No governed baseline yet',
|
|
14
|
+
starter: 'Starter baseline',
|
|
15
|
+
developing: 'Developing baseline',
|
|
16
|
+
mature: 'Mature governed baseline',
|
|
17
|
+
};
|
|
18
|
+
return labels[maturity] || maturity;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function detectTopology(ctx, pkg) {
|
|
22
|
+
const signals = [];
|
|
23
|
+
const hasPackageWorkspaces = Array.isArray(pkg.workspaces) ||
|
|
24
|
+
Boolean(pkg.workspaces && Array.isArray(pkg.workspaces.packages));
|
|
25
|
+
|
|
26
|
+
if (hasPackageWorkspaces) signals.push('package.json workspaces');
|
|
27
|
+
if (ctx.fileContent('pnpm-workspace.yaml')) signals.push('pnpm-workspace.yaml');
|
|
28
|
+
if (ctx.fileContent('nx.json')) signals.push('nx.json');
|
|
29
|
+
if (ctx.fileContent('turbo.json')) signals.push('turbo.json');
|
|
30
|
+
if (ctx.fileContent('lerna.json')) signals.push('lerna.json');
|
|
31
|
+
if (ctx.hasDir('packages')) signals.push('packages/');
|
|
32
|
+
if (ctx.hasDir('apps')) signals.push('apps/');
|
|
33
|
+
|
|
34
|
+
if (signals.length > 0) {
|
|
35
|
+
return {
|
|
36
|
+
key: 'monorepo',
|
|
37
|
+
label: 'Monorepo',
|
|
38
|
+
rationale: 'Workspace or multi-package signals indicate a shared-root repo topology.',
|
|
39
|
+
signals,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const multiSurfaceSignals = [];
|
|
44
|
+
if (ctx.hasDir('app')) multiSurfaceSignals.push('app/');
|
|
45
|
+
if (ctx.hasDir('api')) multiSurfaceSignals.push('api/');
|
|
46
|
+
if (ctx.hasDir('services')) multiSurfaceSignals.push('services/');
|
|
47
|
+
if (ctx.hasDir('workers')) multiSurfaceSignals.push('workers/');
|
|
48
|
+
|
|
49
|
+
if (multiSurfaceSignals.length >= 2) {
|
|
50
|
+
return {
|
|
51
|
+
key: 'multi-surface',
|
|
52
|
+
label: 'Multi-surface repo',
|
|
53
|
+
rationale: 'Several product/service surfaces live in one repository even without formal workspace tooling.',
|
|
54
|
+
signals: multiSurfaceSignals,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
key: 'single-repo',
|
|
60
|
+
label: 'Single-repo',
|
|
61
|
+
rationale: 'No workspace or multi-package signals detected.',
|
|
62
|
+
signals: [],
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function detectStackFamily(stackKeys, domainKeys, ctx) {
|
|
67
|
+
if (hasAny(stackKeys, ['flutter', 'swift', 'kotlin']) || domainKeys.has('mobile')) {
|
|
68
|
+
return { key: 'mobile', label: 'Mobile', rationale: 'Native or mobile-platform stacks dominate the repo.' };
|
|
69
|
+
}
|
|
70
|
+
if (domainKeys.has('infra-platform') || domainKeys.has('devops-cicd') || hasAny(stackKeys, ['terraform', 'kubernetes', 'docker'])) {
|
|
71
|
+
return { key: 'infra', label: 'Infrastructure / platform', rationale: 'Deployment, infrastructure, or CI surfaces are central.' };
|
|
72
|
+
}
|
|
73
|
+
if (domainKeys.has('ai-ml') || domainKeys.has('data-pipeline') || ctx.hasDir('experiments') || ctx.hasDir('notebooks')) {
|
|
74
|
+
return { key: 'data-ml', label: 'Data / AI / ML', rationale: 'Pipeline, experiment, or model workflow signals are present.' };
|
|
75
|
+
}
|
|
76
|
+
const hasFrontend = hasAny(stackKeys, ['react', 'nextjs', 'vue', 'angular', 'svelte']) || ctx.hasDir('components');
|
|
77
|
+
const hasBackend = hasAny(stackKeys, ['node', 'python', 'django', 'fastapi', 'go', 'rust', 'java', 'ruby', 'php', 'dotnet']) || ctx.hasDir('api') || ctx.hasDir('services');
|
|
78
|
+
if (hasFrontend && hasBackend) {
|
|
79
|
+
return { key: 'fullstack', label: 'Full-stack application', rationale: 'Both UI and service-layer signals are present.' };
|
|
80
|
+
}
|
|
81
|
+
if (hasFrontend) {
|
|
82
|
+
return { key: 'frontend', label: 'Frontend application', rationale: 'UI and component signals dominate the repo.' };
|
|
83
|
+
}
|
|
84
|
+
if (hasBackend) {
|
|
85
|
+
return { key: 'backend', label: 'Backend service', rationale: 'Service, API, or backend stack signals dominate the repo.' };
|
|
86
|
+
}
|
|
87
|
+
if (domainKeys.has('docs-content')) {
|
|
88
|
+
return { key: 'docs', label: 'Docs / content', rationale: 'Documentation and content workflow signals dominate the repo.' };
|
|
89
|
+
}
|
|
90
|
+
return { key: 'general', label: 'General codebase', rationale: 'No strong stack family dominates yet.' };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function detectRepoClass({ ctx, pkg, domainKeys, stackKeys, topology }) {
|
|
94
|
+
const signals = [];
|
|
95
|
+
const hasFrontend = hasAny(stackKeys, ['react', 'nextjs', 'vue', 'angular', 'svelte']) || ctx.hasDir('components') || ctx.hasDir('pages');
|
|
96
|
+
const hasBackend = hasAny(stackKeys, ['node', 'python', 'django', 'fastapi', 'go', 'rust', 'java', 'ruby', 'php', 'dotnet']) || ctx.hasDir('api') || ctx.hasDir('services') || ctx.hasDir('routes');
|
|
97
|
+
const isInfra = domainKeys.has('infra-platform') || domainKeys.has('devops-cicd') || hasAny(stackKeys, ['terraform', 'kubernetes', 'docker']) || ctx.hasDir('infra') || ctx.hasDir('deploy') || ctx.hasDir('helm');
|
|
98
|
+
const isMl = domainKeys.has('ai-ml') || domainKeys.has('data-pipeline') || ctx.hasDir('experiments') || ctx.hasDir('datasets') || ctx.hasDir('notebooks');
|
|
99
|
+
const isMobile = hasAny(stackKeys, ['flutter', 'swift', 'kotlin']) || domainKeys.has('mobile') || ctx.hasDir('ios') || ctx.hasDir('android');
|
|
100
|
+
const isLibrary = domainKeys.has('oss-library') || ((!hasFrontend && !hasBackend) && Boolean(pkg.main || pkg.module || pkg.exports || pkg.types));
|
|
101
|
+
const isCli = domainKeys.has('cli-tool') || Boolean(pkg.bin) || ctx.hasDir('bin');
|
|
102
|
+
|
|
103
|
+
if (topology.key === 'monorepo' && isInfra) {
|
|
104
|
+
signals.push('workspace topology', 'infra / deploy signals');
|
|
105
|
+
return {
|
|
106
|
+
key: 'platform-monorepo',
|
|
107
|
+
label: 'Platform monorepo',
|
|
108
|
+
rationale: 'Shared-root workspace plus infrastructure signals indicate a platform-style repo.',
|
|
109
|
+
signals,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (topology.key === 'monorepo' && (hasFrontend || hasBackend)) {
|
|
114
|
+
signals.push('workspace topology', hasFrontend ? 'frontend signals' : null, hasBackend ? 'backend signals' : null);
|
|
115
|
+
return {
|
|
116
|
+
key: 'application-monorepo',
|
|
117
|
+
label: 'Application monorepo',
|
|
118
|
+
rationale: 'Multiple app/service surfaces share one governed root.',
|
|
119
|
+
signals: unique(signals),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (isInfra) {
|
|
124
|
+
signals.push('infra / deploy signals');
|
|
125
|
+
return {
|
|
126
|
+
key: 'infra-platform',
|
|
127
|
+
label: 'Infrastructure / platform repo',
|
|
128
|
+
rationale: 'Infrastructure or release-engineering surfaces define the repo.',
|
|
129
|
+
signals,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (isMobile) {
|
|
134
|
+
signals.push('mobile stack signals');
|
|
135
|
+
return {
|
|
136
|
+
key: 'mobile-app',
|
|
137
|
+
label: 'Mobile application',
|
|
138
|
+
rationale: 'Mobile-native directories or stacks define the repo.',
|
|
139
|
+
signals,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (isMl) {
|
|
144
|
+
signals.push('data / ML workflow signals');
|
|
145
|
+
return {
|
|
146
|
+
key: 'data-ml-system',
|
|
147
|
+
label: 'Data / AI system',
|
|
148
|
+
rationale: 'Pipelines, experiments, or model workflows are central.',
|
|
149
|
+
signals,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (isCli) {
|
|
154
|
+
signals.push('CLI distribution surface');
|
|
155
|
+
return {
|
|
156
|
+
key: 'developer-tool',
|
|
157
|
+
label: 'Developer tool',
|
|
158
|
+
rationale: 'Command-line packaging or tool UX is part of the product contract.',
|
|
159
|
+
signals,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (isLibrary) {
|
|
164
|
+
signals.push('package export surface');
|
|
165
|
+
return {
|
|
166
|
+
key: 'library-sdk',
|
|
167
|
+
label: 'Library / SDK',
|
|
168
|
+
rationale: 'The repo behaves like a reusable package rather than a deployed application.',
|
|
169
|
+
signals,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (hasFrontend && hasBackend) {
|
|
174
|
+
signals.push('frontend signals', 'backend signals');
|
|
175
|
+
return {
|
|
176
|
+
key: 'fullstack-product',
|
|
177
|
+
label: 'Full-stack product',
|
|
178
|
+
rationale: 'UI and service layers coexist as one application surface.',
|
|
179
|
+
signals,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (hasFrontend) {
|
|
184
|
+
signals.push('frontend signals');
|
|
185
|
+
return {
|
|
186
|
+
key: 'frontend-product',
|
|
187
|
+
label: 'Frontend product',
|
|
188
|
+
rationale: 'UI and component workflows dominate the repo.',
|
|
189
|
+
signals,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (hasBackend) {
|
|
194
|
+
signals.push('backend signals');
|
|
195
|
+
return {
|
|
196
|
+
key: 'backend-service',
|
|
197
|
+
label: 'Backend service',
|
|
198
|
+
rationale: 'API or service workflows dominate the repo.',
|
|
199
|
+
signals,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
key: 'general-codebase',
|
|
205
|
+
label: 'General codebase',
|
|
206
|
+
rationale: 'No stronger repo class dominates yet.',
|
|
207
|
+
signals: [],
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function detectWorkflowTraits({ ctx, platform, assets, maturity, topology, domainKeys, recommendedMcpPacks = [] }) {
|
|
212
|
+
const traits = [];
|
|
213
|
+
const hasCi = ctx.hasDir('.github/workflows') || ctx.hasDir('.circleci') || Boolean(ctx.fileContent('.gitlab-ci.yml')) || Boolean(ctx.fileContent('Jenkinsfile'));
|
|
214
|
+
const hasMcp = (assets && assets.counts && assets.counts.mcpServers > 0) || (recommendedMcpPacks || []).length > 0;
|
|
215
|
+
const hasGovernanceSignals = domainKeys.has('enterprise-governed') || domainKeys.has('regulated-lite') || domainKeys.has('security-focused');
|
|
216
|
+
|
|
217
|
+
if (hasGovernanceSignals && hasCi) {
|
|
218
|
+
traits.push({
|
|
219
|
+
key: 'governed-rollout',
|
|
220
|
+
label: 'Governed rollout',
|
|
221
|
+
rationale: 'CI plus governance/regulatory signals suggest reviewable rollout discipline.',
|
|
222
|
+
signals: unique([
|
|
223
|
+
domainKeys.has('enterprise-governed') ? 'enterprise-governed pack' : null,
|
|
224
|
+
domainKeys.has('regulated-lite') ? 'regulated-lite pack' : null,
|
|
225
|
+
domainKeys.has('security-focused') ? 'security-focused pack' : null,
|
|
226
|
+
hasCi ? 'CI workflows' : null,
|
|
227
|
+
]),
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (topology.key === 'monorepo') {
|
|
232
|
+
traits.push({
|
|
233
|
+
key: 'workspace-coordinated',
|
|
234
|
+
label: 'Workspace-coordinated',
|
|
235
|
+
rationale: 'The repo needs path-aware and package-aware workflow coordination.',
|
|
236
|
+
signals: ['workspace topology'],
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (hasCi) {
|
|
241
|
+
traits.push({
|
|
242
|
+
key: 'ci-driven',
|
|
243
|
+
label: 'CI-driven',
|
|
244
|
+
rationale: 'Automated CI surfaces are part of the normal engineering loop.',
|
|
245
|
+
signals: ['CI workflows'],
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (hasMcp) {
|
|
250
|
+
traits.push({
|
|
251
|
+
key: 'tool-enriched',
|
|
252
|
+
label: 'Tool-enriched',
|
|
253
|
+
rationale: 'Live MCP or external-context tooling is part of the intended workflow.',
|
|
254
|
+
signals: unique([
|
|
255
|
+
assets && assets.counts && assets.counts.mcpServers > 0 ? 'declared MCP servers' : null,
|
|
256
|
+
recommendedMcpPacks.length > 0 ? 'recommended MCP packs' : null,
|
|
257
|
+
]),
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (maturity === 'mature' || maturity === 'developing') {
|
|
262
|
+
traits.push({
|
|
263
|
+
key: 'managed',
|
|
264
|
+
label: 'Managed baseline',
|
|
265
|
+
rationale: 'The repo already has enough governance assets that Nerviq should extend, not replace, the current baseline.',
|
|
266
|
+
signals: [humanizeMaturity(maturity)],
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (traits.length === 0) {
|
|
271
|
+
traits.push({
|
|
272
|
+
key: 'bootstrap-local',
|
|
273
|
+
label: 'Bootstrap local workflow',
|
|
274
|
+
rationale: 'The repo still looks early-stage and should start from a simple local baseline.',
|
|
275
|
+
signals: [humanizeMaturity(maturity)],
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return traits;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function detectRiskProfile({ ctx, platform, assets, domainKeys, maturity }) {
|
|
283
|
+
const reasons = [];
|
|
284
|
+
|
|
285
|
+
if (domainKeys.has('regulated-lite')) reasons.push('regulated-lite pack');
|
|
286
|
+
if (domainKeys.has('security-focused')) reasons.push('security-focused pack');
|
|
287
|
+
if (ctx.files.includes('SECURITY.md')) reasons.push('SECURITY.md');
|
|
288
|
+
if (ctx.files.includes('COMPLIANCE.md') || ctx.hasDir('compliance') || ctx.hasDir('policies')) reasons.push('compliance surface');
|
|
289
|
+
|
|
290
|
+
if (platform === 'codex' && assets && assets.trust) {
|
|
291
|
+
if (assets.trust.approvalPolicy === 'never') reasons.push('approval_policy=never');
|
|
292
|
+
if (assets.trust.sandboxMode === 'danger-full-access') reasons.push('sandbox_mode=danger-full-access');
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (platform !== 'codex' && assets && assets.permissions) {
|
|
296
|
+
if (assets.permissions.defaultMode === 'bypassPermissions') reasons.push('bypassPermissions');
|
|
297
|
+
if (!assets.permissions.hasDenyRules) reasons.push('missing deny rules');
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (domainKeys.has('enterprise-governed')) reasons.push('enterprise-governed pack');
|
|
301
|
+
|
|
302
|
+
let key = 'standard';
|
|
303
|
+
let label = 'Standard';
|
|
304
|
+
let level = 'medium';
|
|
305
|
+
let rationale = 'No strong regulatory or unusually permissive-risk posture dominates the repo.';
|
|
306
|
+
|
|
307
|
+
if (reasons.some((reason) => /regulated|compliance|SECURITY|security-focused/i.test(reason))) {
|
|
308
|
+
key = 'regulated';
|
|
309
|
+
label = 'Regulated / security-sensitive';
|
|
310
|
+
level = 'high';
|
|
311
|
+
rationale = 'Compliance or security-sensitive signals mean recommendations should skew toward traceability and review.';
|
|
312
|
+
} else if (reasons.some((reason) => /bypassPermissions|danger-full-access|approval_policy=never/i.test(reason))) {
|
|
313
|
+
key = 'elevated';
|
|
314
|
+
label = 'Elevated operational risk';
|
|
315
|
+
level = 'high';
|
|
316
|
+
rationale = 'The repo is configured with permissive runtime posture that increases blast radius.';
|
|
317
|
+
} else if (maturity === 'none' || maturity === 'starter') {
|
|
318
|
+
key = 'bootstrap';
|
|
319
|
+
label = 'Bootstrap risk';
|
|
320
|
+
level = 'medium';
|
|
321
|
+
rationale = 'The main risk is missing baseline governance, not over-permissive automation.';
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
key,
|
|
326
|
+
label,
|
|
327
|
+
level,
|
|
328
|
+
rationale,
|
|
329
|
+
reasons: unique(reasons),
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function buildRepoArchetypeProfile(options) {
|
|
334
|
+
const {
|
|
335
|
+
ctx,
|
|
336
|
+
platform,
|
|
337
|
+
stacks = [],
|
|
338
|
+
assets,
|
|
339
|
+
recommendedDomainPacks = [],
|
|
340
|
+
recommendedMcpPacks = [],
|
|
341
|
+
maturity = 'none',
|
|
342
|
+
} = options || {};
|
|
343
|
+
|
|
344
|
+
const pkg = ctx && typeof ctx.jsonFile === 'function' ? (ctx.jsonFile('package.json') || {}) : {};
|
|
345
|
+
const stackKeys = new Set((stacks || []).map((stack) => stack.key));
|
|
346
|
+
const domainKeys = new Set((recommendedDomainPacks || []).map((pack) => pack.key));
|
|
347
|
+
|
|
348
|
+
const topology = detectTopology(ctx, pkg);
|
|
349
|
+
const stackFamily = detectStackFamily(stackKeys, domainKeys, ctx);
|
|
350
|
+
const repoClass = detectRepoClass({ ctx, pkg, domainKeys, stackKeys, topology });
|
|
351
|
+
const workflowTraits = detectWorkflowTraits({ ctx, platform, assets, maturity, topology, domainKeys, recommendedMcpPacks });
|
|
352
|
+
const primaryWorkflow = workflowTraits[0];
|
|
353
|
+
const riskProfile = detectRiskProfile({ ctx, platform, assets, domainKeys, maturity });
|
|
354
|
+
const signals = unique([
|
|
355
|
+
stackFamily.label,
|
|
356
|
+
...topology.signals,
|
|
357
|
+
...repoClass.signals,
|
|
358
|
+
...workflowTraits.flatMap((trait) => trait.signals || []),
|
|
359
|
+
...riskProfile.reasons,
|
|
360
|
+
...recommendedDomainPacks.slice(0, 3).map((pack) => pack.key),
|
|
361
|
+
]);
|
|
362
|
+
|
|
363
|
+
const confidence = signals.length >= 6 ? 'high' : signals.length >= 3 ? 'medium' : 'low';
|
|
364
|
+
|
|
365
|
+
return {
|
|
366
|
+
key: `${topology.key}:${repoClass.key}`,
|
|
367
|
+
label: repoClass.label,
|
|
368
|
+
summary: `${repoClass.label} with ${topology.label.toLowerCase()} topology, ${primaryWorkflow.label.toLowerCase()} workflow, and ${riskProfile.label.toLowerCase()} posture.`,
|
|
369
|
+
confidence,
|
|
370
|
+
stackFamily,
|
|
371
|
+
topology,
|
|
372
|
+
maturity: {
|
|
373
|
+
key: maturity,
|
|
374
|
+
label: humanizeMaturity(maturity),
|
|
375
|
+
},
|
|
376
|
+
repoClass,
|
|
377
|
+
primaryWorkflow,
|
|
378
|
+
workflowTraits,
|
|
379
|
+
riskProfile,
|
|
380
|
+
signals: signals.slice(0, 8),
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
module.exports = {
|
|
385
|
+
buildRepoArchetypeProfile,
|
|
386
|
+
};
|