forge-workflow 0.0.4 → 0.0.5

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 (209) hide show
  1. package/.claude/commands/dev.md +340 -340
  2. package/.claude/commands/plan.md +521 -521
  3. package/.claude/commands/premerge.md +176 -176
  4. package/.claude/commands/research.md +42 -42
  5. package/.claude/commands/review.md +442 -442
  6. package/.claude/commands/rollback.md +721 -721
  7. package/.claude/commands/ship.md +164 -164
  8. package/.claude/commands/sonarcloud.md +152 -152
  9. package/.claude/commands/status.md +48 -48
  10. package/.claude/commands/validate.md +282 -282
  11. package/.claude/commands/verify.md +221 -221
  12. package/.claude/rules/greptile-review-process.md +285 -285
  13. package/.claude/rules/workflow.md +105 -105
  14. package/.claude/scripts/greptile-resolve.sh +526 -526
  15. package/.claude/scripts/load-env.sh +32 -32
  16. package/.cline/workflows/dev.md +337 -337
  17. package/.cline/workflows/plan.md +518 -518
  18. package/.cline/workflows/premerge.md +173 -173
  19. package/.cline/workflows/research.md +39 -39
  20. package/.cline/workflows/review.md +439 -439
  21. package/.cline/workflows/rollback.md +718 -718
  22. package/.cline/workflows/ship.md +161 -161
  23. package/.cline/workflows/sonarcloud.md +146 -146
  24. package/.cline/workflows/status.md +45 -45
  25. package/.cline/workflows/validate.md +279 -279
  26. package/.cline/workflows/verify.md +218 -218
  27. package/.codex/config.toml +11 -11
  28. package/.codex/skills/dev/SKILL.md +340 -340
  29. package/.codex/skills/plan/SKILL.md +521 -521
  30. package/.codex/skills/premerge/SKILL.md +176 -176
  31. package/.codex/skills/research/SKILL.md +42 -42
  32. package/.codex/skills/review/SKILL.md +442 -442
  33. package/.codex/skills/rollback/SKILL.md +721 -721
  34. package/.codex/skills/ship/SKILL.md +164 -164
  35. package/.codex/skills/sonarcloud/SKILL.md +149 -149
  36. package/.codex/skills/status/SKILL.md +48 -48
  37. package/.codex/skills/validate/SKILL.md +282 -282
  38. package/.codex/skills/verify/SKILL.md +221 -221
  39. package/.cursor/commands/dev.md +337 -337
  40. package/.cursor/commands/plan.md +518 -518
  41. package/.cursor/commands/premerge.md +173 -173
  42. package/.cursor/commands/research.md +39 -39
  43. package/.cursor/commands/review.md +439 -439
  44. package/.cursor/commands/rollback.md +718 -718
  45. package/.cursor/commands/ship.md +161 -161
  46. package/.cursor/commands/sonarcloud.md +146 -146
  47. package/.cursor/commands/status.md +45 -45
  48. package/.cursor/commands/validate.md +279 -279
  49. package/.cursor/commands/verify.md +218 -218
  50. package/.cursor/rules/permissions-guidance.mdc +37 -37
  51. package/.forge/hooks/check-tdd.js +240 -240
  52. package/.github/PLUGIN_TEMPLATE.json +32 -32
  53. package/.github/prompts/dev.prompt.md +342 -342
  54. package/.github/prompts/plan.prompt.md +523 -523
  55. package/.github/prompts/premerge.prompt.md +178 -178
  56. package/.github/prompts/research.prompt.md +44 -44
  57. package/.github/prompts/review.prompt.md +444 -444
  58. package/.github/prompts/rollback.prompt.md +723 -723
  59. package/.github/prompts/ship.prompt.md +166 -166
  60. package/.github/prompts/sonarcloud.prompt.md +151 -151
  61. package/.github/prompts/status.prompt.md +50 -50
  62. package/.github/prompts/validate.prompt.md +284 -284
  63. package/.github/prompts/verify.prompt.md +223 -223
  64. package/.github/workflows/beads-to-github.yml +56 -0
  65. package/.github/workflows/github-to-beads.yml +97 -0
  66. package/.kilocode/workflows/dev.md +341 -341
  67. package/.kilocode/workflows/plan.md +522 -522
  68. package/.kilocode/workflows/premerge.md +177 -177
  69. package/.kilocode/workflows/research.md +43 -43
  70. package/.kilocode/workflows/review.md +443 -443
  71. package/.kilocode/workflows/rollback.md +722 -722
  72. package/.kilocode/workflows/ship.md +165 -165
  73. package/.kilocode/workflows/sonarcloud.md +150 -150
  74. package/.kilocode/workflows/status.md +49 -49
  75. package/.kilocode/workflows/validate.md +283 -283
  76. package/.kilocode/workflows/verify.md +222 -222
  77. package/.mcp.json.example +12 -12
  78. package/.opencode/commands/dev.md +340 -340
  79. package/.opencode/commands/plan.md +521 -521
  80. package/.opencode/commands/premerge.md +176 -176
  81. package/.opencode/commands/research.md +42 -42
  82. package/.opencode/commands/review.md +442 -442
  83. package/.opencode/commands/rollback.md +721 -721
  84. package/.opencode/commands/ship.md +164 -164
  85. package/.opencode/commands/sonarcloud.md +149 -149
  86. package/.opencode/commands/status.md +48 -48
  87. package/.opencode/commands/validate.md +282 -282
  88. package/.opencode/commands/verify.md +221 -221
  89. package/.roo/commands/dev.md +341 -341
  90. package/.roo/commands/plan.md +522 -522
  91. package/.roo/commands/premerge.md +177 -177
  92. package/.roo/commands/research.md +43 -43
  93. package/.roo/commands/review.md +443 -443
  94. package/.roo/commands/rollback.md +722 -722
  95. package/.roo/commands/ship.md +165 -165
  96. package/.roo/commands/sonarcloud.md +150 -150
  97. package/.roo/commands/status.md +49 -49
  98. package/.roo/commands/validate.md +283 -283
  99. package/.roo/commands/verify.md +222 -222
  100. package/AGENTS.md +175 -175
  101. package/CLAUDE.md +100 -100
  102. package/README.md +429 -416
  103. package/bin/forge-cmd.js +313 -313
  104. package/bin/forge-preflight.js +309 -309
  105. package/bin/forge.js +4596 -4303
  106. package/docs/AGENT_INSTALL_PROMPT.md +342 -342
  107. package/docs/BEADS_GITHUB_SYNC.md +251 -251
  108. package/docs/ENHANCED_ONBOARDING.md +602 -602
  109. package/docs/EXAMPLES.md +482 -482
  110. package/docs/GREPTILE_SETUP.md +400 -400
  111. package/docs/MANUAL_REVIEW_GUIDE.md +106 -106
  112. package/docs/ROADMAP.md +359 -359
  113. package/docs/SETUP.md +663 -631
  114. package/docs/TOOLCHAIN.md +630 -630
  115. package/docs/VALIDATION.md +363 -363
  116. package/install.sh +40 -1056
  117. package/lefthook.yml +39 -39
  118. package/lib/agents/README.md +198 -198
  119. package/lib/agents/claude.plugin.json +28 -28
  120. package/lib/agents/cline.plugin.json +22 -22
  121. package/lib/agents/codex.plugin.json +19 -19
  122. package/lib/agents/copilot.plugin.json +24 -24
  123. package/lib/agents/cursor.plugin.json +25 -25
  124. package/lib/agents/kilocode.plugin.json +22 -22
  125. package/lib/agents/opencode.plugin.json +20 -20
  126. package/lib/agents/roo.plugin.json +23 -23
  127. package/lib/agents-config.js +2112 -2112
  128. package/lib/beads-health-check.js +143 -0
  129. package/lib/beads-setup.js +341 -0
  130. package/lib/beads-sync-scaffold.js +260 -0
  131. package/lib/commands/dev.js +513 -513
  132. package/lib/commands/plan.js +692 -692
  133. package/lib/commands/recommend.js +119 -119
  134. package/lib/commands/ship.js +377 -377
  135. package/lib/commands/status.js +378 -378
  136. package/lib/commands/validate.js +602 -602
  137. package/lib/context-merge.js +359 -359
  138. package/lib/dep-guard/analyzer.js +294 -294
  139. package/lib/dep-guard/behavior-detector.js +98 -98
  140. package/lib/dep-guard/contract-detector.js +162 -162
  141. package/lib/dep-guard/import-detector.js +498 -498
  142. package/lib/dep-guard/path-utils.js +13 -13
  143. package/lib/dep-guard/rubric.js +120 -120
  144. package/lib/dep-guard/task-parser.js +318 -318
  145. package/lib/detect-agent.js +191 -191
  146. package/lib/detect-worktree.js +47 -47
  147. package/lib/file-hash.js +26 -26
  148. package/lib/husky-migration.js +450 -0
  149. package/lib/lefthook-check.js +65 -0
  150. package/lib/pat-setup.js +207 -0
  151. package/lib/plugin-catalog.js +350 -350
  152. package/lib/plugin-manager.js +166 -166
  153. package/lib/plugin-recommender.js +141 -141
  154. package/lib/project-discovery.js +491 -491
  155. package/lib/setup-action-log.js +139 -139
  156. package/lib/setup-summary-renderer.js +106 -106
  157. package/lib/setup-utils.js +96 -0
  158. package/lib/setup.js +192 -192
  159. package/lib/smart-merge.js +64 -0
  160. package/lib/symlink-utils.js +81 -0
  161. package/lib/workflow-profiles.js +197 -197
  162. package/package.json +131 -128
  163. package/scripts/beads-context.sh +291 -0
  164. package/scripts/beads-context.test.js +563 -0
  165. package/scripts/behavioral-judge.sh +378 -0
  166. package/scripts/benchmark.js +85 -0
  167. package/scripts/branch-protection.js +183 -0
  168. package/scripts/check-agents.js +172 -0
  169. package/scripts/commitlint.js +42 -0
  170. package/scripts/conflict-detect.sh +323 -0
  171. package/scripts/dep-guard-analyze.js +71 -0
  172. package/scripts/dep-guard.sh +811 -0
  173. package/scripts/eval_win.py +249 -0
  174. package/scripts/file-index.sh +399 -0
  175. package/scripts/github-beads-sync/comment.mjs +64 -0
  176. package/scripts/github-beads-sync/config.mjs +148 -0
  177. package/scripts/github-beads-sync/github-api.mjs +131 -0
  178. package/scripts/github-beads-sync/index.mjs +332 -0
  179. package/scripts/github-beads-sync/label-mapper.mjs +54 -0
  180. package/scripts/github-beads-sync/mapping.mjs +78 -0
  181. package/scripts/github-beads-sync/reverse-sync-cli.mjs +31 -0
  182. package/scripts/github-beads-sync/reverse-sync.mjs +138 -0
  183. package/scripts/github-beads-sync/run-bd.mjs +159 -0
  184. package/scripts/github-beads-sync/sanitize.mjs +121 -0
  185. package/scripts/github-beads-sync.config.json +26 -0
  186. package/scripts/improve-command.js +375 -0
  187. package/scripts/lib/eval-runner.js +229 -0
  188. package/scripts/lib/eval-schema.js +135 -0
  189. package/scripts/lib/eval-storage.js +78 -0
  190. package/scripts/lib/grading.js +203 -0
  191. package/scripts/lib/transcript-parser.js +63 -0
  192. package/scripts/lint.js +47 -0
  193. package/scripts/migrate-to-bun-test.js +412 -0
  194. package/scripts/run-command-eval.js +236 -0
  195. package/scripts/smart-status.sh +782 -0
  196. package/scripts/sync-commands.js +571 -0
  197. package/scripts/sync-utils.sh +460 -0
  198. package/scripts/test-dashboard.js +123 -0
  199. package/scripts/test.js +44 -0
  200. package/scripts/validate.sh +94 -0
  201. package/skills/parallel-deep-research/SKILL.md +108 -108
  202. package/skills/parallel-deep-research/evals/README.md +27 -27
  203. package/skills/parallel-deep-research/evals/evals.json +62 -62
  204. package/skills/sonarcloud-analysis/SKILL.md +171 -171
  205. package/skills/sonarcloud-analysis/evals/README.md +27 -27
  206. package/skills/sonarcloud-analysis/evals/evals.json +50 -50
  207. package/skills/sonarcloud-analysis/references/api-reference.md +466 -466
  208. package/.cursor/hooks/state/continual-learning-index.json +0 -19
  209. package/.cursor/hooks/state/continual-learning.json +0 -8
@@ -1,498 +1,498 @@
1
- const fs = require('node:fs');
2
- const path = require('node:path');
3
- const { parse } = require('@babel/parser');
4
- const { normalizeRepoPath } = require('./path-utils.js');
5
- const { extractSymbols } = require('./task-parser.js');
6
-
7
- const SOURCE_EXTENSIONS = new Set(['.js', '.cjs', '.mjs', '.jsx', '.ts', '.tsx']);
8
- const DEFAULT_EXCLUDED_DIRECTORIES = new Set([
9
- '.git',
10
- '.worktrees',
11
- 'coverage',
12
- 'dist',
13
- 'build',
14
- 'docs',
15
- 'fixtures',
16
- 'node_modules',
17
- 'test',
18
- 'test-env',
19
- ]);
20
-
21
- function isIdentifierStart(character) {
22
- return /[A-Za-z_]/.test(character);
23
- }
24
-
25
- function isIdentifierPart(character) {
26
- return /\w/.test(character);
27
- }
28
-
29
- function listSourceFiles(rootDir) {
30
- const results = [];
31
- const queue = [rootDir];
32
-
33
- while (queue.length > 0) {
34
- const current = queue.pop();
35
- let entries;
36
- try {
37
- entries = fs.readdirSync(current, { withFileTypes: true });
38
- } catch (_error) {
39
- // Inaccessible directory (permissions, broken symlink) — skip silently
40
- continue;
41
- }
42
-
43
- for (const entry of entries) {
44
- if (entry.isDirectory() && DEFAULT_EXCLUDED_DIRECTORIES.has(entry.name)) {
45
- continue;
46
- }
47
-
48
- const absolutePath = path.join(current, entry.name);
49
- if (entry.isDirectory()) {
50
- queue.push(absolutePath);
51
- continue;
52
- }
53
-
54
- if (SOURCE_EXTENSIONS.has(path.extname(entry.name))) {
55
- results.push(absolutePath);
56
- }
57
- }
58
- }
59
-
60
- return results;
61
- }
62
-
63
- function extractTargetSymbols(taskContext) {
64
- const symbols = new Set();
65
-
66
- for (const task of taskContext.tasks) {
67
- for (const symbol of extractSymbols(task.whatToImplement ?? '')) {
68
- if (symbol && isIdentifierStart(symbol[0]) && Array.from(symbol).every(isIdentifierPart)) {
69
- symbols.add(symbol);
70
- }
71
- }
72
- }
73
-
74
- return symbols;
75
- }
76
-
77
- function extractTargetFiles(taskContext) {
78
- const files = new Set();
79
-
80
- for (const task of taskContext.tasks) {
81
- for (const file of task.files) {
82
- files.add(normalizeRepoPath(file));
83
- }
84
- }
85
-
86
- return files;
87
- }
88
-
89
- function resolveImportSource(sourceFile, specifier, repositoryRoot) {
90
- if (!specifier || typeof specifier !== 'string' || !specifier.startsWith('.')) {
91
- return null;
92
- }
93
-
94
- const basePath = path.resolve(path.dirname(sourceFile), specifier);
95
- const candidates = [
96
- basePath,
97
- `${basePath}.js`,
98
- `${basePath}.cjs`,
99
- `${basePath}.mjs`,
100
- `${basePath}.jsx`,
101
- `${basePath}.ts`,
102
- `${basePath}.tsx`,
103
- path.join(basePath, 'index.js'),
104
- path.join(basePath, 'index.cjs'),
105
- path.join(basePath, 'index.mjs'),
106
- ];
107
-
108
- for (const candidate of candidates) {
109
- if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
110
- return normalizeRepoPath(path.relative(repositoryRoot, candidate));
111
- }
112
- }
113
-
114
- return null;
115
- }
116
-
117
- function isAstNode(value) {
118
- return Boolean(value) && typeof value === 'object' && typeof value.type === 'string';
119
- }
120
-
121
- function walkNode(node, visitor) {
122
- if (!isAstNode(node)) {
123
- return;
124
- }
125
-
126
- visitor(node);
127
-
128
- for (const value of Object.values(node)) {
129
- if (Array.isArray(value)) {
130
- for (const item of value) {
131
- if (isAstNode(item)) {
132
- walkNode(item, visitor);
133
- }
134
- }
135
- continue;
136
- }
137
-
138
- if (isAstNode(value)) {
139
- walkNode(value, visitor);
140
- }
141
- }
142
- }
143
-
144
- function collectFileAnalysis(filePath, repositoryRoot) {
145
- const relativeFile = normalizeRepoPath(path.relative(repositoryRoot, filePath));
146
- const content = fs.readFileSync(filePath, 'utf8');
147
- const ast = parse(content, {
148
- sourceType: 'unambiguous',
149
- errorRecovery: true,
150
- plugins: ['jsx', 'typescript'],
151
- });
152
-
153
- const directImports = new Map();
154
- const defaultImports = new Map();
155
- const namespaceImports = new Map();
156
- const directCalls = new Set();
157
- const memberCalls = [];
158
- const defaultExportSymbols = new Set();
159
-
160
- walkNode(ast, (node) => {
161
- recordImportDeclaration(node, filePath, repositoryRoot, directImports, defaultImports, namespaceImports);
162
- recordRequireDeclarator(node, filePath, repositoryRoot, directImports, defaultImports, namespaceImports);
163
- recordCallExpression(node, directCalls, memberCalls);
164
- recordDefaultExport(node, defaultExportSymbols);
165
- });
166
-
167
- return {
168
- file: relativeFile,
169
- directImports,
170
- defaultImports,
171
- namespaceImports,
172
- directCalls,
173
- memberCalls,
174
- defaultExportSymbols,
175
- };
176
- }
177
-
178
- function recordImportDeclaration(node, filePath, repositoryRoot, directImports, defaultImports, namespaceImports) {
179
- if (node.type !== 'ImportDeclaration') {
180
- return;
181
- }
182
-
183
- const resolvedSource = resolveImportSource(filePath, node.source?.value, repositoryRoot);
184
- if (!resolvedSource) {
185
- return;
186
- }
187
-
188
- for (const specifier of node.specifiers ?? []) {
189
- if (specifier.type === 'ImportSpecifier') {
190
- directImports.set(specifier.local.name, {
191
- sourceFile: resolvedSource,
192
- importedName: specifier.imported.name,
193
- });
194
- } else if (specifier.type === 'ImportDefaultSpecifier' && specifier.local?.name) {
195
- defaultImports.set(specifier.local.name, {
196
- sourceFile: resolvedSource,
197
- });
198
- } else if (specifier.local?.name) {
199
- namespaceImports.set(specifier.local.name, {
200
- sourceFile: resolvedSource,
201
- });
202
- }
203
- }
204
- }
205
-
206
- function recordRequireDeclarator(node, filePath, repositoryRoot, directImports, defaultImports, namespaceImports) {
207
- if (
208
- node.type !== 'VariableDeclarator'
209
- || node.init?.type !== 'CallExpression'
210
- || node.init.callee?.type !== 'Identifier'
211
- || node.init.callee.name !== 'require'
212
- || node.init.arguments?.[0]?.type !== 'StringLiteral'
213
- ) {
214
- return;
215
- }
216
-
217
- const resolvedSource = resolveImportSource(
218
- filePath,
219
- node.init.arguments[0].value,
220
- repositoryRoot,
221
- );
222
- if (!resolvedSource) {
223
- return;
224
- }
225
-
226
- if (node.id.type === 'ObjectPattern') {
227
- for (const property of node.id.properties ?? []) {
228
- if (property.type !== 'ObjectProperty' || property.value?.type !== 'Identifier') {
229
- continue;
230
- }
231
-
232
- const importedName = property.key?.name ?? property.key?.value;
233
- if (!importedName) {
234
- continue;
235
- }
236
-
237
- directImports.set(property.value.name, {
238
- sourceFile: resolvedSource,
239
- importedName,
240
- });
241
- }
242
- return;
243
- }
244
-
245
- if (node.id.type === 'Identifier') {
246
- defaultImports.set(node.id.name, {
247
- sourceFile: resolvedSource,
248
- });
249
- namespaceImports.set(node.id.name, {
250
- sourceFile: resolvedSource,
251
- });
252
- }
253
- }
254
-
255
- function recordCallExpression(node, directCalls, memberCalls) {
256
- if (node.type !== 'CallExpression') {
257
- return;
258
- }
259
-
260
- if (node.callee?.type === 'Identifier') {
261
- directCalls.add(node.callee.name);
262
- return;
263
- }
264
-
265
- if (
266
- node.callee?.type === 'MemberExpression'
267
- && node.callee.object?.type === 'Identifier'
268
- && !node.callee.computed
269
- && node.callee.property?.type === 'Identifier'
270
- ) {
271
- memberCalls.push({
272
- objectName: node.callee.object.name,
273
- propertyName: node.callee.property.name,
274
- });
275
- }
276
- }
277
-
278
- function recordDefaultExport(node, defaultExportSymbols) {
279
- if (node.type === 'ExportDefaultDeclaration') {
280
- if (node.declaration?.type === 'Identifier') {
281
- defaultExportSymbols.add(node.declaration.name);
282
- return;
283
- }
284
-
285
- if (
286
- (node.declaration?.type === 'FunctionDeclaration' || node.declaration?.type === 'ClassDeclaration')
287
- && node.declaration.id?.name
288
- ) {
289
- defaultExportSymbols.add(node.declaration.id.name);
290
- }
291
- return;
292
- }
293
-
294
- if (
295
- node.type === 'AssignmentExpression'
296
- && node.operator === '='
297
- && node.left?.type === 'MemberExpression'
298
- && node.left.object?.type === 'Identifier'
299
- && node.left.object.name === 'module'
300
- && !node.left.computed
301
- && node.left.property?.type === 'Identifier'
302
- && node.left.property.name === 'exports'
303
- ) {
304
- if (node.right?.type === 'Identifier') {
305
- defaultExportSymbols.add(node.right.name);
306
- return;
307
- }
308
-
309
- if (
310
- (node.right?.type === 'FunctionExpression' || node.right?.type === 'ClassExpression')
311
- && node.right.id?.name
312
- ) {
313
- defaultExportSymbols.add(node.right.id.name);
314
- }
315
- }
316
- }
317
-
318
- function issueTouchesFile(issue, filePath) {
319
- if (issue.files.includes(filePath)) {
320
- return true;
321
- }
322
-
323
- return issue.contracts.some((contract) => typeof contract === 'string' && contract.startsWith(`${filePath}:`));
324
- }
325
-
326
- function mergeEvidence(evidence) {
327
- const seen = new Set();
328
- return evidence.filter((item) => {
329
- const key = `${item.type}:${item.consumerFile}:${item.targetFile}:${item.symbol}`;
330
- if (seen.has(key)) {
331
- return false;
332
- }
333
- seen.add(key);
334
- return true;
335
- });
336
- }
337
-
338
- function collectDirectImportEvidence(analysis, targetFiles, targetSymbols) {
339
- const evidence = [];
340
- for (const [localName, importMeta] of analysis.directImports.entries()) {
341
- if (!targetFiles.has(importMeta.sourceFile) || !targetSymbols.has(importMeta.importedName)) {
342
- continue;
343
- }
344
-
345
- evidence.push({
346
- type: 'import',
347
- consumerFile: analysis.file,
348
- sourceFile: analysis.file,
349
- scoreContribution: 1,
350
- targetFile: importMeta.sourceFile,
351
- symbol: importMeta.importedName,
352
- });
353
-
354
- if (analysis.directCalls.has(localName)) {
355
- evidence.push({
356
- type: 'call',
357
- consumerFile: analysis.file,
358
- sourceFile: analysis.file,
359
- scoreContribution: 1,
360
- targetFile: importMeta.sourceFile,
361
- symbol: importMeta.importedName,
362
- });
363
- }
364
- }
365
- return evidence;
366
- }
367
-
368
- function collectDefaultImportEvidence(analysis, targetFiles, targetSymbols, defaultExportSymbolsByFile) {
369
- const evidence = [];
370
- for (const [localName, importMeta] of analysis.defaultImports.entries()) {
371
- if (!targetFiles.has(importMeta.sourceFile)) {
372
- continue;
373
- }
374
-
375
- const defaultExportSymbols = defaultExportSymbolsByFile.get(importMeta.sourceFile) ?? new Set();
376
- const matchedSymbol = targetSymbols.has(localName)
377
- ? localName
378
- : Array.from(defaultExportSymbols).find((symbol) => targetSymbols.has(symbol));
379
-
380
- if (!matchedSymbol) {
381
- continue;
382
- }
383
-
384
- evidence.push({
385
- type: 'import',
386
- consumerFile: analysis.file,
387
- sourceFile: analysis.file,
388
- scoreContribution: 1,
389
- targetFile: importMeta.sourceFile,
390
- symbol: matchedSymbol,
391
- });
392
-
393
- if (analysis.directCalls.has(localName)) {
394
- evidence.push({
395
- type: 'call',
396
- consumerFile: analysis.file,
397
- sourceFile: analysis.file,
398
- scoreContribution: 1,
399
- targetFile: importMeta.sourceFile,
400
- symbol: matchedSymbol,
401
- });
402
- }
403
- }
404
- return evidence;
405
- }
406
-
407
- function collectMemberCallEvidence(analysis, targetFiles, targetSymbols) {
408
- const evidence = [];
409
- for (const memberCall of analysis.memberCalls) {
410
- const namespaceImport = analysis.namespaceImports.get(memberCall.objectName);
411
- if (!namespaceImport) {
412
- continue;
413
- }
414
-
415
- if (!targetFiles.has(namespaceImport.sourceFile) || !targetSymbols.has(memberCall.propertyName)) {
416
- continue;
417
- }
418
-
419
- evidence.push({
420
- type: 'call',
421
- consumerFile: analysis.file,
422
- sourceFile: analysis.file,
423
- scoreContribution: 1,
424
- targetFile: namespaceImport.sourceFile,
425
- symbol: memberCall.propertyName,
426
- });
427
- }
428
- return evidence;
429
- }
430
-
431
- function scoreImportDependencies(normalizedInput) {
432
- const targetSymbols = extractTargetSymbols(normalizedInput.taskContext);
433
- const targetFiles = extractTargetFiles(normalizedInput.taskContext);
434
-
435
- if (targetSymbols.size === 0 || targetFiles.size === 0) {
436
- return {
437
- score: 0,
438
- findings: [],
439
- evidence: [],
440
- };
441
- }
442
-
443
- const findings = [];
444
- const evidence = [];
445
- const analyses = [];
446
-
447
- for (const absoluteFile of listSourceFiles(normalizedInput.repositoryRoot)) {
448
- let analysis;
449
- try {
450
- analysis = collectFileAnalysis(absoluteFile, normalizedInput.repositoryRoot);
451
- } catch (_error) {
452
- // Unparseable file (binary, syntax error) — skip silently
453
- continue;
454
- }
455
- analyses.push(analysis);
456
- }
457
-
458
- const defaultExportSymbolsByFile = new Map(
459
- analyses.map((analysis) => [analysis.file, analysis.defaultExportSymbols]),
460
- );
461
-
462
- for (const analysis of analyses) {
463
- const fileEvidence = [
464
- ...collectDirectImportEvidence(analysis, targetFiles, targetSymbols),
465
- ...collectDefaultImportEvidence(analysis, targetFiles, targetSymbols, defaultExportSymbolsByFile),
466
- ...collectMemberCallEvidence(analysis, targetFiles, targetSymbols),
467
- ];
468
-
469
- if (fileEvidence.length === 0) {
470
- continue;
471
- }
472
-
473
- const touchedIssues = normalizedInput.openIssues.filter((issue) => issueTouchesFile(issue, analysis.file));
474
- for (const issue of touchedIssues) {
475
- const issueEvidence = mergeEvidence(fileEvidence);
476
- const score = issueEvidence.length;
477
-
478
- findings.push({
479
- sourceIssueId: normalizedInput.currentIssue.id,
480
- targetIssueId: issue.id,
481
- score,
482
- evidence: issueEvidence,
483
- });
484
- evidence.push(...issueEvidence);
485
- }
486
- }
487
-
488
- return {
489
- score: findings.reduce((total, finding) => total + finding.score, 0),
490
- findings,
491
- evidence: mergeEvidence(evidence),
492
- };
493
- }
494
-
495
- module.exports = {
496
- normalizeRepoPath,
497
- scoreImportDependencies,
498
- };
1
+ const fs = require('node:fs');
2
+ const path = require('node:path');
3
+ const { parse } = require('@babel/parser');
4
+ const { normalizeRepoPath } = require('./path-utils.js');
5
+ const { extractSymbols } = require('./task-parser.js');
6
+
7
+ const SOURCE_EXTENSIONS = new Set(['.js', '.cjs', '.mjs', '.jsx', '.ts', '.tsx']);
8
+ const DEFAULT_EXCLUDED_DIRECTORIES = new Set([
9
+ '.git',
10
+ '.worktrees',
11
+ 'coverage',
12
+ 'dist',
13
+ 'build',
14
+ 'docs',
15
+ 'fixtures',
16
+ 'node_modules',
17
+ 'test',
18
+ 'test-env',
19
+ ]);
20
+
21
+ function isIdentifierStart(character) {
22
+ return /[A-Za-z_]/.test(character);
23
+ }
24
+
25
+ function isIdentifierPart(character) {
26
+ return /\w/.test(character);
27
+ }
28
+
29
+ function listSourceFiles(rootDir) {
30
+ const results = [];
31
+ const queue = [rootDir];
32
+
33
+ while (queue.length > 0) {
34
+ const current = queue.pop();
35
+ let entries;
36
+ try {
37
+ entries = fs.readdirSync(current, { withFileTypes: true });
38
+ } catch (_error) {
39
+ // Inaccessible directory (permissions, broken symlink) — skip silently
40
+ continue;
41
+ }
42
+
43
+ for (const entry of entries) {
44
+ if (entry.isDirectory() && DEFAULT_EXCLUDED_DIRECTORIES.has(entry.name)) {
45
+ continue;
46
+ }
47
+
48
+ const absolutePath = path.join(current, entry.name);
49
+ if (entry.isDirectory()) {
50
+ queue.push(absolutePath);
51
+ continue;
52
+ }
53
+
54
+ if (SOURCE_EXTENSIONS.has(path.extname(entry.name))) {
55
+ results.push(absolutePath);
56
+ }
57
+ }
58
+ }
59
+
60
+ return results;
61
+ }
62
+
63
+ function extractTargetSymbols(taskContext) {
64
+ const symbols = new Set();
65
+
66
+ for (const task of taskContext.tasks) {
67
+ for (const symbol of extractSymbols(task.whatToImplement ?? '')) {
68
+ if (symbol && isIdentifierStart(symbol[0]) && Array.from(symbol).every(isIdentifierPart)) {
69
+ symbols.add(symbol);
70
+ }
71
+ }
72
+ }
73
+
74
+ return symbols;
75
+ }
76
+
77
+ function extractTargetFiles(taskContext) {
78
+ const files = new Set();
79
+
80
+ for (const task of taskContext.tasks) {
81
+ for (const file of task.files) {
82
+ files.add(normalizeRepoPath(file));
83
+ }
84
+ }
85
+
86
+ return files;
87
+ }
88
+
89
+ function resolveImportSource(sourceFile, specifier, repositoryRoot) {
90
+ if (!specifier || typeof specifier !== 'string' || !specifier.startsWith('.')) {
91
+ return null;
92
+ }
93
+
94
+ const basePath = path.resolve(path.dirname(sourceFile), specifier);
95
+ const candidates = [
96
+ basePath,
97
+ `${basePath}.js`,
98
+ `${basePath}.cjs`,
99
+ `${basePath}.mjs`,
100
+ `${basePath}.jsx`,
101
+ `${basePath}.ts`,
102
+ `${basePath}.tsx`,
103
+ path.join(basePath, 'index.js'),
104
+ path.join(basePath, 'index.cjs'),
105
+ path.join(basePath, 'index.mjs'),
106
+ ];
107
+
108
+ for (const candidate of candidates) {
109
+ if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
110
+ return normalizeRepoPath(path.relative(repositoryRoot, candidate));
111
+ }
112
+ }
113
+
114
+ return null;
115
+ }
116
+
117
+ function isAstNode(value) {
118
+ return Boolean(value) && typeof value === 'object' && typeof value.type === 'string';
119
+ }
120
+
121
+ function walkNode(node, visitor) {
122
+ if (!isAstNode(node)) {
123
+ return;
124
+ }
125
+
126
+ visitor(node);
127
+
128
+ for (const value of Object.values(node)) {
129
+ if (Array.isArray(value)) {
130
+ for (const item of value) {
131
+ if (isAstNode(item)) {
132
+ walkNode(item, visitor);
133
+ }
134
+ }
135
+ continue;
136
+ }
137
+
138
+ if (isAstNode(value)) {
139
+ walkNode(value, visitor);
140
+ }
141
+ }
142
+ }
143
+
144
+ function collectFileAnalysis(filePath, repositoryRoot) {
145
+ const relativeFile = normalizeRepoPath(path.relative(repositoryRoot, filePath));
146
+ const content = fs.readFileSync(filePath, 'utf8');
147
+ const ast = parse(content, {
148
+ sourceType: 'unambiguous',
149
+ errorRecovery: true,
150
+ plugins: ['jsx', 'typescript'],
151
+ });
152
+
153
+ const directImports = new Map();
154
+ const defaultImports = new Map();
155
+ const namespaceImports = new Map();
156
+ const directCalls = new Set();
157
+ const memberCalls = [];
158
+ const defaultExportSymbols = new Set();
159
+
160
+ walkNode(ast, (node) => {
161
+ recordImportDeclaration(node, filePath, repositoryRoot, directImports, defaultImports, namespaceImports);
162
+ recordRequireDeclarator(node, filePath, repositoryRoot, directImports, defaultImports, namespaceImports);
163
+ recordCallExpression(node, directCalls, memberCalls);
164
+ recordDefaultExport(node, defaultExportSymbols);
165
+ });
166
+
167
+ return {
168
+ file: relativeFile,
169
+ directImports,
170
+ defaultImports,
171
+ namespaceImports,
172
+ directCalls,
173
+ memberCalls,
174
+ defaultExportSymbols,
175
+ };
176
+ }
177
+
178
+ function recordImportDeclaration(node, filePath, repositoryRoot, directImports, defaultImports, namespaceImports) {
179
+ if (node.type !== 'ImportDeclaration') {
180
+ return;
181
+ }
182
+
183
+ const resolvedSource = resolveImportSource(filePath, node.source?.value, repositoryRoot);
184
+ if (!resolvedSource) {
185
+ return;
186
+ }
187
+
188
+ for (const specifier of node.specifiers ?? []) {
189
+ if (specifier.type === 'ImportSpecifier') {
190
+ directImports.set(specifier.local.name, {
191
+ sourceFile: resolvedSource,
192
+ importedName: specifier.imported.name,
193
+ });
194
+ } else if (specifier.type === 'ImportDefaultSpecifier' && specifier.local?.name) {
195
+ defaultImports.set(specifier.local.name, {
196
+ sourceFile: resolvedSource,
197
+ });
198
+ } else if (specifier.local?.name) {
199
+ namespaceImports.set(specifier.local.name, {
200
+ sourceFile: resolvedSource,
201
+ });
202
+ }
203
+ }
204
+ }
205
+
206
+ function recordRequireDeclarator(node, filePath, repositoryRoot, directImports, defaultImports, namespaceImports) {
207
+ if (
208
+ node.type !== 'VariableDeclarator'
209
+ || node.init?.type !== 'CallExpression'
210
+ || node.init.callee?.type !== 'Identifier'
211
+ || node.init.callee.name !== 'require'
212
+ || node.init.arguments?.[0]?.type !== 'StringLiteral'
213
+ ) {
214
+ return;
215
+ }
216
+
217
+ const resolvedSource = resolveImportSource(
218
+ filePath,
219
+ node.init.arguments[0].value,
220
+ repositoryRoot,
221
+ );
222
+ if (!resolvedSource) {
223
+ return;
224
+ }
225
+
226
+ if (node.id.type === 'ObjectPattern') {
227
+ for (const property of node.id.properties ?? []) {
228
+ if (property.type !== 'ObjectProperty' || property.value?.type !== 'Identifier') {
229
+ continue;
230
+ }
231
+
232
+ const importedName = property.key?.name ?? property.key?.value;
233
+ if (!importedName) {
234
+ continue;
235
+ }
236
+
237
+ directImports.set(property.value.name, {
238
+ sourceFile: resolvedSource,
239
+ importedName,
240
+ });
241
+ }
242
+ return;
243
+ }
244
+
245
+ if (node.id.type === 'Identifier') {
246
+ defaultImports.set(node.id.name, {
247
+ sourceFile: resolvedSource,
248
+ });
249
+ namespaceImports.set(node.id.name, {
250
+ sourceFile: resolvedSource,
251
+ });
252
+ }
253
+ }
254
+
255
+ function recordCallExpression(node, directCalls, memberCalls) {
256
+ if (node.type !== 'CallExpression') {
257
+ return;
258
+ }
259
+
260
+ if (node.callee?.type === 'Identifier') {
261
+ directCalls.add(node.callee.name);
262
+ return;
263
+ }
264
+
265
+ if (
266
+ node.callee?.type === 'MemberExpression'
267
+ && node.callee.object?.type === 'Identifier'
268
+ && !node.callee.computed
269
+ && node.callee.property?.type === 'Identifier'
270
+ ) {
271
+ memberCalls.push({
272
+ objectName: node.callee.object.name,
273
+ propertyName: node.callee.property.name,
274
+ });
275
+ }
276
+ }
277
+
278
+ function recordDefaultExport(node, defaultExportSymbols) {
279
+ if (node.type === 'ExportDefaultDeclaration') {
280
+ if (node.declaration?.type === 'Identifier') {
281
+ defaultExportSymbols.add(node.declaration.name);
282
+ return;
283
+ }
284
+
285
+ if (
286
+ (node.declaration?.type === 'FunctionDeclaration' || node.declaration?.type === 'ClassDeclaration')
287
+ && node.declaration.id?.name
288
+ ) {
289
+ defaultExportSymbols.add(node.declaration.id.name);
290
+ }
291
+ return;
292
+ }
293
+
294
+ if (
295
+ node.type === 'AssignmentExpression'
296
+ && node.operator === '='
297
+ && node.left?.type === 'MemberExpression'
298
+ && node.left.object?.type === 'Identifier'
299
+ && node.left.object.name === 'module'
300
+ && !node.left.computed
301
+ && node.left.property?.type === 'Identifier'
302
+ && node.left.property.name === 'exports'
303
+ ) {
304
+ if (node.right?.type === 'Identifier') {
305
+ defaultExportSymbols.add(node.right.name);
306
+ return;
307
+ }
308
+
309
+ if (
310
+ (node.right?.type === 'FunctionExpression' || node.right?.type === 'ClassExpression')
311
+ && node.right.id?.name
312
+ ) {
313
+ defaultExportSymbols.add(node.right.id.name);
314
+ }
315
+ }
316
+ }
317
+
318
+ function issueTouchesFile(issue, filePath) {
319
+ if (issue.files.includes(filePath)) {
320
+ return true;
321
+ }
322
+
323
+ return issue.contracts.some((contract) => typeof contract === 'string' && contract.startsWith(`${filePath}:`));
324
+ }
325
+
326
+ function mergeEvidence(evidence) {
327
+ const seen = new Set();
328
+ return evidence.filter((item) => {
329
+ const key = `${item.type}:${item.consumerFile}:${item.targetFile}:${item.symbol}`;
330
+ if (seen.has(key)) {
331
+ return false;
332
+ }
333
+ seen.add(key);
334
+ return true;
335
+ });
336
+ }
337
+
338
+ function collectDirectImportEvidence(analysis, targetFiles, targetSymbols) {
339
+ const evidence = [];
340
+ for (const [localName, importMeta] of analysis.directImports.entries()) {
341
+ if (!targetFiles.has(importMeta.sourceFile) || !targetSymbols.has(importMeta.importedName)) {
342
+ continue;
343
+ }
344
+
345
+ evidence.push({
346
+ type: 'import',
347
+ consumerFile: analysis.file,
348
+ sourceFile: analysis.file,
349
+ scoreContribution: 1,
350
+ targetFile: importMeta.sourceFile,
351
+ symbol: importMeta.importedName,
352
+ });
353
+
354
+ if (analysis.directCalls.has(localName)) {
355
+ evidence.push({
356
+ type: 'call',
357
+ consumerFile: analysis.file,
358
+ sourceFile: analysis.file,
359
+ scoreContribution: 1,
360
+ targetFile: importMeta.sourceFile,
361
+ symbol: importMeta.importedName,
362
+ });
363
+ }
364
+ }
365
+ return evidence;
366
+ }
367
+
368
+ function collectDefaultImportEvidence(analysis, targetFiles, targetSymbols, defaultExportSymbolsByFile) {
369
+ const evidence = [];
370
+ for (const [localName, importMeta] of analysis.defaultImports.entries()) {
371
+ if (!targetFiles.has(importMeta.sourceFile)) {
372
+ continue;
373
+ }
374
+
375
+ const defaultExportSymbols = defaultExportSymbolsByFile.get(importMeta.sourceFile) ?? new Set();
376
+ const matchedSymbol = targetSymbols.has(localName)
377
+ ? localName
378
+ : Array.from(defaultExportSymbols).find((symbol) => targetSymbols.has(symbol));
379
+
380
+ if (!matchedSymbol) {
381
+ continue;
382
+ }
383
+
384
+ evidence.push({
385
+ type: 'import',
386
+ consumerFile: analysis.file,
387
+ sourceFile: analysis.file,
388
+ scoreContribution: 1,
389
+ targetFile: importMeta.sourceFile,
390
+ symbol: matchedSymbol,
391
+ });
392
+
393
+ if (analysis.directCalls.has(localName)) {
394
+ evidence.push({
395
+ type: 'call',
396
+ consumerFile: analysis.file,
397
+ sourceFile: analysis.file,
398
+ scoreContribution: 1,
399
+ targetFile: importMeta.sourceFile,
400
+ symbol: matchedSymbol,
401
+ });
402
+ }
403
+ }
404
+ return evidence;
405
+ }
406
+
407
+ function collectMemberCallEvidence(analysis, targetFiles, targetSymbols) {
408
+ const evidence = [];
409
+ for (const memberCall of analysis.memberCalls) {
410
+ const namespaceImport = analysis.namespaceImports.get(memberCall.objectName);
411
+ if (!namespaceImport) {
412
+ continue;
413
+ }
414
+
415
+ if (!targetFiles.has(namespaceImport.sourceFile) || !targetSymbols.has(memberCall.propertyName)) {
416
+ continue;
417
+ }
418
+
419
+ evidence.push({
420
+ type: 'call',
421
+ consumerFile: analysis.file,
422
+ sourceFile: analysis.file,
423
+ scoreContribution: 1,
424
+ targetFile: namespaceImport.sourceFile,
425
+ symbol: memberCall.propertyName,
426
+ });
427
+ }
428
+ return evidence;
429
+ }
430
+
431
+ function scoreImportDependencies(normalizedInput) {
432
+ const targetSymbols = extractTargetSymbols(normalizedInput.taskContext);
433
+ const targetFiles = extractTargetFiles(normalizedInput.taskContext);
434
+
435
+ if (targetSymbols.size === 0 || targetFiles.size === 0) {
436
+ return {
437
+ score: 0,
438
+ findings: [],
439
+ evidence: [],
440
+ };
441
+ }
442
+
443
+ const findings = [];
444
+ const evidence = [];
445
+ const analyses = [];
446
+
447
+ for (const absoluteFile of listSourceFiles(normalizedInput.repositoryRoot)) {
448
+ let analysis;
449
+ try {
450
+ analysis = collectFileAnalysis(absoluteFile, normalizedInput.repositoryRoot);
451
+ } catch (_error) {
452
+ // Unparseable file (binary, syntax error) — skip silently
453
+ continue;
454
+ }
455
+ analyses.push(analysis);
456
+ }
457
+
458
+ const defaultExportSymbolsByFile = new Map(
459
+ analyses.map((analysis) => [analysis.file, analysis.defaultExportSymbols]),
460
+ );
461
+
462
+ for (const analysis of analyses) {
463
+ const fileEvidence = [
464
+ ...collectDirectImportEvidence(analysis, targetFiles, targetSymbols),
465
+ ...collectDefaultImportEvidence(analysis, targetFiles, targetSymbols, defaultExportSymbolsByFile),
466
+ ...collectMemberCallEvidence(analysis, targetFiles, targetSymbols),
467
+ ];
468
+
469
+ if (fileEvidence.length === 0) {
470
+ continue;
471
+ }
472
+
473
+ const touchedIssues = normalizedInput.openIssues.filter((issue) => issueTouchesFile(issue, analysis.file));
474
+ for (const issue of touchedIssues) {
475
+ const issueEvidence = mergeEvidence(fileEvidence);
476
+ const score = issueEvidence.length;
477
+
478
+ findings.push({
479
+ sourceIssueId: normalizedInput.currentIssue.id,
480
+ targetIssueId: issue.id,
481
+ score,
482
+ evidence: issueEvidence,
483
+ });
484
+ evidence.push(...issueEvidence);
485
+ }
486
+ }
487
+
488
+ return {
489
+ score: findings.reduce((total, finding) => total + finding.score, 0),
490
+ findings,
491
+ evidence: mergeEvidence(evidence),
492
+ };
493
+ }
494
+
495
+ module.exports = {
496
+ normalizeRepoPath,
497
+ scoreImportDependencies,
498
+ };