@codeyam/codeyam-cli 0.1.0-staging.8aea589 → 0.1.0-staging.bbe4da9

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 (485) hide show
  1. package/analyzer-template/.build-info.json +7 -7
  2. package/analyzer-template/log.txt +3 -3
  3. package/analyzer-template/package.json +8 -7
  4. package/analyzer-template/packages/ai/index.ts +6 -2
  5. package/analyzer-template/packages/ai/package.json +2 -2
  6. package/analyzer-template/packages/ai/scripts/ai-test-matrix.mjs +424 -0
  7. package/analyzer-template/packages/ai/src/lib/analyzeScope.ts +24 -0
  8. package/analyzer-template/packages/ai/src/lib/astScopes/astScopeAnalyzer.ts +6 -16
  9. package/analyzer-template/packages/ai/src/lib/astScopes/methodSemantics.ts +197 -0
  10. package/analyzer-template/packages/ai/src/lib/astScopes/paths.ts +28 -2
  11. package/analyzer-template/packages/ai/src/lib/astScopes/processExpression.ts +145 -4
  12. package/analyzer-template/packages/ai/src/lib/checkAllAttributes.ts +1 -3
  13. package/analyzer-template/packages/ai/src/lib/dataStructure/ScopeDataStructure.ts +1877 -542
  14. package/analyzer-template/packages/ai/src/lib/dataStructure/equivalencyManagers/FunctionCallManager.ts +138 -0
  15. package/analyzer-template/packages/ai/src/lib/dataStructure/equivalencyManagers/frameworks/ReactFrameworkManager.ts +1 -1
  16. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/BatchSchemaProcessor.ts +139 -0
  17. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/DebugTracer.ts +224 -0
  18. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/PathManager.ts +203 -0
  19. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/README.md +294 -0
  20. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/ScopeTreeManager.ts +161 -0
  21. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/VisitedTracker.ts +235 -0
  22. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/cleanNonObjectFunctions.ts +64 -1
  23. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/fillInSchemaGapsAndUnknowns.ts +14 -6
  24. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/selectBestValue.ts +70 -0
  25. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/uniqueIdUtils.ts +113 -0
  26. package/analyzer-template/packages/ai/src/lib/gatherRelevantDependentKeyAttributes.ts +36 -0
  27. package/analyzer-template/packages/ai/src/lib/generateChangesEntityDocumentation.ts +20 -2
  28. package/analyzer-template/packages/ai/src/lib/generateChangesEntityScenarioData.ts +56 -160
  29. package/analyzer-template/packages/ai/src/lib/generateChangesEntityScenarios.ts +79 -265
  30. package/analyzer-template/packages/ai/src/lib/generateEntityDocumentation.ts +16 -2
  31. package/analyzer-template/packages/ai/src/lib/generateEntityKeyAttributes.ts +32 -8
  32. package/analyzer-template/packages/ai/src/lib/generateEntityScenarioData.ts +53 -154
  33. package/analyzer-template/packages/ai/src/lib/generateEntityScenarios.ts +84 -254
  34. package/analyzer-template/packages/ai/src/lib/generateStatementAnalysis.ts +48 -71
  35. package/analyzer-template/packages/ai/src/lib/getConditionalUsagesFromCode.ts +27 -6
  36. package/analyzer-template/packages/ai/src/lib/getLLMCallStats.ts +0 -14
  37. package/analyzer-template/packages/ai/src/lib/modelInfo.ts +15 -0
  38. package/analyzer-template/packages/ai/src/lib/promptGenerators/gatherAttributesMap.ts +42 -4
  39. package/analyzer-template/packages/ai/src/lib/promptGenerators/generateChangesEntityDocumentationGenerator.ts +8 -33
  40. package/analyzer-template/packages/ai/src/lib/promptGenerators/generateChangesEntityScenarioDataGenerator.ts +54 -62
  41. package/analyzer-template/packages/ai/src/lib/promptGenerators/generateChangesEntityScenariosGenerator.ts +93 -109
  42. package/analyzer-template/packages/ai/src/lib/promptGenerators/generateEntityDocumentationGenerator.ts +8 -27
  43. package/analyzer-template/packages/ai/src/lib/promptGenerators/generateEntityScenarioDataGenerator.ts +33 -38
  44. package/analyzer-template/packages/ai/src/lib/promptGenerators/generateEntityScenariosGenerator.ts +30 -30
  45. package/analyzer-template/packages/ai/src/lib/types/index.ts +2 -0
  46. package/analyzer-template/packages/ai/src/lib/worker/SerializableDataStructure.ts +41 -0
  47. package/analyzer-template/packages/analyze/src/lib/FileAnalyzer.ts +47 -8
  48. package/analyzer-template/packages/analyze/src/lib/asts/nodes/getNodeType.ts +1 -1
  49. package/analyzer-template/packages/analyze/src/lib/asts/nodes/index.ts +2 -1
  50. package/analyzer-template/packages/analyze/src/lib/asts/sourceFiles/getAllExportedNodes.ts +4 -2
  51. package/analyzer-template/packages/analyze/src/lib/asts/sourceFiles/getAllExports.ts +5 -3
  52. package/analyzer-template/packages/analyze/src/lib/files/analyze/analyzeEntities/prepareDataStructures.ts +8 -10
  53. package/analyzer-template/packages/analyze/src/lib/files/analyze/analyzeEntities.ts +6 -1
  54. package/analyzer-template/packages/analyze/src/lib/files/analyze/gatherEntityMap.ts +8 -6
  55. package/analyzer-template/packages/analyze/src/lib/files/analyze/trackEntityCircularDependencies.ts +5 -13
  56. package/analyzer-template/packages/analyze/src/lib/files/analyzeChange.ts +34 -15
  57. package/analyzer-template/packages/analyze/src/lib/files/analyzeEntity.ts +17 -3
  58. package/analyzer-template/packages/analyze/src/lib/files/analyzeInitial.ts +35 -16
  59. package/analyzer-template/packages/analyze/src/lib/files/analyzeRemixRoute.ts +21 -33
  60. package/analyzer-template/packages/analyze/src/lib/files/getImportedExports.ts +75 -10
  61. package/analyzer-template/packages/analyze/src/lib/files/scenarios/gatherDataForMocks.ts +26 -0
  62. package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateDataStructure.ts +7 -1
  63. package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateKeyAttributes.ts +9 -1
  64. package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateScenarioData.ts +6 -1
  65. package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateScenarios.ts +9 -1
  66. package/analyzer-template/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.ts +15 -7
  67. package/analyzer-template/packages/analyze/src/lib/files/scenarios/mergeValidatedDataStructures.ts +12 -2
  68. package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.d.ts +23 -0
  69. package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.d.ts.map +1 -0
  70. package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.js +30 -0
  71. package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.js.map +1 -0
  72. package/analyzer-template/packages/aws/package.json +5 -4
  73. package/analyzer-template/packages/aws/s3/index.ts +4 -0
  74. package/analyzer-template/packages/aws/src/lib/s3/getPresignedUrl.ts +62 -0
  75. package/analyzer-template/packages/generate/src/lib/componentScenarioPage/componentScenarioPageNext.ts +28 -21
  76. package/analyzer-template/packages/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.ts +18 -11
  77. package/analyzer-template/packages/generate/src/lib/scenarioComponent.ts +6 -3
  78. package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageNext.d.ts.map +1 -1
  79. package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageNext.js +28 -21
  80. package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageNext.js.map +1 -1
  81. package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.d.ts.map +1 -1
  82. package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.js +18 -11
  83. package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.js.map +1 -1
  84. package/analyzer-template/packages/github/dist/generate/src/lib/scenarioComponent.d.ts.map +1 -1
  85. package/analyzer-template/packages/github/dist/generate/src/lib/scenarioComponent.js +5 -3
  86. package/analyzer-template/packages/github/dist/generate/src/lib/scenarioComponent.js.map +1 -1
  87. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/db.d.ts +2 -0
  88. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/db.d.ts.map +1 -1
  89. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/db.js +3 -0
  90. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/db.js.map +1 -1
  91. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tableRelations.d.ts +2 -0
  92. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tableRelations.d.ts.map +1 -1
  93. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tables/debugReportsTable.d.ts +37 -0
  94. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tables/debugReportsTable.d.ts.map +1 -0
  95. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tables/debugReportsTable.js +27 -0
  96. package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tables/debugReportsTable.js.map +1 -0
  97. package/analyzer-template/packages/github/dist/supabase/src/lib/scenarioToDb.d.ts.map +1 -1
  98. package/analyzer-template/packages/github/dist/supabase/src/lib/scenarioToDb.js +1 -1
  99. package/analyzer-template/packages/github/dist/supabase/src/lib/scenarioToDb.js.map +1 -1
  100. package/analyzer-template/packages/github/dist/utils/index.d.ts +2 -0
  101. package/analyzer-template/packages/github/dist/utils/index.d.ts.map +1 -1
  102. package/analyzer-template/packages/github/dist/utils/index.js +2 -0
  103. package/analyzer-template/packages/github/dist/utils/index.js.map +1 -1
  104. package/analyzer-template/packages/github/dist/utils/src/lib/Semaphore.d.ts +25 -0
  105. package/analyzer-template/packages/github/dist/utils/src/lib/Semaphore.d.ts.map +1 -0
  106. package/analyzer-template/packages/github/dist/utils/src/lib/Semaphore.js +40 -0
  107. package/analyzer-template/packages/github/dist/utils/src/lib/Semaphore.js.map +1 -0
  108. package/analyzer-template/packages/github/dist/utils/src/lib/applyUniversalMocks.d.ts.map +1 -1
  109. package/analyzer-template/packages/github/dist/utils/src/lib/applyUniversalMocks.js +39 -5
  110. package/analyzer-template/packages/github/dist/utils/src/lib/applyUniversalMocks.js.map +1 -1
  111. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getNextRoutePath.d.ts.map +1 -1
  112. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getNextRoutePath.js +2 -1
  113. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getNextRoutePath.js.map +1 -1
  114. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getRemixRoutePath.d.ts.map +1 -1
  115. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getRemixRoutePath.js +2 -1
  116. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getRemixRoutePath.js.map +1 -1
  117. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.d.ts.map +1 -1
  118. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.js +2 -1
  119. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.js.map +1 -1
  120. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.d.ts.map +1 -1
  121. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.js +1 -0
  122. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.js.map +1 -1
  123. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.d.ts +12 -0
  124. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.d.ts.map +1 -0
  125. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.js +32 -0
  126. package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.js.map +1 -0
  127. package/analyzer-template/packages/github/dist/utils/src/lib/lightweightEntityExtractor.d.ts.map +1 -1
  128. package/analyzer-template/packages/github/dist/utils/src/lib/lightweightEntityExtractor.js +17 -0
  129. package/analyzer-template/packages/github/dist/utils/src/lib/lightweightEntityExtractor.js.map +1 -1
  130. package/analyzer-template/packages/supabase/src/lib/kysely/db.ts +6 -0
  131. package/analyzer-template/packages/supabase/src/lib/kysely/tableRelations.ts +3 -0
  132. package/analyzer-template/packages/supabase/src/lib/kysely/tables/debugReportsTable.ts +61 -0
  133. package/analyzer-template/packages/supabase/src/lib/scenarioToDb.ts +1 -0
  134. package/analyzer-template/packages/ui-components/src/scenario-editor/components/DataItemEditor.tsx +1 -1
  135. package/analyzer-template/packages/utils/dist/utils/index.d.ts +2 -0
  136. package/analyzer-template/packages/utils/dist/utils/index.d.ts.map +1 -1
  137. package/analyzer-template/packages/utils/dist/utils/index.js +2 -0
  138. package/analyzer-template/packages/utils/dist/utils/index.js.map +1 -1
  139. package/analyzer-template/packages/utils/dist/utils/src/lib/Semaphore.d.ts +25 -0
  140. package/analyzer-template/packages/utils/dist/utils/src/lib/Semaphore.d.ts.map +1 -0
  141. package/analyzer-template/packages/utils/dist/utils/src/lib/Semaphore.js +40 -0
  142. package/analyzer-template/packages/utils/dist/utils/src/lib/Semaphore.js.map +1 -0
  143. package/analyzer-template/packages/utils/dist/utils/src/lib/applyUniversalMocks.d.ts.map +1 -1
  144. package/analyzer-template/packages/utils/dist/utils/src/lib/applyUniversalMocks.js +39 -5
  145. package/analyzer-template/packages/utils/dist/utils/src/lib/applyUniversalMocks.js.map +1 -1
  146. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getNextRoutePath.d.ts.map +1 -1
  147. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getNextRoutePath.js +2 -1
  148. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getNextRoutePath.js.map +1 -1
  149. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getRemixRoutePath.d.ts.map +1 -1
  150. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getRemixRoutePath.js +2 -1
  151. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getRemixRoutePath.js.map +1 -1
  152. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.d.ts.map +1 -1
  153. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.js +2 -1
  154. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.js.map +1 -1
  155. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.d.ts.map +1 -1
  156. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.js +1 -0
  157. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.js.map +1 -1
  158. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.d.ts +12 -0
  159. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.d.ts.map +1 -0
  160. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.js +32 -0
  161. package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.js.map +1 -0
  162. package/analyzer-template/packages/utils/dist/utils/src/lib/lightweightEntityExtractor.d.ts.map +1 -1
  163. package/analyzer-template/packages/utils/dist/utils/src/lib/lightweightEntityExtractor.js +17 -0
  164. package/analyzer-template/packages/utils/dist/utils/src/lib/lightweightEntityExtractor.js.map +1 -1
  165. package/analyzer-template/packages/utils/index.ts +2 -0
  166. package/analyzer-template/packages/utils/src/lib/Semaphore.ts +42 -0
  167. package/analyzer-template/packages/utils/src/lib/applyUniversalMocks.ts +46 -7
  168. package/analyzer-template/packages/utils/src/lib/frameworks/getNextRoutePath.ts +2 -1
  169. package/analyzer-template/packages/utils/src/lib/frameworks/getRemixRoutePath.ts +2 -1
  170. package/analyzer-template/packages/utils/src/lib/frameworks/nextRouteFileNameToRoute.ts +2 -1
  171. package/analyzer-template/packages/utils/src/lib/frameworks/remixRouteFileNameToRoute.ts +1 -0
  172. package/analyzer-template/packages/utils/src/lib/frameworks/sanitizeNextRouteSegments.ts +33 -0
  173. package/analyzer-template/packages/utils/src/lib/lightweightEntityExtractor.ts +16 -0
  174. package/analyzer-template/project/constructMockCode.ts +199 -9
  175. package/analyzer-template/project/reconcileMockDataKeys.ts +13 -0
  176. package/analyzer-template/project/runMultiScenarioServer.ts +0 -4
  177. package/analyzer-template/project/runScenarioServer.ts +0 -4
  178. package/analyzer-template/project/start.ts +1 -11
  179. package/analyzer-template/project/startScenarioCapture.ts +24 -0
  180. package/analyzer-template/project/startServer.ts +50 -70
  181. package/analyzer-template/project/trackGeneratedFiles.ts +41 -0
  182. package/analyzer-template/project/writeMockDataTsx.ts +191 -7
  183. package/analyzer-template/project/writeScenarioComponents.ts +643 -63
  184. package/analyzer-template/project/writeUniversalMocks.ts +66 -8
  185. package/analyzer-template/scripts/postbuild.cjs +12 -1
  186. package/background/src/lib/virtualized/project/constructMockCode.js +183 -11
  187. package/background/src/lib/virtualized/project/constructMockCode.js.map +1 -1
  188. package/background/src/lib/virtualized/project/reconcileMockDataKeys.js +12 -0
  189. package/background/src/lib/virtualized/project/reconcileMockDataKeys.js.map +1 -1
  190. package/background/src/lib/virtualized/project/runMultiScenarioServer.js +0 -3
  191. package/background/src/lib/virtualized/project/runMultiScenarioServer.js.map +1 -1
  192. package/background/src/lib/virtualized/project/start.js +1 -8
  193. package/background/src/lib/virtualized/project/start.js.map +1 -1
  194. package/background/src/lib/virtualized/project/startScenarioCapture.js +18 -0
  195. package/background/src/lib/virtualized/project/startScenarioCapture.js.map +1 -1
  196. package/background/src/lib/virtualized/project/startServer.js +40 -68
  197. package/background/src/lib/virtualized/project/startServer.js.map +1 -1
  198. package/background/src/lib/virtualized/project/trackGeneratedFiles.js +30 -0
  199. package/background/src/lib/virtualized/project/trackGeneratedFiles.js.map +1 -0
  200. package/background/src/lib/virtualized/project/writeMockDataTsx.js +156 -6
  201. package/background/src/lib/virtualized/project/writeMockDataTsx.js.map +1 -1
  202. package/background/src/lib/virtualized/project/writeScenarioComponents.js +433 -41
  203. package/background/src/lib/virtualized/project/writeScenarioComponents.js.map +1 -1
  204. package/background/src/lib/virtualized/project/writeUniversalMocks.js +56 -7
  205. package/background/src/lib/virtualized/project/writeUniversalMocks.js.map +1 -1
  206. package/codeyam-cli/src/cli.js +6 -0
  207. package/codeyam-cli/src/cli.js.map +1 -1
  208. package/codeyam-cli/src/codeyam-cli.js +0 -0
  209. package/codeyam-cli/src/commands/debug.js +222 -0
  210. package/codeyam-cli/src/commands/debug.js.map +1 -0
  211. package/codeyam-cli/src/commands/init.js +4 -23
  212. package/codeyam-cli/src/commands/init.js.map +1 -1
  213. package/codeyam-cli/src/commands/report.js +102 -0
  214. package/codeyam-cli/src/commands/report.js.map +1 -0
  215. package/codeyam-cli/src/commands/setup-sandbox.js +165 -0
  216. package/codeyam-cli/src/commands/setup-sandbox.js.map +1 -0
  217. package/codeyam-cli/src/commands/test-startup.js +14 -5
  218. package/codeyam-cli/src/commands/test-startup.js.map +1 -1
  219. package/codeyam-cli/src/utils/__tests__/cleanupAnalysisFiles.test.js +6 -6
  220. package/codeyam-cli/src/utils/__tests__/cleanupAnalysisFiles.test.js.map +1 -1
  221. package/codeyam-cli/src/utils/__tests__/setupClaudeCodeSettings.test.js +8 -0
  222. package/codeyam-cli/src/utils/__tests__/setupClaudeCodeSettings.test.js.map +1 -1
  223. package/codeyam-cli/src/utils/analysisRunner.js +2 -1
  224. package/codeyam-cli/src/utils/analysisRunner.js.map +1 -1
  225. package/codeyam-cli/src/utils/analyzer.js +24 -2
  226. package/codeyam-cli/src/utils/analyzer.js.map +1 -1
  227. package/codeyam-cli/src/utils/cleanupAnalysisFiles.js +2 -2
  228. package/codeyam-cli/src/utils/cleanupAnalysisFiles.js.map +1 -1
  229. package/codeyam-cli/src/utils/generateReport.js +219 -0
  230. package/codeyam-cli/src/utils/generateReport.js.map +1 -0
  231. package/codeyam-cli/src/utils/install-skills.js +7 -0
  232. package/codeyam-cli/src/utils/install-skills.js.map +1 -1
  233. package/codeyam-cli/src/utils/queue/__tests__/job.pidTracking.test.js +1 -0
  234. package/codeyam-cli/src/utils/queue/__tests__/job.pidTracking.test.js.map +1 -1
  235. package/codeyam-cli/src/utils/queue/job.js +10 -5
  236. package/codeyam-cli/src/utils/queue/job.js.map +1 -1
  237. package/codeyam-cli/src/utils/sandbox.js +190 -0
  238. package/codeyam-cli/src/utils/sandbox.js.map +1 -0
  239. package/codeyam-cli/src/utils/setupClaudeCodeSettings.js +4 -0
  240. package/codeyam-cli/src/utils/setupClaudeCodeSettings.js.map +1 -1
  241. package/codeyam-cli/src/utils/webappDetection.js +2 -1
  242. package/codeyam-cli/src/utils/webappDetection.js.map +1 -1
  243. package/codeyam-cli/src/webserver/app/lib/database.js +50 -2
  244. package/codeyam-cli/src/webserver/app/lib/database.js.map +1 -1
  245. package/codeyam-cli/src/webserver/build/client/assets/EntityTypeIcon-Dp_FTAs1.js +1 -0
  246. package/codeyam-cli/src/webserver/build/client/assets/InteractivePreview-TlHocYno.js +26 -0
  247. package/codeyam-cli/src/webserver/build/client/assets/LibraryFunctionPreview-CVMmGuIc.js +3 -0
  248. package/codeyam-cli/src/webserver/build/client/assets/{LogViewer-BKKG1s2B.js → LogViewer-JkfQ-VaI.js} +1 -1
  249. package/codeyam-cli/src/webserver/build/client/assets/ReportIssueModal-Cqce0_KG.js +1 -0
  250. package/codeyam-cli/src/webserver/build/client/assets/SafeScreenshot-BrMAP1nP.js +1 -0
  251. package/codeyam-cli/src/webserver/build/client/assets/ScenarioPreview-Bi-__7HT.js +6 -0
  252. package/codeyam-cli/src/webserver/build/client/assets/ScenarioViewer-XmIpHcLJ.js +5 -0
  253. package/codeyam-cli/src/webserver/build/client/assets/_index-BmfhU6CA.js +1 -0
  254. package/codeyam-cli/src/webserver/build/client/assets/activity.(_tab)-Dm8lM73z.js +10 -0
  255. package/codeyam-cli/src/webserver/build/client/assets/api.generate-report-l0sNRNKZ.js +1 -0
  256. package/codeyam-cli/src/webserver/build/client/assets/chart-column-kA4jn9if.js +1 -0
  257. package/codeyam-cli/src/webserver/build/client/assets/chunk-WWGJGFF6-CgXbbZRx.js +26 -0
  258. package/codeyam-cli/src/webserver/build/client/assets/circle-check-B2oHQ-zo.js +1 -0
  259. package/codeyam-cli/src/webserver/build/client/assets/clock-BAfbP_iK.js +1 -0
  260. package/codeyam-cli/src/webserver/build/client/assets/codeyam-name-logo-CvKwUgHo.svg +9 -0
  261. package/codeyam-cli/src/webserver/build/client/assets/createLucideIcon-BBYuR56H.js +1 -0
  262. package/codeyam-cli/src/webserver/build/client/assets/dev.empty-BgPXZbm0.js +1 -0
  263. package/codeyam-cli/src/webserver/build/client/assets/{entity._sha._-BHiWkb_W.js → entity._sha._-BkoAXaOa.js} +10 -10
  264. package/codeyam-cli/src/webserver/build/client/assets/entity._sha_.create-scenario-Bj5GHkhb.js +1 -0
  265. package/codeyam-cli/src/webserver/build/client/assets/entity._sha_.edit._scenarioId-eW5z9AyZ.js +5 -0
  266. package/codeyam-cli/src/webserver/build/client/assets/entityStatus-C5Okl18j.js +1 -0
  267. package/codeyam-cli/src/webserver/build/client/assets/{entityVersioning-Bk_YB1jM.js → entityVersioning-CU_Lchhc.js} +1 -1
  268. package/codeyam-cli/src/webserver/build/client/assets/entry.client-B9tSboXM.js +5 -0
  269. package/codeyam-cli/src/webserver/build/client/assets/file-text-18aYHZGd.js +1 -0
  270. package/codeyam-cli/src/webserver/build/client/assets/files-Df79EyEb.js +1 -0
  271. package/codeyam-cli/src/webserver/build/client/assets/git-CDEwTVH_.js +12 -0
  272. package/codeyam-cli/src/webserver/build/client/assets/globals-DXRB6jBc.css +1 -0
  273. package/codeyam-cli/src/webserver/build/client/assets/html2canvas-pro.esm-XQCGvadH.js +5 -0
  274. package/codeyam-cli/src/webserver/build/client/assets/index-_LjBsTxX.js +8 -0
  275. package/codeyam-cli/src/webserver/build/client/assets/loader-circle-D_EGChhq.js +1 -0
  276. package/codeyam-cli/src/webserver/build/client/assets/manifest-3e0ffbcc.js +1 -0
  277. package/codeyam-cli/src/webserver/build/client/assets/root-CGyT4J4b.js +16 -0
  278. package/codeyam-cli/src/webserver/build/client/assets/settings-CEPbAsom.js +1 -0
  279. package/codeyam-cli/src/webserver/build/client/assets/settings-R8QF_mHX.js +1 -0
  280. package/codeyam-cli/src/webserver/build/client/assets/simulations-B_PXvFom.js +1 -0
  281. package/codeyam-cli/src/webserver/build/client/assets/triangle-alert-BthANBVv.js +1 -0
  282. package/codeyam-cli/src/webserver/build/client/assets/{useLastLogLine-Lumm1t01.js → useLastLogLine-Blr5oZDE.js} +1 -1
  283. package/codeyam-cli/src/webserver/build/client/assets/useReportContext-CANr3QJ5.js +1 -0
  284. package/codeyam-cli/src/webserver/build/client/assets/useToast-Bbf4Hokd.js +1 -0
  285. package/codeyam-cli/src/webserver/build/server/assets/index-vf1FETCO.js +1 -0
  286. package/codeyam-cli/src/webserver/build/server/assets/server-build-B5s58TvB.js +169 -0
  287. package/codeyam-cli/src/webserver/build/server/index.js +1 -1
  288. package/codeyam-cli/src/webserver/build-info.json +5 -5
  289. package/codeyam-cli/src/webserver/server.js +1 -1
  290. package/codeyam-cli/src/webserver/server.js.map +1 -1
  291. package/codeyam-cli/templates/codeyam-setup-skill.md +70 -85
  292. package/codeyam-cli/templates/debug-command.md +125 -0
  293. package/package.json +9 -11
  294. package/packages/ai/index.js +2 -3
  295. package/packages/ai/index.js.map +1 -1
  296. package/packages/ai/src/lib/analyzeScope.js +13 -0
  297. package/packages/ai/src/lib/analyzeScope.js.map +1 -1
  298. package/packages/ai/src/lib/astScopes/astScopeAnalyzer.js +6 -15
  299. package/packages/ai/src/lib/astScopes/astScopeAnalyzer.js.map +1 -1
  300. package/packages/ai/src/lib/astScopes/methodSemantics.js +134 -0
  301. package/packages/ai/src/lib/astScopes/methodSemantics.js.map +1 -1
  302. package/packages/ai/src/lib/astScopes/paths.js +28 -3
  303. package/packages/ai/src/lib/astScopes/paths.js.map +1 -1
  304. package/packages/ai/src/lib/astScopes/processExpression.js +123 -3
  305. package/packages/ai/src/lib/astScopes/processExpression.js.map +1 -1
  306. package/packages/ai/src/lib/checkAllAttributes.js +1 -3
  307. package/packages/ai/src/lib/checkAllAttributes.js.map +1 -1
  308. package/packages/ai/src/lib/dataStructure/ScopeDataStructure.js +1358 -396
  309. package/packages/ai/src/lib/dataStructure/ScopeDataStructure.js.map +1 -1
  310. package/packages/ai/src/lib/dataStructure/equivalencyManagers/FunctionCallManager.js +137 -1
  311. package/packages/ai/src/lib/dataStructure/equivalencyManagers/FunctionCallManager.js.map +1 -1
  312. package/packages/ai/src/lib/dataStructure/equivalencyManagers/frameworks/ReactFrameworkManager.js +1 -1
  313. package/packages/ai/src/lib/dataStructure/equivalencyManagers/frameworks/ReactFrameworkManager.js.map +1 -1
  314. package/packages/ai/src/lib/dataStructure/helpers/BatchSchemaProcessor.js +112 -0
  315. package/packages/ai/src/lib/dataStructure/helpers/BatchSchemaProcessor.js.map +1 -0
  316. package/packages/ai/src/lib/dataStructure/helpers/DebugTracer.js +176 -0
  317. package/packages/ai/src/lib/dataStructure/helpers/DebugTracer.js.map +1 -0
  318. package/packages/ai/src/lib/dataStructure/helpers/PathManager.js +178 -0
  319. package/packages/ai/src/lib/dataStructure/helpers/PathManager.js.map +1 -0
  320. package/packages/ai/src/lib/dataStructure/helpers/ScopeTreeManager.js +138 -0
  321. package/packages/ai/src/lib/dataStructure/helpers/ScopeTreeManager.js.map +1 -0
  322. package/packages/ai/src/lib/dataStructure/helpers/VisitedTracker.js +199 -0
  323. package/packages/ai/src/lib/dataStructure/helpers/VisitedTracker.js.map +1 -0
  324. package/packages/ai/src/lib/dataStructure/helpers/cleanNonObjectFunctions.js +55 -1
  325. package/packages/ai/src/lib/dataStructure/helpers/cleanNonObjectFunctions.js.map +1 -1
  326. package/packages/ai/src/lib/dataStructure/helpers/fillInSchemaGapsAndUnknowns.js +14 -6
  327. package/packages/ai/src/lib/dataStructure/helpers/fillInSchemaGapsAndUnknowns.js.map +1 -1
  328. package/packages/ai/src/lib/dataStructure/helpers/selectBestValue.js +62 -0
  329. package/packages/ai/src/lib/dataStructure/helpers/selectBestValue.js.map +1 -0
  330. package/packages/ai/src/lib/dataStructure/helpers/uniqueIdUtils.js +90 -0
  331. package/packages/ai/src/lib/dataStructure/helpers/uniqueIdUtils.js.map +1 -0
  332. package/packages/ai/src/lib/gatherRelevantDependentKeyAttributes.js +22 -0
  333. package/packages/ai/src/lib/gatherRelevantDependentKeyAttributes.js.map +1 -1
  334. package/packages/ai/src/lib/generateChangesEntityDocumentation.js +19 -1
  335. package/packages/ai/src/lib/generateChangesEntityDocumentation.js.map +1 -1
  336. package/packages/ai/src/lib/generateChangesEntityScenarioData.js +55 -156
  337. package/packages/ai/src/lib/generateChangesEntityScenarioData.js.map +1 -1
  338. package/packages/ai/src/lib/generateChangesEntityScenarios.js +79 -262
  339. package/packages/ai/src/lib/generateChangesEntityScenarios.js.map +1 -1
  340. package/packages/ai/src/lib/generateEntityDocumentation.js +15 -1
  341. package/packages/ai/src/lib/generateEntityDocumentation.js.map +1 -1
  342. package/packages/ai/src/lib/generateEntityKeyAttributes.js +32 -8
  343. package/packages/ai/src/lib/generateEntityKeyAttributes.js.map +1 -1
  344. package/packages/ai/src/lib/generateEntityScenarioData.js +52 -152
  345. package/packages/ai/src/lib/generateEntityScenarioData.js.map +1 -1
  346. package/packages/ai/src/lib/generateEntityScenarios.js +88 -258
  347. package/packages/ai/src/lib/generateEntityScenarios.js.map +1 -1
  348. package/packages/ai/src/lib/generateStatementAnalysis.js +46 -71
  349. package/packages/ai/src/lib/generateStatementAnalysis.js.map +1 -1
  350. package/packages/ai/src/lib/getConditionalUsagesFromCode.js +13 -8
  351. package/packages/ai/src/lib/getConditionalUsagesFromCode.js.map +1 -1
  352. package/packages/ai/src/lib/getLLMCallStats.js +0 -14
  353. package/packages/ai/src/lib/getLLMCallStats.js.map +1 -1
  354. package/packages/ai/src/lib/modelInfo.js +15 -0
  355. package/packages/ai/src/lib/modelInfo.js.map +1 -1
  356. package/packages/ai/src/lib/promptGenerators/gatherAttributesMap.js +36 -3
  357. package/packages/ai/src/lib/promptGenerators/gatherAttributesMap.js.map +1 -1
  358. package/packages/ai/src/lib/promptGenerators/generateChangesEntityDocumentationGenerator.js +8 -33
  359. package/packages/ai/src/lib/promptGenerators/generateChangesEntityDocumentationGenerator.js.map +1 -1
  360. package/packages/ai/src/lib/promptGenerators/generateChangesEntityScenarioDataGenerator.js +35 -41
  361. package/packages/ai/src/lib/promptGenerators/generateChangesEntityScenarioDataGenerator.js.map +1 -1
  362. package/packages/ai/src/lib/promptGenerators/generateChangesEntityScenariosGenerator.js +59 -72
  363. package/packages/ai/src/lib/promptGenerators/generateChangesEntityScenariosGenerator.js.map +1 -1
  364. package/packages/ai/src/lib/promptGenerators/generateEntityDocumentationGenerator.js +8 -27
  365. package/packages/ai/src/lib/promptGenerators/generateEntityDocumentationGenerator.js.map +1 -1
  366. package/packages/ai/src/lib/promptGenerators/generateEntityScenarioDataGenerator.js +24 -27
  367. package/packages/ai/src/lib/promptGenerators/generateEntityScenarioDataGenerator.js.map +1 -1
  368. package/packages/ai/src/lib/promptGenerators/generateEntityScenariosGenerator.js +21 -22
  369. package/packages/ai/src/lib/promptGenerators/generateEntityScenariosGenerator.js.map +1 -1
  370. package/packages/ai/src/lib/types/index.js +2 -0
  371. package/packages/ai/src/lib/types/index.js.map +1 -1
  372. package/packages/ai/src/lib/worker/SerializableDataStructure.js +7 -0
  373. package/packages/ai/src/lib/worker/SerializableDataStructure.js.map +1 -1
  374. package/packages/analyze/src/lib/FileAnalyzer.js +39 -7
  375. package/packages/analyze/src/lib/FileAnalyzer.js.map +1 -1
  376. package/packages/analyze/src/lib/asts/nodes/getNodeType.js +1 -1
  377. package/packages/analyze/src/lib/asts/nodes/getNodeType.js.map +1 -1
  378. package/packages/analyze/src/lib/asts/nodes/index.js +2 -1
  379. package/packages/analyze/src/lib/asts/nodes/index.js.map +1 -1
  380. package/packages/analyze/src/lib/asts/sourceFiles/getAllExportedNodes.js +3 -2
  381. package/packages/analyze/src/lib/asts/sourceFiles/getAllExportedNodes.js.map +1 -1
  382. package/packages/analyze/src/lib/asts/sourceFiles/getAllExports.js +4 -3
  383. package/packages/analyze/src/lib/asts/sourceFiles/getAllExports.js.map +1 -1
  384. package/packages/analyze/src/lib/files/analyze/analyzeEntities/prepareDataStructures.js +6 -8
  385. package/packages/analyze/src/lib/files/analyze/analyzeEntities/prepareDataStructures.js.map +1 -1
  386. package/packages/analyze/src/lib/files/analyze/analyzeEntities.js +5 -1
  387. package/packages/analyze/src/lib/files/analyze/analyzeEntities.js.map +1 -1
  388. package/packages/analyze/src/lib/files/analyze/gatherEntityMap.js +8 -2
  389. package/packages/analyze/src/lib/files/analyze/gatherEntityMap.js.map +1 -1
  390. package/packages/analyze/src/lib/files/analyze/trackEntityCircularDependencies.js +5 -8
  391. package/packages/analyze/src/lib/files/analyze/trackEntityCircularDependencies.js.map +1 -1
  392. package/packages/analyze/src/lib/files/analyzeChange.js +21 -9
  393. package/packages/analyze/src/lib/files/analyzeChange.js.map +1 -1
  394. package/packages/analyze/src/lib/files/analyzeEntity.js +10 -4
  395. package/packages/analyze/src/lib/files/analyzeEntity.js.map +1 -1
  396. package/packages/analyze/src/lib/files/analyzeInitial.js +21 -9
  397. package/packages/analyze/src/lib/files/analyzeInitial.js.map +1 -1
  398. package/packages/analyze/src/lib/files/analyzeRemixRoute.js +18 -23
  399. package/packages/analyze/src/lib/files/analyzeRemixRoute.js.map +1 -1
  400. package/packages/analyze/src/lib/files/getImportedExports.js +56 -4
  401. package/packages/analyze/src/lib/files/getImportedExports.js.map +1 -1
  402. package/packages/analyze/src/lib/files/scenarios/gatherDataForMocks.js +24 -0
  403. package/packages/analyze/src/lib/files/scenarios/gatherDataForMocks.js.map +1 -1
  404. package/packages/analyze/src/lib/files/scenarios/generateDataStructure.js +6 -1
  405. package/packages/analyze/src/lib/files/scenarios/generateDataStructure.js.map +1 -1
  406. package/packages/analyze/src/lib/files/scenarios/generateKeyAttributes.js +9 -1
  407. package/packages/analyze/src/lib/files/scenarios/generateKeyAttributes.js.map +1 -1
  408. package/packages/analyze/src/lib/files/scenarios/generateScenarioData.js +5 -1
  409. package/packages/analyze/src/lib/files/scenarios/generateScenarioData.js.map +1 -1
  410. package/packages/analyze/src/lib/files/scenarios/generateScenarios.js +9 -1
  411. package/packages/analyze/src/lib/files/scenarios/generateScenarios.js.map +1 -1
  412. package/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.js +16 -7
  413. package/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.js.map +1 -1
  414. package/packages/analyze/src/lib/files/scenarios/mergeValidatedDataStructures.js +8 -2
  415. package/packages/analyze/src/lib/files/scenarios/mergeValidatedDataStructures.js.map +1 -1
  416. package/packages/generate/src/lib/componentScenarioPage/componentScenarioPageNext.js +28 -21
  417. package/packages/generate/src/lib/componentScenarioPage/componentScenarioPageNext.js.map +1 -1
  418. package/packages/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.js +18 -11
  419. package/packages/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.js.map +1 -1
  420. package/packages/generate/src/lib/scenarioComponent.js +5 -3
  421. package/packages/generate/src/lib/scenarioComponent.js.map +1 -1
  422. package/packages/supabase/src/lib/kysely/db.js +3 -0
  423. package/packages/supabase/src/lib/kysely/db.js.map +1 -1
  424. package/packages/supabase/src/lib/kysely/tables/debugReportsTable.js +27 -0
  425. package/packages/supabase/src/lib/kysely/tables/debugReportsTable.js.map +1 -0
  426. package/packages/supabase/src/lib/scenarioToDb.js +1 -1
  427. package/packages/supabase/src/lib/scenarioToDb.js.map +1 -1
  428. package/packages/utils/index.js +2 -0
  429. package/packages/utils/index.js.map +1 -1
  430. package/packages/utils/src/lib/Semaphore.js +40 -0
  431. package/packages/utils/src/lib/Semaphore.js.map +1 -0
  432. package/packages/utils/src/lib/applyUniversalMocks.js +39 -5
  433. package/packages/utils/src/lib/applyUniversalMocks.js.map +1 -1
  434. package/packages/utils/src/lib/frameworks/getNextRoutePath.js +2 -1
  435. package/packages/utils/src/lib/frameworks/getNextRoutePath.js.map +1 -1
  436. package/packages/utils/src/lib/frameworks/getRemixRoutePath.js +2 -1
  437. package/packages/utils/src/lib/frameworks/getRemixRoutePath.js.map +1 -1
  438. package/packages/utils/src/lib/frameworks/nextRouteFileNameToRoute.js +2 -1
  439. package/packages/utils/src/lib/frameworks/nextRouteFileNameToRoute.js.map +1 -1
  440. package/packages/utils/src/lib/frameworks/remixRouteFileNameToRoute.js +1 -0
  441. package/packages/utils/src/lib/frameworks/remixRouteFileNameToRoute.js.map +1 -1
  442. package/packages/utils/src/lib/frameworks/sanitizeNextRouteSegments.js +32 -0
  443. package/packages/utils/src/lib/frameworks/sanitizeNextRouteSegments.js.map +1 -0
  444. package/packages/utils/src/lib/lightweightEntityExtractor.js +17 -0
  445. package/packages/utils/src/lib/lightweightEntityExtractor.js.map +1 -1
  446. package/analyzer-template/packages/ai/src/lib/generateEntityDataMap.ts +0 -375
  447. package/codeyam-cli/src/webserver/build/client/assets/EntityTypeIcon-rqv54FUY.js +0 -1
  448. package/codeyam-cli/src/webserver/build/client/assets/InteractivePreview-B0oiPem-.js +0 -26
  449. package/codeyam-cli/src/webserver/build/client/assets/LibraryFunctionPreview-DqXXjAJ7.js +0 -3
  450. package/codeyam-cli/src/webserver/build/client/assets/SafeScreenshot-DU_jxCPD.js +0 -1
  451. package/codeyam-cli/src/webserver/build/client/assets/ScenarioPreview-5DY-YIxu.js +0 -6
  452. package/codeyam-cli/src/webserver/build/client/assets/ScenarioViewer-DmjXUj6m.js +0 -5
  453. package/codeyam-cli/src/webserver/build/client/assets/_index-DvSrcxsk.js +0 -1
  454. package/codeyam-cli/src/webserver/build/client/assets/activity.(_tab)-CsaMd9mb.js +0 -10
  455. package/codeyam-cli/src/webserver/build/client/assets/chart-column-VXBS6qOn.js +0 -1
  456. package/codeyam-cli/src/webserver/build/client/assets/circle-alert-n5GUC2AS.js +0 -1
  457. package/codeyam-cli/src/webserver/build/client/assets/clock-DKqtX8js.js +0 -1
  458. package/codeyam-cli/src/webserver/build/client/assets/components-Dj-Ggnl2.js +0 -40
  459. package/codeyam-cli/src/webserver/build/client/assets/dev.empty-BbR3FwNc.js +0 -1
  460. package/codeyam-cli/src/webserver/build/client/assets/entity._sha_.create-scenario-L7M9Vr5z.js +0 -1
  461. package/codeyam-cli/src/webserver/build/client/assets/entity._sha_.edit._scenarioId-C9w-q7P3.js +0 -5
  462. package/codeyam-cli/src/webserver/build/client/assets/entry.client-CdGoUs8A.js +0 -1
  463. package/codeyam-cli/src/webserver/build/client/assets/file-text-B6Er7j5k.js +0 -1
  464. package/codeyam-cli/src/webserver/build/client/assets/files-KcDVw1FY.js +0 -1
  465. package/codeyam-cli/src/webserver/build/client/assets/git-B9uZ8eSJ.js +0 -12
  466. package/codeyam-cli/src/webserver/build/client/assets/globals-B0f88RTV.css +0 -1
  467. package/codeyam-cli/src/webserver/build/client/assets/loader-circle-v3c6DFp4.js +0 -1
  468. package/codeyam-cli/src/webserver/build/client/assets/manifest-fca08d7e.js +0 -1
  469. package/codeyam-cli/src/webserver/build/client/assets/root-Cf8VBqIb.js +0 -16
  470. package/codeyam-cli/src/webserver/build/client/assets/search-DA14wXpu.js +0 -1
  471. package/codeyam-cli/src/webserver/build/client/assets/settings-COJUrwGu.js +0 -1
  472. package/codeyam-cli/src/webserver/build/client/assets/settings-NU_ZquhK.js +0 -1
  473. package/codeyam-cli/src/webserver/build/client/assets/simulations-CNaMJ-nR.js +0 -1
  474. package/codeyam-cli/src/webserver/build/client/assets/useToast-BRShB17p.js +0 -1
  475. package/codeyam-cli/src/webserver/build/client/assets/zap-BvukH0eN.js +0 -1
  476. package/codeyam-cli/src/webserver/build/client/cy-logo-cli.svg +0 -13
  477. package/codeyam-cli/src/webserver/build/client/favicon.svg +0 -13
  478. package/codeyam-cli/src/webserver/build/server/assets/index-DHr4rT4u.js +0 -1
  479. package/codeyam-cli/src/webserver/build/server/assets/server-build-Bi1mj14J.js +0 -166
  480. package/codeyam-cli/src/webserver/public/cy-logo-cli.svg +0 -13
  481. package/codeyam-cli/src/webserver/public/favicon.svg +0 -13
  482. package/packages/ai/src/lib/generateEntityDataMap.js +0 -335
  483. package/packages/ai/src/lib/generateEntityDataMap.js.map +0 -1
  484. package/packages/ai/src/lib/promptGenerators/generateEntityDataMapGenerator.js +0 -17
  485. package/packages/ai/src/lib/promptGenerators/generateEntityDataMapGenerator.js.map +0 -1
@@ -1,36 +1,104 @@
1
- /* ScopeDataStructure
1
+ /**
2
+ * ScopeDataStructure
2
3
  *
3
- * The ScopeDateStructure takes the output of the ASTAnalyzer and merges
4
- * all scopes to determine a complete schema for the function.
4
+ * The core engine for CodeYam's static analysis. Takes AST analysis output and
5
+ * determines the complete data schema for a function by tracking how data flows
6
+ * through the code.
5
7
  *
6
- * It does two passes through all scopes.
8
+ * ## High-Level Architecture
7
9
  *
8
- * The first establishes equivalencies,
9
- * such as implicit parent scope -> nested child scope equivalencies.
10
+ * ```
11
+ * ┌─────────────────┐ ┌─────────────────────────────────────────────┐
12
+ * │ AST Analyzer │────▶│ ScopeDataStructure │
13
+ * │ (per-scope) │ │ │
14
+ * └─────────────────┘ │ Phase 1: Discovery (onlyEquivalencies) │
15
+ * │ ├─ Process each scope's analysis │
16
+ * │ ├─ Build equivalency relationships │
17
+ * │ └─ Track nested scope hierarchy │
18
+ * │ │
19
+ * │ Phase 2: Schema Building │
20
+ * │ ├─ Follow all equivalencies │
21
+ * │ ├─ Build complete schemas at data sources │
22
+ * │ └─ Populate equivalencyDatabase │
23
+ * └─────────────────────────────────────────────┘
24
+ * │
25
+ * ▼
26
+ * ┌─────────────────────────────────────────────┐
27
+ * │ Output │
28
+ * │ • Function signature schemas │
29
+ * │ • Return value types │
30
+ * │ • External API call schemas │
31
+ * │ • Data flow (source → usage) mappings │
32
+ * └─────────────────────────────────────────────┘
33
+ * ```
10
34
  *
11
- * The second pass follows all equivalencies to build up a complete schema at the
12
- * source of the data (either the function's signature arguments or an external
13
- * API call made from within the function). While following the equivalencies the
14
- * second pass populates the equivalencyDatabase which tracks all equivalent values
15
- * and provides easy access to determine where data comes from and where it is used
16
- * to call external functions. This makes it possible to combine schemas between
17
- * functions to create a complete schema for a dependency tree.
35
+ * ## Key Concepts
18
36
  *
19
- * There are a number of getter functions that provide easy access to signature data
20
- * and return values for the function being analyzed and any function called from the
21
- * function being analyzed.
37
+ * ### Equivalencies
38
+ * When code assigns one variable to another (`const user = props.user`), we create
39
+ * an "equivalency" linking the two paths. Following these links lets us trace data
40
+ * back to its source (function arguments or API returns).
41
+ *
42
+ * ### Schema Paths
43
+ * Dot-separated strings describing data structure:
44
+ * - `signature[0].user.id` - First arg's user.id property
45
+ * - `items[].name` - name property of array elements
46
+ * - `functionCallReturnValue(fetch).data` - Return from fetch() call
47
+ *
48
+ * ### Two-Phase Processing
49
+ * 1. **Phase 1 (Discovery)**: `onlyEquivalencies = true`
50
+ * - Processes scopes, builds equivalency graph
51
+ * - Does NOT populate schemas yet
52
+ *
53
+ * 2. **Phase 2 (Schema Building)**: `captureCompleteSchema()`
54
+ * - Follows equivalencies to build complete schemas
55
+ * - Populates `equivalencyDatabase` for source/usage queries
56
+ *
57
+ * ## Key Methods (Entry Points)
58
+ *
59
+ * - `constructor(scopeName, scopeText, managers, scopeAnalysis)` - Initialize with root scope
60
+ * - `addScopeAnalysis(scopeName, text, analysis)` - Add nested scope
61
+ * - `captureCompleteSchema()` - Trigger Phase 2 processing
62
+ * - `getSchema({scopeName})` - Get schema for a scope
63
+ * - `getFunctionSignature({functionName})` - Get function signature schema
64
+ * - `getSourceEquivalencies()` - Find where data comes from
65
+ * - `getUsageEquivalencies()` - Find where data is used
66
+ *
67
+ * ## Debugging Tips
68
+ *
69
+ * - Enable metrics: `addToSchemaCallCount`, `followEquivalenciesCallCount`
70
+ * - Check `scopeNode.equivalencies` for raw equivalency data
71
+ * - Use `getEquivalenciesDatabaseEntry(scope, path)` to trace a specific path
72
+ * - The `visitedTracker` prevents infinite loops - if data is missing, check if
73
+ * it was already visited with a different value
74
+ *
75
+ * ## Related Files
76
+ *
77
+ * - `helpers/` - Extracted utility modules (PathManager, VisitedTracker, etc.)
78
+ * - `equivalencyManagers/` - Plugins that create equivalencies from AST patterns
79
+ * - `helpers/README.md` - Overview of the helper module architecture
22
80
  */
23
81
 
24
82
  import { ScopeAnalysis } from '~codeyam/types';
25
- import {
26
- joinParenthesesAndArrays,
27
- splitOutsideParenthesesAndArrays,
28
- } from '../splitOutsideParentheses';
29
83
  import { EquivalencyManager } from './equivalencyManagers/EquivalencyManager';
30
84
  import fillInSchemaGapsAndUnknowns from './helpers/fillInSchemaGapsAndUnknowns';
31
85
  import ensureSchemaConsistency from './helpers/ensureSchemaConsistency';
32
- import cleanOutBoundary from '../cleanOutBoundary';
33
86
  import cleanPath from './helpers/cleanPath';
87
+ import { PathManager } from './helpers/PathManager';
88
+ import {
89
+ uniqueId,
90
+ uniqueScopeVariables,
91
+ uniqueScopeAndPaths,
92
+ } from './helpers/uniqueIdUtils';
93
+ import selectBestValue from './helpers/selectBestValue';
94
+ import { VisitedTracker } from './helpers/VisitedTracker';
95
+ import { DebugTracer } from './helpers/DebugTracer';
96
+ import { BatchSchemaProcessor } from './helpers/BatchSchemaProcessor';
97
+ import {
98
+ ScopeTreeManager,
99
+ ROOT_SCOPE_NAME,
100
+ ScopeTreeNode,
101
+ } from './helpers/ScopeTreeManager';
34
102
  import cleanScopeNodeName from './helpers/cleanScopeNodeName';
35
103
  import getFunctionCallRoot from './helpers/getFunctionCallRoot';
36
104
  import cleanPathOfNonTransformingFunctions from './helpers/cleanPathOfNonTransformingFunctions';
@@ -42,38 +110,10 @@ import type {
42
110
  SerializableScopeVariable,
43
111
  } from '../worker/SerializableDataStructure';
44
112
 
45
- // import { appendFileSync } from 'node:fs';
46
- // function writeEquivalencyToFile(
47
- // scopeNodeName: string,
48
- // path: string,
49
- // equivalentScopeNodeName: string,
50
- // equivalentPath: string,
51
- // newEquivalentPath: string,
52
- // equivalencyReason: string,
53
- // ) {
54
- // if ([
55
- // 'original equivalency'
56
- // ].includes(equivalencyReason)) {
57
- // return
58
- // }
59
-
60
- // appendFileSync('equivalencies.txt', '\n\n');
61
- // appendFileSync('equivalencies.txt',
62
- // JSON.stringify(
63
- // {
64
- // newEquivalentPath,
65
- // scopeNodeName,
66
- // path,
67
- // equivalentScopeNodeName,
68
- // equivalentPath,
69
- // equivalencyReason,
70
- // },
71
- // null,
72
- // 2,
73
- // ),
74
- // );
75
- // }
76
-
113
+ /**
114
+ * ScopeInfo represents a function scope discovered during AST analysis.
115
+ * Contains the raw analysis data before it's processed into a ScopeNode.
116
+ */
77
117
  export interface ScopeInfo {
78
118
  name: string;
79
119
  text: string;
@@ -87,46 +127,100 @@ export interface ScopeInfo {
87
127
  analysis?: any;
88
128
  }
89
129
 
130
+ /**
131
+ * ScopeNode is the processed representation of a function scope.
132
+ * Contains the schema and equivalencies built during analysis.
133
+ *
134
+ * Key fields:
135
+ * - `schema`: Map of path → type (e.g., `{ 'user.id': 'string' }`)
136
+ * - `equivalencies`: Map of path → equivalent paths in other scopes
137
+ * - `functionCalls`: External function calls made from this scope
138
+ */
90
139
  export interface ScopeNode {
140
+ /** Unique scope name, may include suffix like `____cyScope0A` for disambiguation */
91
141
  name: string;
142
+ /** Path from root scope to this scope (e.g., ['outer', 'inner']) */
92
143
  tree: string[];
144
+ /** Original source code text of the function */
93
145
  text: string;
146
+ /** Raw AST analysis output */
94
147
  analysis: ScopeAnalysis;
148
+ /** True if this is a static class method */
95
149
  isStatic?: boolean;
150
+ /** The built schema: maps schema paths to type values */
96
151
  schema: Record<string, string>;
97
- equivalencies: Record<string, ScopeVariable[]>; // Trace data attributes to the source of the data
152
+ /** Maps local paths to equivalent paths in other scopes (for tracing data flow) */
153
+ equivalencies: Record<string, ScopeVariable[]>;
154
+ /** External function calls made from this scope */
98
155
  functionCalls: FunctionCallInfo[];
156
+ /** True if this scope is a function definition (vs. class/module) */
99
157
  isFunctionDefinition: boolean;
100
- instantiatedVariables?: string[]; // Variables instantiated in this scope
101
- parentInstantiatedVariables?: string[]; // Variables instantiated in the parent scope that are available in this scope
158
+ /** Variables created in this scope (e.g., via `const x = ...`) */
159
+ instantiatedVariables?: string[];
160
+ /** Variables from parent scope accessible in this scope */
161
+ parentInstantiatedVariables?: string[];
102
162
  }
103
163
 
104
- export interface ScopeTreeNode {
105
- name: string;
106
- children: ScopeTreeNode[];
107
- }
164
+ // Re-export ScopeTreeNode from ScopeTreeManager for backward compatibility
165
+ export type { ScopeTreeNode };
108
166
 
167
+ /**
168
+ * ScopeVariable represents a reference to a specific path in a specific scope.
169
+ * Used in equivalency chains to track data flow.
170
+ */
109
171
  export interface ScopeVariable {
110
- id: number; // Never go through the same equivalency twice
172
+ /** Unique ID to prevent processing the same equivalency twice */
173
+ id: number;
174
+ /** Name of the scope containing this variable */
111
175
  scopeNodeName: string;
176
+ /** Schema path (e.g., 'signature[0].user.id') */
112
177
  schemaPath: string;
178
+ /** Why this equivalency exists (e.g., 'destructuring', 'assignment') */
113
179
  equivalencyReason: string;
114
180
  }
115
181
 
182
+ /**
183
+ * VariableInfo is the public representation of a tracked variable.
184
+ * Returned by getter methods like `getVariableInfo()`.
185
+ */
116
186
  export interface VariableInfo {
117
187
  name: string;
118
188
  equivalentTo?: ScopeVariable[];
119
189
  schema: Record<string, string>;
120
190
  }
121
191
 
192
+ /**
193
+ * FunctionCallInfo tracks an external function call and its data flow.
194
+ * Used to understand what data is passed to external APIs.
195
+ */
122
196
  export interface FunctionCallInfo {
197
+ /** Function name (e.g., 'fetch', 'console.log') */
123
198
  name: string;
199
+ /** How the function was called (e.g., 'fetch(url, options)') */
124
200
  callSignature: string;
201
+ /** Scope where the call was made */
125
202
  callScope: string;
126
- schema?: Record<string, string>; // For tracking schema of external function calls
127
- equivalencies?: Record<string, ScopeVariable[]>; // Trace all signature data back to original source (either root scope signature of functionCallReturnValue of another external function call)
203
+ /** Schema of arguments passed to the function */
204
+ schema?: Record<string, string>;
205
+ /** Traces argument data back to original source */
206
+ equivalencies?: Record<string, ScopeVariable[]>;
207
+ /**
208
+ * All call signatures that were merged into this entry.
209
+ * When multiple method calls on the same object (e.g., logger.info, logger.debug)
210
+ * are encountered, they get merged into one FunctionCallInfo but we track all signatures.
211
+ */
212
+ allCallSignatures?: string[];
128
213
  }
129
214
 
215
+ /**
216
+ * EquivalencyValueChainItem tracks a single step in an equivalency chain.
217
+ * Used internally during schema building to trace data from usage back to source.
218
+ *
219
+ * Example chain for `const { user } = props; return user.name;`:
220
+ * 1. returnValue → user.name (reason: 'return statement')
221
+ * 2. user.name → props.user.name (reason: 'destructuring')
222
+ * 3. props.user.name → signature[0].user.name (reason: 'parameter')
223
+ */
130
224
  export interface EquivalencyValueChainItem {
131
225
  id: number;
132
226
  addToSchemaId?: number;
@@ -145,41 +239,124 @@ export interface EquivalencyValueChainItem {
145
239
  traceId?: number;
146
240
  }
147
241
 
242
+ /**
243
+ * EquivalencyDatabaseEntry links data sources to their usages.
244
+ *
245
+ * After analysis, use `getSourceEquivalencies()` and `getUsageEquivalencies()`
246
+ * to query this database and understand data flow:
247
+ * - sourceCandidates: Where the data originates (function args, API returns)
248
+ * - usages: Where the data is consumed (return values, JSX props, API calls)
249
+ * - intermediatesOrder: All paths in the chain, ordered by distance from source
250
+ */
148
251
  export interface EquivalencyDatabaseEntry {
149
252
  id: number;
253
+ /** Paths where data originates (signature args, API returns) */
150
254
  sourceCandidates: Pick<ScopeVariable, 'schemaPath' | 'scopeNodeName'>[];
255
+ /** Paths where data is consumed (return values, JSX props) */
151
256
  usages: Pick<ScopeVariable, 'schemaPath' | 'scopeNodeName'>[];
257
+ /** All intermediate paths with their distance from source (0 = source) */
152
258
  intermediatesOrder: Record<string, number>;
153
- complexSourceEquivalencies: Set<string>; // subsets of this equivalent path that each have different data sources (e.g. the variable analysisEntityPair has a different source for analysisEntityPair.analysis vs analysisEntityPair.entity)
259
+ /** Paths with multiple distinct sources (e.g., object with props from different origins) */
260
+ complexSourceEquivalencies: Set<string>;
154
261
  }
155
262
 
156
- const ROOT_SCOPE_NAME = 'root';
157
-
158
263
  // DEBUG: Performance metrics
159
264
  export let maxEquivalencyChainDepth = 0;
160
265
  export let addToSchemaCallCount = 0;
161
266
  export let followEquivalenciesCallCount = 0;
267
+ export let followEquivalenciesEarlyExitCount = 0;
268
+ export let followEquivalenciesEarlyExitPhase1Count = 0;
269
+ export let followEquivalenciesWithWorkCount = 0;
270
+ export let addEquivalencyCallCount = 0;
162
271
  export function resetScopeDataStructureMetrics() {
163
272
  maxEquivalencyChainDepth = 0;
164
273
  addToSchemaCallCount = 0;
165
274
  followEquivalenciesCallCount = 0;
275
+ followEquivalenciesEarlyExitCount = 0;
276
+ followEquivalenciesEarlyExitPhase1Count = 0;
277
+ followEquivalenciesWithWorkCount = 0;
278
+ addEquivalencyCallCount = 0;
166
279
  }
167
280
 
281
+ // Performance: Pre-computed Sets for equivalency reason filtering (O(1) vs O(n))
282
+ const ALLOWED_EQUIVALENCY_REASONS = new Set([
283
+ 'original equivalency',
284
+ 'implicit parent equivalency',
285
+ 'explicit parent equivalency',
286
+ 'transformed non-object function equivalency - original equivalency',
287
+ 'equivalency to external function call',
288
+ 'functionCall to function signature equivalency',
289
+ 'explicit parent signature',
290
+ 'useState setter call equivalency',
291
+ 'non-object function argument function signature equivalency1',
292
+ 'non-object function argument function signature equivalency2',
293
+ 'returnValue to functionCallReturnValue equivalency',
294
+ '.map() function deconstruction',
295
+ 'useMemo return value equivalent to first argument return value',
296
+ 'useState value equivalency',
297
+ 'Node .then() equivalency',
298
+ 'Explicit array deconstruction equivalency value',
299
+ 'forwardRef equivalency',
300
+ 'forwardRef signature to child signature equivalency',
301
+ 'captured function call return value equivalency',
302
+ 'original equivalency - rerouted via useCallback',
303
+ 'non-object function argument function signature equivalency1 - rerouted via useCallback',
304
+ 'non-object function argument function signature equivalency2 - rerouted via useCallback',
305
+ 'implicit parent equivalency - rerouted via useCallback',
306
+ 'Spread operator equivalency key update: implicit parent equivalency',
307
+ 'Array.from() equivalency',
308
+ 'propagated sub-property equivalency',
309
+ 'propagated function call return sub-property equivalency',
310
+ 'where was this function called from', // Added: tracks which scope called an external function
311
+ ]);
312
+
313
+ const SILENTLY_IGNORED_EQUIVALENCY_REASONS = new Set([
314
+ 'signature of functionCall',
315
+ 'transformed non-object function equivalency - implicit parent equivalency',
316
+ 'transformed non-object function equivalency - non-object function argument function signature equivalency1',
317
+ 'transformed non-object function equivalency - non-object function argument function signature equivalency2',
318
+ 'transformed non-object function equivalency - equivalency to external function call',
319
+ 'Explicit array deconstruction equivalency key',
320
+ 'transformed non-object function equivalency - useState setter call equivalency',
321
+ 'transformed non-object function equivalency - implicit parent equivalency - rerouted via useCallback',
322
+ 'transformed non-object function equivalency - Array.from() equivalency',
323
+ 'Spread operator equivalency key update: Explicit array deconstruction equivalency value',
324
+ ]);
325
+
168
326
  export class ScopeDataStructure {
169
327
  debugCount = 0;
170
328
  onlyEquivalencies = true;
171
329
  equivalencyManagers: EquivalencyManager[] = [];
172
330
  scopeNodes: Record<string, ScopeNode> = {};
173
- scopeTree: ScopeTreeNode = { name: ROOT_SCOPE_NAME, children: [] };
331
+ private scopeTreeManager: ScopeTreeManager = new ScopeTreeManager();
174
332
  externalFunctionCalls: FunctionCallInfo[] = [];
333
+
334
+ // Getter for backward compatibility - returns the tree structure
335
+ get scopeTree(): ScopeTreeNode {
336
+ return this.scopeTreeManager.getTree();
337
+ }
175
338
  environmentVariables: string[] = [];
176
339
  equivalencyDatabase: EquivalencyDatabaseEntry[] = [];
177
340
 
341
+ /**
342
+ * Conditional usages collected during AST analysis.
343
+ * Maps local variable path to array of usages.
344
+ */
345
+ private rawConditionalUsages: Record<
346
+ string,
347
+ Array<{
348
+ path: string;
349
+ conditionType: 'truthiness' | 'comparison' | 'switch';
350
+ comparedValues?: string[];
351
+ location: 'if' | 'ternary' | 'logical-and' | 'switch';
352
+ }>
353
+ > = {};
354
+
178
355
  private lastAddToSchemaId = 0;
179
356
  private lastEquivalencyId = 0;
180
357
  private lastEquivalencyDatabaseId = 0;
181
- private pathPartsCache: Map<string, string[]> = new Map();
182
- private pathJoinCache: Map<string, string> = new Map();
358
+ private pathManager: PathManager = new PathManager();
359
+ private visitedTracker: VisitedTracker = new VisitedTracker();
183
360
  private equivalencyDatabaseCache: Map<
184
361
  string,
185
362
  EquivalencyDatabaseEntry | undefined
@@ -187,7 +364,31 @@ export class ScopeDataStructure {
187
364
  // Inverted index: maps uniqueId -> EquivalencyDatabaseEntry for O(1) lookup
188
365
  private intermediatesOrderIndex: Map<string, EquivalencyDatabaseEntry> =
189
366
  new Map();
190
- private globalVisited: Set<string> = new Set();
367
+
368
+ // Index for O(1) lookup of external function calls by name
369
+ // Invalidated by setting to null; rebuilt lazily on next access
370
+ private externalFunctionCallsIndex: Map<string, FunctionCallInfo> | null =
371
+ null;
372
+
373
+ // Debug tracer for selective path/scope tracing
374
+ // Enable via: CODEYAM_DEBUG=true CODEYAM_DEBUG_PATHS="user.*,signature" npm test
375
+ private tracer: DebugTracer = new DebugTracer({
376
+ enabled: process.env.CODEYAM_DEBUG === 'true',
377
+ pathPatterns: process.env.CODEYAM_DEBUG_PATHS
378
+ ? process.env.CODEYAM_DEBUG_PATHS.split(',').map((p) => new RegExp(p))
379
+ : [],
380
+ scopePatterns: process.env.CODEYAM_DEBUG_SCOPES
381
+ ? process.env.CODEYAM_DEBUG_SCOPES.split(',').map((p) => new RegExp(p))
382
+ : [],
383
+ maxDepth: 20,
384
+ });
385
+
386
+ // Batch processor for converting recursive addToSchema calls to iterative processing
387
+ // This eliminates deep recursion and allows for better work deduplication
388
+ private batchProcessor: BatchSchemaProcessor | null = null;
389
+ // Track items already in the batch queue to prevent duplicates
390
+ // For external function calls, uses path-only keys (no value) for more aggressive deduplication
391
+ private batchQueuedSet: Set<string> | null = null;
191
392
 
192
393
  constructor(
193
394
  managers: EquivalencyManager[],
@@ -196,7 +397,7 @@ export class ScopeDataStructure {
196
397
  scopeAnalysis: ScopeAnalysis,
197
398
  ) {
198
399
  this.equivalencyManagers.push(...managers);
199
- this.scopeTree.name = scopeName;
400
+ this.scopeTreeManager.setRootName(scopeName);
200
401
  this.addScopeAnalysis(scopeName, scopeText, scopeAnalysis);
201
402
  }
202
403
 
@@ -208,8 +409,11 @@ export class ScopeDataStructure {
208
409
  isStatic?: boolean,
209
410
  ) {
210
411
  try {
211
- if (scopeNodeName !== this.scopeTree.name && !parentScopeName) {
212
- parentScopeName = this.scopeTree.name;
412
+ if (
413
+ scopeNodeName !== this.scopeTreeManager.getRootName() &&
414
+ !parentScopeName
415
+ ) {
416
+ parentScopeName = this.scopeTreeManager.getRootName();
213
417
  }
214
418
 
215
419
  let tree: any[] = [];
@@ -262,7 +466,7 @@ export class ScopeDataStructure {
262
466
  'CodeYam Error: Failed to add scope analysis for scope:',
263
467
  JSON.stringify(
264
468
  {
265
- rootScopeName: this.scopeTree.name,
469
+ rootScopeName: this.scopeTreeManager.getRootName(),
266
470
  scopeNodeName,
267
471
  scopeNodeText: scopeText,
268
472
  isolatedStructure: scopeAnalysis.isolatedStructure,
@@ -277,30 +481,51 @@ export class ScopeDataStructure {
277
481
  }
278
482
  }
279
483
 
484
+ /**
485
+ * Phase 2: Build complete schemas by following all equivalencies.
486
+ *
487
+ * This is the main entry point for schema building. Call this after all
488
+ * scopes have been added via `addScopeAnalysis()`.
489
+ *
490
+ * ## What Happens
491
+ *
492
+ * 1. Resets state (visited tracker, schemas, database)
493
+ * 2. For each scope, follows equivalencies to build complete schemas
494
+ * 3. Filters out internal function calls (keeps only external-facing)
495
+ * 4. Propagates source/usage relationships for querying
496
+ *
497
+ * ## After Calling
498
+ *
499
+ * You can then use:
500
+ * - `getSchema()` - Get the built schema for a scope
501
+ * - `getSourceEquivalencies()` - Find where data comes from
502
+ * - `getUsageEquivalencies()` - Find where data is used
503
+ */
280
504
  async captureCompleteSchema() {
505
+ // Reset state for Phase 2
281
506
  this.onlyEquivalencies = false;
282
507
  this.equivalencyDatabase = [];
283
- this.equivalencyDatabaseCache.clear(); // Clear cache when database is reset
284
- this.intermediatesOrderIndex.clear(); // Clear inverted index when database is reset
285
- this.globalVisited = new Set([]);
508
+ this.equivalencyDatabaseCache.clear();
509
+ this.intermediatesOrderIndex.clear();
510
+ this.visitedTracker.resetGlobalVisited();
286
511
 
287
512
  for (const externalFunctionCall of this.externalFunctionCalls) {
288
513
  externalFunctionCall.schema = {};
289
514
  }
290
515
 
291
- // for (const scopeNode of Object.values(this.scopeNodes)) {
516
+ // Build schemas for all scopes in parallel
292
517
  await Promise.all(
293
518
  Object.values(this.scopeNodes).map(async (scopeNode) => {
294
519
  scopeNode.schema = {};
295
520
  await this.determineEquivalenciesAndBuildSchema(scopeNode);
296
521
  }),
297
522
  );
298
- // }
523
+
299
524
  const validExternalFacingScopeNames = new Set<string>([
300
- this.scopeTree.name,
525
+ this.scopeTreeManager.getRootName(),
301
526
  ]);
302
527
  this.externalFunctionCalls = this.externalFunctionCalls.filter((efc) => {
303
- const efcName = efc.name.split('<')[0];
528
+ const efcName = this.pathManager.stripGenerics(efc.name);
304
529
  for (const manager of this.equivalencyManagers) {
305
530
  if (manager.internalFunctions.has(efcName)) {
306
531
  return false;
@@ -309,65 +534,71 @@ export class ScopeDataStructure {
309
534
  validExternalFacingScopeNames.add(efcName);
310
535
  return true;
311
536
  });
537
+ this.invalidateExternalFunctionCallsIndex();
538
+
539
+ // Pre-compute array methods list once (avoids Array.from() in hot loop)
540
+ const arrayMethodsList = Array.from(arrayMethodsSet);
541
+ const containsArrayMethod = (path: string) =>
542
+ arrayMethodsList.some((method) => path.includes(`.${method}(`));
312
543
 
313
544
  for (const entry of this.equivalencyDatabase) {
314
- entry.usages = entry.usages.filter(
315
- (usage) =>
316
- validExternalFacingScopeNames.has(
317
- usage.scopeNodeName.split('<')[0],
318
- ) &&
545
+ entry.usages = entry.usages.filter((usage) => {
546
+ const baseName = this.pathManager.stripGenerics(usage.scopeNodeName);
547
+ return (
548
+ validExternalFacingScopeNames.has(baseName) &&
319
549
  (usage.schemaPath.startsWith('returnValue') ||
320
- usage.schemaPath.startsWith(usage.scopeNodeName.split('<')[0])) &&
321
- !Array.from(arrayMethodsSet).some((method) =>
322
- usage.schemaPath.includes(`.${method}(`),
323
- ),
324
- );
325
- entry.sourceCandidates = entry.sourceCandidates.filter(
326
- (candidate) =>
327
- validExternalFacingScopeNames.has(
328
- candidate.scopeNodeName.split('<')[0],
329
- ) &&
550
+ usage.schemaPath.startsWith(baseName)) &&
551
+ !containsArrayMethod(usage.schemaPath)
552
+ );
553
+ });
554
+ entry.sourceCandidates = entry.sourceCandidates.filter((candidate) => {
555
+ const baseName = this.pathManager.stripGenerics(
556
+ candidate.scopeNodeName,
557
+ );
558
+ return (
559
+ validExternalFacingScopeNames.has(baseName) &&
330
560
  (candidate.schemaPath.startsWith('signature[') ||
331
- candidate.schemaPath.startsWith(
332
- candidate.scopeNodeName.split('<')[0],
333
- )) &&
334
- !Array.from(arrayMethodsSet).some((method) =>
335
- candidate.schemaPath.includes(`.${method}(`),
336
- ),
337
- );
561
+ candidate.schemaPath.startsWith(baseName)) &&
562
+ !containsArrayMethod(candidate.schemaPath)
563
+ );
564
+ });
338
565
  }
339
566
 
340
567
  this.propagateSourceAndUsageEquivalencies(
341
- this.scopeNodes[this.scopeTree.name],
568
+ this.scopeNodes[this.scopeTreeManager.getRootName()],
342
569
  );
343
570
 
344
571
  for (const externalFunctionCall of this.externalFunctionCalls) {
345
- const t = new Date().getTime();
346
- // console.info("Start Propagating", JSON.stringify({
347
- // externalFunctionCallName: externalFunctionCall.name,
348
- // // sourceEquivalencies: externalFunctionCall.sourceEquivalencies,
349
- // }, null, 2));
350
-
351
572
  this.propagateSourceAndUsageEquivalencies(externalFunctionCall);
352
-
353
- const elapsedTime = new Date().getTime() - t;
354
- if (elapsedTime > 100) {
355
- console.warn('Long Propagation Time', {
356
- externalFunctionCallName: externalFunctionCall.name,
357
- time: elapsedTime,
358
- });
359
- // throw new Error("STOP - LONG PROPAGATION TIME")
360
- }
361
-
362
- // console.info("Done Propagating", JSON.stringify({
363
- // externalFunctionCallName: externalFunctionCall.name,
364
- // // sourceEquivalencies: externalFunctionCall.sourceEquivalencies,
365
- // // usageEquivalencies: externalFunctionCall.usageEquivalencies,
366
- // time: elapsedTime
367
- // }, null, 2))
368
573
  }
369
574
  }
370
575
 
576
+ /**
577
+ * Core method: Adds a path/value to a scope's schema and follows equivalencies.
578
+ *
579
+ * This is the heart of schema building. For each path, it:
580
+ * 1. Validates the path and checks if already visited
581
+ * 2. Adds the path → value mapping to the scope's schema
582
+ * 3. Follows any equivalencies to propagate the schema to linked paths
583
+ * 4. Tracks the equivalency chain for source/usage database
584
+ *
585
+ * ## Cycle Detection
586
+ *
587
+ * Uses `visitedTracker.checkAndMarkGlobalVisited()` to prevent infinite loops.
588
+ * A path is considered visited if the exact (scope, path, value) tuple has
589
+ * been processed before.
590
+ *
591
+ * ## Equivalency Following
592
+ *
593
+ * When a path has equivalencies (e.g., `user` → `props.user`), this method
594
+ * recursively calls itself to add the equivalent path to its scope's schema.
595
+ *
596
+ * @param path - Schema path to add (e.g., 'user.id')
597
+ * @param value - Type value (e.g., 'string', 'number')
598
+ * @param scopeNode - The scope to add to
599
+ * @param equivalencyValueChain - Tracks the chain for database population
600
+ * @param traceId - Debug ID for tracing specific paths
601
+ */
371
602
  addToSchema({
372
603
  path,
373
604
  value,
@@ -382,11 +613,34 @@ export class ScopeDataStructure {
382
613
  traceId?: number;
383
614
  }) {
384
615
  addToSchemaCallCount++;
616
+
617
+ // Trace entry for debugging
618
+ this.tracer.traceEnter('addToSchema', {
619
+ path,
620
+ scope: scopeNode?.name,
621
+ value,
622
+ chainDepth: equivalencyValueChain.length,
623
+ });
624
+
625
+ // Safety: Detect runaway recursion
626
+ if (addToSchemaCallCount > 500000) {
627
+ console.error('INFINITE LOOP DETECTED in addToSchema', {
628
+ callCount: addToSchemaCallCount,
629
+ path,
630
+ value,
631
+ scopeNodeName: scopeNode?.name,
632
+ });
633
+ throw new Error(
634
+ `Infinite loop detected: addToSchema called ${addToSchemaCallCount} times`,
635
+ );
636
+ }
385
637
  // Track max chain depth
386
638
  if (equivalencyValueChain.length > maxEquivalencyChainDepth) {
387
639
  maxEquivalencyChainDepth = equivalencyValueChain.length;
388
640
  }
389
- if (!scopeNode) return;
641
+ if (!scopeNode) {
642
+ return;
643
+ }
390
644
 
391
645
  if (!this.isValidPath(path)) {
392
646
  if (traceId) {
@@ -395,6 +649,7 @@ export class ScopeDataStructure {
395
649
  return;
396
650
  }
397
651
 
652
+ // Update chain metadata for database tracking
398
653
  if (equivalencyValueChain.length > 0) {
399
654
  equivalencyValueChain[equivalencyValueChain.length - 1].addToSchemaId =
400
655
  ++this.lastAddToSchemaId;
@@ -405,78 +660,18 @@ export class ScopeDataStructure {
405
660
  };
406
661
  }
407
662
 
408
- // Use this debugging to track down an infinite loop
409
- // traceId ||= 0;
410
- // traceId += 1;
411
- // if (traceId) {
412
- // console.info(JSON.stringify({ traceId, path, value, scopeNodeName: scopeNode.name }, null, 2));
413
- // }
414
- // if (traceId > 30) {
415
- // console.warn(
416
- // 'HIGH TRACEID',
417
- // JSON.stringify(
418
- // {
419
- // path,
420
- // value,
421
- // scopeNodeName: scopeNode.name,
422
- // equivalencyValueChain,
423
- // },
424
- // null,
425
- // 2,
426
- // ),
427
- // );
428
- // throw new Error('STOP - HIGH TRACEID');
429
- // }
430
-
431
- // Use this debugging to trace a specific path through the propagation
432
- const debugLevel = 0; // 0 for minimal, 1 for executed paths, 2 for all paths info
433
- // if ((!this.onlyEquivalencies && path === 'entity.sha') || traceId) {
434
- // traceId ||= 0;
435
- // traceId += 1;
436
- // if (traceId === 1) {
437
- // this.debugCount += 1;
438
- // if (this.debugCount > 5) throw new Error('STOP - HIGH DEBUG COUNT');
439
- // console.warn('START');
440
- // }
441
-
442
- // console.info(
443
- // 'Debug Propagation: Adding to schema',
444
- // JSON.stringify(
445
- // {
446
- // // traceId,
447
- // path,
448
- // value,
449
- // scopeNodeName: scopeNode.name,
450
- // reason:
451
- // equivalencyValueChain[equivalencyValueChain.length - 1]?.reason,
452
- // // previous:
453
- // // equivalencyValueChain[equivalencyValueChain.length - 1],
454
- // text: scopeNode.text,
455
- // // tree: scopeNode.tree,
456
- // // onlyEquivalencies: this.onlyEquivalencies,
457
- // // equivalencyValueChain,
458
- // // scopeNode
459
- // // instatiatedVeriables: scopeNode.instantiatedVariables,
460
- // // parentInstantiatedVariables: scopeNode.parentInstantiatedVariables,
461
- // // isolatedStructure: scopeNode.analysis.isolatedStructure,
462
- // // isolatedEquivalencies: scopeNode.analysis.isolatedEquivalentVariables,
463
- // // equivalencies: traceId === 1 ? scopeNode.equivalencies : 'skipped',
464
- // // functionCalls: scopeNode.functionCalls,
465
- // // externalFunctionCalls: this.externalFunctionCalls.filter(fc => fc.callScope.includes(scopeNode.name))
466
- // },
467
- // null,
468
- // 2,
469
- // ),
470
- // );
471
- // // if (traceId > 1) traceId = undefined;
472
- // throw new Error('STOP ON ADD');
473
- // }
663
+ // Debug level: 0=minimal, 1=executed paths, 2=all paths (set via traceId)
664
+ const debugLevel = 0;
474
665
 
666
+ // ─────────────────────────────────────────────────────────────────────────
667
+ // SECTION 1: Handle complex source paths (paths with multiple data origins)
668
+ // ─────────────────────────────────────────────────────────────────────────
475
669
  const equivalenciesDatabaseEntry = this.getEquivalenciesDatabaseEntry(
476
670
  scopeNode.name,
477
671
  path,
478
672
  );
479
673
 
674
+ // Process complex source paths (before visited check, per original design)
480
675
  this.addComplexSourcePathVariables(
481
676
  equivalenciesDatabaseEntry,
482
677
  scopeNode,
@@ -485,23 +680,19 @@ export class ScopeDataStructure {
485
680
  traceId,
486
681
  );
487
682
 
488
- const uniqueIdentifier = `${scopeNode.name}::${path}::${value}`;
489
-
490
- if (this.globalVisited.has(uniqueIdentifier)) {
683
+ // ─────────────────────────────────────────────────────────────────────────
684
+ // SECTION 2: Cycle detection - skip if already visited with this value
685
+ // ─────────────────────────────────────────────────────────────────────────
686
+ if (
687
+ this.visitedTracker.checkAndMarkGlobalVisited(scopeNode.name, path, value)
688
+ ) {
491
689
  if (traceId) {
492
- console.info(
493
- 'Debug propagation: already globally visited path',
494
- JSON.stringify(
495
- {
496
- uniqueIdentifier,
497
- // equivalencyValueChain,
498
- },
499
- null,
500
- 2,
501
- ),
502
- );
690
+ console.info('Debug: already visited', {
691
+ key: `${scopeNode.name}::${path}::${value}`,
692
+ });
503
693
  }
504
694
 
695
+ // Still record in database even if visited (captures full chain)
505
696
  if (!this.onlyEquivalencies) {
506
697
  this.addToEquivalencyDatabase(equivalencyValueChain, traceId);
507
698
  }
@@ -509,8 +700,7 @@ export class ScopeDataStructure {
509
700
  return;
510
701
  }
511
702
 
512
- this.globalVisited.add(uniqueIdentifier);
513
-
703
+ // Continue complex source processing after visited check
514
704
  this.captureComplexSourcePaths(
515
705
  equivalenciesDatabaseEntry,
516
706
  scopeNode,
@@ -519,6 +709,10 @@ export class ScopeDataStructure {
519
709
  traceId,
520
710
  );
521
711
 
712
+ // ─────────────────────────────────────────────────────────────────────────
713
+ // SECTION 3: Process each subpath to follow equivalencies
714
+ // For path "user.profile.name", processes: "user", "user.profile", "user.profile.name"
715
+ // ─────────────────────────────────────────────────────────────────────────
522
716
  const pathParts = this.splitPath(path);
523
717
 
524
718
  this.checkForArrayItemPath(pathParts, scopeNode, equivalencyValueChain);
@@ -637,6 +831,11 @@ export class ScopeDataStructure {
637
831
  }
638
832
  }
639
833
 
834
+ // ─────────────────────────────────────────────────────────────────────────
835
+ // SECTION 4: Write to schema if appropriate
836
+ // Only writes if: (a) path not set or is 'unknown', AND
837
+ // (b) path belongs to this scope (instantiated, signature, or return)
838
+ // ─────────────────────────────────────────────────────────────────────────
640
839
  const writeToCurrentScopeSchema =
641
840
  (!scopeNode.schema[path] || scopeNode.schema[path] === 'unknown') &&
642
841
  (!scopeNode.instantiatedVariables ||
@@ -644,50 +843,35 @@ export class ScopeDataStructure {
644
843
  pathParts[0].startsWith('signature[') ||
645
844
  pathParts[0].startsWith('returnValue'));
646
845
 
647
- // Safe early exit: if we've already set this exact schema value, skip processing
846
+ // Early exit if schema already has this exact value
648
847
  if (scopeNode.schema[path] === value && value !== 'unknown') {
649
848
  return;
650
849
  }
651
850
 
652
851
  if (writeToCurrentScopeSchema) {
653
852
  if (traceId && debugLevel > 0) {
654
- console.info('Debug Propagation: currentScope executed', {
655
- traceId,
853
+ console.info('Debug: writing schema', {
656
854
  path,
657
- pathParts,
658
- scopeNodeName: scopeNode.name,
855
+ value,
856
+ scope: scopeNode.name,
659
857
  });
660
858
  }
661
859
  scopeNode.schema[path] = value;
662
860
  }
663
861
 
862
+ // ─────────────────────────────────────────────────────────────────────────
863
+ // SECTION 5: Record in equivalency database (Phase 2 only)
864
+ // ─────────────────────────────────────────────────────────────────────────
664
865
  if (!this.onlyEquivalencies) {
665
866
  this.addToEquivalencyDatabase(equivalencyValueChain, traceId);
666
867
  }
667
868
 
668
869
  if (traceId) {
669
- console.info(
670
- 'Debug Propagation: exiting',
671
- JSON.stringify(
672
- {
673
- traceId,
674
- scopeNodeName: scopeNode.name,
675
- path,
676
- value,
677
- // schemaValue: scopeNode.schema[path],
678
- // propagated,
679
- // scopeInfoText: scopeNode.text,
680
- // equivalencyValueChain//: equivalencyValueChain.map(
681
- // (ev) => ev.currentPath.schemaPath,
682
- // ),
683
- // sourceEquivalencies: scopeNode.sourceEquivalencies[path] ?? [],
684
- // usageEquivalencies: scopeNode.usageEquivalencies[path] ?? [],
685
- // checkpoints,
686
- },
687
- null,
688
- 2,
689
- ),
690
- );
870
+ console.info('Debug: addToSchema complete', {
871
+ scope: scopeNode.name,
872
+ path,
873
+ value,
874
+ });
691
875
  }
692
876
  }
693
877
 
@@ -708,53 +892,30 @@ export class ScopeDataStructure {
708
892
  equivalencyValueChain?: EquivalencyValueChainItem[],
709
893
  traceId?: number,
710
894
  ) {
711
- // Temporary debugging to track down unnecessary equivalencies
712
- if (
713
- ![
714
- 'original equivalency',
715
- 'implicit parent equivalency',
716
- 'explicit parent equivalency',
717
- 'transformed non-object function equivalency - original equivalency',
718
- 'equivalency to external function call',
719
- 'functionCall to function signature equivalency', // 1 failure
720
- 'explicit parent signature',
721
- 'useState setter call equivalency',
722
- 'non-object function argument function signature equivalency1',
723
- 'non-object function argument function signature equivalency2',
724
- 'returnValue to functionCallReturnValue equivalency', // 2 failures
725
- '.map() function deconstruction', // 1 failure
726
- 'useMemo return value equivalent to first argument return value',
727
- 'useState value equivalency', // 1 failure
728
- 'Node .then() equivalency', // 1 failure
729
- 'Explicit array deconstruction equivalency value', // 1 failure
730
- 'forwardRef equivalency',
731
- 'forwardRef signature to child signature equivalency', // 1 failure
732
- 'captured function call return value equivalency', // 1 failure (in ScopeDataStructure.test.ts)
733
- 'original equivalency - rerouted via useCallback', // 1 failure
734
- 'non-object function argument function signature equivalency1 - rerouted via useCallback', // 1 failure
735
- 'non-object function argument function signature equivalency2 - rerouted via useCallback', // 1 failure
736
- 'implicit parent equivalency - rerouted via useCallback', // 1 failure
737
- 'Spread operator equivalency key update: implicit parent equivalency', // 1 failure
738
- 'Array.from() equivalency',
739
- ].includes(equivalencyReason)
740
- ) {
741
- if (
742
- [
743
- 'signature of functionCall',
744
- 'transformed non-object function equivalency - implicit parent equivalency',
745
- 'transformed non-object function equivalency - non-object function argument function signature equivalency1',
746
- 'transformed non-object function equivalency - non-object function argument function signature equivalency2',
747
- 'transformed non-object function equivalency - equivalency to external function call',
748
- 'Explicit array deconstruction equivalency key',
749
- 'transformed non-object function equivalency - useState setter call equivalency',
750
- 'transformed non-object function equivalency - implicit parent equivalency - rerouted via useCallback',
751
- 'transformed non-object function equivalency - Array.from() equivalency',
752
- 'Spread operator equivalency key update: Explicit array deconstruction equivalency value',
753
- ].includes(equivalencyReason)
754
- ) {
895
+ // DEBUG: Detect infinite loops
896
+ addEquivalencyCallCount++;
897
+ if (addEquivalencyCallCount > 50000) {
898
+ console.error('INFINITE LOOP DETECTED in addEquivalency', {
899
+ callCount: addEquivalencyCallCount,
900
+ path,
901
+ equivalentPath,
902
+ equivalentScopeName,
903
+ scopeNodeName: scopeNode.name,
904
+ equivalencyReason,
905
+ });
906
+ throw new Error(
907
+ `Infinite loop detected: addEquivalency called ${addEquivalencyCallCount} times`,
908
+ );
909
+ }
910
+ // Filter equivalency reasons - use pre-computed Sets for O(1) lookup
911
+ if (!ALLOWED_EQUIVALENCY_REASONS.has(equivalencyReason)) {
912
+ if (SILENTLY_IGNORED_EQUIVALENCY_REASONS.has(equivalencyReason)) {
755
913
  return;
756
914
  } else {
915
+ // Log and skip - if an equivalency reason isn't in ALLOWED or SILENTLY_IGNORED,
916
+ // it shouldn't be stored (was previously missing the return)
757
917
  console.info('Not tracked equivalency reason', { equivalencyReason });
918
+ return;
758
919
  }
759
920
  }
760
921
 
@@ -771,13 +932,47 @@ export class ScopeDataStructure {
771
932
  scopeNode.equivalencies[path] ||= [];
772
933
 
773
934
  const existing = scopeNode.equivalencies[path];
774
- if (
775
- existing.find(
776
- (v) =>
777
- v.schemaPath === equivalentPath &&
778
- v.scopeNodeName === equivalentScopeName,
779
- )
780
- ) {
935
+ const existingEquivalency = existing.find(
936
+ (v) =>
937
+ v.schemaPath === equivalentPath &&
938
+ v.scopeNodeName === equivalentScopeName,
939
+ );
940
+ if (existingEquivalency) {
941
+ // During Phase 2 (onlyEquivalencies=false), we need to still process the equivalency
942
+ // to build the chain and add to the database, even if the equivalency already exists
943
+ if (!this.onlyEquivalencies) {
944
+ const equivalentScopeNode = this.getScopeOrFunctionCallInfo(
945
+ equivalentScopeName,
946
+ ) as ScopeNode;
947
+ if (equivalentScopeNode) {
948
+ // Extract function call name from path if it looks like a function call path
949
+ // e.g., "ChildComponent().signature[0].dataItem" -> "ChildComponent"
950
+ const pathMatch = path.match(/^([^().]+)\(\)/);
951
+ const previousPathScopeNodeName = pathMatch
952
+ ? pathMatch[1]
953
+ : scopeNode.name;
954
+
955
+ this.addToSchema({
956
+ path: equivalentPath,
957
+ value: 'unknown',
958
+ scopeNode: equivalentScopeNode,
959
+ equivalencyValueChain: [
960
+ ...(equivalencyValueChain ?? []),
961
+ {
962
+ id: existingEquivalency.id,
963
+ source: 'duplicate equivalency - Phase 2',
964
+ reason: equivalencyReason,
965
+ previousPath: {
966
+ scopeNodeName: previousPathScopeNodeName,
967
+ schemaPath: path,
968
+ value: scopeNode.schema[path],
969
+ },
970
+ },
971
+ ],
972
+ traceId,
973
+ });
974
+ }
975
+ }
781
976
  return;
782
977
  }
783
978
 
@@ -884,9 +1079,9 @@ export class ScopeDataStructure {
884
1079
  functionCallInfo.equivalencies = {};
885
1080
  }
886
1081
 
887
- const existingFunctionCall = this.externalFunctionCalls.find(
888
- (fc) => getFunctionCallRoot(functionCallInfo.callSignature) === fc.name,
889
- );
1082
+ const searchKey = getFunctionCallRoot(functionCallInfo.callSignature);
1083
+ const existingFunctionCall =
1084
+ this.getExternalFunctionCallsIndex().get(searchKey);
890
1085
  if (existingFunctionCall) {
891
1086
  existingFunctionCall.schema = {
892
1087
  ...existingFunctionCall.schema,
@@ -897,6 +1092,22 @@ export class ScopeDataStructure {
897
1092
  ...existingFunctionCall.equivalencies,
898
1093
  ...functionCallInfo.equivalencies,
899
1094
  };
1095
+
1096
+ // Track all call signatures that get merged (e.g., logger.info, logger.debug, etc.)
1097
+ if (!existingFunctionCall.allCallSignatures) {
1098
+ existingFunctionCall.allCallSignatures = [
1099
+ existingFunctionCall.callSignature,
1100
+ ];
1101
+ }
1102
+ if (
1103
+ !existingFunctionCall.allCallSignatures.includes(
1104
+ functionCallInfo.callSignature,
1105
+ )
1106
+ ) {
1107
+ existingFunctionCall.allCallSignatures.push(
1108
+ functionCallInfo.callSignature,
1109
+ );
1110
+ }
900
1111
  } else {
901
1112
  const functionCallInfoNameParts = this.splitPath(functionCallInfo.name);
902
1113
  const isExternal =
@@ -909,6 +1120,7 @@ export class ScopeDataStructure {
909
1120
 
910
1121
  if (isExternal) {
911
1122
  this.externalFunctionCalls.push(functionCallInfo);
1123
+ this.invalidateExternalFunctionCallsIndex();
912
1124
  }
913
1125
  }
914
1126
  }
@@ -935,7 +1147,7 @@ export class ScopeDataStructure {
935
1147
  // }
936
1148
 
937
1149
  const getRootOrExternalFunctionCallInfo = (name: string) => {
938
- return name === this.scopeTree.name
1150
+ return name === this.scopeTreeManager.getRootName()
939
1151
  ? this.getScopeNode()
940
1152
  : this.getExternalFunctionCallInfo(name);
941
1153
  };
@@ -966,6 +1178,40 @@ export class ScopeDataStructure {
966
1178
  },
967
1179
  );
968
1180
 
1181
+ // PERF: Build a Map for O(1) lookup instead of O(n) inner loop
1182
+ // Map from "remaining parts joined" to equivalentSchemaPath
1183
+ const equivalentSchemaPathMap = new Map<string, string>();
1184
+ for (const equivalentSchemaPath of Object.keys(
1185
+ equivalentScopeNode.schema,
1186
+ )) {
1187
+ // Quick string check before expensive path splitting
1188
+ if (
1189
+ !(
1190
+ equivalentSchemaPath.startsWith(equivalentPath) ||
1191
+ equivalentSchemaPath === equivalentPath ||
1192
+ equivalentPath.startsWith(equivalentSchemaPath)
1193
+ )
1194
+ ) {
1195
+ continue;
1196
+ }
1197
+
1198
+ const equivalentSchemaPathParts = this.splitPath(equivalentSchemaPath);
1199
+
1200
+ if (
1201
+ !equivalentPathParts.every(
1202
+ (p, i) => equivalentSchemaPathParts[i] === p,
1203
+ )
1204
+ ) {
1205
+ continue;
1206
+ }
1207
+
1208
+ const remainingEquivalentSchemaPathParts =
1209
+ equivalentSchemaPathParts.slice(equivalentPathParts.length);
1210
+ // Use | as separator since it won't appear in path parts
1211
+ const key = remainingEquivalentSchemaPathParts.join('|');
1212
+ equivalentSchemaPathMap.set(key, equivalentSchemaPath);
1213
+ }
1214
+
969
1215
  for (const schemaPath of relevantSchemaPaths) {
970
1216
  const schemaPathParts = this.splitPath(schemaPath);
971
1217
 
@@ -977,106 +1223,29 @@ export class ScopeDataStructure {
977
1223
  pathParts.length,
978
1224
  );
979
1225
 
980
- let missingEquivalentPath = false;
981
-
982
- // Performance optimization: pre-filter equivalent schema paths
983
- const relevantEquivalentSchemaPaths = Object.keys(
984
- equivalentScopeNode.schema,
985
- ).filter((equivalentSchemaPath) => {
986
- // Quick string check before expensive path splitting
987
- return (
988
- equivalentSchemaPath.startsWith(equivalentPath) ||
989
- equivalentSchemaPath === equivalentPath ||
990
- equivalentPath.startsWith(equivalentSchemaPath)
991
- );
992
- });
993
-
994
- for (const equivalentSchemaPath of relevantEquivalentSchemaPaths) {
995
- const equivalentSchemaPathParts =
996
- this.splitPath(equivalentSchemaPath);
997
-
998
- if (
999
- !equivalentPathParts.every(
1000
- (p, i) => equivalentSchemaPathParts[i] === p,
1001
- )
1002
- ) {
1003
- continue;
1004
- }
1005
-
1006
- const remainingEquivalentSchemaPathParts =
1007
- equivalentSchemaPathParts.slice(equivalentPathParts.length);
1008
-
1009
- if (
1010
- remainingSchemaPathParts.length !==
1011
- remainingEquivalentSchemaPathParts.length ||
1012
- !remainingEquivalentSchemaPathParts.every(
1013
- (p, i) => p === remainingSchemaPathParts[i],
1014
- )
1015
- ) {
1016
- missingEquivalentPath = true;
1017
- continue;
1018
- }
1019
- missingEquivalentPath = false;
1226
+ // PERF: O(1) Map lookup instead of O(n) inner loop
1227
+ const remainingKey = remainingSchemaPathParts.join('|');
1228
+ const equivalentSchemaPath = equivalentSchemaPathMap.get(remainingKey);
1020
1229
 
1230
+ if (equivalentSchemaPath) {
1021
1231
  const value1 = scopeNode.schema[schemaPath];
1022
1232
  const value2 = equivalentScopeNode.schema[equivalentSchemaPath];
1023
1233
 
1024
- let bestValue = value1 ?? value2 ?? 'unknown';
1025
- if (
1026
- value2 &&
1027
- ((bestValue === 'unknown' && value2 !== 'unknown') ||
1028
- (bestValue.includes('unknown') && !value2.includes('unknown')))
1029
- ) {
1030
- bestValue = value2;
1031
- }
1234
+ const bestValue = selectBestValue(value1, value2);
1032
1235
 
1033
1236
  scopeNode.schema[schemaPath] = bestValue;
1034
1237
  equivalentScopeNode.schema[equivalentSchemaPath] = bestValue;
1035
-
1036
- // if (traceId) {
1037
- // console.info('Debug Propagate: Assign Best Value', {
1038
- // traceId,
1039
- // sourceScopeNodeName: scopeNode.name,
1040
- // path,
1041
- // schemaPath,
1042
- // usageScopeNodeName: equivalentScopeNode.name,
1043
- // equivalentPath,
1044
- // equivalentSchemaPath,
1045
- // remainingSchemaPathParts,
1046
- // remainingEquivalentSchemaPathParts,
1047
- // value1,
1048
- // value2,
1049
- // bestValue,
1050
- // });
1051
- // }
1052
- break;
1053
- }
1054
-
1055
- if (
1056
- missingEquivalentPath &&
1057
- (scopeNode.name.includes('____cyScope') ||
1058
- !('instantiatedVariables' in equivalentScopeNode))
1238
+ } else if (
1239
+ scopeNode.name.includes('____cyScope') ||
1240
+ !('instantiatedVariables' in equivalentScopeNode)
1059
1241
  ) {
1242
+ // No matching equivalent path found - create one if needed
1060
1243
  // Only necessary for internal function scopes or external function scopes
1061
1244
  const newEquivalentPath = this.joinPathParts([
1062
1245
  equivalentPath,
1063
1246
  ...remainingSchemaPathParts,
1064
1247
  ]);
1065
1248
 
1066
- // if (traceId) {
1067
- // console.info('Debug Propagate: missing equivalent path', {
1068
- // traceId,
1069
- // scopeNodeName: scopeNode.name,
1070
- // path,
1071
- // equivalentPath,
1072
- // schemaPath,
1073
- // remainingSchemaPathParts,
1074
- // equivalentScopeNodeName: equivalentScopeNode.name,
1075
- // // equivalentScopeNodeSchema: equivalentScopeNode.schema,
1076
- // newEquivalentPath,
1077
- // });
1078
- // }
1079
-
1080
1249
  equivalentScopeNode.schema[newEquivalentPath] =
1081
1250
  scopeNode.schema[schemaPath];
1082
1251
  }
@@ -1118,111 +1287,40 @@ export class ScopeDataStructure {
1118
1287
  }
1119
1288
  }
1120
1289
 
1121
- splitPath(path: string): string[] {
1122
- if (this.pathPartsCache.has(path)) {
1123
- return structuredClone(this.pathPartsCache.get(path)!);
1124
- }
1125
- const pathParts = splitOutsideParenthesesAndArrays(path);
1126
- this.pathPartsCache.set(path, structuredClone(pathParts));
1127
- return pathParts;
1290
+ splitPath(path: string): readonly string[] {
1291
+ return this.pathManager.splitPath(path);
1128
1292
  }
1129
1293
 
1130
- joinPathParts(pathParts: string[]): string {
1131
- const cacheKey = pathParts.join('|');
1132
- if (this.pathJoinCache.has(cacheKey)) {
1133
- return this.pathJoinCache.get(cacheKey)!;
1134
- }
1135
- const result = joinParenthesesAndArrays(pathParts);
1136
- this.pathJoinCache.set(cacheKey, result);
1137
- return result;
1294
+ joinPathParts(pathParts: readonly string[]): string {
1295
+ return this.pathManager.joinPathParts(pathParts);
1138
1296
  }
1139
1297
 
1140
1298
  // PRIVATE METHODS //
1141
1299
 
1142
- private uniqueId({
1143
- scopeNodeName,
1144
- schemaPath,
1145
- value,
1146
- }: {
1300
+ private uniqueId(params: {
1147
1301
  scopeNodeName: string;
1148
1302
  schemaPath: string;
1149
1303
  value?: string;
1150
1304
  }): string {
1151
- const parts = [scopeNodeName, schemaPath, value].filter(Boolean);
1152
- return parts.join('::');
1305
+ return uniqueId(params);
1153
1306
  }
1154
1307
 
1155
1308
  private uniqueScopeVariables(scopeVariables: ScopeVariable[]) {
1156
- return this.uniqueScopeAndPaths(scopeVariables) as ScopeVariable[];
1309
+ return uniqueScopeVariables(scopeVariables);
1157
1310
  }
1158
1311
 
1159
1312
  private uniqueScopeAndPaths(
1160
1313
  scopeVariables: Pick<ScopeVariable, 'scopeNodeName' | 'schemaPath'>[],
1161
1314
  ) {
1162
- if (!scopeVariables || scopeVariables.length === 0) return [];
1163
-
1164
- // Optimize from O(n²) to O(n) using Set for deduplication
1165
- const seen = new Set<string>();
1166
- return scopeVariables.filter((varItem) => {
1167
- const key = `${varItem.scopeNodeName}::${varItem.schemaPath}`;
1168
- if (seen.has(key)) {
1169
- return false;
1170
- }
1171
- seen.add(key);
1172
- return true;
1173
- });
1315
+ return uniqueScopeAndPaths(scopeVariables);
1174
1316
  }
1175
1317
 
1176
1318
  private isValidPath(path: string): boolean {
1177
- if (typeof path !== 'string') {
1178
- return false;
1179
- }
1180
-
1181
- if (!path) {
1182
- return false;
1183
- }
1184
-
1185
- if (path === 'true' || path === 'false') {
1186
- return false;
1187
- }
1188
-
1189
- if (!isNaN(Number(path))) {
1190
- return false;
1191
- }
1192
-
1193
- if (path.match(/^['"]/)) {
1194
- return false;
1195
- }
1196
-
1197
- return this.splitPath(path).every((part) => {
1198
- if (
1199
- cleanOutBoundary(
1200
- cleanOutBoundary(cleanOutBoundary(part, '<', '>'), '[', ']'),
1201
- ).match(/\s/)
1202
- ) {
1203
- return false;
1204
- }
1205
- return true;
1206
- });
1319
+ return this.pathManager.isValidPath(path);
1207
1320
  }
1208
1321
 
1209
1322
  private addToTree(pathParts: string[]) {
1210
- let scopeTreeNode = { children: [this.scopeTree] };
1211
- for (const pathPart of pathParts) {
1212
- const existingChild = scopeTreeNode.children.find(
1213
- (child) => child.name === pathPart,
1214
- );
1215
- if (existingChild) {
1216
- scopeTreeNode = existingChild;
1217
- } else {
1218
- const childScopeTreeNode = {
1219
- name: pathPart,
1220
- children: [] as any[],
1221
- };
1222
- scopeTreeNode.children.push(childScopeTreeNode);
1223
- scopeTreeNode = childScopeTreeNode;
1224
- }
1225
- }
1323
+ this.scopeTreeManager.addPath(pathParts);
1226
1324
  }
1227
1325
 
1228
1326
  private setInstantiatedVariables(scopeNode: ScopeNode) {
@@ -1317,9 +1415,92 @@ export class ScopeDataStructure {
1317
1415
  scopeNode,
1318
1416
  'original equivalency',
1319
1417
  );
1418
+
1419
+ // Propagate sub-property equivalencies when the equivalentValue is a simple variable
1420
+ // that has sub-properties defined in the isolatedEquivalentVariables.
1421
+ // This handles cases like: dataItem={{ structure: completeDataStructure }}
1422
+ // where completeDataStructure has sub-properties like completeDataStructure['Function Arguments']
1423
+ // We need to propagate these to create: dataItem.structure['Function Arguments'] equivalencies
1424
+ const isSimpleVariable =
1425
+ !equivalentValue.startsWith('signature[') &&
1426
+ !equivalentValue.includes('functionCallReturnValue') &&
1427
+ !equivalentValue.includes('.') &&
1428
+ !equivalentValue.includes('[');
1429
+
1430
+ if (isSimpleVariable) {
1431
+ // Look in current scope and all parent scopes for sub-properties
1432
+ const scopesToCheck = [scopeNode.name, ...scopeNode.tree];
1433
+ for (const scopeName of scopesToCheck) {
1434
+ const checkScope = this.scopeNodes[scopeName];
1435
+ if (!checkScope?.analysis?.isolatedEquivalentVariables) continue;
1436
+
1437
+ for (const [subPath, subValue] of Object.entries(
1438
+ checkScope.analysis.isolatedEquivalentVariables,
1439
+ )) {
1440
+ // Check if this is a sub-property of the equivalentValue variable
1441
+ // e.g., completeDataStructure['Function Arguments'] or completeDataStructure.foo
1442
+ const matchesDot = subPath.startsWith(equivalentValue + '.');
1443
+ const matchesBracket = subPath.startsWith(equivalentValue + '[');
1444
+ if (matchesDot || matchesBracket) {
1445
+ const subPropertyPath = subPath.substring(
1446
+ equivalentValue.length,
1447
+ );
1448
+ const newPath = cleanPath(path + subPropertyPath, allPaths);
1449
+ const newEquivalentValue = cleanPath(
1450
+ (subValue as string).replace(/::cyDuplicateKey\d+::/g, ''),
1451
+ allPaths,
1452
+ );
1453
+
1454
+ if (
1455
+ newEquivalentValue &&
1456
+ this.isValidPath(newEquivalentValue)
1457
+ ) {
1458
+ this.addEquivalency(
1459
+ newPath,
1460
+ newEquivalentValue,
1461
+ checkScope.name, // Use the scope where the sub-property was found
1462
+ scopeNode,
1463
+ 'propagated sub-property equivalency',
1464
+ );
1465
+ }
1466
+ }
1467
+
1468
+ // Also check if equivalentValue itself maps to a functionCallReturnValue
1469
+ // e.g., result = useMemo(...).functionCallReturnValue
1470
+ if (
1471
+ subPath === equivalentValue &&
1472
+ typeof subValue === 'string' &&
1473
+ subValue.endsWith('.functionCallReturnValue')
1474
+ ) {
1475
+ this.propagateFunctionCallReturnSubProperties(
1476
+ path,
1477
+ subValue,
1478
+ scopeNode,
1479
+ allPaths,
1480
+ );
1481
+ }
1482
+ }
1483
+ }
1484
+ }
1485
+
1486
+ // Handle function call return values by propagating returnValue.* sub-properties
1487
+ // from the callback scope to the usage path
1488
+ if (equivalentValue.endsWith('.functionCallReturnValue')) {
1489
+ this.propagateFunctionCallReturnSubProperties(
1490
+ path,
1491
+ equivalentValue,
1492
+ scopeNode,
1493
+ allPaths,
1494
+ );
1495
+ }
1320
1496
  }
1321
1497
  }
1322
1498
 
1499
+ // PERF: Enable batch processing to convert recursive addToSchema calls to iterative
1500
+ // This eliminates deep call stacks and improves deduplication
1501
+ this.batchProcessor = new BatchSchemaProcessor();
1502
+ this.batchQueuedSet = new Set();
1503
+
1323
1504
  for (const key of Array.from(allPaths)) {
1324
1505
  let value = isolatedStructure[key] ?? 'unknown';
1325
1506
 
@@ -1327,7 +1508,6 @@ export class ScopeDataStructure {
1327
1508
  value = 'unknown';
1328
1509
  }
1329
1510
 
1330
- const startTime = new Date().getTime();
1331
1511
  this.addToSchema({
1332
1512
  path: cleanPath(key, allPaths),
1333
1513
  value,
@@ -1341,31 +1521,308 @@ export class ScopeDataStructure {
1341
1521
  ],
1342
1522
  });
1343
1523
 
1344
- // const timeDiff = new Date().getTime() - startTime;
1345
- // if (timeDiff / 1000 > 1) {
1346
- // console.info('SLOW', {
1347
- // timeDiff,
1348
- // root: this.scopeTree.name,
1349
- // scopeNodeName: scopeNode.name,
1350
- // schemaPath: key,
1351
- // onlyEquivalencies: this.onlyEquivalencies,
1352
- // });
1353
- // }
1524
+ // Process any work queued by followEquivalencies
1525
+ this.processBatchQueue();
1354
1526
  }
1355
1527
 
1528
+ // Final pass to ensure all queued work is processed
1529
+ this.processBatchQueue();
1530
+
1531
+ // Clean up batch processor
1532
+ this.batchProcessor = null;
1533
+ this.batchQueuedSet = null;
1534
+
1356
1535
  this.validateSchema(scopeNode, false, false);
1357
1536
  }
1358
1537
 
1538
+ /**
1539
+ * Process all items in the batch queue.
1540
+ * Each item may queue more work via followEquivalencies.
1541
+ */
1542
+ private processBatchQueue(): void {
1543
+ if (!this.batchProcessor) return;
1544
+
1545
+ while (this.batchProcessor.hasWork()) {
1546
+ const item = this.batchProcessor.getNextWork();
1547
+ if (!item) break;
1548
+
1549
+ const scopeNode = this.getScopeOrFunctionCallInfo(
1550
+ item.scopeNodeName,
1551
+ ) as ScopeNode;
1552
+ if (!scopeNode) continue;
1553
+
1554
+ this.addToSchema({
1555
+ path: item.path,
1556
+ value: item.value,
1557
+ scopeNode,
1558
+ equivalencyValueChain:
1559
+ item.equivalencyValueChain as EquivalencyValueChainItem[],
1560
+ traceId: item.traceId,
1561
+ });
1562
+ }
1563
+ }
1564
+
1565
+ /**
1566
+ * Propagates returnValue.* sub-properties from a callback scope to the usage path.
1567
+ *
1568
+ * When we have an equivalency like:
1569
+ * DataItemEditor().signature[0].structure -> getData().functionCallReturnValue
1570
+ *
1571
+ * And the callback scope for getData has:
1572
+ * returnValue.args -> dataStructure.arguments
1573
+ *
1574
+ * This method creates the transitive equivalency:
1575
+ * DataItemEditor().signature[0].structure.args -> signature[0].dataStructure.arguments
1576
+ *
1577
+ * It also resolves variable references through parent scopes (e.g., dataStructure -> signature[0].dataStructure)
1578
+ * and ensures the database entry is updated with the usage path.
1579
+ */
1580
+ private propagateFunctionCallReturnSubProperties(
1581
+ path: string,
1582
+ equivalentValue: string,
1583
+ scopeNode: ScopeNode,
1584
+ allPaths: string[],
1585
+ ) {
1586
+ // Try to find the callback scope name from different patterns:
1587
+ // 1. "getData().functionCallReturnValue" → look up getData variable to find scope
1588
+ // 2. "useMemo(cyScope1(), [...]).functionCallReturnValue" → extract cyScope1 directly
1589
+ let callbackScopeName: string | undefined;
1590
+
1591
+ // Pattern 1: Simple function call like getData().functionCallReturnValue
1592
+ const simpleFunctionMatch = equivalentValue.match(
1593
+ /^(\w+)\(\)\.functionCallReturnValue$/,
1594
+ );
1595
+ if (simpleFunctionMatch) {
1596
+ const functionName = simpleFunctionMatch[1];
1597
+ // Find the function reference in current or parent scopes
1598
+ const scopesToCheck = [scopeNode.name, ...scopeNode.tree];
1599
+ for (const scopeName of scopesToCheck) {
1600
+ const checkScope = this.scopeNodes[scopeName];
1601
+ if (!checkScope?.analysis?.isolatedEquivalentVariables) continue;
1602
+
1603
+ const functionRef =
1604
+ checkScope.analysis.isolatedEquivalentVariables[functionName];
1605
+ if (typeof functionRef === 'string' && functionRef.endsWith('F')) {
1606
+ callbackScopeName = functionRef.slice(0, -1);
1607
+ break;
1608
+ }
1609
+ }
1610
+ }
1611
+
1612
+ // Pattern 2: useMemo/useCallback with callback scope
1613
+ // e.g., "useMemo(cyScope1(), [deps]).functionCallReturnValue"
1614
+ if (!callbackScopeName) {
1615
+ const useMemoMatch = equivalentValue.match(
1616
+ /^useMemo\((\w+)\(\),\s*\[.*\]\)\.functionCallReturnValue$/,
1617
+ );
1618
+ if (useMemoMatch) {
1619
+ callbackScopeName = useMemoMatch[1];
1620
+ }
1621
+ }
1622
+
1623
+ if (!callbackScopeName) return;
1624
+
1625
+ const callbackScope = this.scopeNodes[callbackScopeName];
1626
+ if (!callbackScope) return;
1627
+
1628
+ // Look for returnValue.* sub-properties in the callback scope
1629
+ if (!callbackScope.analysis?.isolatedEquivalentVariables) return;
1630
+
1631
+ const isolatedVars = callbackScope.analysis.isolatedEquivalentVariables;
1632
+
1633
+ // First, check if returnValue is an alias to another variable (e.g., returnValue = intermediate)
1634
+ // If so, we need to look for that variable's sub-properties too
1635
+ const returnValueAlias =
1636
+ typeof isolatedVars.returnValue === 'string' &&
1637
+ !isolatedVars.returnValue.includes('.')
1638
+ ? isolatedVars.returnValue
1639
+ : undefined;
1640
+
1641
+ // Pattern 3: Object.keys(X).reduce() - the reduce result has the same sub-properties as X
1642
+ // When returnValue = "Object.keys(source).reduce(...).functionCallReturnValue", look for source.* sub-properties
1643
+ let reduceSourceVar: string | undefined;
1644
+ if (typeof isolatedVars.returnValue === 'string') {
1645
+ const reduceMatch = isolatedVars.returnValue.match(
1646
+ /^Object\.keys\((\w+)\)\.reduce\(.*\)\.functionCallReturnValue$/,
1647
+ );
1648
+ if (reduceMatch) {
1649
+ reduceSourceVar = reduceMatch[1];
1650
+ }
1651
+ }
1652
+
1653
+ for (const [subPath, subValue] of Object.entries(isolatedVars)) {
1654
+ // Check for direct returnValue.* sub-properties
1655
+ const isReturnValueSub =
1656
+ subPath.startsWith('returnValue.') ||
1657
+ subPath.startsWith('returnValue[');
1658
+
1659
+ // Also check for alias.* sub-properties (e.g., intermediate.args when returnValue = intermediate)
1660
+ const isAliasSub =
1661
+ returnValueAlias &&
1662
+ (subPath.startsWith(returnValueAlias + '.') ||
1663
+ subPath.startsWith(returnValueAlias + '['));
1664
+
1665
+ // Also check for reduce source.* sub-properties (e.g., source['Function Arguments'] when returnValue = Object.keys(source).reduce())
1666
+ const isReduceSourceSub =
1667
+ reduceSourceVar &&
1668
+ (subPath.startsWith(reduceSourceVar + '.') ||
1669
+ subPath.startsWith(reduceSourceVar + '['));
1670
+
1671
+ if (
1672
+ typeof subValue !== 'string' ||
1673
+ (!isReturnValueSub && !isAliasSub && !isReduceSourceSub)
1674
+ )
1675
+ continue;
1676
+
1677
+ // Convert alias/reduceSource paths to returnValue paths
1678
+ let effectiveSubPath = subPath;
1679
+ if (isAliasSub && !isReturnValueSub) {
1680
+ // Replace the alias prefix with returnValue
1681
+ effectiveSubPath =
1682
+ 'returnValue' + subPath.substring(returnValueAlias!.length);
1683
+ } else if (isReduceSourceSub && !isReturnValueSub && !isAliasSub) {
1684
+ // Replace the reduce source prefix with returnValue
1685
+ effectiveSubPath =
1686
+ 'returnValue' + subPath.substring(reduceSourceVar!.length);
1687
+ }
1688
+ const subPropertyPath = effectiveSubPath.substring('returnValue'.length);
1689
+ const newPath = cleanPath(path + subPropertyPath, allPaths);
1690
+ let newEquivalentValue = cleanPath(
1691
+ subValue.replace(/::cyDuplicateKey\d+::/g, ''),
1692
+ allPaths,
1693
+ );
1694
+
1695
+ // Resolve variable references through parent scope equivalencies
1696
+ const resolved = this.resolveVariableThroughParentScopes(
1697
+ newEquivalentValue,
1698
+ callbackScope,
1699
+ allPaths,
1700
+ );
1701
+ newEquivalentValue = resolved.resolvedPath;
1702
+ const equivalentScopeName = resolved.scopeName;
1703
+
1704
+ if (!newEquivalentValue || !this.isValidPath(newEquivalentValue))
1705
+ continue;
1706
+
1707
+ this.addEquivalency(
1708
+ newPath,
1709
+ newEquivalentValue,
1710
+ equivalentScopeName,
1711
+ scopeNode,
1712
+ 'propagated function call return sub-property equivalency',
1713
+ );
1714
+
1715
+ // Ensure the database entry has the usage path
1716
+ this.addUsageToEquivalencyDatabaseEntry(
1717
+ newPath,
1718
+ newEquivalentValue,
1719
+ equivalentScopeName,
1720
+ scopeNode.name,
1721
+ );
1722
+ }
1723
+ }
1724
+
1725
+ /**
1726
+ * Resolves a variable path through parent scope equivalencies.
1727
+ *
1728
+ * For example, if the path is "dataStructure.arguments" and the parent scope has
1729
+ * dataStructure -> signature[0].dataStructure, this returns:
1730
+ * { resolvedPath: "signature[0].dataStructure.arguments", scopeName: "ParentScope" }
1731
+ */
1732
+ private resolveVariableThroughParentScopes(
1733
+ path: string | undefined,
1734
+ callbackScope: ScopeNode,
1735
+ allPaths: string[],
1736
+ ): { resolvedPath: string | undefined; scopeName: string } {
1737
+ if (!path)
1738
+ return { resolvedPath: undefined, scopeName: callbackScope.name };
1739
+
1740
+ // Extract the root variable name
1741
+ const dotIndex = path.indexOf('.');
1742
+ const bracketIndex = path.indexOf('[');
1743
+ let firstDelimiter = -1;
1744
+ if (dotIndex > -1 && bracketIndex > -1) {
1745
+ firstDelimiter = Math.min(dotIndex, bracketIndex);
1746
+ } else {
1747
+ firstDelimiter = dotIndex > -1 ? dotIndex : bracketIndex;
1748
+ }
1749
+
1750
+ if (firstDelimiter === -1)
1751
+ return { resolvedPath: path, scopeName: callbackScope.name };
1752
+
1753
+ const rootVar = path.substring(0, firstDelimiter);
1754
+ const restOfPath = path.substring(firstDelimiter);
1755
+
1756
+ // Look in parent scopes for the root variable's equivalency
1757
+ for (const parentScopeName of callbackScope.tree || []) {
1758
+ const parentScope = this.scopeNodes[parentScopeName];
1759
+ if (!parentScope?.analysis?.isolatedEquivalentVariables) continue;
1760
+
1761
+ const rootEquiv =
1762
+ parentScope.analysis.isolatedEquivalentVariables[rootVar];
1763
+ if (typeof rootEquiv === 'string') {
1764
+ return {
1765
+ resolvedPath: cleanPath(rootEquiv + restOfPath, allPaths),
1766
+ scopeName: parentScopeName,
1767
+ };
1768
+ }
1769
+ }
1770
+
1771
+ return { resolvedPath: path, scopeName: callbackScope.name };
1772
+ }
1773
+
1774
+ /**
1775
+ * Adds a usage path to the equivalency database entry that has the given source.
1776
+ *
1777
+ * The addEquivalency call creates the equivalency but doesn't always properly
1778
+ * link the usage in the database, so we need to do it explicitly.
1779
+ */
1780
+ private addUsageToEquivalencyDatabaseEntry(
1781
+ usagePath: string,
1782
+ sourcePath: string,
1783
+ sourceScopeName: string,
1784
+ fallbackScopeName: string,
1785
+ ) {
1786
+ const usageEntry = {
1787
+ scopeNodeName: usagePath.match(/^([^().]+)\(\)/)
1788
+ ? (usagePath.match(/^([^().]+)\(\)/)?.[1] ?? fallbackScopeName)
1789
+ : fallbackScopeName,
1790
+ schemaPath: usagePath,
1791
+ };
1792
+
1793
+ const sourceKey = `${sourceScopeName}::${sourcePath}`;
1794
+ for (const entry of this.equivalencyDatabase) {
1795
+ if (
1796
+ entry.intermediatesOrder[sourceKey] === 0 ||
1797
+ entry.sourceCandidates.some(
1798
+ (sc) =>
1799
+ sc.scopeNodeName === sourceScopeName &&
1800
+ sc.schemaPath === sourcePath,
1801
+ )
1802
+ ) {
1803
+ const usageExists = entry.usages.some(
1804
+ (u) =>
1805
+ u.scopeNodeName === usageEntry.scopeNodeName &&
1806
+ u.schemaPath === usageEntry.schemaPath,
1807
+ );
1808
+ if (!usageExists) {
1809
+ entry.usages.push(usageEntry);
1810
+ }
1811
+ break;
1812
+ }
1813
+ }
1814
+ }
1815
+
1359
1816
  private checkForArrayItemPath(
1360
- pathParts: string[],
1817
+ pathParts: readonly string[],
1361
1818
  scopeNode: ScopeNode,
1362
1819
  equivalencyValueChain: EquivalencyValueChainItem[],
1363
1820
  ) {
1364
- const arrayItemPath = joinParenthesesAndArrays([...pathParts, '[]']);
1821
+ const arrayItemPath = this.joinPathParts([...pathParts, '[]']);
1365
1822
  if (scopeNode.equivalencies[arrayItemPath]) {
1366
1823
  const firstEquivalency = equivalencyValueChain[0].currentPath;
1367
- const newFirstEquivalencyPath = joinParenthesesAndArrays([
1368
- ...splitOutsideParenthesesAndArrays(firstEquivalency.schemaPath),
1824
+ const newFirstEquivalencyPath = this.joinPathParts([
1825
+ ...this.splitPath(firstEquivalency.schemaPath),
1369
1826
  '[]',
1370
1827
  ]);
1371
1828
  this.addToSchema({
@@ -1383,41 +1840,95 @@ export class ScopeDataStructure {
1383
1840
  }
1384
1841
  }
1385
1842
 
1843
+ /**
1844
+ * Follows equivalencies for a subpath and recursively calls addToSchema.
1845
+ *
1846
+ * This is the recursive heart of schema propagation. When we have:
1847
+ * `user` equivalent to `props.user`
1848
+ * And we're processing `user.id`, this method will:
1849
+ * 1. Find the equivalency `user` → `props.user`
1850
+ * 2. Reconstruct the full path: `props.user.id`
1851
+ * 3. Recursively call addToSchema for the equivalent scope
1852
+ *
1853
+ * ## Function-Ambivalent Paths
1854
+ *
1855
+ * Handles paths like `data.map(fn)` by also checking equivalencies
1856
+ * for `data.map` (without the function argument). This allows
1857
+ * array method chains to propagate correctly.
1858
+ *
1859
+ * @param scopeNode - Current scope being processed
1860
+ * @param path - Full original path (e.g., 'user.profile.name')
1861
+ * @param pathParts - Split version of path
1862
+ * @param subPath - Current subpath being checked (e.g., 'user.profile')
1863
+ * @param subPathParts - Split version of subPath
1864
+ * @param value - Type value to propagate
1865
+ * @param equivalencyValueChain - Chain tracking for database
1866
+ * @param traceId - Debug ID
1867
+ * @param debugLevel - 0=minimal, 1=executed, 2=verbose
1868
+ */
1386
1869
  private followEquivalencies(
1387
1870
  scopeNode: ScopeNode,
1388
1871
  path: string,
1389
- pathParts: string[],
1872
+ pathParts: readonly string[],
1390
1873
  subPath: string,
1391
- subPathParts: string[],
1874
+ subPathParts: readonly string[],
1392
1875
  value: string,
1393
1876
  equivalencyValueChain: EquivalencyValueChainItem[],
1394
1877
  traceId?: number,
1395
1878
  debugLevel = 0,
1396
1879
  ) {
1397
1880
  followEquivalenciesCallCount++;
1881
+
1882
+ // Trace for debugging
1883
+ this.tracer.trace('followEquivalencies', {
1884
+ path,
1885
+ scope: scopeNode.name,
1886
+ subPath,
1887
+ hasEquivalencies: (scopeNode.equivalencies[subPath]?.length ?? 0) > 0,
1888
+ });
1889
+
1398
1890
  let propagated = false;
1891
+
1892
+ // ─────────────────────────────────────────────────────────────────────────
1893
+ // Step 1: Collect equivalencies (including function-ambivalent paths)
1894
+ // ─────────────────────────────────────────────────────────────────────────
1399
1895
  let equivalentValues = scopeNode.equivalencies[subPath] ?? [];
1400
1896
  let functionAmbivalentSubPath = subPath;
1401
1897
  let functionAmbivalentSubPathParts = subPathParts;
1402
1898
 
1403
- if (subPathParts[subPathParts.length - 1].indexOf('(') > -1) {
1899
+ // For paths like `data.map(fn)`, also check `data.map` equivalencies
1900
+ // PERF: Only do the expensive function-ambivalent processing if:
1901
+ // 1. No equivalencies found yet, AND
1902
+ // 2. The subpath ends with a function call (contains '(')
1903
+ const lastPart = subPathParts[subPathParts.length - 1];
1904
+ if (lastPart.indexOf('(') > -1) {
1905
+ // Check function-ambivalent path for equivalencies
1404
1906
  functionAmbivalentSubPathParts = [
1405
1907
  ...subPathParts.slice(0, -1),
1406
- subPathParts[subPathParts.length - 1].split('(')[0],
1908
+ lastPart.split('(')[0],
1407
1909
  ];
1408
1910
  functionAmbivalentSubPath = this.joinPathParts(
1409
1911
  functionAmbivalentSubPathParts,
1410
1912
  );
1411
- equivalentValues = [
1412
- ...(equivalentValues ?? []),
1413
- ...(scopeNode.equivalencies[functionAmbivalentSubPath] ?? []),
1414
- ];
1913
+ const functionAmbivalentEquivalencies =
1914
+ scopeNode.equivalencies[functionAmbivalentSubPath];
1915
+ if (functionAmbivalentEquivalencies?.length > 0) {
1916
+ equivalentValues = [
1917
+ ...equivalentValues,
1918
+ ...functionAmbivalentEquivalencies,
1919
+ ];
1920
+ }
1415
1921
  }
1416
1922
 
1417
1923
  // Early exit optimization: if no equivalencies to follow, skip expensive processing
1418
1924
  if (equivalentValues.length === 0) {
1925
+ followEquivalenciesEarlyExitCount++;
1926
+ if (this.onlyEquivalencies) {
1927
+ followEquivalenciesEarlyExitPhase1Count++;
1928
+ }
1419
1929
  return false;
1420
1930
  }
1931
+ followEquivalenciesWithWorkCount++;
1421
1932
 
1422
1933
  if (traceId && debugLevel > 1) {
1423
1934
  console.info(
@@ -1443,12 +1954,21 @@ export class ScopeDataStructure {
1443
1954
 
1444
1955
  const uniqueEquivalentValues = this.uniqueScopeVariables(equivalentValues);
1445
1956
 
1957
+ // PERF: Only create Set when chain is non-empty (common case avoids allocation)
1958
+ // Pre-compute Set of chain IDs for O(1) cycle detection instead of O(n) .some()
1959
+ const chainIdSet =
1960
+ equivalencyValueChain.length > 0
1961
+ ? new Set(equivalencyValueChain.map((ev) => ev.id))
1962
+ : null;
1963
+
1446
1964
  for (let index = 0; index < uniqueEquivalentValues.length; ++index) {
1447
1965
  const equivalentValue = uniqueEquivalentValues[index];
1448
1966
 
1967
+ // O(1) cycle check using Set instead of O(n) .some()
1449
1968
  if (
1969
+ chainIdSet &&
1450
1970
  equivalentValue.id !== -1 &&
1451
- equivalencyValueChain.some((ev) => ev.id === equivalentValue.id)
1971
+ chainIdSet.has(equivalentValue.id)
1452
1972
  ) {
1453
1973
  if (traceId) {
1454
1974
  console.info(
@@ -1499,8 +2019,12 @@ export class ScopeDataStructure {
1499
2019
  schemaPathParts[schemaPathParts.length - 1] ===
1500
2020
  lastRelevantSubPathPart.split('(')[0]
1501
2021
  ) {
1502
- schemaPathParts[schemaPathParts.length - 1] = lastRelevantSubPathPart;
1503
- schemaPath = this.joinPathParts(schemaPathParts);
2022
+ // PERF: Don't mutate the array - create a new one to avoid requiring structuredClone
2023
+ const updatedSchemaPathParts = [
2024
+ ...schemaPathParts.slice(0, -1),
2025
+ lastRelevantSubPathPart,
2026
+ ];
2027
+ schemaPath = this.joinPathParts(updatedSchemaPathParts);
1504
2028
  }
1505
2029
 
1506
2030
  const remainingPathParts = pathParts.slice(relevantSubPathParts.length);
@@ -1560,25 +2084,27 @@ export class ScopeDataStructure {
1560
2084
  );
1561
2085
  }
1562
2086
 
1563
- const localEquivalencyValueChain = structuredClone(
1564
- equivalencyValueChain,
1565
- );
1566
- localEquivalencyValueChain.push({
1567
- id: equivalentValue.id,
1568
- source: 'equivalence',
1569
- previousPath: {
1570
- scopeNodeName: scopeNode.name,
1571
- schemaPath: path,
1572
- value,
1573
- },
1574
- currentPath: {
1575
- scopeNodeName: equivalentScopeNode.name,
1576
- schemaPath: newEquivalentPath,
1577
- value,
2087
+ // PERF: Use spread instead of structuredClone - the chain items are immutable
2088
+ // so shallow copy is sufficient and much faster (structuredClone is expensive)
2089
+ const localEquivalencyValueChain = [
2090
+ ...equivalencyValueChain,
2091
+ {
2092
+ id: equivalentValue.id,
2093
+ source: 'equivalence',
2094
+ previousPath: {
2095
+ scopeNodeName: scopeNode.name,
2096
+ schemaPath: path,
2097
+ value,
2098
+ },
2099
+ currentPath: {
2100
+ scopeNodeName: equivalentScopeNode.name,
2101
+ schemaPath: newEquivalentPath,
2102
+ value,
2103
+ },
2104
+ reason: equivalentValue.equivalencyReason,
2105
+ traceId,
1578
2106
  },
1579
- reason: equivalentValue.equivalencyReason,
1580
- traceId,
1581
- });
2107
+ ];
1582
2108
 
1583
2109
  // writeEquivalencyToFile(
1584
2110
  // scopeNode.name,
@@ -1590,13 +2116,57 @@ export class ScopeDataStructure {
1590
2116
  // )
1591
2117
 
1592
2118
  propagated = true;
1593
- this.addToSchema({
1594
- path: newEquivalentPath,
1595
- value,
1596
- scopeNode: equivalentScopeNode,
1597
- equivalencyValueChain: localEquivalencyValueChain,
1598
- traceId,
1599
- });
2119
+
2120
+ // PERF: If batch processor is active, queue work instead of recursing
2121
+ // This converts deep recursion to iterative processing
2122
+ if (this.batchProcessor) {
2123
+ // PERF OPTIMIZATION: For external function calls, use path-only key (ignore value)
2124
+ // External FCs don't have internals to analyze, so processing the same path
2125
+ // with different values is redundant - we're just tracking data flow
2126
+ const isExternalFc =
2127
+ equivalentValue.equivalencyReason ===
2128
+ 'equivalency to external function call' ||
2129
+ this.getExternalFunctionCallInfo(equivalentScopeNode.name) !==
2130
+ undefined;
2131
+ const queueKey = isExternalFc
2132
+ ? `${equivalentScopeNode.name}::${newEquivalentPath}` // path-only for external FC
2133
+ : `${equivalentScopeNode.name}::${newEquivalentPath}::${value}`; // full key otherwise
2134
+
2135
+ // Always check visited - it catches already-processed items
2136
+ const alreadyVisited = this.visitedTracker.hasGlobalVisited(
2137
+ equivalentScopeNode.name,
2138
+ newEquivalentPath,
2139
+ value,
2140
+ );
2141
+ // For external FCs, queueKey is path-only so this catches any-value duplicates
2142
+ const alreadyQueued = this.batchQueuedSet?.has(queueKey);
2143
+
2144
+ if (alreadyVisited || alreadyQueued) {
2145
+ // Still record in database to capture the chain (as addToSchema does)
2146
+ this.addToEquivalencyDatabase(
2147
+ localEquivalencyValueChain as EquivalencyValueChainItem[],
2148
+ traceId,
2149
+ );
2150
+ } else {
2151
+ // Mark as queued to prevent duplicate queue entries
2152
+ this.batchQueuedSet?.add(queueKey);
2153
+ this.batchProcessor.addWork({
2154
+ scopeNodeName: equivalentScopeNode.name,
2155
+ path: newEquivalentPath,
2156
+ value,
2157
+ equivalencyValueChain: localEquivalencyValueChain,
2158
+ traceId,
2159
+ });
2160
+ }
2161
+ } else {
2162
+ this.addToSchema({
2163
+ path: newEquivalentPath,
2164
+ value,
2165
+ scopeNode: equivalentScopeNode,
2166
+ equivalencyValueChain: localEquivalencyValueChain,
2167
+ traceId,
2168
+ });
2169
+ }
1600
2170
  }
1601
2171
  }
1602
2172
 
@@ -1635,6 +2205,17 @@ export class ScopeDataStructure {
1635
2205
  usageEquivalency.scopeNodeName,
1636
2206
  ) as ScopeNode;
1637
2207
 
2208
+ // Guard against infinite recursion by tracking which paths we've already
2209
+ // added from addComplexSourcePathVariables
2210
+ if (
2211
+ this.visitedTracker.checkAndMarkComplexSourceVisited(
2212
+ usageScopeNode.name,
2213
+ newUsageEquivalentPath,
2214
+ )
2215
+ ) {
2216
+ continue;
2217
+ }
2218
+
1638
2219
  this.addToSchema({
1639
2220
  path: newUsageEquivalentPath,
1640
2221
  value: 'unknown',
@@ -1785,6 +2366,7 @@ export class ScopeDataStructure {
1785
2366
 
1786
2367
  for (let i = 0; i < cleanEquivalencyValueChain.length; ++i) {
1787
2368
  const pathInfo = cleanEquivalencyValueChain[i].currentPath;
2369
+ const previousPath = cleanEquivalencyValueChain[i].previousPath;
1788
2370
 
1789
2371
  const schemaPathParts = this.splitPath(pathInfo.schemaPath);
1790
2372
  const isSignaturePath =
@@ -1798,6 +2380,21 @@ export class ScopeDataStructure {
1798
2380
  databaseEntry.usages.push(pathInfo);
1799
2381
  }
1800
2382
 
2383
+ // Also add previousPath as a usage if it matches the criteria
2384
+ // This handles propagated sub-property equivalencies where the JSX prop path
2385
+ // is in previousPath and should be tracked as a usage
2386
+ if (previousPath) {
2387
+ const prevSchemaPathParts = this.splitPath(previousPath.schemaPath);
2388
+ const isPrevSignaturePath =
2389
+ prevSchemaPathParts.findIndex((p) => p.startsWith('signature[')) > 0;
2390
+ if (
2391
+ prevSchemaPathParts[0].startsWith('returnValue') ||
2392
+ isPrevSignaturePath
2393
+ ) {
2394
+ databaseEntry.usages.push(previousPath);
2395
+ }
2396
+ }
2397
+
1801
2398
  const pathId = this.uniqueId(pathInfo);
1802
2399
  let intermediateIndex = cleanEquivalencyValueChain.length - i - 1;
1803
2400
  const existingIntermediateIndex =
@@ -1905,6 +2502,7 @@ export class ScopeDataStructure {
1905
2502
  if (functionScope) {
1906
2503
  functionScope.functionCalls.push(externalFunctionCallInfo);
1907
2504
  this.externalFunctionCalls.splice(i, 1);
2505
+ this.invalidateExternalFunctionCallsIndex();
1908
2506
 
1909
2507
  for (const equivalentPath in externalFunctionCallInfo.equivalencies) {
1910
2508
  let localEquivalentPath = equivalentPath;
@@ -1972,18 +2570,21 @@ export class ScopeDataStructure {
1972
2570
  for (const key in scopeNode.equivalencies) {
1973
2571
  const keyParts = this.splitPath(key);
1974
2572
  if (keyParts.length > 1) {
1975
- const lastPart = keyParts.pop();
2573
+ // PERF: Don't mutate the array - use indexing instead of pop() to avoid requiring structuredClone
2574
+ const lastPart = keyParts[keyParts.length - 1];
1976
2575
  if (lastPart.startsWith('signature[')) {
1977
- const functionCall = keyParts[keyParts.length - 1];
2576
+ const keyPartsWithoutLast = keyParts.slice(0, -1);
2577
+ const functionCall =
2578
+ keyPartsWithoutLast[keyPartsWithoutLast.length - 1];
1978
2579
  const argumentsIndex = functionCall.indexOf('(');
1979
2580
  const functionName = this.joinPathParts([
1980
- ...keyParts.slice(0, -1),
2581
+ ...keyPartsWithoutLast.slice(0, -1),
1981
2582
  functionCall.slice(
1982
2583
  0,
1983
2584
  argumentsIndex === -1 ? undefined : argumentsIndex,
1984
2585
  ),
1985
2586
  ]);
1986
- const callSignature = this.joinPathParts(keyParts);
2587
+ const callSignature = this.joinPathParts(keyPartsWithoutLast);
1987
2588
 
1988
2589
  const functionCallInfo = {
1989
2590
  name: functionName,
@@ -2000,19 +2601,21 @@ export class ScopeDataStructure {
2000
2601
  equivalentValues.schemaPath,
2001
2602
  );
2002
2603
  if (equivalentValueParts.length > 1) {
2003
- const lastPart = equivalentValueParts.pop();
2604
+ // PERF: Don't mutate the array - use indexing instead of pop() to avoid requiring structuredClone
2605
+ const lastPart =
2606
+ equivalentValueParts[equivalentValueParts.length - 1];
2004
2607
  if (lastPart.startsWith('functionCallReturnValue')) {
2005
- const functionCall =
2006
- equivalentValueParts[equivalentValueParts.length - 1];
2608
+ const partsWithoutLast = equivalentValueParts.slice(0, -1);
2609
+ const functionCall = partsWithoutLast[partsWithoutLast.length - 1];
2007
2610
  const argumentsIndex = functionCall.indexOf('(');
2008
2611
  const functionName = this.joinPathParts([
2009
- ...equivalentValueParts.slice(0, -1),
2612
+ ...partsWithoutLast.slice(0, -1),
2010
2613
  functionCall.slice(
2011
2614
  0,
2012
2615
  argumentsIndex === -1 ? undefined : argumentsIndex,
2013
2616
  ),
2014
2617
  ]);
2015
- const callSignature = this.joinPathParts(equivalentValueParts);
2618
+ const callSignature = this.joinPathParts(partsWithoutLast);
2016
2619
 
2017
2620
  const functionCallInfo = {
2018
2621
  name: functionName,
@@ -2213,7 +2816,8 @@ export class ScopeDataStructure {
2213
2816
  }
2214
2817
 
2215
2818
  getScopeNode(scopeName?: string): ScopeNode | undefined {
2216
- const scopeNode = this.scopeNodes[scopeName ?? this.scopeTree.name];
2819
+ const scopeNode =
2820
+ this.scopeNodes[scopeName ?? this.scopeTreeManager.getRootName()];
2217
2821
  if (scopeNode) return scopeNode;
2218
2822
 
2219
2823
  for (const scopeNodeName in this.scopeNodes) {
@@ -2223,18 +2827,54 @@ export class ScopeDataStructure {
2223
2827
  }
2224
2828
  }
2225
2829
 
2830
+ /**
2831
+ * Gets or builds the external function calls index for O(1) lookup.
2832
+ * The index maps both the full name and the name without generics to the FunctionCallInfo.
2833
+ */
2834
+ private getExternalFunctionCallsIndex(): Map<string, FunctionCallInfo> {
2835
+ if (this.externalFunctionCallsIndex === null) {
2836
+ this.externalFunctionCallsIndex = new Map();
2837
+ for (const efc of this.externalFunctionCalls) {
2838
+ this.externalFunctionCallsIndex.set(efc.name, efc);
2839
+ // Also index by name without generics (e.g., 'MyFunction<T>' -> 'MyFunction')
2840
+ const nameWithoutGenerics = this.pathManager.stripGenerics(efc.name);
2841
+ if (nameWithoutGenerics !== efc.name) {
2842
+ this.externalFunctionCallsIndex.set(nameWithoutGenerics, efc);
2843
+ }
2844
+ }
2845
+ }
2846
+ return this.externalFunctionCallsIndex;
2847
+ }
2848
+
2849
+ /**
2850
+ * Invalidates the external function calls index.
2851
+ * Call this after any mutation to externalFunctionCalls array.
2852
+ */
2853
+ private invalidateExternalFunctionCallsIndex(): void {
2854
+ this.externalFunctionCallsIndex = null;
2855
+ }
2856
+
2226
2857
  getExternalFunctionCallInfo(
2227
2858
  functionName: string,
2228
2859
  ): FunctionCallInfo | undefined {
2860
+ const searchKey = getFunctionCallRoot(functionName);
2861
+ const index = this.getExternalFunctionCallsIndex();
2862
+
2863
+ // First try exact match
2864
+ const exact = index.get(searchKey);
2865
+ if (exact) return exact;
2866
+
2867
+ // Fallback to the original find for edge cases not covered by index
2229
2868
  return this.externalFunctionCalls.find(
2230
2869
  (efc) =>
2231
- efc.name === getFunctionCallRoot(functionName) ||
2232
- efc.name.split('<')[0] === getFunctionCallRoot(functionName),
2870
+ efc.name === searchKey ||
2871
+ this.pathManager.stripGenerics(efc.name) === searchKey,
2233
2872
  );
2234
2873
  }
2235
2874
 
2236
2875
  getScopeAnalysis(scopeName?: string): ScopeAnalysis | undefined {
2237
- return this.scopeNodes[scopeName ?? this.scopeTree.name].analysis;
2876
+ return this.scopeNodes[scopeName ?? this.scopeTreeManager.getRootName()]
2877
+ .analysis;
2238
2878
  }
2239
2879
 
2240
2880
  getAnalysisTree(): ScopeTreeNode {
@@ -2248,12 +2888,13 @@ export class ScopeDataStructure {
2248
2888
  scopeName?: string;
2249
2889
  fillInUnknowns?: boolean;
2250
2890
  }): any {
2251
- const scopeNode = this.scopeNodes[scopeName ?? this.scopeTree.name];
2891
+ const scopeNode =
2892
+ this.scopeNodes[scopeName ?? this.scopeTreeManager.getRootName()];
2252
2893
 
2253
2894
  if (!scopeNode) {
2254
- const externalFunctionCallSchema = this.externalFunctionCalls.find(
2255
- (call) => call.name === getFunctionCallRoot(scopeName),
2256
- )?.schema;
2895
+ const searchKey = getFunctionCallRoot(scopeName);
2896
+ const externalFunctionCallSchema =
2897
+ this.getExternalFunctionCallsIndex().get(searchKey)?.schema;
2257
2898
 
2258
2899
  if (!externalFunctionCallSchema) return;
2259
2900
 
@@ -2271,6 +2912,40 @@ export class ScopeDataStructure {
2271
2912
  this.validateSchema(scopeNode, true, fillInUnknowns);
2272
2913
 
2273
2914
  const { schema } = scopeNode;
2915
+
2916
+ // For root scope, merge in external function call schemas
2917
+ // This ensures that imported objects used as method call targets (like logger.error())
2918
+ // appear in the schema as objects with function properties
2919
+ if (
2920
+ scopeName === undefined ||
2921
+ scopeName === this.scopeTreeManager.getRootName()
2922
+ ) {
2923
+ const mergedSchema = { ...schema };
2924
+ for (const efc of this.externalFunctionCalls) {
2925
+ // Add the base object name (e.g., "logger") as an object
2926
+ const baseName = this.splitPath(efc.name)[0];
2927
+ if (!mergedSchema[baseName]) {
2928
+ mergedSchema[baseName] = 'object';
2929
+ }
2930
+
2931
+ // Get all call signatures (use allCallSignatures if available, otherwise just the single callSignature)
2932
+ const signatures = efc.allCallSignatures ?? [efc.callSignature];
2933
+ for (const signature of signatures) {
2934
+ // Add the method as a function (e.g., "logger.error")
2935
+ // Extract method name from callSignature like "logger.error(args...)"
2936
+ // Get everything before the first '(' to get "logger.error"
2937
+ const parenIndex = signature.indexOf('(');
2938
+ if (parenIndex > 0) {
2939
+ const methodPath = signature.substring(0, parenIndex);
2940
+ if (methodPath.includes('.') && !mergedSchema[methodPath]) {
2941
+ mergedSchema[methodPath] = 'function';
2942
+ }
2943
+ }
2944
+ }
2945
+ }
2946
+ return mergedSchema;
2947
+ }
2948
+
2274
2949
  return schema;
2275
2950
  }
2276
2951
 
@@ -2286,9 +2961,9 @@ export class ScopeDataStructure {
2286
2961
  return this.equivalencyDatabaseCache.get(cacheKey);
2287
2962
  }
2288
2963
 
2289
- const result = this.equivalencyDatabase.find(
2290
- (entry) => entry.intermediatesOrder[cacheKey] !== undefined,
2291
- );
2964
+ // PERF: Use inverted index for O(1) lookup instead of O(n) array scan
2965
+ // The intermediatesOrderIndex maps pathId (scopeNodeName::schemaPath) to entry
2966
+ const result = this.intermediatesOrderIndex.get(cacheKey);
2292
2967
 
2293
2968
  this.equivalencyDatabaseCache.set(cacheKey, result);
2294
2969
  return result;
@@ -2392,7 +3067,7 @@ export class ScopeDataStructure {
2392
3067
  }
2393
3068
 
2394
3069
  const tempScopeNode = this.createTempScopeNode(
2395
- functionName ?? this.scopeTree.name,
3070
+ functionName ?? this.scopeTreeManager.getRootName(),
2396
3071
  signatureInSchema,
2397
3072
  equivalencies,
2398
3073
  );
@@ -2409,7 +3084,7 @@ export class ScopeDataStructure {
2409
3084
  functionName?: string;
2410
3085
  fillInUnknowns?: boolean;
2411
3086
  }) {
2412
- const scopeName = functionName ?? this.scopeTree.name;
3087
+ const scopeName = functionName ?? this.scopeTreeManager.getRootName();
2413
3088
  const scopeNode = this.scopeNodes[scopeName];
2414
3089
 
2415
3090
  let schema: ScopeNode['schema'] = {};
@@ -2434,14 +3109,7 @@ export class ScopeDataStructure {
2434
3109
  for (const schemaPath in externalFunctionCall.schema) {
2435
3110
  const value1 = schema[schemaPath];
2436
3111
  const value2 = externalFunctionCall.schema[schemaPath];
2437
- let bestValue = value1 ?? value2;
2438
- if (
2439
- bestValue === 'unknown' ||
2440
- (bestValue.includes('unknown') && !value2.includes('unknown'))
2441
- ) {
2442
- bestValue = value2;
2443
- }
2444
- schema[schemaPath] = bestValue;
3112
+ schema[schemaPath] = selectBestValue(value1, value2, value2);
2445
3113
  }
2446
3114
  }
2447
3115
  }
@@ -2567,7 +3235,7 @@ export class ScopeDataStructure {
2567
3235
  }
2568
3236
 
2569
3237
  getEquivalentSignatureVariables() {
2570
- const scopeNode = this.scopeNodes[this.scopeTree.name];
3238
+ const scopeNode = this.scopeNodes[this.scopeTreeManager.getRootName()];
2571
3239
 
2572
3240
  const equivalentSignatureVariables: Record<string, string> = {};
2573
3241
  for (const [path, equivalentValues] of Object.entries(
@@ -2589,7 +3257,7 @@ export class ScopeDataStructure {
2589
3257
  final?: boolean,
2590
3258
  ): VariableInfo | undefined {
2591
3259
  const scopeNode = this.getScopeOrFunctionCallInfo(
2592
- scopeName ?? this.scopeTree.name,
3260
+ scopeName ?? this.scopeTreeManager.getRootName(),
2593
3261
  );
2594
3262
  if (!scopeNode) return;
2595
3263
 
@@ -2626,7 +3294,7 @@ export class ScopeDataStructure {
2626
3294
  );
2627
3295
 
2628
3296
  const tempScopeNode = this.createTempScopeNode(
2629
- scopeName ?? this.scopeTree.name,
3297
+ scopeName ?? this.scopeTreeManager.getRootName(),
2630
3298
  relevantSchema,
2631
3299
  );
2632
3300
 
@@ -2647,6 +3315,87 @@ export class ScopeDataStructure {
2647
3315
  return this.environmentVariables;
2648
3316
  }
2649
3317
 
3318
+ /**
3319
+ * Add conditional usages from AST analysis.
3320
+ * Called during scope analysis to collect all conditionals.
3321
+ */
3322
+ addConditionalUsages(
3323
+ usages: Record<
3324
+ string,
3325
+ Array<{
3326
+ path: string;
3327
+ conditionType: 'truthiness' | 'comparison' | 'switch';
3328
+ comparedValues?: string[];
3329
+ location: 'if' | 'ternary' | 'logical-and' | 'switch';
3330
+ }>
3331
+ >,
3332
+ ): void {
3333
+ for (const [path, pathUsages] of Object.entries(usages)) {
3334
+ if (!this.rawConditionalUsages[path]) {
3335
+ this.rawConditionalUsages[path] = [];
3336
+ }
3337
+ // Deduplicate usages
3338
+ for (const usage of pathUsages) {
3339
+ const exists = this.rawConditionalUsages[path].some(
3340
+ (existing) =>
3341
+ existing.location === usage.location &&
3342
+ existing.conditionType === usage.conditionType &&
3343
+ JSON.stringify(existing.comparedValues) ===
3344
+ JSON.stringify(usage.comparedValues),
3345
+ );
3346
+ if (!exists) {
3347
+ this.rawConditionalUsages[path].push(usage);
3348
+ }
3349
+ }
3350
+ }
3351
+ }
3352
+
3353
+ /**
3354
+ * Get enriched conditional usages with source tracing.
3355
+ * Uses explainPath to trace each local variable back to its data source.
3356
+ */
3357
+ getEnrichedConditionalUsages(): Record<
3358
+ string,
3359
+ Array<{
3360
+ path: string;
3361
+ conditionType: 'truthiness' | 'comparison' | 'switch';
3362
+ comparedValues?: string[];
3363
+ location: 'if' | 'ternary' | 'logical-and' | 'switch';
3364
+ sourceDataPath?: string;
3365
+ }>
3366
+ > {
3367
+ const enriched: Record<
3368
+ string,
3369
+ Array<{
3370
+ path: string;
3371
+ conditionType: 'truthiness' | 'comparison' | 'switch';
3372
+ comparedValues?: string[];
3373
+ location: 'if' | 'ternary' | 'logical-and' | 'switch';
3374
+ sourceDataPath?: string;
3375
+ }>
3376
+ > = {};
3377
+
3378
+ for (const [path, usages] of Object.entries(this.rawConditionalUsages)) {
3379
+ // Try to trace this path back to a data source
3380
+ // First, try the root scope
3381
+ const rootScopeName = this.scopeTreeManager.getTree().name;
3382
+ const explanation = this.explainPath(rootScopeName, path);
3383
+
3384
+ let sourceDataPath: string | undefined;
3385
+ if (explanation.source) {
3386
+ // Build the full data path: scopeName.path
3387
+ sourceDataPath = `${explanation.source.scope}.${explanation.source.path}`;
3388
+ }
3389
+
3390
+ enriched[path] = usages.map((usage) => ({
3391
+ ...usage,
3392
+ sourceDataPath,
3393
+ }));
3394
+ }
3395
+
3396
+ return enriched;
3397
+ }
3398
+
2650
3399
  toSerializable(): SerializableDataStructure {
2651
3400
  // Helper to convert ScopeVariable to SerializableScopeVariable
2652
3401
  const toSerializableVariable = (
@@ -2725,12 +3474,598 @@ export class ScopeDataStructure {
2725
3474
 
2726
3475
  const environmentVariables = this.getEnvironmentVariables();
2727
3476
 
3477
+ // Get enriched conditional usages with source tracing
3478
+ const enrichedConditionalUsages = this.getEnrichedConditionalUsages();
3479
+ const conditionalUsages =
3480
+ Object.keys(enrichedConditionalUsages).length > 0
3481
+ ? enrichedConditionalUsages
3482
+ : undefined;
3483
+
2728
3484
  return {
2729
3485
  externalFunctionCalls,
2730
3486
  rootFunction,
2731
3487
  functionResults,
2732
3488
  equivalentSignatureVariables,
2733
3489
  environmentVariables,
3490
+ conditionalUsages,
3491
+ };
3492
+ }
3493
+
3494
+ // ═══════════════════════════════════════════════════════════════════════════
3495
+ // DEBUGGING HELPERS
3496
+ // These methods help understand what's happening during analysis.
3497
+ // Use them when investigating why a path is missing or has the wrong type.
3498
+ // ═══════════════════════════════════════════════════════════════════════════
3499
+
3500
+ /**
3501
+ * Explains a path by tracing its equivalencies and showing where data comes from.
3502
+ *
3503
+ * @example
3504
+ * ```typescript
3505
+ * const explanation = sds.explainPath('MyComponent', 'user.id');
3506
+ * console.log(explanation);
3507
+ * // {
3508
+ * // path: 'user.id',
3509
+ * // scope: 'MyComponent',
3510
+ * // schemaValue: 'string',
3511
+ * // equivalencies: [
3512
+ * // { scope: 'MyComponent', path: 'signature[0].user.id' },
3513
+ * // { scope: 'UserProfile', path: 'props.user.id' }
3514
+ * // ],
3515
+ * // source: { scope: 'fetchUser', path: 'functionCallReturnValue(fetch).data.user.id' }
3516
+ * // }
3517
+ * ```
3518
+ */
3519
+ explainPath(
3520
+ scopeName: string,
3521
+ path: string,
3522
+ ): {
3523
+ path: string;
3524
+ scope: string;
3525
+ schemaValue: string | undefined;
3526
+ equivalencies: Array<{ scope: string; path: string; reason?: string }>;
3527
+ databaseEntry: EquivalencyDatabaseEntry | undefined;
3528
+ source: { scope: string; path: string } | undefined;
3529
+ } {
3530
+ const scopeNode = this.getScopeOrFunctionCallInfo(scopeName);
3531
+ if (!scopeNode) {
3532
+ return {
3533
+ path,
3534
+ scope: scopeName,
3535
+ schemaValue: undefined,
3536
+ equivalencies: [],
3537
+ databaseEntry: undefined,
3538
+ source: undefined,
3539
+ };
3540
+ }
3541
+
3542
+ const schemaValue = scopeNode.schema?.[path];
3543
+ const rawEquivalencies = scopeNode.equivalencies?.[path] ?? [];
3544
+
3545
+ const equivalencies = rawEquivalencies.map((eq) => ({
3546
+ scope: eq.scopeNodeName,
3547
+ path: eq.schemaPath,
3548
+ reason: eq.equivalencyReason,
3549
+ }));
3550
+
3551
+ const databaseEntry = this.getEquivalenciesDatabaseEntry(scopeName, path);
3552
+ const source =
3553
+ databaseEntry?.sourceCandidates?.[0] &&
3554
+ databaseEntry.sourceCandidates.length > 0
3555
+ ? {
3556
+ scope: databaseEntry.sourceCandidates[0].scopeNodeName,
3557
+ path: databaseEntry.sourceCandidates[0].schemaPath,
3558
+ }
3559
+ : undefined;
3560
+
3561
+ return {
3562
+ path,
3563
+ scope: scopeName,
3564
+ schemaValue,
3565
+ equivalencies,
3566
+ databaseEntry,
3567
+ source,
3568
+ };
3569
+ }
3570
+
3571
+ /**
3572
+ * Dumps the current state of analysis for inspection.
3573
+ * Useful for understanding the overall state or finding issues.
3574
+ *
3575
+ * @param options - What to include in the dump
3576
+ * @returns A summary object with requested data
3577
+ */
3578
+ dumpState(options?: {
3579
+ includeSchemas?: boolean;
3580
+ includeEquivalencies?: boolean;
3581
+ scopeFilter?: string;
3582
+ }): {
3583
+ scopeCount: number;
3584
+ externalCallCount: number;
3585
+ equivalencyDatabaseSize: number;
3586
+ metrics: {
3587
+ addToSchemaCallCount: number;
3588
+ followEquivalenciesCallCount: number;
3589
+ maxEquivalencyChainDepth: number;
3590
+ };
3591
+ scopes: Array<{
3592
+ name: string;
3593
+ schemaKeyCount: number;
3594
+ equivalencyKeyCount: number;
3595
+ functionCallCount: number;
3596
+ schema?: Record<string, string>;
3597
+ equivalencies?: Record<string, Array<{ scope: string; path: string }>>;
3598
+ }>;
3599
+ } {
3600
+ const includeSchemas = options?.includeSchemas ?? false;
3601
+ const includeEquivalencies = options?.includeEquivalencies ?? false;
3602
+ const scopeFilter = options?.scopeFilter;
3603
+
3604
+ const scopeNames = Object.keys(this.scopeNodes).filter(
3605
+ (name) => !scopeFilter || name.includes(scopeFilter),
3606
+ );
3607
+
3608
+ const scopes = scopeNames.map((name) => {
3609
+ const node = this.scopeNodes[name];
3610
+ const result: {
3611
+ name: string;
3612
+ schemaKeyCount: number;
3613
+ equivalencyKeyCount: number;
3614
+ functionCallCount: number;
3615
+ schema?: Record<string, string>;
3616
+ equivalencies?: Record<string, Array<{ scope: string; path: string }>>;
3617
+ } = {
3618
+ name,
3619
+ schemaKeyCount: Object.keys(node.schema ?? {}).length,
3620
+ equivalencyKeyCount: Object.keys(node.equivalencies ?? {}).length,
3621
+ functionCallCount: node.functionCalls?.length ?? 0,
3622
+ };
3623
+
3624
+ if (includeSchemas) {
3625
+ result.schema = node.schema;
3626
+ }
3627
+
3628
+ if (includeEquivalencies) {
3629
+ result.equivalencies = Object.entries(node.equivalencies ?? {}).reduce(
3630
+ (acc, [key, vals]) => {
3631
+ acc[key] = vals.map((v) => ({
3632
+ scope: v.scopeNodeName,
3633
+ path: v.schemaPath,
3634
+ }));
3635
+ return acc;
3636
+ },
3637
+ {} as Record<string, Array<{ scope: string; path: string }>>,
3638
+ );
3639
+ }
3640
+
3641
+ return result;
3642
+ });
3643
+
3644
+ return {
3645
+ scopeCount: Object.keys(this.scopeNodes).length,
3646
+ externalCallCount: this.externalFunctionCalls.length,
3647
+ equivalencyDatabaseSize: this.equivalencyDatabase.length,
3648
+ metrics: {
3649
+ addToSchemaCallCount,
3650
+ followEquivalenciesCallCount,
3651
+ maxEquivalencyChainDepth,
3652
+ },
3653
+ scopes,
3654
+ };
3655
+ }
3656
+
3657
+ /**
3658
+ * Traces the full equivalency chain for a path, showing how data flows.
3659
+ * This is the most detailed debugging tool for understanding data tracing.
3660
+ *
3661
+ * @example
3662
+ * ```typescript
3663
+ * const chain = sds.traceEquivalencyChain('MyComponent', 'user.id');
3664
+ * // Returns array of steps showing how user.id is traced back to its source
3665
+ * ```
3666
+ */
3667
+ traceEquivalencyChain(
3668
+ scopeName: string,
3669
+ path: string,
3670
+ maxDepth = 20,
3671
+ ): Array<{
3672
+ step: number;
3673
+ scope: string;
3674
+ path: string;
3675
+ schemaValue?: string;
3676
+ nextEquivalencies: Array<{ scope: string; path: string; reason?: string }>;
3677
+ }> {
3678
+ const chain: Array<{
3679
+ step: number;
3680
+ scope: string;
3681
+ path: string;
3682
+ schemaValue?: string;
3683
+ nextEquivalencies: Array<{
3684
+ scope: string;
3685
+ path: string;
3686
+ reason?: string;
3687
+ }>;
3688
+ }> = [];
3689
+
3690
+ const visited = new Set<string>();
3691
+ const queue: Array<{ scope: string; path: string; depth: number }> = [
3692
+ { scope: scopeName, path, depth: 0 },
3693
+ ];
3694
+
3695
+ while (queue.length > 0 && chain.length < maxDepth) {
3696
+ const current = queue.shift()!;
3697
+ const key = `${current.scope}::${current.path}`;
3698
+
3699
+ if (visited.has(key)) continue;
3700
+ visited.add(key);
3701
+
3702
+ const scopeNode = this.getScopeOrFunctionCallInfo(current.scope);
3703
+ const schemaValue = scopeNode?.schema?.[current.path];
3704
+ const rawEquivalencies = scopeNode?.equivalencies?.[current.path] ?? [];
3705
+
3706
+ const nextEquivalencies = rawEquivalencies.map((eq) => ({
3707
+ scope: eq.scopeNodeName,
3708
+ path: eq.schemaPath,
3709
+ reason: eq.equivalencyReason,
3710
+ }));
3711
+
3712
+ chain.push({
3713
+ step: chain.length + 1,
3714
+ scope: current.scope,
3715
+ path: current.path,
3716
+ schemaValue,
3717
+ nextEquivalencies,
3718
+ });
3719
+
3720
+ // Add next equivalencies to queue for further tracing
3721
+ for (const eq of nextEquivalencies) {
3722
+ const nextKey = `${eq.scope}::${eq.path}`;
3723
+ if (!visited.has(nextKey)) {
3724
+ queue.push({
3725
+ scope: eq.scope,
3726
+ path: eq.path,
3727
+ depth: current.depth + 1,
3728
+ });
3729
+ }
3730
+ }
3731
+ }
3732
+
3733
+ return chain;
3734
+ }
3735
+
3736
+ /**
3737
+ * Finds all paths in a scope that match a pattern.
3738
+ * Useful for finding related paths when debugging.
3739
+ *
3740
+ * @example
3741
+ * ```typescript
3742
+ * const paths = sds.findPaths('MyComponent', /user/);
3743
+ * // Returns all schema paths containing "user"
3744
+ * ```
3745
+ */
3746
+ findPaths(
3747
+ scopeName: string,
3748
+ pattern: RegExp | string,
3749
+ ): Array<{ path: string; value: string; hasEquivalencies: boolean }> {
3750
+ const scopeNode = this.getScopeOrFunctionCallInfo(scopeName);
3751
+ if (!scopeNode) return [];
3752
+
3753
+ const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;
3754
+ const results: Array<{
3755
+ path: string;
3756
+ value: string;
3757
+ hasEquivalencies: boolean;
3758
+ }> = [];
3759
+
3760
+ // Search in schema
3761
+ for (const [path, value] of Object.entries(scopeNode.schema ?? {})) {
3762
+ if (regex.test(path)) {
3763
+ results.push({
3764
+ path,
3765
+ value,
3766
+ hasEquivalencies: !!scopeNode.equivalencies?.[path]?.length,
3767
+ });
3768
+ }
3769
+ }
3770
+
3771
+ // Also search equivalency keys that might not be in schema
3772
+ for (const path of Object.keys(scopeNode.equivalencies ?? {})) {
3773
+ if (regex.test(path) && !results.find((r) => r.path === path)) {
3774
+ results.push({
3775
+ path,
3776
+ value: scopeNode.schema?.[path] ?? 'not-in-schema',
3777
+ hasEquivalencies: true,
3778
+ });
3779
+ }
3780
+ }
3781
+
3782
+ return results.sort((a, b) => a.path.localeCompare(b.path));
3783
+ }
3784
+
3785
+ /**
3786
+ * Returns a summary of why a path might be missing from the schema.
3787
+ * Checks common issues that cause paths to not appear.
3788
+ */
3789
+ diagnoseMissingPath(
3790
+ scopeName: string,
3791
+ path: string,
3792
+ ): {
3793
+ exists: boolean;
3794
+ inSchema: boolean;
3795
+ inEquivalencies: boolean;
3796
+ possibleReasons: string[];
3797
+ suggestions: string[];
3798
+ } {
3799
+ const scopeNode = this.getScopeOrFunctionCallInfo(scopeName);
3800
+ const possibleReasons: string[] = [];
3801
+ const suggestions: string[] = [];
3802
+
3803
+ if (!scopeNode) {
3804
+ possibleReasons.push(`Scope "${scopeName}" does not exist`);
3805
+ suggestions.push(
3806
+ `Available scopes: ${Object.keys(this.scopeNodes).join(', ')}`,
3807
+ );
3808
+ return {
3809
+ exists: false,
3810
+ inSchema: false,
3811
+ inEquivalencies: false,
3812
+ possibleReasons,
3813
+ suggestions,
3814
+ };
3815
+ }
3816
+
3817
+ const inSchema = path in (scopeNode.schema ?? {});
3818
+ const inEquivalencies = path in (scopeNode.equivalencies ?? {});
3819
+
3820
+ if (inSchema) {
3821
+ return {
3822
+ exists: true,
3823
+ inSchema: true,
3824
+ inEquivalencies,
3825
+ possibleReasons: [],
3826
+ suggestions: [],
3827
+ };
3828
+ }
3829
+
3830
+ // Check for similar paths
3831
+ const allPaths = [
3832
+ ...Object.keys(scopeNode.schema ?? {}),
3833
+ ...Object.keys(scopeNode.equivalencies ?? {}),
3834
+ ];
3835
+
3836
+ const similarPaths = allPaths.filter((p) => {
3837
+ const pathParts = path.split('.');
3838
+ const pParts = p.split('.');
3839
+ return (
3840
+ pathParts.some((part) => pParts.includes(part)) ||
3841
+ p.includes(path) ||
3842
+ path.includes(p)
3843
+ );
3844
+ });
3845
+
3846
+ if (similarPaths.length > 0) {
3847
+ possibleReasons.push(`Path "${path}" not found but similar paths exist`);
3848
+ suggestions.push(`Similar paths: ${similarPaths.slice(0, 5).join(', ')}`);
3849
+ }
3850
+
3851
+ // Check if it's a partial path
3852
+ const parentPath = path.split('.').slice(0, -1).join('.');
3853
+ if (parentPath && scopeNode.schema?.[parentPath]) {
3854
+ possibleReasons.push(
3855
+ `Parent path "${parentPath}" exists with value "${scopeNode.schema[parentPath]}"`,
3856
+ );
3857
+ suggestions.push(
3858
+ `The property might not have been accessed in the analyzed code`,
3859
+ );
3860
+ }
3861
+
3862
+ // Check visited tracker
3863
+ const wasVisited = this.visitedTracker.wasVisited(scopeName, path);
3864
+ if (wasVisited) {
3865
+ possibleReasons.push(`Path was visited during analysis but not written`);
3866
+ suggestions.push(
3867
+ `Check if the path was filtered by isValidPath() or hit cycle detection`,
3868
+ );
3869
+ }
3870
+
3871
+ if (possibleReasons.length === 0) {
3872
+ possibleReasons.push(
3873
+ `Path "${path}" was never encountered during analysis`,
3874
+ );
3875
+ suggestions.push(
3876
+ `Verify the path is accessed in the source code being analyzed`,
3877
+ );
3878
+ }
3879
+
3880
+ return {
3881
+ exists: false,
3882
+ inSchema,
3883
+ inEquivalencies,
3884
+ possibleReasons,
3885
+ suggestions,
3886
+ };
3887
+ }
3888
+
3889
+ /**
3890
+ * Analyzes the stored equivalencies to provide aggregate statistics.
3891
+ * Call this AFTER analysis completes to understand equivalency patterns.
3892
+ *
3893
+ * @example
3894
+ * ```typescript
3895
+ * const analysis = sds.analyzeEquivalencies();
3896
+ * console.log(analysis.byReason); // Count by reason
3897
+ * console.log(analysis.byScope); // Count by scope
3898
+ * console.log(analysis.hotSpots); // Scopes with most equivalencies
3899
+ * ```
3900
+ */
3901
+ analyzeEquivalencies(): {
3902
+ total: number;
3903
+ byReason: Record<string, number>;
3904
+ byScope: Record<string, number>;
3905
+ hotSpots: Array<{ scope: string; count: number }>;
3906
+ unusedReasons: string[];
3907
+ } {
3908
+ const byReason: Record<string, number> = {};
3909
+ const byScope: Record<string, number> = {};
3910
+ let total = 0;
3911
+
3912
+ // Collect from all scope nodes
3913
+ for (const scopeName of Object.keys(this.scopeNodes)) {
3914
+ const scopeNode = this.scopeNodes[scopeName];
3915
+ const equivalencies = scopeNode.equivalencies ?? {};
3916
+
3917
+ for (const path of Object.keys(equivalencies)) {
3918
+ for (const eq of equivalencies[path]) {
3919
+ total++;
3920
+ const reason = eq.equivalencyReason ?? 'unknown';
3921
+ byReason[reason] = (byReason[reason] ?? 0) + 1;
3922
+ byScope[scopeName] = (byScope[scopeName] ?? 0) + 1;
3923
+ }
3924
+ }
3925
+ }
3926
+
3927
+ // Also check external function calls
3928
+ for (const fc of this.externalFunctionCalls) {
3929
+ const equivalencies = fc.equivalencies ?? {};
3930
+ for (const path of Object.keys(equivalencies)) {
3931
+ for (const eq of equivalencies[path]) {
3932
+ total++;
3933
+ const reason = eq.equivalencyReason ?? 'unknown';
3934
+ byReason[reason] = (byReason[reason] ?? 0) + 1;
3935
+ byScope[fc.name] = (byScope[fc.name] ?? 0) + 1;
3936
+ }
3937
+ }
3938
+ }
3939
+
3940
+ // Find hot spots (scopes with most equivalencies)
3941
+ const hotSpots = Object.entries(byScope)
3942
+ .map(([scope, count]) => ({ scope, count }))
3943
+ .sort((a, b) => b.count - a.count)
3944
+ .slice(0, 10);
3945
+
3946
+ // Find reasons that appear in stored equivalencies but aren't in ALLOWED list
3947
+ const unusedReasons = Object.keys(byReason).filter(
3948
+ (reason) => !ALLOWED_EQUIVALENCY_REASONS.has(reason),
3949
+ );
3950
+
3951
+ return {
3952
+ total,
3953
+ byReason,
3954
+ byScope,
3955
+ hotSpots,
3956
+ unusedReasons,
2734
3957
  };
2735
3958
  }
3959
+
3960
+ /**
3961
+ * Validates equivalencies and identifies potential problems.
3962
+ * Returns actionable issues that may indicate bugs or optimization opportunities.
3963
+ *
3964
+ * @example
3965
+ * ```typescript
3966
+ * const issues = sds.validateEquivalencies();
3967
+ * if (issues.length > 0) {
3968
+ * console.log('Found issues:', issues);
3969
+ * }
3970
+ * ```
3971
+ */
3972
+ validateEquivalencies(): Array<{
3973
+ type: 'broken_reference' | 'dead_end' | 'long_chain' | 'circular';
3974
+ severity: 'error' | 'warning';
3975
+ scope: string;
3976
+ path: string;
3977
+ details: string;
3978
+ }> {
3979
+ const issues: Array<{
3980
+ type: 'broken_reference' | 'dead_end' | 'long_chain' | 'circular';
3981
+ severity: 'error' | 'warning';
3982
+ scope: string;
3983
+ path: string;
3984
+ details: string;
3985
+ }> = [];
3986
+
3987
+ const allScopeNames = new Set([
3988
+ ...Object.keys(this.scopeNodes),
3989
+ ...this.externalFunctionCalls.map((fc) => fc.name),
3990
+ ]);
3991
+
3992
+ // Check all scope nodes
3993
+ for (const scopeName of Object.keys(this.scopeNodes)) {
3994
+ const scopeNode = this.scopeNodes[scopeName];
3995
+ const equivalencies = scopeNode.equivalencies ?? {};
3996
+
3997
+ for (const [path, eqs] of Object.entries(equivalencies)) {
3998
+ for (const eq of eqs) {
3999
+ // Check 1: Does the target scope exist?
4000
+ if (!allScopeNames.has(eq.scopeNodeName)) {
4001
+ issues.push({
4002
+ type: 'broken_reference',
4003
+ severity: 'error',
4004
+ scope: scopeName,
4005
+ path,
4006
+ details: `Equivalency points to non-existent scope "${eq.scopeNodeName}"`,
4007
+ });
4008
+ continue;
4009
+ }
4010
+
4011
+ // Check 2: Does the target path exist in target scope's schema or equivalencies?
4012
+ const targetScope = this.getScopeOrFunctionCallInfo(eq.scopeNodeName);
4013
+ if (targetScope) {
4014
+ const hasInSchema = eq.schemaPath in (targetScope.schema ?? {});
4015
+ const hasInEquivalencies =
4016
+ eq.schemaPath in (targetScope.equivalencies ?? {});
4017
+
4018
+ // Only flag as dead end if it's a leaf path (no sub-properties exist)
4019
+ if (!hasInSchema && !hasInEquivalencies) {
4020
+ const hasSubPaths = Object.keys(targetScope.schema ?? {}).some(
4021
+ (p) =>
4022
+ p.startsWith(eq.schemaPath + '.') ||
4023
+ p.startsWith(eq.schemaPath + '['),
4024
+ );
4025
+ if (!hasSubPaths) {
4026
+ issues.push({
4027
+ type: 'dead_end',
4028
+ severity: 'warning',
4029
+ scope: scopeName,
4030
+ path,
4031
+ details: `Equivalency to "${eq.scopeNodeName}::${eq.schemaPath}" - path not in schema`,
4032
+ });
4033
+ }
4034
+ }
4035
+ }
4036
+ }
4037
+
4038
+ // Check 3: Trace chain depth
4039
+ const chain = this.traceEquivalencyChain(scopeName, path, 50);
4040
+ if (chain.length >= 20) {
4041
+ issues.push({
4042
+ type: 'long_chain',
4043
+ severity: 'warning',
4044
+ scope: scopeName,
4045
+ path,
4046
+ details: `Equivalency chain depth is ${chain.length} (may impact performance)`,
4047
+ });
4048
+ }
4049
+
4050
+ // Check for potential cycles (chain that returns to starting point)
4051
+ const visited = new Set<string>();
4052
+ for (const step of chain) {
4053
+ const key = `${step.scope}::${step.path}`;
4054
+ if (visited.has(key)) {
4055
+ issues.push({
4056
+ type: 'circular',
4057
+ severity: 'warning',
4058
+ scope: scopeName,
4059
+ path,
4060
+ details: `Circular reference detected at ${key}`,
4061
+ });
4062
+ break;
4063
+ }
4064
+ visited.add(key);
4065
+ }
4066
+ }
4067
+ }
4068
+
4069
+ return issues;
4070
+ }
2736
4071
  }