@nerviq/cli 1.29.0 → 1.30.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 (93) hide show
  1. package/CHANGELOG.md +1764 -1493
  2. package/README.md +568 -538
  3. package/SECURITY.md +78 -82
  4. package/bin/cli.js +2838 -2558
  5. package/docs/api-reference.md +356 -356
  6. package/docs/audit-fix.md +109 -0
  7. package/docs/autofix.md +3 -62
  8. package/docs/getting-started.md +1 -1
  9. package/docs/index.html +592 -592
  10. package/docs/integration-contracts.md +287 -287
  11. package/docs/maintenance.md +128 -128
  12. package/docs/new-platform-guide.md +202 -202
  13. package/docs/release-process.md +63 -0
  14. package/docs/shallow-risk.md +244 -244
  15. package/docs/why-nerviq.md +82 -82
  16. package/package.json +75 -67
  17. package/sdk/README.md +12 -3
  18. package/sdk/examples/langchain-integration.md +128 -0
  19. package/sdk/examples/self-governing-agent.js +135 -0
  20. package/sdk/index.d.ts +115 -0
  21. package/sdk/index.js +94 -0
  22. package/sdk/package.json +11 -0
  23. package/src/activity.js +13 -0
  24. package/src/aider/activity.js +226 -226
  25. package/src/aider/context.js +162 -162
  26. package/src/aider/freshness.js +123 -123
  27. package/src/aider/techniques.js +3465 -3465
  28. package/src/audit/layers.js +180 -180
  29. package/src/audit.js +1133 -1032
  30. package/src/auto-suggest.js +9 -2
  31. package/src/behavioral-drift.js +37 -2
  32. package/src/benchmark.js +299 -299
  33. package/src/codex/activity.js +324 -324
  34. package/src/codex/freshness.js +149 -142
  35. package/src/codex/techniques.js +4895 -4895
  36. package/src/context.js +326 -326
  37. package/src/continuous-ops.js +11 -1
  38. package/src/convert.js +340 -340
  39. package/src/copilot/config-parser.js +280 -280
  40. package/src/copilot/context.js +218 -218
  41. package/src/copilot/freshness.js +184 -177
  42. package/src/copilot/patch.js +238 -238
  43. package/src/copilot/techniques.js +3578 -3578
  44. package/src/cursor/freshness.js +194 -194
  45. package/src/cursor/patch.js +243 -243
  46. package/src/cursor/techniques.js +3735 -3735
  47. package/src/doctor.js +201 -201
  48. package/src/fix-engine.js +511 -8
  49. package/src/formatters/csv.js +86 -86
  50. package/src/formatters/junit.js +123 -123
  51. package/src/formatters/markdown.js +164 -164
  52. package/src/formatters/otel.js +151 -151
  53. package/src/freshness.js +163 -156
  54. package/src/gemini/activity.js +402 -402
  55. package/src/gemini/context.js +290 -290
  56. package/src/gemini/freshness.js +188 -188
  57. package/src/gemini/patch.js +229 -229
  58. package/src/gemini/techniques.js +3811 -3811
  59. package/src/governance.js +533 -533
  60. package/src/harmony/audit.js +306 -306
  61. package/src/i18n.js +63 -63
  62. package/src/insights.js +119 -119
  63. package/src/integrations.js +134 -134
  64. package/src/locales/en.json +33 -33
  65. package/src/locales/es.json +33 -33
  66. package/src/migrate.js +354 -354
  67. package/src/opencode/activity.js +286 -286
  68. package/src/opencode/freshness.js +137 -137
  69. package/src/opencode/techniques.js +3450 -3450
  70. package/src/safe-glyph.js +97 -0
  71. package/src/setup/analysis.js +12 -12
  72. package/src/setup.js +13 -6
  73. package/src/shallow-risk/index.js +113 -56
  74. package/src/shallow-risk/patterns/agent-config-cross-platform-drift.js +51 -50
  75. package/src/shallow-risk/patterns/agent-config-dangerous-autoapprove.js +47 -46
  76. package/src/shallow-risk/patterns/agent-config-deprecated-keys.js +47 -46
  77. package/src/shallow-risk/patterns/agent-config-framework-version-mismatch.js +138 -0
  78. package/src/shallow-risk/patterns/agent-config-missing-file.js +318 -317
  79. package/src/shallow-risk/patterns/agent-config-script-not-in-package-json.js +108 -0
  80. package/src/shallow-risk/patterns/agent-config-secret-literal.js +52 -49
  81. package/src/shallow-risk/patterns/agent-config-stack-contradiction.js +35 -34
  82. package/src/shallow-risk/patterns/hook-script-missing.js +71 -70
  83. package/src/shallow-risk/patterns/mcp-server-no-allowlist.js +53 -52
  84. package/src/shallow-risk/shared.js +653 -648
  85. package/src/source-urls.js +295 -295
  86. package/src/state-paths.js +85 -85
  87. package/src/supplemental-checks.js +805 -805
  88. package/src/telemetry.js +160 -160
  89. package/src/watch.js +46 -0
  90. package/src/windsurf/context.js +359 -359
  91. package/src/windsurf/freshness.js +194 -194
  92. package/src/windsurf/patch.js +231 -231
  93. package/src/windsurf/techniques.js +3779 -3779
@@ -1,317 +1,318 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
-
5
- const {
6
- SHALLOW_RISK_DOC_URL,
7
- escapeRegExp,
8
- findFirstRepoPath,
9
- getAgentConfigEntries,
10
- getScannableLines,
11
- isKnownConventionPath,
12
- lineHasExampleContext,
13
- looksLikeRelativeFileReference,
14
- normalizeCandidatePath,
15
- resolveRepoPath,
16
- toPosix,
17
- } = require('../shared');
18
-
19
- const POINTER_RE = /(?:^|[\s([`'"])(@?(?:\.{1,2}\/)?[A-Za-z0-9._/-]+)(?=$|[\s)\]`'",:;!?])/g;
20
- const MARKDOWN_LINK_RE = /\[[^\]]+\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g;
21
- const BACKTICK_TOKEN_RE = /`([^`]+)`/g;
22
- const PLACEHOLDER_PATH_RE = /(?:^|\/)(?:path(?:_to)?|to)(?:\/|$)|(?:^|\/)test_file\.py$|(?:^|\/)path_to_test\.py$|(?:^|\/)module_name\.[A-Za-z0-9._-]+$/i;
23
- const ENV_POLICY_RE = /\b(?:dotenv|environment variables?|api keys?|secrets?|credential|gitignore|removed\s+\.env|look for\s+\.env|via\s+`?\.env|defaults?\s+to|do not commit)\b/i;
24
- const OWNERSHIP_CONTEXT_RE = /\b(?:subdirectory|integration|folder|workspace|extension|module|package|component|app|generated file|composition root|entrypoint|directory structure|utility functions|updated in|register feature|build from)s?(?:['’]s)?\b/i;
25
- const SOFT_REFERENCE_CONTEXT_RE = /\b(?:can be deleted afterwards|quality scale|search result|scrape the web page content)\b/i;
26
- const ALWAYS_AMBIGUOUS_BASENAMES = new Set([
27
- 'findings.md',
28
- 'manifest.json',
29
- 'progress.md',
30
- 'quality_scale.yaml',
31
- 'task_plan.md',
32
- 'todo.md',
33
- ]);
34
-
35
- function repoHasBasename(ctx, basename, state) {
36
- if (!basename) {
37
- return false;
38
- }
39
- if (state.basenameCache.has(basename)) {
40
- return state.basenameCache.get(basename);
41
- }
42
-
43
- const match = findFirstRepoPath(ctx, (_relPath, entryName) => entryName === basename, { maxDepth: 10 });
44
- const exists = Boolean(match);
45
- state.basenameCache.set(basename, exists);
46
- return exists;
47
- }
48
-
49
- function repoHasPathSuffix(ctx, candidate, state) {
50
- const normalized = toPosix(candidate || '').replace(/^\.?\//, '');
51
- if (!normalized) {
52
- return false;
53
- }
54
- if (state.suffixCache.has(normalized)) {
55
- return state.suffixCache.get(normalized);
56
- }
57
-
58
- const match = findFirstRepoPath(
59
- ctx,
60
- (relPath) => {
61
- const normalizedPath = toPosix(relPath);
62
- return normalizedPath === normalized || normalizedPath.endsWith(`/${normalized}`);
63
- },
64
- { maxDepth: 10 },
65
- );
66
- const exists = Boolean(match);
67
- state.suffixCache.set(normalized, exists);
68
- return exists;
69
- }
70
-
71
- function lineHasEnvPolicyContext(line) {
72
- return ENV_POLICY_RE.test(String(line || ''));
73
- }
74
-
75
- function lineHasScopedOwnershipContext(line) {
76
- const text = String(line || '');
77
- return OWNERSHIP_CONTEXT_RE.test(text) || SOFT_REFERENCE_CONTEXT_RE.test(text) || /<[^>]+>/.test(text);
78
- }
79
-
80
- function extractLineAnchors(line) {
81
- const anchors = new Set();
82
- const text = String(line || '');
83
-
84
- BACKTICK_TOKEN_RE.lastIndex = 0;
85
- let match = BACKTICK_TOKEN_RE.exec(text);
86
- while (match) {
87
- const rawToken = String(match[1] || '');
88
- const token = normalizeCandidatePath(rawToken)
89
- .replace(/<[^>]+>/g, '')
90
- .replace(/^\/+/, '')
91
- .replace(/\/+$/, '');
92
- if (!token || !rawToken.includes('/')) {
93
- match = BACKTICK_TOKEN_RE.exec(text);
94
- continue;
95
- }
96
- anchors.add(token);
97
- match = BACKTICK_TOKEN_RE.exec(text);
98
- }
99
-
100
- MARKDOWN_LINK_RE.lastIndex = 0;
101
- match = MARKDOWN_LINK_RE.exec(text);
102
- while (match) {
103
- const rawToken = String(match[1] || '');
104
- const token = normalizeCandidatePath(rawToken)
105
- .replace(/<[^>]+>/g, '')
106
- .replace(/^\/+/, '')
107
- .replace(/\/+$/, '');
108
- if (!token || !rawToken.includes('/')) {
109
- match = MARKDOWN_LINK_RE.exec(text);
110
- continue;
111
- }
112
- anchors.add(token);
113
- match = MARKDOWN_LINK_RE.exec(text);
114
- }
115
-
116
- return [...anchors];
117
- }
118
-
119
- function anchorDirsForToken(token) {
120
- if (!token) {
121
- return [];
122
- }
123
-
124
- const normalized = normalizeCandidatePath(token)
125
- .replace(/<[^>]+>/g, '')
126
- .replace(/^\/+/, '')
127
- .replace(/\/+$/, '');
128
- if (!normalized) {
129
- return [];
130
- }
131
-
132
- const dirs = new Set();
133
- const looksFileLike = looksLikeRelativeFileReference(normalized);
134
- const direct = normalized.includes('/')
135
- ? (looksFileLike ? path.posix.dirname(normalized) : normalized)
136
- : normalized;
137
- if (direct && direct !== '.') {
138
- dirs.add(direct);
139
- }
140
-
141
- const parent = path.posix.dirname(direct || normalized);
142
- if (parent && parent !== '.' && parent !== direct) {
143
- dirs.add(parent);
144
- }
145
-
146
- return [...dirs];
147
- }
148
-
149
- function lineResolvesBareCandidate(ctx, line, candidate, state) {
150
- const base = path.posix.basename(candidate);
151
- const anchors = extractLineAnchors(line);
152
-
153
- for (const anchor of anchors) {
154
- const normalizedAnchor = normalizeCandidatePath(anchor);
155
- if (path.posix.basename(normalizedAnchor) === base && (ctx.fileContent(normalizedAnchor) !== null || repoHasPathSuffix(ctx, normalizedAnchor, state))) {
156
- return true;
157
- }
158
-
159
- for (const dir of anchorDirsForToken(anchor)) {
160
- const match = findFirstRepoPath(
161
- ctx,
162
- (relPath, entryName) => entryName === base && toPosix(relPath).startsWith(`${dir}/`),
163
- { maxDepth: 10 },
164
- );
165
- if (match) {
166
- return true;
167
- }
168
- }
169
- }
170
-
171
- if (anchors.length > 0 && repoHasBasename(ctx, base, state)) {
172
- return true;
173
- }
174
-
175
- if (lineHasScopedOwnershipContext(line) && repoHasBasename(ctx, base, state)) {
176
- return true;
177
- }
178
-
179
- return false;
180
- }
181
-
182
- function lineHasAnchorContext(line) {
183
- return extractLineAnchors(line).length > 0;
184
- }
185
-
186
- function lineResolvesPathSuffix(ctx, line, candidate, state) {
187
- if (!candidate || !candidate.includes('/')) {
188
- return false;
189
- }
190
- if (!lineHasAnchorContext(line) && !lineHasScopedOwnershipContext(line)) {
191
- return false;
192
- }
193
- return repoHasPathSuffix(ctx, candidate, state);
194
- }
195
-
196
- function shouldIgnoreCandidate(ctx, line, candidate, state) {
197
- const normalized = String(candidate || '');
198
- const base = path.posix.basename(normalized);
199
- if (!normalized) {
200
- return true;
201
- }
202
- if (PLACEHOLDER_PATH_RE.test(normalized)) {
203
- return true;
204
- }
205
- if (ALWAYS_AMBIGUOUS_BASENAMES.has(base) && repoHasBasename(ctx, base, state)) {
206
- return true;
207
- }
208
- if (SOFT_REFERENCE_CONTEXT_RE.test(String(line || '')) && (base === 'PLAN.md' || base === 'web_scraper.py')) {
209
- return true;
210
- }
211
- if (normalized === '.env' && lineHasEnvPolicyContext(line)) {
212
- return true;
213
- }
214
- if (lineResolvesPathSuffix(ctx, line, normalized, state)) {
215
- return true;
216
- }
217
- if (!normalized.includes('/') && lineResolvesBareCandidate(ctx, line, normalized, state)) {
218
- return true;
219
- }
220
- return false;
221
- }
222
-
223
- function resolveMissingCandidate(ctx, fromFile, candidate) {
224
- const isNestedAgentDoc = toPosix(fromFile).includes('/');
225
- const prefersRepoRoot = isNestedAgentDoc && !candidate.startsWith('../');
226
- const modes = prefersRepoRoot
227
- ? ['repo-root', 'relative-to-file']
228
- : ['relative-to-file', 'repo-root'];
229
-
230
- let firstMissing = null;
231
- for (const mode of modes) {
232
- const resolvedPath = resolveRepoPath(ctx, fromFile, candidate, mode);
233
- if (!resolvedPath || isKnownConventionPath(resolvedPath)) {
234
- continue;
235
- }
236
- if (!firstMissing) {
237
- firstMissing = resolvedPath;
238
- }
239
- if (ctx.fileContent(resolvedPath) !== null) {
240
- return { exists: true, resolvedPath };
241
- }
242
- }
243
-
244
- return { exists: false, resolvedPath: firstMissing };
245
- }
246
-
247
- function rewriteMarkdownLinksForScanning(text) {
248
- return String(text || '').replace(MARKDOWN_LINK_RE, (_match, target) => ` ${target} `);
249
- }
250
-
251
- module.exports = {
252
- key: 'agent-config-missing-file',
253
- name: 'Agent config references missing file',
254
- severity: 'high',
255
- layer: 'shallow-risk',
256
- sourceUrl: SHALLOW_RISK_DOC_URL,
257
- run(ctx) {
258
- const findings = [];
259
- const seen = new Set();
260
- const state = {
261
- basenameCache: new Map(),
262
- suffixCache: new Map(),
263
- };
264
-
265
- for (const entry of getAgentConfigEntries(ctx)) {
266
- if (!/\.(?:md|mdc|txt|rst)$/i.test(entry.path) && !/\.cursorrules$|\.windsurfrules$/i.test(entry.path)) {
267
- continue;
268
- }
269
- for (const { lineNumber, text } of getScannableLines(entry.content)) {
270
- const scanText = rewriteMarkdownLinksForScanning(text);
271
- POINTER_RE.lastIndex = 0;
272
- let match = POINTER_RE.exec(scanText);
273
- while (match) {
274
- const candidate = normalizeCandidatePath(match[1]);
275
- if (!looksLikeRelativeFileReference(candidate)) {
276
- match = POINTER_RE.exec(scanText);
277
- continue;
278
- }
279
-
280
- if (lineHasExampleContext(text)) {
281
- match = POINTER_RE.exec(scanText);
282
- continue;
283
- }
284
-
285
- if (shouldIgnoreCandidate(ctx, text, candidate, state)) {
286
- match = POINTER_RE.exec(scanText);
287
- continue;
288
- }
289
-
290
- const resolution = resolveMissingCandidate(ctx, entry.path, candidate);
291
- if (!resolution.resolvedPath || resolution.exists) {
292
- match = POINTER_RE.exec(scanText);
293
- continue;
294
- }
295
- const resolvedPath = resolution.resolvedPath;
296
-
297
- const dedupeKey = `${entry.path}:${toPosix(resolvedPath)}`;
298
- if (seen.has(dedupeKey)) {
299
- match = POINTER_RE.exec(scanText);
300
- continue;
301
- }
302
- seen.add(dedupeKey);
303
-
304
- findings.push({
305
- file: entry.path,
306
- line: lineNumber || ctx.lineNumber(entry.path, new RegExp(escapeRegExp(candidate))),
307
- fix: `${entry.path} references \`${toPosix(resolvedPath)}\`, but the file is missing. Create the file or update the agent guidance to point at a real repo path.`,
308
- });
309
-
310
- match = POINTER_RE.exec(scanText);
311
- }
312
- }
313
- }
314
-
315
- return findings;
316
- },
317
- };
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+
5
+ const {
6
+ SHALLOW_RISK_DOC_URL,
7
+ escapeRegExp,
8
+ findFirstRepoPath,
9
+ getAgentConfigEntries,
10
+ getScannableLines,
11
+ isKnownConventionPath,
12
+ lineHasExampleContext,
13
+ looksLikeRelativeFileReference,
14
+ normalizeCandidatePath,
15
+ resolveRepoPath,
16
+ toPosix,
17
+ } = require('../shared');
18
+
19
+ const POINTER_RE = /(?:^|[\s([`'"])(@?(?:\.{1,2}\/)?[A-Za-z0-9._/-]+)(?=$|[\s)\]`'",:;!?])/g;
20
+ const MARKDOWN_LINK_RE = /\[[^\]]+\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g;
21
+ const BACKTICK_TOKEN_RE = /`([^`]+)`/g;
22
+ const PLACEHOLDER_PATH_RE = /(?:^|\/)(?:path(?:_to)?|to)(?:\/|$)|(?:^|\/)test_file\.py$|(?:^|\/)path_to_test\.py$|(?:^|\/)module_name\.[A-Za-z0-9._-]+$/i;
23
+ const ENV_POLICY_RE = /\b(?:dotenv|environment variables?|api keys?|secrets?|credential|gitignore|removed\s+\.env|look for\s+\.env|via\s+`?\.env|defaults?\s+to|do not commit)\b/i;
24
+ const OWNERSHIP_CONTEXT_RE = /\b(?:subdirectory|integration|folder|workspace|extension|module|package|component|app|generated file|composition root|entrypoint|directory structure|utility functions|updated in|register feature|build from)s?(?:['’]s)?\b/i;
25
+ const SOFT_REFERENCE_CONTEXT_RE = /\b(?:can be deleted afterwards|quality scale|search result|scrape the web page content)\b/i;
26
+ const ALWAYS_AMBIGUOUS_BASENAMES = new Set([
27
+ 'findings.md',
28
+ 'manifest.json',
29
+ 'progress.md',
30
+ 'quality_scale.yaml',
31
+ 'task_plan.md',
32
+ 'todo.md',
33
+ ]);
34
+
35
+ function repoHasBasename(ctx, basename, state) {
36
+ if (!basename) {
37
+ return false;
38
+ }
39
+ if (state.basenameCache.has(basename)) {
40
+ return state.basenameCache.get(basename);
41
+ }
42
+
43
+ const match = findFirstRepoPath(ctx, (_relPath, entryName) => entryName === basename, { maxDepth: 10 });
44
+ const exists = Boolean(match);
45
+ state.basenameCache.set(basename, exists);
46
+ return exists;
47
+ }
48
+
49
+ function repoHasPathSuffix(ctx, candidate, state) {
50
+ const normalized = toPosix(candidate || '').replace(/^\.?\//, '');
51
+ if (!normalized) {
52
+ return false;
53
+ }
54
+ if (state.suffixCache.has(normalized)) {
55
+ return state.suffixCache.get(normalized);
56
+ }
57
+
58
+ const match = findFirstRepoPath(
59
+ ctx,
60
+ (relPath) => {
61
+ const normalizedPath = toPosix(relPath);
62
+ return normalizedPath === normalized || normalizedPath.endsWith(`/${normalized}`);
63
+ },
64
+ { maxDepth: 10 },
65
+ );
66
+ const exists = Boolean(match);
67
+ state.suffixCache.set(normalized, exists);
68
+ return exists;
69
+ }
70
+
71
+ function lineHasEnvPolicyContext(line) {
72
+ return ENV_POLICY_RE.test(String(line || ''));
73
+ }
74
+
75
+ function lineHasScopedOwnershipContext(line) {
76
+ const text = String(line || '');
77
+ return OWNERSHIP_CONTEXT_RE.test(text) || SOFT_REFERENCE_CONTEXT_RE.test(text) || /<[^>]+>/.test(text);
78
+ }
79
+
80
+ function extractLineAnchors(line) {
81
+ const anchors = new Set();
82
+ const text = String(line || '');
83
+
84
+ BACKTICK_TOKEN_RE.lastIndex = 0;
85
+ let match = BACKTICK_TOKEN_RE.exec(text);
86
+ while (match) {
87
+ const rawToken = String(match[1] || '');
88
+ const token = normalizeCandidatePath(rawToken)
89
+ .replace(/<[^>]+>/g, '')
90
+ .replace(/^\/+/, '')
91
+ .replace(/\/+$/, '');
92
+ if (!token || !rawToken.includes('/')) {
93
+ match = BACKTICK_TOKEN_RE.exec(text);
94
+ continue;
95
+ }
96
+ anchors.add(token);
97
+ match = BACKTICK_TOKEN_RE.exec(text);
98
+ }
99
+
100
+ MARKDOWN_LINK_RE.lastIndex = 0;
101
+ match = MARKDOWN_LINK_RE.exec(text);
102
+ while (match) {
103
+ const rawToken = String(match[1] || '');
104
+ const token = normalizeCandidatePath(rawToken)
105
+ .replace(/<[^>]+>/g, '')
106
+ .replace(/^\/+/, '')
107
+ .replace(/\/+$/, '');
108
+ if (!token || !rawToken.includes('/')) {
109
+ match = MARKDOWN_LINK_RE.exec(text);
110
+ continue;
111
+ }
112
+ anchors.add(token);
113
+ match = MARKDOWN_LINK_RE.exec(text);
114
+ }
115
+
116
+ return [...anchors];
117
+ }
118
+
119
+ function anchorDirsForToken(token) {
120
+ if (!token) {
121
+ return [];
122
+ }
123
+
124
+ const normalized = normalizeCandidatePath(token)
125
+ .replace(/<[^>]+>/g, '')
126
+ .replace(/^\/+/, '')
127
+ .replace(/\/+$/, '');
128
+ if (!normalized) {
129
+ return [];
130
+ }
131
+
132
+ const dirs = new Set();
133
+ const looksFileLike = looksLikeRelativeFileReference(normalized);
134
+ const direct = normalized.includes('/')
135
+ ? (looksFileLike ? path.posix.dirname(normalized) : normalized)
136
+ : normalized;
137
+ if (direct && direct !== '.') {
138
+ dirs.add(direct);
139
+ }
140
+
141
+ const parent = path.posix.dirname(direct || normalized);
142
+ if (parent && parent !== '.' && parent !== direct) {
143
+ dirs.add(parent);
144
+ }
145
+
146
+ return [...dirs];
147
+ }
148
+
149
+ function lineResolvesBareCandidate(ctx, line, candidate, state) {
150
+ const base = path.posix.basename(candidate);
151
+ const anchors = extractLineAnchors(line);
152
+
153
+ for (const anchor of anchors) {
154
+ const normalizedAnchor = normalizeCandidatePath(anchor);
155
+ if (path.posix.basename(normalizedAnchor) === base && (ctx.fileContent(normalizedAnchor) !== null || repoHasPathSuffix(ctx, normalizedAnchor, state))) {
156
+ return true;
157
+ }
158
+
159
+ for (const dir of anchorDirsForToken(anchor)) {
160
+ const match = findFirstRepoPath(
161
+ ctx,
162
+ (relPath, entryName) => entryName === base && toPosix(relPath).startsWith(`${dir}/`),
163
+ { maxDepth: 10 },
164
+ );
165
+ if (match) {
166
+ return true;
167
+ }
168
+ }
169
+ }
170
+
171
+ if (anchors.length > 0 && repoHasBasename(ctx, base, state)) {
172
+ return true;
173
+ }
174
+
175
+ if (lineHasScopedOwnershipContext(line) && repoHasBasename(ctx, base, state)) {
176
+ return true;
177
+ }
178
+
179
+ return false;
180
+ }
181
+
182
+ function lineHasAnchorContext(line) {
183
+ return extractLineAnchors(line).length > 0;
184
+ }
185
+
186
+ function lineResolvesPathSuffix(ctx, line, candidate, state) {
187
+ if (!candidate || !candidate.includes('/')) {
188
+ return false;
189
+ }
190
+ if (!lineHasAnchorContext(line) && !lineHasScopedOwnershipContext(line)) {
191
+ return false;
192
+ }
193
+ return repoHasPathSuffix(ctx, candidate, state);
194
+ }
195
+
196
+ function shouldIgnoreCandidate(ctx, line, candidate, state) {
197
+ const normalized = String(candidate || '');
198
+ const base = path.posix.basename(normalized);
199
+ if (!normalized) {
200
+ return true;
201
+ }
202
+ if (PLACEHOLDER_PATH_RE.test(normalized)) {
203
+ return true;
204
+ }
205
+ if (ALWAYS_AMBIGUOUS_BASENAMES.has(base) && repoHasBasename(ctx, base, state)) {
206
+ return true;
207
+ }
208
+ if (SOFT_REFERENCE_CONTEXT_RE.test(String(line || '')) && (base === 'PLAN.md' || base === 'web_scraper.py')) {
209
+ return true;
210
+ }
211
+ if (normalized === '.env' && lineHasEnvPolicyContext(line)) {
212
+ return true;
213
+ }
214
+ if (lineResolvesPathSuffix(ctx, line, normalized, state)) {
215
+ return true;
216
+ }
217
+ if (!normalized.includes('/') && lineResolvesBareCandidate(ctx, line, normalized, state)) {
218
+ return true;
219
+ }
220
+ return false;
221
+ }
222
+
223
+ function resolveMissingCandidate(ctx, fromFile, candidate) {
224
+ const isNestedAgentDoc = toPosix(fromFile).includes('/');
225
+ const prefersRepoRoot = isNestedAgentDoc && !candidate.startsWith('../');
226
+ const modes = prefersRepoRoot
227
+ ? ['repo-root', 'relative-to-file']
228
+ : ['relative-to-file', 'repo-root'];
229
+
230
+ let firstMissing = null;
231
+ for (const mode of modes) {
232
+ const resolvedPath = resolveRepoPath(ctx, fromFile, candidate, mode);
233
+ if (!resolvedPath || isKnownConventionPath(resolvedPath)) {
234
+ continue;
235
+ }
236
+ if (!firstMissing) {
237
+ firstMissing = resolvedPath;
238
+ }
239
+ if (ctx.fileContent(resolvedPath) !== null) {
240
+ return { exists: true, resolvedPath };
241
+ }
242
+ }
243
+
244
+ return { exists: false, resolvedPath: firstMissing };
245
+ }
246
+
247
+ function rewriteMarkdownLinksForScanning(text) {
248
+ return String(text || '').replace(MARKDOWN_LINK_RE, (_match, target) => ` ${target} `);
249
+ }
250
+
251
+ module.exports = {
252
+ key: 'agent-config-missing-file',
253
+ name: 'Agent config references missing file',
254
+ severity: 'high',
255
+ layer: 'shallow-risk',
256
+ sourceUrl: SHALLOW_RISK_DOC_URL,
257
+ owaspTags: ['agentic-top-10:tool-instruction-integrity'],
258
+ run(ctx) {
259
+ const findings = [];
260
+ const seen = new Set();
261
+ const state = {
262
+ basenameCache: new Map(),
263
+ suffixCache: new Map(),
264
+ };
265
+
266
+ for (const entry of getAgentConfigEntries(ctx)) {
267
+ if (!/\.(?:md|mdc|txt|rst)$/i.test(entry.path) && !/\.cursorrules$|\.windsurfrules$/i.test(entry.path)) {
268
+ continue;
269
+ }
270
+ for (const { lineNumber, text } of getScannableLines(entry.content)) {
271
+ const scanText = rewriteMarkdownLinksForScanning(text);
272
+ POINTER_RE.lastIndex = 0;
273
+ let match = POINTER_RE.exec(scanText);
274
+ while (match) {
275
+ const candidate = normalizeCandidatePath(match[1]);
276
+ if (!looksLikeRelativeFileReference(candidate)) {
277
+ match = POINTER_RE.exec(scanText);
278
+ continue;
279
+ }
280
+
281
+ if (lineHasExampleContext(text)) {
282
+ match = POINTER_RE.exec(scanText);
283
+ continue;
284
+ }
285
+
286
+ if (shouldIgnoreCandidate(ctx, text, candidate, state)) {
287
+ match = POINTER_RE.exec(scanText);
288
+ continue;
289
+ }
290
+
291
+ const resolution = resolveMissingCandidate(ctx, entry.path, candidate);
292
+ if (!resolution.resolvedPath || resolution.exists) {
293
+ match = POINTER_RE.exec(scanText);
294
+ continue;
295
+ }
296
+ const resolvedPath = resolution.resolvedPath;
297
+
298
+ const dedupeKey = `${entry.path}:${toPosix(resolvedPath)}`;
299
+ if (seen.has(dedupeKey)) {
300
+ match = POINTER_RE.exec(scanText);
301
+ continue;
302
+ }
303
+ seen.add(dedupeKey);
304
+
305
+ findings.push({
306
+ file: entry.path,
307
+ line: lineNumber || ctx.lineNumber(entry.path, new RegExp(escapeRegExp(candidate))),
308
+ fix: `${entry.path} references \`${toPosix(resolvedPath)}\`, but the file is missing. Create the file or update the agent guidance to point at a real repo path.`,
309
+ });
310
+
311
+ match = POINTER_RE.exec(scanText);
312
+ }
313
+ }
314
+ }
315
+
316
+ return findings;
317
+ },
318
+ };