forgeos 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (540) hide show
  1. package/.npmignore +1 -0
  2. package/AGENTS.md +277 -0
  3. package/CHANGELOG.md +8 -0
  4. package/CONTRIBUTING.md +58 -0
  5. package/README.md +377 -0
  6. package/bin/forge-bun.mjs +110 -0
  7. package/bin/forge.mjs +19 -0
  8. package/package.json +96 -0
  9. package/packages/eslint-plugin-forge/index.ts +15 -0
  10. package/packages/eslint-plugin-forge/package.json +10 -0
  11. package/packages/eslint-plugin-forge/src/check-source.ts +95 -0
  12. package/packages/eslint-plugin-forge/src/load-artifacts.ts +24 -0
  13. package/packages/eslint-plugin-forge/src/rule-no-forge-guard-violation.ts +93 -0
  14. package/src/forge/_generated/actionSubscriptions.json +2 -0
  15. package/src/forge/_generated/actionSubscriptions.ts +10 -0
  16. package/src/forge/_generated/agentAdapterManifest.json +2 -0
  17. package/src/forge/_generated/agentAdapterManifest.ts +73 -0
  18. package/src/forge/_generated/agentContract.json +2 -0
  19. package/src/forge/_generated/agentContract.ts +912 -0
  20. package/src/forge/_generated/agentQuickstart.md +32 -0
  21. package/src/forge/_generated/aiContext.ts +59 -0
  22. package/src/forge/_generated/aiModels.json +2 -0
  23. package/src/forge/_generated/aiModels.ts +35 -0
  24. package/src/forge/_generated/aiProviders.json +2 -0
  25. package/src/forge/_generated/aiProviders.ts +23 -0
  26. package/src/forge/_generated/aiRegistry.json +2 -0
  27. package/src/forge/_generated/aiRegistry.ts +29 -0
  28. package/src/forge/_generated/api.json +2 -0
  29. package/src/forge/_generated/api.ts +8 -0
  30. package/src/forge/_generated/appGraph.json +2 -0
  31. package/src/forge/_generated/appGraph.ts +14511 -0
  32. package/src/forge/_generated/appMap.md +35 -0
  33. package/src/forge/_generated/artifactManifest.json +2 -0
  34. package/src/forge/_generated/artifactManifest.ts +7 -0
  35. package/src/forge/_generated/authClaims.json +2 -0
  36. package/src/forge/_generated/authClaims.ts +13 -0
  37. package/src/forge/_generated/authConfig.json +2 -0
  38. package/src/forge/_generated/authConfig.ts +17 -0
  39. package/src/forge/_generated/authContext.ts +23 -0
  40. package/src/forge/_generated/authRegistry.json +2 -0
  41. package/src/forge/_generated/authRegistry.ts +25 -0
  42. package/src/forge/_generated/buildInfo.json +2 -0
  43. package/src/forge/_generated/buildInfo.ts +9 -0
  44. package/src/forge/_generated/capabilityMap.json +2 -0
  45. package/src/forge/_generated/capabilityMap.md +15 -0
  46. package/src/forge/_generated/capabilityMap.ts +17 -0
  47. package/src/forge/_generated/client.ts +282 -0
  48. package/src/forge/_generated/clientApi.ts +9 -0
  49. package/src/forge/_generated/clientManifest.json +2 -0
  50. package/src/forge/_generated/clientManifest.ts +39 -0
  51. package/src/forge/_generated/clientTypes.ts +78 -0
  52. package/src/forge/_generated/configRegistry.json +2 -0
  53. package/src/forge/_generated/configRegistry.ts +4 -0
  54. package/src/forge/_generated/dataGraph.json +2 -0
  55. package/src/forge/_generated/dataGraph.ts +8 -0
  56. package/src/forge/_generated/db.json +2 -0
  57. package/src/forge/_generated/db.ts +2 -0
  58. package/src/forge/_generated/dbSecurityManifest.json +2 -0
  59. package/src/forge/_generated/dbSecurityManifest.ts +15 -0
  60. package/src/forge/_generated/dbSessionContext.json +2 -0
  61. package/src/forge/_generated/dbSessionContext.ts +39 -0
  62. package/src/forge/_generated/deployManifest.json +2 -0
  63. package/src/forge/_generated/deployManifest.ts +14 -0
  64. package/src/forge/_generated/devManifest.json +2 -0
  65. package/src/forge/_generated/devManifest.ts +47 -0
  66. package/src/forge/_generated/envSchema.json +2 -0
  67. package/src/forge/_generated/envSchema.ts +59 -0
  68. package/src/forge/_generated/frontendGraph.json +2 -0
  69. package/src/forge/_generated/frontendGraph.ts +27 -0
  70. package/src/forge/_generated/importGuards.json +2 -0
  71. package/src/forge/_generated/importGuards.ts +652 -0
  72. package/src/forge/_generated/index.ts +67 -0
  73. package/src/forge/_generated/liveProductionManifest.json +2 -0
  74. package/src/forge/_generated/liveProductionManifest.ts +23 -0
  75. package/src/forge/_generated/liveProtocol.json +2 -0
  76. package/src/forge/_generated/liveProtocol.ts +21 -0
  77. package/src/forge/_generated/liveQueryRegistry.json +2 -0
  78. package/src/forge/_generated/liveQueryRegistry.ts +9 -0
  79. package/src/forge/_generated/liveTransportConfig.json +2 -0
  80. package/src/forge/_generated/liveTransportConfig.ts +19 -0
  81. package/src/forge/_generated/makeRegistry.json +2 -0
  82. package/src/forge/_generated/makeRegistry.ts +163 -0
  83. package/src/forge/_generated/makeTemplates.json +2 -0
  84. package/src/forge/_generated/makeTemplates.ts +61 -0
  85. package/src/forge/_generated/mockMap.json +2 -0
  86. package/src/forge/_generated/mockMap.ts +7 -0
  87. package/src/forge/_generated/operationPlaybooks.md +145 -0
  88. package/src/forge/_generated/packageGraph.json +2 -0
  89. package/src/forge/_generated/packageGraph.ts +168569 -0
  90. package/src/forge/_generated/packageUpgradeRegistry.json +2 -0
  91. package/src/forge/_generated/packageUpgradeRegistry.ts +15 -0
  92. package/src/forge/_generated/permissionMatrix.json +2 -0
  93. package/src/forge/_generated/permissionMatrix.ts +7 -0
  94. package/src/forge/_generated/policyRegistry.json +2 -0
  95. package/src/forge/_generated/policyRegistry.ts +11 -0
  96. package/src/forge/_generated/queryRegistry.json +2 -0
  97. package/src/forge/_generated/queryRegistry.ts +9 -0
  98. package/src/forge/_generated/react.d.ts +22 -0
  99. package/src/forge/_generated/react.ts +29 -0
  100. package/src/forge/_generated/reactManifest.json +2 -0
  101. package/src/forge/_generated/reactManifest.ts +19 -0
  102. package/src/forge/_generated/releaseManifest.json +2 -0
  103. package/src/forge/_generated/releaseManifest.ts +25 -0
  104. package/src/forge/_generated/rlsPolicies.json +2 -0
  105. package/src/forge/_generated/rlsPolicies.sql +34 -0
  106. package/src/forge/_generated/rlsPolicies.ts +6 -0
  107. package/src/forge/_generated/runtimeGraph.json +2 -0
  108. package/src/forge/_generated/runtimeGraph.ts +8 -0
  109. package/src/forge/_generated/runtimeMatrix.json +2 -0
  110. package/src/forge/_generated/runtimeMatrix.ts +229125 -0
  111. package/src/forge/_generated/runtimeRegistry.ts +2 -0
  112. package/src/forge/_generated/runtimeRules.md +79 -0
  113. package/src/forge/_generated/secretRegistry.json +2 -0
  114. package/src/forge/_generated/secretRegistry.ts +50 -0
  115. package/src/forge/_generated/secretsContext.ts +11 -0
  116. package/src/forge/_generated/serverApi.ts +10 -0
  117. package/src/forge/_generated/sourceMapManifest.json +2 -0
  118. package/src/forge/_generated/sourceMapManifest.ts +7 -0
  119. package/src/forge/_generated/sqlPlan.json +2 -0
  120. package/src/forge/_generated/sqlPlan.ts +88 -0
  121. package/src/forge/_generated/subscriptionManifest.json +2 -0
  122. package/src/forge/_generated/subscriptionManifest.ts +7 -0
  123. package/src/forge/_generated/symbolicationManifest.json +2 -0
  124. package/src/forge/_generated/symbolicationManifest.ts +17 -0
  125. package/src/forge/_generated/telemetryRegistry.json +2 -0
  126. package/src/forge/_generated/telemetryRegistry.ts +9 -0
  127. package/src/forge/_generated/telemetrySinks.json +2 -0
  128. package/src/forge/_generated/telemetrySinks.ts +11 -0
  129. package/src/forge/_generated/tenantScope.json +2 -0
  130. package/src/forge/_generated/tenantScope.ts +8 -0
  131. package/src/forge/_generated/testGraph.json +2 -0
  132. package/src/forge/_generated/testGraph.ts +3054 -0
  133. package/src/forge/_generated/testPlanRegistry.json +2 -0
  134. package/src/forge/_generated/testPlanRegistry.ts +33 -0
  135. package/src/forge/_generated/uiRoutes.json +2 -0
  136. package/src/forge/_generated/uiRoutes.ts +16 -0
  137. package/src/forge/_generated/uiScenarios.json +2 -0
  138. package/src/forge/_generated/uiScenarios.ts +30 -0
  139. package/src/forge/_generated/uiTestManifest.json +2 -0
  140. package/src/forge/_generated/uiTestManifest.ts +27 -0
  141. package/src/forge/_generated/workflowRegistry.json +2 -0
  142. package/src/forge/_generated/workflowRegistry.ts +9 -0
  143. package/src/forge/_generated/workflowSubscriptions.json +2 -0
  144. package/src/forge/_generated/workflowSubscriptions.ts +10 -0
  145. package/src/forge/agent-adapters/index.ts +1002 -0
  146. package/src/forge/agent-adapters/types.ts +135 -0
  147. package/src/forge/cli/agent-contract.ts +50 -0
  148. package/src/forge/cli/ai.ts +148 -0
  149. package/src/forge/cli/auth.ts +198 -0
  150. package/src/forge/cli/build.ts +105 -0
  151. package/src/forge/cli/bun-exec.ts +4 -0
  152. package/src/forge/cli/commands.ts +1130 -0
  153. package/src/forge/cli/db.ts +316 -0
  154. package/src/forge/cli/deps.ts +277 -0
  155. package/src/forge/cli/dev.ts +529 -0
  156. package/src/forge/cli/doctor.ts +209 -0
  157. package/src/forge/cli/feature.ts +485 -0
  158. package/src/forge/cli/index.ts +25 -0
  159. package/src/forge/cli/lint-forge.ts +119 -0
  160. package/src/forge/cli/live.ts +179 -0
  161. package/src/forge/cli/main.ts +92 -0
  162. package/src/forge/cli/make.ts +133 -0
  163. package/src/forge/cli/new.ts +505 -0
  164. package/src/forge/cli/outbox.ts +297 -0
  165. package/src/forge/cli/output.ts +114 -0
  166. package/src/forge/cli/parse.ts +2211 -0
  167. package/src/forge/cli/policy.ts +204 -0
  168. package/src/forge/cli/query.ts +91 -0
  169. package/src/forge/cli/refactor.ts +221 -0
  170. package/src/forge/cli/release.ts +285 -0
  171. package/src/forge/cli/rls.ts +322 -0
  172. package/src/forge/cli/run.ts +76 -0
  173. package/src/forge/cli/secrets.ts +274 -0
  174. package/src/forge/cli/self-host.ts +468 -0
  175. package/src/forge/cli/serve.ts +93 -0
  176. package/src/forge/cli/telemetry.ts +219 -0
  177. package/src/forge/cli/verify.ts +587 -0
  178. package/src/forge/cli/version.ts +1 -0
  179. package/src/forge/cli/windows.ts +413 -0
  180. package/src/forge/cli/worker.ts +87 -0
  181. package/src/forge/cli/workflow.ts +424 -0
  182. package/src/forge/compiler/action-subscriptions/build.ts +116 -0
  183. package/src/forge/compiler/action-subscriptions/constants.ts +2 -0
  184. package/src/forge/compiler/action-subscriptions/index.ts +6 -0
  185. package/src/forge/compiler/action-subscriptions/parse.ts +6 -0
  186. package/src/forge/compiler/agent-contract/build.ts +1651 -0
  187. package/src/forge/compiler/agent-contract/types.ts +326 -0
  188. package/src/forge/compiler/ai-registry/build.ts +165 -0
  189. package/src/forge/compiler/ai-registry/constants.ts +2 -0
  190. package/src/forge/compiler/ai-registry/parse.ts +56 -0
  191. package/src/forge/compiler/api-surface/build.ts +107 -0
  192. package/src/forge/compiler/app-graph/build.ts +121 -0
  193. package/src/forge/compiler/app-graph/classify.ts +10 -0
  194. package/src/forge/compiler/app-graph/dup-symbol.ts +29 -0
  195. package/src/forge/compiler/app-graph/extract.ts +124 -0
  196. package/src/forge/compiler/app-graph/forge-apis.ts +29 -0
  197. package/src/forge/compiler/app-graph/index.ts +15 -0
  198. package/src/forge/compiler/app-graph/module-graph.ts +320 -0
  199. package/src/forge/compiler/app-graph/parser.ts +119 -0
  200. package/src/forge/compiler/app-graph/symbols.ts +48 -0
  201. package/src/forge/compiler/app-graph/tsconfig-hash.ts +62 -0
  202. package/src/forge/compiler/app-graph/types.ts +43 -0
  203. package/src/forge/compiler/app-graph/versions.ts +14 -0
  204. package/src/forge/compiler/cache/index.ts +17 -0
  205. package/src/forge/compiler/cache/key.ts +46 -0
  206. package/src/forge/compiler/cache/scheduler.ts +72 -0
  207. package/src/forge/compiler/cache/store.ts +78 -0
  208. package/src/forge/compiler/classifier/capabilities.ts +78 -0
  209. package/src/forge/compiler/classifier/classify.ts +113 -0
  210. package/src/forge/compiler/classifier/contexts.ts +188 -0
  211. package/src/forge/compiler/classifier/index.ts +18 -0
  212. package/src/forge/compiler/classifier/runtime-matrix.ts +45 -0
  213. package/src/forge/compiler/classifier/secrets.ts +41 -0
  214. package/src/forge/compiler/classifier/signals.ts +129 -0
  215. package/src/forge/compiler/client-sdk/build-manifest.ts +151 -0
  216. package/src/forge/compiler/client-sdk/render-client.ts +432 -0
  217. package/src/forge/compiler/data-graph/build.ts +131 -0
  218. package/src/forge/compiler/data-graph/constants.ts +5 -0
  219. package/src/forge/compiler/data-graph/index.ts +6 -0
  220. package/src/forge/compiler/data-graph/parse.ts +176 -0
  221. package/src/forge/compiler/data-graph/rls/build.ts +222 -0
  222. package/src/forge/compiler/data-graph/rls/types.ts +62 -0
  223. package/src/forge/compiler/data-graph/sql/ddl.ts +390 -0
  224. package/src/forge/compiler/data-graph/sql/naming.ts +10 -0
  225. package/src/forge/compiler/data-graph/sql/serialize.ts +85 -0
  226. package/src/forge/compiler/data-graph/sql/types.ts +37 -0
  227. package/src/forge/compiler/dev-manifest/build.ts +170 -0
  228. package/src/forge/compiler/dev-manifest/constants.ts +5 -0
  229. package/src/forge/compiler/diagnostics/codes.ts +611 -0
  230. package/src/forge/compiler/diagnostics/create.ts +245 -0
  231. package/src/forge/compiler/diagnostics/index.ts +55 -0
  232. package/src/forge/compiler/emitter/artifact-kind.ts +14 -0
  233. package/src/forge/compiler/emitter/barrel.ts +44 -0
  234. package/src/forge/compiler/emitter/constants.ts +7 -0
  235. package/src/forge/compiler/emitter/emit.ts +237 -0
  236. package/src/forge/compiler/emitter/index.ts +24 -0
  237. package/src/forge/compiler/emitter/lock.ts +62 -0
  238. package/src/forge/compiler/emitter/render.ts +73 -0
  239. package/src/forge/compiler/emitter/write.ts +35 -0
  240. package/src/forge/compiler/frontend-graph/build.ts +495 -0
  241. package/src/forge/compiler/fs/index.ts +23 -0
  242. package/src/forge/compiler/fs/memory.ts +233 -0
  243. package/src/forge/compiler/fs/node.ts +139 -0
  244. package/src/forge/compiler/fs/profile.ts +108 -0
  245. package/src/forge/compiler/fs/types.ts +52 -0
  246. package/src/forge/compiler/guards/artifacts.ts +96 -0
  247. package/src/forge/compiler/guards/check-ai-usage.ts +98 -0
  248. package/src/forge/compiler/guards/check-import-guards.ts +106 -0
  249. package/src/forge/compiler/guards/check-process-env.ts +98 -0
  250. package/src/forge/compiler/guards/check-query-usage.ts +76 -0
  251. package/src/forge/compiler/guards/index.ts +11 -0
  252. package/src/forge/compiler/guards/propagate-contexts.ts +57 -0
  253. package/src/forge/compiler/index.ts +17 -0
  254. package/src/forge/compiler/integration/add.ts +496 -0
  255. package/src/forge/compiler/integration/index.ts +17 -0
  256. package/src/forge/compiler/integration/plan.ts +283 -0
  257. package/src/forge/compiler/integration/render.ts +189 -0
  258. package/src/forge/compiler/integration/snapshot.ts +52 -0
  259. package/src/forge/compiler/integration/templates/ai.ts +131 -0
  260. package/src/forge/compiler/integration/templates/index.ts +8 -0
  261. package/src/forge/compiler/integration/templates/posthog.ts +145 -0
  262. package/src/forge/compiler/integration/templates/render.ts +113 -0
  263. package/src/forge/compiler/integration/templates/sentry.ts +151 -0
  264. package/src/forge/compiler/integration/templates/stripe.ts +109 -0
  265. package/src/forge/compiler/integration/templates/types.ts +14 -0
  266. package/src/forge/compiler/integration/templates/zod.ts +55 -0
  267. package/src/forge/compiler/live-production/types.ts +122 -0
  268. package/src/forge/compiler/live-query-registry/build.ts +150 -0
  269. package/src/forge/compiler/live-query-registry/constants.ts +2 -0
  270. package/src/forge/compiler/make-registry/build.ts +179 -0
  271. package/src/forge/compiler/orchestrator/discover.ts +214 -0
  272. package/src/forge/compiler/orchestrator/fast-check.ts +117 -0
  273. package/src/forge/compiler/orchestrator/generate-lock.ts +138 -0
  274. package/src/forge/compiler/orchestrator/guards.ts +5 -0
  275. package/src/forge/compiler/orchestrator/index.ts +27 -0
  276. package/src/forge/compiler/orchestrator/manifest-hashes.ts +21 -0
  277. package/src/forge/compiler/orchestrator/manifest.ts +92 -0
  278. package/src/forge/compiler/orchestrator/orphans.ts +51 -0
  279. package/src/forge/compiler/orchestrator/plan.ts +876 -0
  280. package/src/forge/compiler/orchestrator/profile.ts +36 -0
  281. package/src/forge/compiler/orchestrator/run.ts +277 -0
  282. package/src/forge/compiler/orchestrator/serialize.ts +886 -0
  283. package/src/forge/compiler/orchestrator/session.ts +96 -0
  284. package/src/forge/compiler/orchestrator/types.ts +31 -0
  285. package/src/forge/compiler/orchestrator/verify.ts +38 -0
  286. package/src/forge/compiler/orchestrator/workspace-index.ts +154 -0
  287. package/src/forge/compiler/package-graph/capabilities-stub.ts +33 -0
  288. package/src/forge/compiler/package-graph/checksum.ts +97 -0
  289. package/src/forge/compiler/package-graph/compiler.ts +392 -0
  290. package/src/forge/compiler/package-graph/constants.ts +4 -0
  291. package/src/forge/compiler/package-graph/dts-extractor.ts +142 -0
  292. package/src/forge/compiler/package-graph/exports-discovery.ts +84 -0
  293. package/src/forge/compiler/package-graph/extract-dts.ts +32 -0
  294. package/src/forge/compiler/package-graph/index.ts +33 -0
  295. package/src/forge/compiler/package-graph/jsdoc.ts +62 -0
  296. package/src/forge/compiler/package-graph/read-file.ts +21 -0
  297. package/src/forge/compiler/package-graph/resolve.ts +127 -0
  298. package/src/forge/compiler/package-manager/adapter.ts +237 -0
  299. package/src/forge/compiler/package-manager/bun-executable.ts +92 -0
  300. package/src/forge/compiler/package-manager/commands.ts +47 -0
  301. package/src/forge/compiler/package-manager/detect.ts +79 -0
  302. package/src/forge/compiler/package-manager/executor.ts +117 -0
  303. package/src/forge/compiler/package-manager/index.ts +22 -0
  304. package/src/forge/compiler/package-manager/parse-spec.ts +16 -0
  305. package/src/forge/compiler/package-manager/version.ts +27 -0
  306. package/src/forge/compiler/package-upgrades/apply.ts +195 -0
  307. package/src/forge/compiler/package-upgrades/comparator.ts +181 -0
  308. package/src/forge/compiler/package-upgrades/impact.ts +139 -0
  309. package/src/forge/compiler/package-upgrades/markdown.ts +97 -0
  310. package/src/forge/compiler/package-upgrades/planner.ts +532 -0
  311. package/src/forge/compiler/package-upgrades/risk.ts +208 -0
  312. package/src/forge/compiler/package-upgrades/types.ts +174 -0
  313. package/src/forge/compiler/policy-registry/build.ts +266 -0
  314. package/src/forge/compiler/policy-registry/constants.ts +2 -0
  315. package/src/forge/compiler/policy-registry/parse.ts +81 -0
  316. package/src/forge/compiler/primitives/compare.ts +26 -0
  317. package/src/forge/compiler/primitives/hash.ts +40 -0
  318. package/src/forge/compiler/primitives/header.ts +45 -0
  319. package/src/forge/compiler/primitives/index.ts +45 -0
  320. package/src/forge/compiler/primitives/paths.ts +24 -0
  321. package/src/forge/compiler/primitives/result.ts +164 -0
  322. package/src/forge/compiler/primitives/serialize.ts +66 -0
  323. package/src/forge/compiler/primitives/sort.ts +87 -0
  324. package/src/forge/compiler/query-registry/build.ts +114 -0
  325. package/src/forge/compiler/query-registry/constants.ts +2 -0
  326. package/src/forge/compiler/recipes/definitions.ts +289 -0
  327. package/src/forge/compiler/recipes/helpers.ts +37 -0
  328. package/src/forge/compiler/recipes/index.ts +21 -0
  329. package/src/forge/compiler/recipes/registry.ts +102 -0
  330. package/src/forge/compiler/release/build.ts +100 -0
  331. package/src/forge/compiler/release/types.ts +119 -0
  332. package/src/forge/compiler/runtime-graph/build.ts +137 -0
  333. package/src/forge/compiler/runtime-graph/constants.ts +5 -0
  334. package/src/forge/compiler/runtime-graph/index.ts +5 -0
  335. package/src/forge/compiler/sandbox/artifact-sanitize.ts +26 -0
  336. package/src/forge/compiler/sandbox/backends/child.ts +123 -0
  337. package/src/forge/compiler/sandbox/backends/docker.ts +173 -0
  338. package/src/forge/compiler/sandbox/index.ts +51 -0
  339. package/src/forge/compiler/sandbox/inspect.ts +143 -0
  340. package/src/forge/compiler/sandbox/inspector-entry.ts +115 -0
  341. package/src/forge/compiler/sandbox/limits.ts +31 -0
  342. package/src/forge/compiler/sandbox/scrub-env.ts +60 -0
  343. package/src/forge/compiler/sandbox/secret-scan.ts +54 -0
  344. package/src/forge/compiler/sandbox/serialize.ts +106 -0
  345. package/src/forge/compiler/sandbox/types.ts +7 -0
  346. package/src/forge/compiler/secret-registry/build.ts +123 -0
  347. package/src/forge/compiler/telemetry-registry/build.ts +89 -0
  348. package/src/forge/compiler/telemetry-registry/constants.ts +2 -0
  349. package/src/forge/compiler/telemetry-registry/parse.ts +13 -0
  350. package/src/forge/compiler/test-graph/build.ts +277 -0
  351. package/src/forge/compiler/types/action-subscriptions.ts +19 -0
  352. package/src/forge/compiler/types/ai-registry.ts +33 -0
  353. package/src/forge/compiler/types/app-graph.ts +80 -0
  354. package/src/forge/compiler/types/capability.ts +29 -0
  355. package/src/forge/compiler/types/classification.ts +9 -0
  356. package/src/forge/compiler/types/cli.ts +159 -0
  357. package/src/forge/compiler/types/data-graph.ts +24 -0
  358. package/src/forge/compiler/types/dev-manifest.ts +41 -0
  359. package/src/forge/compiler/types/diagnostic.ts +12 -0
  360. package/src/forge/compiler/types/emit.ts +25 -0
  361. package/src/forge/compiler/types/frontend-graph.ts +81 -0
  362. package/src/forge/compiler/types/import-guards.ts +19 -0
  363. package/src/forge/compiler/types/index.ts +98 -0
  364. package/src/forge/compiler/types/integration.ts +25 -0
  365. package/src/forge/compiler/types/json.ts +3 -0
  366. package/src/forge/compiler/types/live-query-registry.ts +32 -0
  367. package/src/forge/compiler/types/lock.ts +37 -0
  368. package/src/forge/compiler/types/package-graph.ts +84 -0
  369. package/src/forge/compiler/types/policy-registry.ts +69 -0
  370. package/src/forge/compiler/types/query-registry.ts +18 -0
  371. package/src/forge/compiler/types/runtime-graph.ts +30 -0
  372. package/src/forge/compiler/types/runtime-matrix.ts +16 -0
  373. package/src/forge/compiler/types/runtime.ts +30 -0
  374. package/src/forge/compiler/types/sandbox.ts +24 -0
  375. package/src/forge/compiler/types/secret-registry.ts +38 -0
  376. package/src/forge/compiler/types/telemetry-registry.ts +26 -0
  377. package/src/forge/compiler/types/test-graph.ts +45 -0
  378. package/src/forge/compiler/types/workflow-registry.ts +42 -0
  379. package/src/forge/compiler/workflow-registry/build.ts +180 -0
  380. package/src/forge/compiler/workflow-registry/constants.ts +2 -0
  381. package/src/forge/compiler/workflow-registry/index.ts +5 -0
  382. package/src/forge/compiler/workflow-registry/parse.ts +19 -0
  383. package/src/forge/dev/server.ts +1379 -0
  384. package/src/forge/dev/types.ts +49 -0
  385. package/src/forge/dev/watch.ts +109 -0
  386. package/src/forge/dev-console/cycle.ts +652 -0
  387. package/src/forge/dev-console/types.ts +99 -0
  388. package/src/forge/feature/compiler.ts +656 -0
  389. package/src/forge/feature/examples.ts +125 -0
  390. package/src/forge/feature/types.ts +177 -0
  391. package/src/forge/impact/index.ts +1160 -0
  392. package/src/forge/impact/types.ts +151 -0
  393. package/src/forge/intent/index.ts +490 -0
  394. package/src/forge/intent/types.ts +73 -0
  395. package/src/forge/make/fields.ts +146 -0
  396. package/src/forge/make/index.ts +1101 -0
  397. package/src/forge/make/naming.ts +42 -0
  398. package/src/forge/make/templates.ts +525 -0
  399. package/src/forge/make/types.ts +151 -0
  400. package/src/forge/platform/module.ts +20 -0
  401. package/src/forge/policy.ts +1 -0
  402. package/src/forge/react/index.ts +418 -0
  403. package/src/forge/refactor/index.ts +1936 -0
  404. package/src/forge/refactor/text-utils.ts +34 -0
  405. package/src/forge/refactor/types.ts +191 -0
  406. package/src/forge/refactor/workspace-fs.ts +171 -0
  407. package/src/forge/repair/index.ts +656 -0
  408. package/src/forge/repair/rules/index.ts +476 -0
  409. package/src/forge/repair/types.ts +175 -0
  410. package/src/forge/review/index.ts +992 -0
  411. package/src/forge/review/types.ts +196 -0
  412. package/src/forge/runtime/ai/check.ts +86 -0
  413. package/src/forge/runtime/ai/context.ts +394 -0
  414. package/src/forge/runtime/ai/cost-estimator.ts +41 -0
  415. package/src/forge/runtime/ai/mock.ts +49 -0
  416. package/src/forge/runtime/ai/providers.ts +78 -0
  417. package/src/forge/runtime/ai/state.ts +17 -0
  418. package/src/forge/runtime/ai/types.ts +67 -0
  419. package/src/forge/runtime/auth/authenticate.ts +58 -0
  420. package/src/forge/runtime/auth/claims.ts +119 -0
  421. package/src/forge/runtime/auth/config.ts +148 -0
  422. package/src/forge/runtime/auth/errors.ts +45 -0
  423. package/src/forge/runtime/auth/evaluate.ts +126 -0
  424. package/src/forge/runtime/auth/resolve.ts +74 -0
  425. package/src/forge/runtime/auth/types.ts +87 -0
  426. package/src/forge/runtime/auth/verifier.ts +138 -0
  427. package/src/forge/runtime/context/create-context.ts +204 -0
  428. package/src/forge/runtime/context/create-query-context.ts +34 -0
  429. package/src/forge/runtime/db/adapter.ts +31 -0
  430. package/src/forge/runtime/db/factory.ts +83 -0
  431. package/src/forge/runtime/db/generated-client.ts +294 -0
  432. package/src/forge/runtime/db/memory-adapter.ts +706 -0
  433. package/src/forge/runtime/db/migrate.ts +132 -0
  434. package/src/forge/runtime/db/outbox.ts +54 -0
  435. package/src/forge/runtime/db/pglite-adapter.ts +51 -0
  436. package/src/forge/runtime/db/postgres-adapter.ts +112 -0
  437. package/src/forge/runtime/db/read-only-client.ts +97 -0
  438. package/src/forge/runtime/db/session-context.ts +62 -0
  439. package/src/forge/runtime/executor.ts +446 -0
  440. package/src/forge/runtime/live/dependency-tracker.ts +57 -0
  441. package/src/forge/runtime/live/invalidation-log.ts +189 -0
  442. package/src/forge/runtime/live/live-query-runner.ts +267 -0
  443. package/src/forge/runtime/live/registry.ts +28 -0
  444. package/src/forge/runtime/live/sse.ts +75 -0
  445. package/src/forge/runtime/live/subscription-manager.ts +443 -0
  446. package/src/forge/runtime/live/types.ts +143 -0
  447. package/src/forge/runtime/outbox/claim.ts +153 -0
  448. package/src/forge/runtime/outbox/process.ts +298 -0
  449. package/src/forge/runtime/outbox/retry.ts +8 -0
  450. package/src/forge/runtime/outbox/subscriptions.ts +33 -0
  451. package/src/forge/runtime/outbox/types.ts +69 -0
  452. package/src/forge/runtime/policy/check.ts +157 -0
  453. package/src/forge/runtime/policy/load.ts +55 -0
  454. package/src/forge/runtime/query/registry.ts +19 -0
  455. package/src/forge/runtime/query/run-query.ts +347 -0
  456. package/src/forge/runtime/release/runtime.ts +322 -0
  457. package/src/forge/runtime/release/symbolicate.ts +175 -0
  458. package/src/forge/runtime/runner/command-transaction.ts +193 -0
  459. package/src/forge/runtime/runner/run-entry.ts +226 -0
  460. package/src/forge/runtime/secrets/check.ts +78 -0
  461. package/src/forge/runtime/secrets/create-context.ts +138 -0
  462. package/src/forge/runtime/secrets/env-loader.ts +94 -0
  463. package/src/forge/runtime/secrets/runtime-bundle.ts +47 -0
  464. package/src/forge/runtime/secrets/types.ts +31 -0
  465. package/src/forge/runtime/telemetry/buffer.ts +87 -0
  466. package/src/forge/runtime/telemetry/context.ts +192 -0
  467. package/src/forge/runtime/telemetry/correlation.ts +13 -0
  468. package/src/forge/runtime/telemetry/flush.ts +190 -0
  469. package/src/forge/runtime/telemetry/process.ts +20 -0
  470. package/src/forge/runtime/telemetry/scrubber.ts +115 -0
  471. package/src/forge/runtime/telemetry/sinks/local-jsonl.ts +39 -0
  472. package/src/forge/runtime/telemetry/sinks/posthog.ts +64 -0
  473. package/src/forge/runtime/telemetry/sinks/sentry.ts +60 -0
  474. package/src/forge/runtime/telemetry/spans.ts +58 -0
  475. package/src/forge/runtime/telemetry/types.ts +64 -0
  476. package/src/forge/runtime/workflows/cancel.ts +26 -0
  477. package/src/forge/runtime/workflows/create-run.ts +98 -0
  478. package/src/forge/runtime/workflows/process-run.ts +182 -0
  479. package/src/forge/runtime/workflows/process-step.ts +190 -0
  480. package/src/forge/runtime/workflows/process.ts +260 -0
  481. package/src/forge/runtime/workflows/registry.ts +51 -0
  482. package/src/forge/runtime/workflows/resolve-step.ts +46 -0
  483. package/src/forge/runtime/workflows/retry-run.ts +44 -0
  484. package/src/forge/runtime/workflows/retry.ts +8 -0
  485. package/src/forge/runtime/workflows/sanitize.ts +19 -0
  486. package/src/forge/runtime/workflows/start-from-outbox.ts +71 -0
  487. package/src/forge/runtime/workflows/types.ts +77 -0
  488. package/src/forge/server.ts +96 -0
  489. package/src/forge/ui/index.ts +770 -0
  490. package/src/forge/ui/types.ts +191 -0
  491. package/templates/b2b-support-web/.env.example +22 -0
  492. package/templates/b2b-support-web/.vscode/settings.json +14 -0
  493. package/templates/b2b-support-web/AGENTS.md +108 -0
  494. package/templates/b2b-support-web/README.md +48 -0
  495. package/templates/b2b-support-web/forge.config.ts +3 -0
  496. package/templates/b2b-support-web/package.json +34 -0
  497. package/templates/b2b-support-web/src/actions/captureTicketCreated.ts +14 -0
  498. package/templates/b2b-support-web/src/commands/closeTicket.ts +20 -0
  499. package/templates/b2b-support-web/src/commands/createTicket.ts +47 -0
  500. package/templates/b2b-support-web/src/commands/manageBilling.ts +9 -0
  501. package/templates/b2b-support-web/src/forge/schema.ts +35 -0
  502. package/templates/b2b-support-web/src/policies.ts +9 -0
  503. package/templates/b2b-support-web/src/queries/getTicket.ts +6 -0
  504. package/templates/b2b-support-web/src/queries/listTickets.ts +6 -0
  505. package/templates/b2b-support-web/src/queries/liveTickets.ts +9 -0
  506. package/templates/b2b-support-web/src/workflows/triageTicketWorkflow.ts +64 -0
  507. package/templates/b2b-support-web/tsconfig.json +14 -0
  508. package/templates/b2b-support-web/web/app/globals.css +77 -0
  509. package/templates/b2b-support-web/web/app/layout.tsx +13 -0
  510. package/templates/b2b-support-web/web/app/page.tsx +13 -0
  511. package/templates/b2b-support-web/web/app/providers.tsx +21 -0
  512. package/templates/b2b-support-web/web/app/tickets/page.tsx +21 -0
  513. package/templates/b2b-support-web/web/components/CreateTicketForm.tsx +43 -0
  514. package/templates/b2b-support-web/web/components/PolicyDeniedDemo.tsx +31 -0
  515. package/templates/b2b-support-web/web/components/TicketList.tsx +52 -0
  516. package/templates/b2b-support-web/web/components/TraceDetails.tsx +18 -0
  517. package/templates/b2b-support-web/web/components/TriageStatus.tsx +13 -0
  518. package/templates/b2b-support-web/web/lib/forge.ts +13 -0
  519. package/templates/b2b-support-web/web/next-env.d.ts +5 -0
  520. package/templates/b2b-support-web/web/next.config.ts +8 -0
  521. package/templates/b2b-support-web/web/package.json +21 -0
  522. package/templates/b2b-support-web/web/tsconfig.json +30 -0
  523. package/templates/minimal-web/.vscode/settings.json +14 -0
  524. package/templates/minimal-web/README.md +21 -0
  525. package/templates/minimal-web/forge.config.ts +3 -0
  526. package/templates/minimal-web/package.json +32 -0
  527. package/templates/minimal-web/src/actions/logNoteCreated.ts +11 -0
  528. package/templates/minimal-web/src/commands/createNote.ts +26 -0
  529. package/templates/minimal-web/src/forge/schema.ts +12 -0
  530. package/templates/minimal-web/src/policies.ts +6 -0
  531. package/templates/minimal-web/src/queries/listNotes.ts +8 -0
  532. package/templates/minimal-web/src/queries/liveNotes.ts +8 -0
  533. package/templates/minimal-web/tsconfig.json +15 -0
  534. package/templates/minimal-web/web/index.html +12 -0
  535. package/templates/minimal-web/web/package.json +21 -0
  536. package/templates/minimal-web/web/src/App.tsx +89 -0
  537. package/templates/minimal-web/web/src/lib/forge.ts +13 -0
  538. package/templates/minimal-web/web/src/main.tsx +13 -0
  539. package/templates/minimal-web/web/src/styles.css +156 -0
  540. package/templates/minimal-web/web/tsconfig.json +18 -0
@@ -0,0 +1,1936 @@
1
+ import { join } from "node:path";
2
+ import ts from "typescript";
3
+ import { createDiagnostic } from "../compiler/diagnostics/create.ts";
4
+ import { GENERATOR_VERSION } from "../compiler/emitter/constants.ts";
5
+ import { hashStable } from "../compiler/primitives/hash.ts";
6
+ import { canonicalJson, serializeCanonical } from "../compiler/primitives/serialize.ts";
7
+ import type { Diagnostic } from "../compiler/types/diagnostic.ts";
8
+ import type { PlannedFile, PlannedPatch } from "../make/types.ts";
9
+ import type {
10
+ RefactorCommandOptions,
11
+ RefactorImpact,
12
+ RefactorIntent,
13
+ RefactorPlan,
14
+ RefactorRecord,
15
+ RefactorResult,
16
+ } from "./types.ts";
17
+ import {
18
+ isGenerated,
19
+ makeFile,
20
+ makePatchFromContent,
21
+ patchFile,
22
+ readDirEntries,
23
+ readText,
24
+ removeFile,
25
+ walkFiles,
26
+ writeText,
27
+ } from "./workspace-fs.ts";
28
+ import {
29
+ parseTableField,
30
+ pushUnique,
31
+ wordReplace,
32
+ } from "./text-utils.ts";
33
+
34
+ const REFACTOR_DIR = ".forge/refactors";
35
+
36
+ interface SnapshotFile {
37
+ file: string;
38
+ existed: boolean;
39
+ content?: string;
40
+ }
41
+
42
+ interface RefactorSnapshot {
43
+ schemaVersion: "0.1.0";
44
+ id: string;
45
+ files: SnapshotFile[];
46
+ }
47
+
48
+ function diagnostic(
49
+ severity: Diagnostic["severity"],
50
+ code: string,
51
+ message: string,
52
+ file?: string,
53
+ ): Diagnostic {
54
+ return createDiagnostic({
55
+ severity,
56
+ code,
57
+ message,
58
+ ...(file ? { file } : {}),
59
+ });
60
+ }
61
+
62
+ function emptyImpact(): RefactorImpact {
63
+ return {
64
+ data: { tables: [], fields: [], refs: [], indexes: [], rlsPolicies: [] },
65
+ runtime: { commands: [], queries: [], liveQueries: [], actions: [], workflows: [], endpoints: [] },
66
+ frontend: { components: [], pages: [], hooks: [] },
67
+ policies: [],
68
+ tests: [],
69
+ blueprints: [],
70
+ generatedArtifacts: [
71
+ "src/forge/_generated/appGraph.json",
72
+ "src/forge/_generated/dataGraph.json",
73
+ "src/forge/_generated/api.json",
74
+ "src/forge/_generated/agentContract.json",
75
+ ],
76
+ };
77
+ }
78
+
79
+ function inferIntent(options: RefactorCommandOptions): {
80
+ intent?: RefactorIntent;
81
+ diagnostics: Diagnostic[];
82
+ } {
83
+ const diagnostics: Diagnostic[] = [];
84
+ if (options.action === "rename") {
85
+ if (options.renameTarget === "field") {
86
+ const from = parseTableField(options.from);
87
+ const to = parseTableField(options.to);
88
+ if (!from || !to || from.table !== to.table) {
89
+ diagnostics.push(
90
+ diagnostic(
91
+ "error",
92
+ "FORGE_REFACTOR_TARGET_NOT_FOUND",
93
+ "rename field requires <table.field> <sameTable.field>",
94
+ ),
95
+ );
96
+ return { diagnostics };
97
+ }
98
+ return {
99
+ diagnostics,
100
+ intent: {
101
+ kind: "renameField",
102
+ table: from.table,
103
+ from: { field: from.field },
104
+ to: { field: to.field },
105
+ updateBlueprints: true,
106
+ updateFrontend: true,
107
+ updateTests: true,
108
+ },
109
+ };
110
+ }
111
+ if (options.renameTarget === "table") {
112
+ if (!options.from || !options.to) {
113
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "rename table requires <from> <to>"));
114
+ return { diagnostics };
115
+ }
116
+ return {
117
+ diagnostics,
118
+ intent: {
119
+ kind: "renameTable",
120
+ from: { table: options.from },
121
+ to: { table: options.to },
122
+ updateFrontend: true,
123
+ updatePolicies: true,
124
+ updateRefs: true,
125
+ updateRuntimeEntries: true,
126
+ },
127
+ };
128
+ }
129
+ if (options.renameTarget === "policy") {
130
+ if (!options.from || !options.to) {
131
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "rename policy requires <from> <to>"));
132
+ return { diagnostics };
133
+ }
134
+ return { diagnostics, intent: { kind: "renamePolicy", from: options.from, to: options.to } };
135
+ }
136
+ if (options.renameTarget === "event") {
137
+ if (!options.from || !options.to) {
138
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "rename event requires <from> <to>"));
139
+ return { diagnostics };
140
+ }
141
+ return { diagnostics, intent: { kind: "renameEvent", from: options.from, to: options.to } };
142
+ }
143
+ const runtimeMap = {
144
+ command: "command",
145
+ query: "query",
146
+ livequery: "liveQuery",
147
+ action: "action",
148
+ workflow: "workflow",
149
+ } as const;
150
+ const entryKind = options.renameTarget ? runtimeMap[options.renameTarget as keyof typeof runtimeMap] : undefined;
151
+ if (entryKind) {
152
+ if (!options.from || !options.to) {
153
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", `rename ${options.renameTarget} requires <from> <to>`));
154
+ return { diagnostics };
155
+ }
156
+ return {
157
+ diagnostics,
158
+ intent: {
159
+ kind: "renameRuntimeEntry",
160
+ entryKind,
161
+ from: options.from,
162
+ to: options.to,
163
+ updateApi: true,
164
+ updateClient: true,
165
+ updateFrontend: true,
166
+ updateTests: true,
167
+ },
168
+ };
169
+ }
170
+ }
171
+ if (options.action === "move" && options.renameTarget === "field") {
172
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "unsupported move target"));
173
+ }
174
+ if (options.action === "move" && options.componentName && options.to) {
175
+ return {
176
+ diagnostics,
177
+ intent: { kind: "moveComponent", name: options.componentName, toPath: options.to },
178
+ };
179
+ }
180
+ if (options.action === "extract-action") {
181
+ if (!options.from || !options.packageName) {
182
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "extract-action requires command and --package"));
183
+ return { diagnostics };
184
+ }
185
+ const eventName = options.eventName ?? `${options.from}.requested`;
186
+ return {
187
+ diagnostics,
188
+ intent: {
189
+ kind: "extractAction",
190
+ command: options.from,
191
+ packageName: options.packageName,
192
+ eventName,
193
+ actionName: options.actionName ?? `${options.from}Action`,
194
+ createAction: true,
195
+ createEventPayload: true,
196
+ removeForbiddenImport: true,
197
+ },
198
+ };
199
+ }
200
+ if (options.action === "replace-process-env") {
201
+ if (!options.from) {
202
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "replace-process-env requires an env var"));
203
+ return { diagnostics };
204
+ }
205
+ return {
206
+ diagnostics,
207
+ intent: { kind: "replaceProcessEnv", name: options.from, replacement: "ctx.secrets" },
208
+ };
209
+ }
210
+ if (options.action === "replace-import") {
211
+ if (!options.from || !options.to) {
212
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "replace-import requires <from> <to>"));
213
+ return { diagnostics };
214
+ }
215
+ return { diagnostics, intent: { kind: "replaceImport", from: options.from, to: options.to } };
216
+ }
217
+ return {
218
+ diagnostics: [
219
+ diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "unsupported refactor intent"),
220
+ ],
221
+ };
222
+ }
223
+
224
+ function findCommandFile(workspaceRoot: string, command: string): string | null {
225
+ for (const file of walkFiles(workspaceRoot)) {
226
+ if (!file.startsWith("src/commands/")) {
227
+ continue;
228
+ }
229
+ const content = readText(workspaceRoot, file) ?? "";
230
+ if (content.includes(`export const ${command}`)) {
231
+ return file;
232
+ }
233
+ }
234
+ return null;
235
+ }
236
+
237
+ function propertyNameText(name: ts.PropertyName): string | null {
238
+ if (ts.isIdentifier(name) || ts.isStringLiteral(name) || ts.isNumericLiteral(name)) {
239
+ return name.text;
240
+ }
241
+ return null;
242
+ }
243
+
244
+ function isCommandCall(node: ts.Node): node is ts.CallExpression {
245
+ return ts.isCallExpression(node) &&
246
+ ((ts.isIdentifier(node.expression) && node.expression.text === "command") ||
247
+ (ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "command"));
248
+ }
249
+
250
+ function findCommandObject(sourceFile: ts.SourceFile, commandName: string): ts.ObjectLiteralExpression | null {
251
+ let found: ts.ObjectLiteralExpression | null = null;
252
+
253
+ function visit(node: ts.Node): void {
254
+ if (found) {
255
+ return;
256
+ }
257
+ if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === commandName) {
258
+ const initializer = node.initializer;
259
+ if (initializer && isCommandCall(initializer) && ts.isObjectLiteralExpression(initializer.arguments[0])) {
260
+ found = initializer.arguments[0];
261
+ return;
262
+ }
263
+ }
264
+ ts.forEachChild(node, visit);
265
+ }
266
+
267
+ visit(sourceFile);
268
+ return found;
269
+ }
270
+
271
+ function findHandlerProperty(commandObject: ts.ObjectLiteralExpression): ts.PropertyAssignment | null {
272
+ for (const property of commandObject.properties) {
273
+ if (!ts.isPropertyAssignment(property)) {
274
+ continue;
275
+ }
276
+ if (propertyNameText(property.name) === "handler") {
277
+ return property;
278
+ }
279
+ }
280
+ return null;
281
+ }
282
+
283
+ function packageNameFromSpecifier(specifier: string): string {
284
+ if (specifier.startsWith("@")) {
285
+ const [scope, name] = specifier.split("/");
286
+ return scope && name ? `${scope}/${name}` : specifier;
287
+ }
288
+ return specifier.split("/")[0] ?? specifier;
289
+ }
290
+
291
+ function isPackageImportDeclaration(statement: ts.Statement, packageName: string): statement is ts.ImportDeclaration {
292
+ return ts.isImportDeclaration(statement) &&
293
+ ts.isStringLiteral(statement.moduleSpecifier) &&
294
+ packageNameFromSpecifier(statement.moduleSpecifier.text) === packageName;
295
+ }
296
+
297
+ function isValueImportDeclarationForPackage(statement: ts.Statement, packageName: string): statement is ts.ImportDeclaration {
298
+ if (!isPackageImportDeclaration(statement, packageName)) {
299
+ return false;
300
+ }
301
+ const clause = statement.importClause;
302
+ if (!clause) {
303
+ return true;
304
+ }
305
+ if (clause.isTypeOnly) {
306
+ return false;
307
+ }
308
+ if (clause.name || (clause.namedBindings && ts.isNamespaceImport(clause.namedBindings))) {
309
+ return true;
310
+ }
311
+ return Boolean(
312
+ clause.namedBindings &&
313
+ ts.isNamedImports(clause.namedBindings) &&
314
+ clause.namedBindings.elements.some((element) => !element.isTypeOnly),
315
+ );
316
+ }
317
+
318
+ function removeImportForPackage(
319
+ sourceFile: ts.SourceFile,
320
+ source: string,
321
+ packageName: string,
322
+ ): { spans: Array<{ start: number; end: number }>; found: boolean } {
323
+ const spans: Array<{ start: number; end: number }> = [];
324
+ for (const statement of sourceFile.statements) {
325
+ if (!isValueImportDeclarationForPackage(statement, packageName)) {
326
+ continue;
327
+ }
328
+ let end = statement.getEnd();
329
+ if (source.slice(end, end + 2) === "\r\n") {
330
+ end += 2;
331
+ } else if (source[end] === "\n") {
332
+ end += 1;
333
+ }
334
+ spans.push({ start: statement.getStart(sourceFile), end });
335
+ }
336
+ return { spans, found: spans.length > 0 };
337
+ }
338
+
339
+ function sideEffectImportDiagnostics(
340
+ sourceFile: ts.SourceFile,
341
+ commandFile: string,
342
+ packageName: string,
343
+ ): Diagnostic[] {
344
+ const sideEffectImports = sourceFile.statements.filter(
345
+ (statement) => isPackageImportDeclaration(statement, packageName) && !statement.importClause,
346
+ );
347
+ if (sideEffectImports.length === 0) {
348
+ return [];
349
+ }
350
+ return [
351
+ createDiagnostic({
352
+ severity: "error",
353
+ code: "FORGE_REFACTOR_PATCH_UNSAFE",
354
+ message: `package '${packageName}' has side-effect import(s) in ${commandFile}`,
355
+ file: commandFile,
356
+ fixHint: "Move or remove side-effect imports manually before running extract-action; they cannot be proven local to a command handler.",
357
+ }),
358
+ ];
359
+ }
360
+
361
+ function typeOnlyImportFromMixedPackageImport(
362
+ statement: ts.ImportDeclaration,
363
+ packageName: string,
364
+ ): ts.ImportDeclaration | undefined {
365
+ if (!isPackageImportDeclaration(statement, packageName)) {
366
+ return statement;
367
+ }
368
+ const clause = statement.importClause;
369
+ if (!clause) {
370
+ return undefined;
371
+ }
372
+ if (clause.isTypeOnly) {
373
+ return statement;
374
+ }
375
+ if (!clause.namedBindings || !ts.isNamedImports(clause.namedBindings)) {
376
+ return undefined;
377
+ }
378
+ const typeElements = clause.namedBindings.elements
379
+ .filter((element) => element.isTypeOnly)
380
+ .map((element) => ts.factory.createImportSpecifier(
381
+ false,
382
+ element.propertyName ? ts.factory.createIdentifier(element.propertyName.text) : undefined,
383
+ ts.factory.createIdentifier(element.name.text),
384
+ ));
385
+ if (typeElements.length === 0) {
386
+ return undefined;
387
+ }
388
+ return ts.factory.updateImportDeclaration(
389
+ statement,
390
+ statement.modifiers,
391
+ ts.factory.createImportClause(
392
+ true,
393
+ undefined,
394
+ ts.factory.createNamedImports(typeElements),
395
+ ),
396
+ statement.moduleSpecifier,
397
+ statement.attributes,
398
+ );
399
+ }
400
+
401
+ interface ImportBinding {
402
+ name: string;
403
+ node: ts.Identifier;
404
+ symbol: ts.Symbol;
405
+ }
406
+
407
+ function createCommandProgram(
408
+ workspaceRoot: string,
409
+ commandFile: string,
410
+ source: string,
411
+ ): { sourceFile: ts.SourceFile; checker: ts.TypeChecker } {
412
+ const absoluteFile = join(workspaceRoot, commandFile);
413
+ const options: ts.CompilerOptions = {
414
+ allowImportingTsExtensions: true,
415
+ esModuleInterop: true,
416
+ jsx: ts.JsxEmit.ReactJSX,
417
+ module: ts.ModuleKind.ESNext,
418
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
419
+ noEmit: true,
420
+ skipLibCheck: true,
421
+ strict: true,
422
+ target: ts.ScriptTarget.ES2022,
423
+ };
424
+ const host = ts.createCompilerHost(options, true);
425
+ const originalReadFile = host.readFile.bind(host);
426
+ host.readFile = (fileName) => fileName === absoluteFile ? source : originalReadFile(fileName);
427
+ const originalFileExists = host.fileExists.bind(host);
428
+ host.fileExists = (fileName) => fileName === absoluteFile || originalFileExists(fileName);
429
+
430
+ const program = ts.createProgram([absoluteFile], options, host);
431
+ const sourceFile = program.getSourceFile(absoluteFile);
432
+ if (!sourceFile) {
433
+ throw new Error(`Unable to create TypeScript source file for ${commandFile}`);
434
+ }
435
+ return { sourceFile, checker: program.getTypeChecker() };
436
+ }
437
+
438
+ function bindingSymbol(checker: ts.TypeChecker, node: ts.Identifier): ts.Symbol | null {
439
+ const symbol = checker.getSymbolAtLocation(node);
440
+ if (!symbol) {
441
+ return null;
442
+ }
443
+ if ((symbol.flags & ts.SymbolFlags.Alias) !== 0) {
444
+ return checker.getAliasedSymbol(symbol);
445
+ }
446
+ return symbol;
447
+ }
448
+
449
+ function importBindingsForPackage(
450
+ sourceFile: ts.SourceFile,
451
+ checker: ts.TypeChecker,
452
+ packageName: string,
453
+ ): ImportBinding[] {
454
+ const bindings: ImportBinding[] = [];
455
+ const seen = new Set<string>();
456
+ const pushBinding = (node: ts.Identifier): void => {
457
+ const symbol = bindingSymbol(checker, node);
458
+ const key = symbol ? `${node.text}:${symbol.escapedName.toString()}` : node.text;
459
+ if (!symbol || seen.has(key)) {
460
+ return;
461
+ }
462
+ bindings.push({ name: node.text, node, symbol });
463
+ seen.add(key);
464
+ };
465
+
466
+ for (const statement of sourceFile.statements) {
467
+ if (!isPackageImportDeclaration(statement, packageName)) {
468
+ continue;
469
+ }
470
+ const clause = statement.importClause;
471
+ if (!clause || clause.isTypeOnly) {
472
+ continue;
473
+ }
474
+ if (clause.name) {
475
+ pushBinding(clause.name);
476
+ }
477
+ if (clause.namedBindings && ts.isNamespaceImport(clause.namedBindings)) {
478
+ pushBinding(clause.namedBindings.name);
479
+ }
480
+ if (clause.namedBindings && ts.isNamedImports(clause.namedBindings)) {
481
+ for (const element of clause.namedBindings.elements) {
482
+ if (element.isTypeOnly) {
483
+ continue;
484
+ }
485
+ pushBinding(element.name);
486
+ }
487
+ }
488
+ }
489
+ return bindings.sort((a, b) => a.name.localeCompare(b.name));
490
+ }
491
+
492
+ function isImportIdentifier(node: ts.Identifier): boolean {
493
+ return ts.isImportClause(node.parent) ||
494
+ ts.isNamespaceImport(node.parent) ||
495
+ ts.isImportSpecifier(node.parent);
496
+ }
497
+
498
+ function isPropertyNameIdentifier(node: ts.Identifier): boolean {
499
+ return (ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) ||
500
+ (ts.isPropertyAssignment(node.parent) && node.parent.name === node) ||
501
+ (ts.isShorthandPropertyAssignment(node.parent) && node.parent.name === node) ||
502
+ (ts.isBindingElement(node.parent) && node.parent.propertyName === node);
503
+ }
504
+
505
+ function isTypePosition(node: ts.Node): boolean {
506
+ let current: ts.Node = node;
507
+ while (current.parent) {
508
+ current = current.parent;
509
+ if (ts.isTypeNode(current) || ts.isInterfaceDeclaration(current) || ts.isTypeAliasDeclaration(current)) {
510
+ return true;
511
+ }
512
+ if (
513
+ ts.isExpressionStatement(current) ||
514
+ ts.isCallExpression(current) ||
515
+ ts.isNewExpression(current) ||
516
+ ts.isVariableDeclaration(current) ||
517
+ ts.isReturnStatement(current) ||
518
+ ts.isPropertyAccessExpression(current)
519
+ ) {
520
+ return false;
521
+ }
522
+ }
523
+ return false;
524
+ }
525
+
526
+ function sameSymbol(checker: ts.TypeChecker, reference: ts.Identifier, binding: ImportBinding): boolean {
527
+ if (reference.text !== binding.name || reference === binding.node) {
528
+ return false;
529
+ }
530
+ const referenceSymbol = bindingSymbol(checker, reference);
531
+ return referenceSymbol === binding.symbol;
532
+ }
533
+
534
+ function importReferencesInNode(
535
+ node: ts.Node,
536
+ checker: ts.TypeChecker,
537
+ bindings: ImportBinding[],
538
+ ): ts.Identifier[] {
539
+ const references: ts.Identifier[] = [];
540
+ function visit(current: ts.Node): void {
541
+ if (
542
+ ts.isIdentifier(current) &&
543
+ !isImportIdentifier(current) &&
544
+ !isPropertyNameIdentifier(current) &&
545
+ !isTypePosition(current) &&
546
+ bindings.some((binding) => sameSymbol(checker, current, binding))
547
+ ) {
548
+ references.push(current);
549
+ }
550
+ ts.forEachChild(current, visit);
551
+ }
552
+ visit(node);
553
+ return references;
554
+ }
555
+
556
+ function importedPackageUsageDiagnostics(
557
+ sourceFile: ts.SourceFile,
558
+ checker: ts.TypeChecker,
559
+ commandFile: string,
560
+ commandName: string,
561
+ packageName: string,
562
+ handler: ts.FunctionExpression | ts.ArrowFunction,
563
+ ): Diagnostic[] {
564
+ const bindings = importBindingsForPackage(sourceFile, checker, packageName);
565
+ if (bindings.length === 0) {
566
+ return [
567
+ createDiagnostic({
568
+ severity: "error",
569
+ code: "FORGE_REFACTOR_PATCH_UNSAFE",
570
+ message: `package import '${packageName}' has no value binding to analyze in ${commandFile}`,
571
+ file: commandFile,
572
+ fixHint: "Use extract-action only for direct value imports used by the command handler. Side-effect imports and type-only imports require a manual refactor.",
573
+ }),
574
+ ];
575
+ }
576
+ const handlerStart = handler.getFullStart();
577
+ const handlerEnd = handler.getEnd();
578
+ const handlerReferences = importReferencesInNode(handler.body, checker, bindings);
579
+ if (handlerReferences.length === 0) {
580
+ return [
581
+ createDiagnostic({
582
+ severity: "error",
583
+ code: "FORGE_REFACTOR_PATCH_UNSAFE",
584
+ message: `package import '${packageName}' is not referenced inside ${commandFile} handler`,
585
+ file: commandFile,
586
+ fixHint: "Run extract-action only when the forbidden package usage is local to the command handler being extracted.",
587
+ }),
588
+ ];
589
+ }
590
+
591
+ const outsideReferences = importReferencesInNode(sourceFile, checker, bindings)
592
+ .filter((reference) => reference.getStart(sourceFile) < handlerStart || reference.getStart(sourceFile) > handlerEnd);
593
+ if (outsideReferences.length === 0) {
594
+ return [];
595
+ }
596
+
597
+ const lines = outsideReferences.slice(0, 3).map((reference) => {
598
+ const position = sourceFile.getLineAndCharacterOfPosition(reference.getStart(sourceFile));
599
+ return `${reference.text}@${position.line + 1}:${position.character + 1}`;
600
+ });
601
+ return [
602
+ createDiagnostic({
603
+ severity: "error",
604
+ code: "FORGE_REFACTOR_PATCH_UNSAFE",
605
+ message: `package import '${packageName}' is referenced outside the extracted handler: ${lines.join(", ")}`,
606
+ file: commandFile,
607
+ fixHint: "Move non-handler package usage first, or extract a smaller helper manually before running extract-action.",
608
+ suggestedCommands: [`forge refactor extract-action ${commandName} --package ${packageName} --dry-run --json`],
609
+ }),
610
+ ];
611
+ }
612
+
613
+ function createExtractedHandlerBody(
614
+ ctxName: string,
615
+ inputName: string | null,
616
+ eventName: string,
617
+ ): ts.Block {
618
+ const payload = inputName
619
+ ? ts.factory.createIdentifier(inputName)
620
+ : ts.factory.createObjectLiteralExpression([], false);
621
+ return ts.factory.createBlock([
622
+ ts.factory.createExpressionStatement(
623
+ ts.factory.createAwaitExpression(
624
+ ts.factory.createCallExpression(
625
+ ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(ctxName), "emit"),
626
+ undefined,
627
+ [ts.factory.createStringLiteral(eventName), payload],
628
+ ),
629
+ ),
630
+ ),
631
+ ts.factory.createReturnStatement(
632
+ ts.factory.createObjectLiteralExpression([
633
+ ts.factory.createPropertyAssignment("emitted", ts.factory.createStringLiteral(eventName)),
634
+ ], false),
635
+ ),
636
+ ], true);
637
+ }
638
+
639
+ function rewriteExtractActionSource(
640
+ sourceFile: ts.SourceFile,
641
+ handler: ts.FunctionExpression | ts.ArrowFunction,
642
+ ctxName: string,
643
+ inputName: string | null,
644
+ intent: Extract<RefactorIntent, { kind: "extractAction" }>,
645
+ ): string {
646
+ const newBody = createExtractedHandlerBody(ctxName, inputName, intent.eventName);
647
+ const transform: ts.TransformerFactory<ts.SourceFile> = (context) => {
648
+ const visit: ts.Visitor = (node) => {
649
+ if (ts.isImportDeclaration(node) && isPackageImportDeclaration(node, intent.packageName)) {
650
+ return typeOnlyImportFromMixedPackageImport(node, intent.packageName);
651
+ }
652
+ if (node === handler) {
653
+ if (ts.isArrowFunction(node)) {
654
+ return ts.factory.updateArrowFunction(
655
+ node,
656
+ node.modifiers,
657
+ node.typeParameters,
658
+ node.parameters,
659
+ node.type,
660
+ node.equalsGreaterThanToken,
661
+ newBody,
662
+ );
663
+ }
664
+ if (ts.isFunctionExpression(node)) {
665
+ return ts.factory.updateFunctionExpression(
666
+ node,
667
+ node.modifiers,
668
+ node.asteriskToken,
669
+ node.name,
670
+ node.typeParameters,
671
+ node.parameters,
672
+ node.type,
673
+ newBody,
674
+ );
675
+ }
676
+ }
677
+ return ts.visitEachChild(node, visit, context);
678
+ };
679
+ return (node) => ts.visitNode(node, visit) as ts.SourceFile;
680
+ };
681
+ const result = ts.transform(sourceFile, [transform]);
682
+ try {
683
+ const transformed = result.transformed[0];
684
+ const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
685
+ return printer.printFile(transformed);
686
+ } finally {
687
+ result.dispose();
688
+ }
689
+ }
690
+
691
+ function rewriteExtractActionCommand(
692
+ workspaceRoot: string,
693
+ source: string,
694
+ commandFile: string,
695
+ intent: Extract<RefactorIntent, { kind: "extractAction" }>,
696
+ ): { after?: string; diagnostics: Diagnostic[] } {
697
+ const { sourceFile, checker } = createCommandProgram(workspaceRoot, commandFile, source);
698
+ const commandObject = findCommandObject(sourceFile, intent.command);
699
+ if (!commandObject) {
700
+ return {
701
+ diagnostics: [
702
+ createDiagnostic({
703
+ severity: "error",
704
+ code: "FORGE_REFACTOR_PATCH_UNSAFE",
705
+ message: `could not find command(${intent.command}) object literal for AST rewrite`,
706
+ file: commandFile,
707
+ fixHint: "Ensure the command is declared as `export const name = command({ handler: ... })`, then re-run extract-action.",
708
+ suggestedCommands: [`forge refactor extract-action ${intent.command} --package ${intent.packageName} --dry-run --json`],
709
+ }),
710
+ ],
711
+ };
712
+ }
713
+
714
+ const handlerProperty = findHandlerProperty(commandObject);
715
+ if (!handlerProperty) {
716
+ return {
717
+ diagnostics: [
718
+ diagnostic("error", "FORGE_REFACTOR_PATCH_UNSAFE", `command '${intent.command}' has no handler property`, commandFile),
719
+ ],
720
+ };
721
+ }
722
+
723
+ const handler = handlerProperty.initializer;
724
+ if (!ts.isArrowFunction(handler) && !ts.isFunctionExpression(handler)) {
725
+ return {
726
+ diagnostics: [
727
+ diagnostic("error", "FORGE_REFACTOR_PATCH_UNSAFE", `command '${intent.command}' handler is not a function expression`, commandFile),
728
+ ],
729
+ };
730
+ }
731
+ if (!ts.isBlock(handler.body)) {
732
+ return {
733
+ diagnostics: [
734
+ diagnostic("error", "FORGE_REFACTOR_PATCH_UNSAFE", `command '${intent.command}' handler must use a block body`, commandFile),
735
+ ],
736
+ };
737
+ }
738
+ const ctxParam = handler.parameters[0]?.name;
739
+ if (!ctxParam || !ts.isIdentifier(ctxParam)) {
740
+ return {
741
+ diagnostics: [
742
+ diagnostic("error", "FORGE_REFACTOR_PATCH_UNSAFE", `command '${intent.command}' handler must have an identifier ctx parameter`, commandFile),
743
+ ],
744
+ };
745
+ }
746
+ const inputParam = handler.parameters[1]?.name;
747
+ if (inputParam && !ts.isIdentifier(inputParam)) {
748
+ return {
749
+ diagnostics: [
750
+ diagnostic("error", "FORGE_REFACTOR_PATCH_UNSAFE", `command '${intent.command}' input parameter must be an identifier`, commandFile),
751
+ ],
752
+ };
753
+ }
754
+
755
+ const imports = removeImportForPackage(sourceFile, source, intent.packageName);
756
+ if (!imports.found) {
757
+ return {
758
+ diagnostics: [
759
+ createDiagnostic({
760
+ severity: "error",
761
+ code: "FORGE_REFACTOR_TARGET_NOT_FOUND",
762
+ message: `package import '${intent.packageName}' was not found in ${commandFile}`,
763
+ file: commandFile,
764
+ fixHint: "extract-action currently rewrites direct imports in the command file. Move the forbidden import into the command or extract the helper manually first.",
765
+ suggestedCommands: ["forge inspect runtime-matrix --json", "forge repair diagnose --diagnostic FORGE_GUARD_VIOLATION --json"],
766
+ }),
767
+ ],
768
+ };
769
+ }
770
+
771
+ const sideEffectDiagnostics = sideEffectImportDiagnostics(sourceFile, commandFile, intent.packageName);
772
+ if (sideEffectDiagnostics.length > 0) {
773
+ return { diagnostics: sideEffectDiagnostics };
774
+ }
775
+
776
+ const usageDiagnostics = importedPackageUsageDiagnostics(sourceFile, checker, commandFile, intent.command, intent.packageName, handler);
777
+ if (usageDiagnostics.length > 0) {
778
+ return { diagnostics: usageDiagnostics };
779
+ }
780
+
781
+ const after = rewriteExtractActionSource(
782
+ sourceFile,
783
+ handler,
784
+ ctxParam.text,
785
+ inputParam ? inputParam.text : null,
786
+ intent,
787
+ );
788
+
789
+ return { after, diagnostics: [] };
790
+ }
791
+
792
+ function safePackageIdentifier(packageName: string): string {
793
+ const base = packageName
794
+ .replace(/^@/, "")
795
+ .split(/[^a-zA-Z0-9]+/)
796
+ .filter(Boolean)
797
+ .map((part) => `${part.charAt(0).toUpperCase()}${part.slice(1)}`)
798
+ .join("");
799
+ const identifier = `${base || "Package"}Integration`;
800
+ return /^[A-Za-z_$]/.test(identifier) ? identifier : `Package${identifier}`;
801
+ }
802
+
803
+ function renderSourceFile(statements: ts.Statement[]): string {
804
+ const file = ts.factory.createSourceFile(
805
+ statements,
806
+ ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
807
+ ts.NodeFlags.None,
808
+ );
809
+ const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
810
+ return `${printer.printFile(file)}\n`;
811
+ }
812
+
813
+ function buildExtractedActionContent(intent: Extract<RefactorIntent, { kind: "extractAction" }>): string {
814
+ const packageIdentifier = safePackageIdentifier(intent.packageName);
815
+ const eventType = ts.factory.createTypeReferenceNode("Record", [
816
+ ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
817
+ ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),
818
+ ]);
819
+ const handler = ts.factory.createArrowFunction(
820
+ [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)],
821
+ undefined,
822
+ [
823
+ ts.factory.createParameterDeclaration(undefined, undefined, "ctx"),
824
+ ts.factory.createParameterDeclaration(undefined, undefined, "event", undefined, eventType),
825
+ ],
826
+ undefined,
827
+ ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
828
+ ts.factory.createBlock([
829
+ ts.factory.createExpressionStatement(
830
+ ts.factory.createVoidExpression(ts.factory.createIdentifier(packageIdentifier)),
831
+ ),
832
+ ts.factory.createExpressionStatement(
833
+ ts.factory.createAwaitExpression(
834
+ ts.factory.createCallExpression(
835
+ ts.factory.createPropertyAccessExpression(
836
+ ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ctx"), "telemetry"),
837
+ "capture",
838
+ ),
839
+ undefined,
840
+ [
841
+ ts.factory.createStringLiteral(`${intent.actionName}_requested`),
842
+ ts.factory.createObjectLiteralExpression([
843
+ ts.factory.createShorthandPropertyAssignment("event"),
844
+ ts.factory.createPropertyAssignment("integration", ts.factory.createStringLiteral(intent.packageName)),
845
+ ], true),
846
+ ],
847
+ ),
848
+ ),
849
+ ),
850
+ ts.factory.createReturnStatement(
851
+ ts.factory.createObjectLiteralExpression([
852
+ ts.factory.createPropertyAssignment("processed", ts.factory.createTrue()),
853
+ ], false),
854
+ ),
855
+ ], true),
856
+ );
857
+
858
+ return renderSourceFile([
859
+ ts.factory.createImportDeclaration(
860
+ undefined,
861
+ ts.factory.createImportClause(
862
+ false,
863
+ undefined,
864
+ ts.factory.createNamedImports([
865
+ ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier("action")),
866
+ ]),
867
+ ),
868
+ ts.factory.createStringLiteral("forge/server"),
869
+ ),
870
+ ts.factory.createImportDeclaration(
871
+ undefined,
872
+ ts.factory.createImportClause(
873
+ false,
874
+ undefined,
875
+ ts.factory.createNamespaceImport(ts.factory.createIdentifier(packageIdentifier)),
876
+ ),
877
+ ts.factory.createStringLiteral(intent.packageName),
878
+ ),
879
+ ts.factory.createVariableStatement(
880
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
881
+ ts.factory.createVariableDeclarationList([
882
+ ts.factory.createVariableDeclaration(
883
+ intent.actionName,
884
+ undefined,
885
+ undefined,
886
+ ts.factory.createCallExpression(ts.factory.createIdentifier("action"), undefined, [
887
+ ts.factory.createObjectLiteralExpression([
888
+ ts.factory.createPropertyAssignment("event", ts.factory.createStringLiteral(intent.eventName)),
889
+ ts.factory.createPropertyAssignment("handler", handler),
890
+ ], true),
891
+ ]),
892
+ ),
893
+ ], ts.NodeFlags.Const),
894
+ ),
895
+ ]);
896
+ }
897
+
898
+ function isTypeScriptLike(file: string): boolean {
899
+ return file.endsWith(".ts") || file.endsWith(".tsx") || file.endsWith(".js") || file.endsWith(".jsx");
900
+ }
901
+
902
+ function scriptKindForFile(file: string): ts.ScriptKind {
903
+ if (file.endsWith(".tsx")) return ts.ScriptKind.TSX;
904
+ if (file.endsWith(".jsx")) return ts.ScriptKind.JSX;
905
+ if (file.endsWith(".js")) return ts.ScriptKind.JS;
906
+ return ts.ScriptKind.TS;
907
+ }
908
+
909
+ function renameIdentifierNode(to: string): ts.Identifier {
910
+ return ts.factory.createIdentifier(to);
911
+ }
912
+
913
+ function renamePropertyName(name: ts.PropertyName, from: string, to: string, prefixMode = false): ts.PropertyName {
914
+ if (ts.isIdentifier(name) && name.text === from) {
915
+ return renameIdentifierNode(to);
916
+ }
917
+ if (ts.isStringLiteral(name)) {
918
+ const next = renameStringValue(name.text, from, to, prefixMode);
919
+ if (next !== name.text) {
920
+ return ts.factory.createStringLiteral(next);
921
+ }
922
+ }
923
+ return name;
924
+ }
925
+
926
+ function renameStringValue(value: string, from: string, to: string, prefixMode: boolean): string {
927
+ if (value === from) {
928
+ return to;
929
+ }
930
+ if (prefixMode && value.startsWith(`${from}.`)) {
931
+ return `${to}${value.slice(from.length)}`;
932
+ }
933
+ return value;
934
+ }
935
+
936
+ function renameStringLiteralNode<T extends ts.StringLiteral | ts.NoSubstitutionTemplateLiteral>(
937
+ node: T,
938
+ from: string,
939
+ to: string,
940
+ prefixMode: boolean,
941
+ ): T {
942
+ const next = renameStringValue(node.text, from, to, prefixMode);
943
+ if (next === node.text) {
944
+ return node;
945
+ }
946
+ return (ts.isNoSubstitutionTemplateLiteral(node)
947
+ ? ts.factory.createNoSubstitutionTemplateLiteral(next)
948
+ : ts.factory.createStringLiteral(next)) as T;
949
+ }
950
+
951
+ function isDefineTableCall(expression: ts.Expression | undefined): boolean {
952
+ return (
953
+ expression !== undefined &&
954
+ ts.isCallExpression(expression) &&
955
+ ts.isIdentifier(expression.expression) &&
956
+ expression.expression.text === "defineTable"
957
+ );
958
+ }
959
+
960
+ function objectLiteralHasStringProperty(node: ts.ObjectLiteralExpression, key: string, value: string): boolean {
961
+ return node.properties.some((property) => {
962
+ if (!ts.isPropertyAssignment(property)) {
963
+ return false;
964
+ }
965
+ if (!ts.isIdentifier(property.name) && !ts.isStringLiteral(property.name)) {
966
+ return false;
967
+ }
968
+ if (property.name.text !== key) {
969
+ return false;
970
+ }
971
+ return ts.isStringLiteralLike(property.initializer) && property.initializer.text === value;
972
+ });
973
+ }
974
+
975
+ function callDefinesTable(node: ts.CallExpression, table: string): boolean {
976
+ if (!isDefineTableCall(node)) {
977
+ return false;
978
+ }
979
+ const firstArg = node.arguments[0];
980
+ return ts.isObjectLiteralExpression(firstArg) && objectLiteralHasStringProperty(firstArg, "name", table);
981
+ }
982
+
983
+ function sourceHasDefineTable(source: string, file: string): boolean {
984
+ const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, scriptKindForFile(file));
985
+ let found = false;
986
+ const visit = (node: ts.Node): void => {
987
+ if (found) {
988
+ return;
989
+ }
990
+ if (ts.isCallExpression(node) && isDefineTableCall(node)) {
991
+ found = true;
992
+ return;
993
+ }
994
+ ts.forEachChild(node, visit);
995
+ };
996
+ visit(sourceFile);
997
+ return found;
998
+ }
999
+
1000
+ function sourceHasFieldTableScope(source: string, file: string, table: string): boolean {
1001
+ if (file.endsWith(".json")) {
1002
+ try {
1003
+ return jsonHasTableScope(JSON.parse(source) as unknown, table);
1004
+ } catch {
1005
+ return source.includes(table);
1006
+ }
1007
+ }
1008
+ if (!isTypeScriptLike(file)) {
1009
+ return source.includes(table);
1010
+ }
1011
+ const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, scriptKindForFile(file));
1012
+ let found = false;
1013
+ const visit = (node: ts.Node): void => {
1014
+ if (found) {
1015
+ return;
1016
+ }
1017
+ if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === table && isDefineTableCall(node.initializer)) {
1018
+ found = true;
1019
+ return;
1020
+ }
1021
+ if (ts.isCallExpression(node) && callDefinesTable(node, table)) {
1022
+ found = true;
1023
+ return;
1024
+ }
1025
+ if (ts.isPropertyAccessExpression(node) && node.name.text === table) {
1026
+ found = true;
1027
+ return;
1028
+ }
1029
+ if (ts.isStringLiteralLike(node) && (node.text === table || node.text.startsWith(`${table}.`))) {
1030
+ found = true;
1031
+ return;
1032
+ }
1033
+ ts.forEachChild(node, visit);
1034
+ };
1035
+ visit(sourceFile);
1036
+ return found;
1037
+ }
1038
+
1039
+ function renameFieldInSource(source: string, file: string, table: string, from: string, to: string): string {
1040
+ const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, scriptKindForFile(file));
1041
+ const transform: ts.TransformerFactory<ts.SourceFile> = (context) => {
1042
+ const renameVisit: ts.Visitor = (node) => {
1043
+ if (ts.isPropertyAccessExpression(node) && node.name.text === from) {
1044
+ return ts.factory.updatePropertyAccessExpression(node, node.expression, renameIdentifierNode(to));
1045
+ }
1046
+ if (ts.isPropertyAssignment(node)) {
1047
+ return ts.factory.updatePropertyAssignment(
1048
+ node,
1049
+ renamePropertyName(node.name, from, to),
1050
+ ts.visitNode(node.initializer, renameVisit, ts.isExpression) ?? node.initializer,
1051
+ );
1052
+ }
1053
+ if (ts.isShorthandPropertyAssignment(node) && node.name.text === from) {
1054
+ return ts.factory.createPropertyAssignment(to, node.name);
1055
+ }
1056
+ if (ts.isPropertySignature(node)) {
1057
+ return ts.factory.updatePropertySignature(
1058
+ node,
1059
+ node.modifiers,
1060
+ renamePropertyName(node.name, from, to),
1061
+ node.questionToken,
1062
+ node.type,
1063
+ );
1064
+ }
1065
+ if (ts.isBindingElement(node) && node.propertyName) {
1066
+ return ts.factory.updateBindingElement(
1067
+ node,
1068
+ node.dotDotDotToken,
1069
+ renamePropertyName(node.propertyName, from, to),
1070
+ node.name,
1071
+ node.initializer,
1072
+ );
1073
+ }
1074
+ if (ts.isBindingElement(node) && ts.isIdentifier(node.name) && node.name.text === from && !node.propertyName) {
1075
+ return ts.factory.updateBindingElement(
1076
+ node,
1077
+ node.dotDotDotToken,
1078
+ ts.factory.createIdentifier(to),
1079
+ node.name,
1080
+ node.initializer,
1081
+ );
1082
+ }
1083
+ if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
1084
+ return renameStringLiteralNode(node, from, to, false);
1085
+ }
1086
+ if (ts.isJsxAttribute(node) && ts.isIdentifier(node.name) && node.name.text === from) {
1087
+ return ts.factory.updateJsxAttribute(node, ts.factory.createIdentifier(to), node.initializer);
1088
+ }
1089
+ return ts.visitEachChild(node, renameVisit, context);
1090
+ };
1091
+ const hasDefineTable = sourceHasDefineTable(source, file);
1092
+ const visit: ts.Visitor = (node) => {
1093
+ if (
1094
+ hasDefineTable &&
1095
+ ts.isVariableDeclaration(node) &&
1096
+ ts.isIdentifier(node.name) &&
1097
+ node.name.text === table &&
1098
+ isDefineTableCall(node.initializer)
1099
+ ) {
1100
+ return ts.factory.updateVariableDeclaration(
1101
+ node,
1102
+ node.name,
1103
+ node.exclamationToken,
1104
+ node.type,
1105
+ ts.visitNode(node.initializer, renameVisit, ts.isExpression) ?? node.initializer,
1106
+ );
1107
+ }
1108
+ if (hasDefineTable && ts.isCallExpression(node) && callDefinesTable(node, table)) {
1109
+ const firstArg = node.arguments[0];
1110
+ const updatedFirstArg = ts.visitNode(firstArg, renameVisit, ts.isExpression) ?? firstArg;
1111
+ return ts.factory.updateCallExpression(node, node.expression, node.typeArguments, [
1112
+ updatedFirstArg,
1113
+ ...node.arguments.slice(1),
1114
+ ]);
1115
+ }
1116
+ return ts.visitEachChild(node, hasDefineTable ? visit : renameVisit, context);
1117
+ };
1118
+ return (node) => ts.visitNode(node, hasDefineTable ? visit : renameVisit) as ts.SourceFile;
1119
+ };
1120
+ const result = ts.transform(sourceFile, [transform]);
1121
+ try {
1122
+ const transformed = result.transformed[0];
1123
+ const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
1124
+ return printer.printFile(transformed);
1125
+ } finally {
1126
+ result.dispose();
1127
+ }
1128
+ }
1129
+
1130
+ function renameTableInSource(source: string, file: string, from: string, to: string): string {
1131
+ const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, scriptKindForFile(file));
1132
+ const transform: ts.TransformerFactory<ts.SourceFile> = (context) => {
1133
+ const visit: ts.Visitor = (node) => {
1134
+ if (
1135
+ ts.isVariableDeclaration(node) &&
1136
+ ts.isIdentifier(node.name) &&
1137
+ node.name.text === from &&
1138
+ isDefineTableCall(node.initializer)
1139
+ ) {
1140
+ return ts.factory.updateVariableDeclaration(
1141
+ node,
1142
+ renameIdentifierNode(to),
1143
+ node.exclamationToken,
1144
+ node.type,
1145
+ ts.visitNode(node.initializer, visit, ts.isExpression) ?? node.initializer,
1146
+ );
1147
+ }
1148
+ if (ts.isImportSpecifier(node)) {
1149
+ if (node.propertyName?.text === from) {
1150
+ return ts.factory.updateImportSpecifier(node, node.isTypeOnly, renameIdentifierNode(to), node.name);
1151
+ }
1152
+ if (!node.propertyName && node.name.text === from) {
1153
+ return ts.factory.updateImportSpecifier(node, node.isTypeOnly, undefined, renameIdentifierNode(to));
1154
+ }
1155
+ }
1156
+ if (ts.isExportSpecifier(node)) {
1157
+ if (node.propertyName?.text === from) {
1158
+ return ts.factory.updateExportSpecifier(node, node.isTypeOnly, renameIdentifierNode(to), node.name);
1159
+ }
1160
+ if (!node.propertyName && node.name.text === from) {
1161
+ return ts.factory.updateExportSpecifier(node, node.isTypeOnly, undefined, renameIdentifierNode(to));
1162
+ }
1163
+ }
1164
+ if (ts.isPropertyAccessExpression(node) && node.name.text === from) {
1165
+ return ts.factory.updatePropertyAccessExpression(node, node.expression, renameIdentifierNode(to));
1166
+ }
1167
+ if (ts.isPropertyAssignment(node)) {
1168
+ return ts.factory.updatePropertyAssignment(
1169
+ node,
1170
+ renamePropertyName(node.name, from, to, true),
1171
+ ts.visitNode(node.initializer, visit, ts.isExpression) ?? node.initializer,
1172
+ );
1173
+ }
1174
+ if (ts.isPropertySignature(node)) {
1175
+ return ts.factory.updatePropertySignature(
1176
+ node,
1177
+ node.modifiers,
1178
+ renamePropertyName(node.name, from, to, true),
1179
+ node.questionToken,
1180
+ node.type,
1181
+ );
1182
+ }
1183
+ if (ts.isBindingElement(node) && node.propertyName) {
1184
+ return ts.factory.updateBindingElement(
1185
+ node,
1186
+ node.dotDotDotToken,
1187
+ renamePropertyName(node.propertyName, from, to),
1188
+ node.name,
1189
+ node.initializer,
1190
+ );
1191
+ }
1192
+ if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
1193
+ return renameStringLiteralNode(node, from, to, true);
1194
+ }
1195
+ return ts.visitEachChild(node, visit, context);
1196
+ };
1197
+ return (node) => ts.visitNode(node, visit) as ts.SourceFile;
1198
+ };
1199
+ const result = ts.transform(sourceFile, [transform]);
1200
+ try {
1201
+ const transformed = result.transformed[0];
1202
+ const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
1203
+ return printer.printFile(transformed);
1204
+ } finally {
1205
+ result.dispose();
1206
+ }
1207
+ }
1208
+
1209
+ function renameJsonValue(value: unknown, from: string, to: string, prefixMode: boolean): unknown {
1210
+ if (typeof value === "string") {
1211
+ return renameStringValue(value, from, to, prefixMode);
1212
+ }
1213
+ if (Array.isArray(value)) {
1214
+ return value.map((entry) => renameJsonValue(entry, from, to, prefixMode));
1215
+ }
1216
+ if (value && typeof value === "object") {
1217
+ const output: Record<string, unknown> = {};
1218
+ for (const [key, entry] of Object.entries(value)) {
1219
+ const nextKey = renameStringValue(key, from, to, prefixMode);
1220
+ output[nextKey] = renameJsonValue(entry, from, to, prefixMode);
1221
+ }
1222
+ return output;
1223
+ }
1224
+ return value;
1225
+ }
1226
+
1227
+ function renameInJson(source: string, from: string, to: string, prefixMode: boolean): string {
1228
+ const parsed = JSON.parse(source) as unknown;
1229
+ return `${JSON.stringify(renameJsonValue(parsed, from, to, prefixMode), null, 2)}\n`;
1230
+ }
1231
+
1232
+ function jsonHasTableScope(value: unknown, table: string): boolean {
1233
+ if (typeof value === "string") {
1234
+ return value === table || value.startsWith(`${table}.`);
1235
+ }
1236
+ if (Array.isArray(value)) {
1237
+ return value.some((entry) => jsonHasTableScope(entry, table));
1238
+ }
1239
+ if (value && typeof value === "object") {
1240
+ return Object.entries(value).some(([key, entry]) => key === table || jsonHasTableScope(entry, table));
1241
+ }
1242
+ return false;
1243
+ }
1244
+
1245
+ function objectHasTableScope(value: Record<string, unknown>, table: string): boolean {
1246
+ const tableValue = value.table;
1247
+ if (typeof tableValue === "string" && tableValue === table) {
1248
+ return true;
1249
+ }
1250
+ const nameValue = value.name;
1251
+ return typeof nameValue === "string" && nameValue === table;
1252
+ }
1253
+
1254
+ function renameFieldJsonValue(value: unknown, table: string, from: string, to: string, inTableScope: boolean): unknown {
1255
+ if (typeof value === "string") {
1256
+ return inTableScope ? renameStringValue(value, from, to, false) : value;
1257
+ }
1258
+ if (Array.isArray(value)) {
1259
+ return value.map((entry) => renameFieldJsonValue(entry, table, from, to, inTableScope));
1260
+ }
1261
+ if (value && typeof value === "object") {
1262
+ const source = value as Record<string, unknown>;
1263
+ const scoped = inTableScope || objectHasTableScope(source, table);
1264
+ const output: Record<string, unknown> = {};
1265
+ for (const [key, entry] of Object.entries(source)) {
1266
+ const nextKey = scoped ? renameStringValue(key, from, to, false) : key;
1267
+ output[nextKey] = renameFieldJsonValue(entry, table, from, to, scoped);
1268
+ }
1269
+ return output;
1270
+ }
1271
+ return value;
1272
+ }
1273
+
1274
+ function renameFieldInJson(source: string, table: string, from: string, to: string): string {
1275
+ const parsed = JSON.parse(source) as unknown;
1276
+ return `${JSON.stringify(renameFieldJsonValue(parsed, table, from, to, false), null, 2)}\n`;
1277
+ }
1278
+
1279
+ function renameFieldContent(source: string, file: string, intent: Extract<RefactorIntent, { kind: "renameField" }>): string {
1280
+ if (isTypeScriptLike(file)) {
1281
+ return renameFieldInSource(source, file, intent.table, intent.from.field, intent.to.field);
1282
+ }
1283
+ if (file.endsWith(".json")) {
1284
+ return renameFieldInJson(source, intent.table, intent.from.field, intent.to.field);
1285
+ }
1286
+ return wordReplace(source, intent.from.field, intent.to.field);
1287
+ }
1288
+
1289
+ function renameTableContent(source: string, file: string, intent: Extract<RefactorIntent, { kind: "renameTable" }>): string {
1290
+ if (isTypeScriptLike(file)) {
1291
+ return renameTableInSource(source, file, intent.from.table, intent.to.table);
1292
+ }
1293
+ if (file.endsWith(".json")) {
1294
+ return renameInJson(source, intent.from.table, intent.to.table, true);
1295
+ }
1296
+ return wordReplace(source, intent.from.table, intent.to.table);
1297
+ }
1298
+
1299
+ function buildRenameFieldPlan(workspaceRoot: string, intent: Extract<RefactorIntent, { kind: "renameField" }>): {
1300
+ patches: PlannedPatch[];
1301
+ diagnostics: Diagnostic[];
1302
+ } {
1303
+ const patches: PlannedPatch[] = [];
1304
+ const diagnostics: Diagnostic[] = [];
1305
+ const files = walkFiles(workspaceRoot);
1306
+ let found = false;
1307
+ for (const file of files) {
1308
+ const content = readText(workspaceRoot, file) ?? "";
1309
+ if (!content.includes(intent.from.field)) {
1310
+ continue;
1311
+ }
1312
+ if (!sourceHasFieldTableScope(content, file, intent.table)) {
1313
+ continue;
1314
+ }
1315
+ if (file.endsWith(".md") && !content.includes(intent.table)) {
1316
+ diagnostics.push(
1317
+ diagnostic(
1318
+ "warning",
1319
+ "FORGE_REFACTOR_AMBIGUOUS_REFERENCE",
1320
+ `possible unrelated field reference in ${file}`,
1321
+ file,
1322
+ ),
1323
+ );
1324
+ continue;
1325
+ }
1326
+ const patch = patchFile(
1327
+ workspaceRoot,
1328
+ file,
1329
+ `Rename field ${intent.table}.${intent.from.field} to ${intent.to.field}`,
1330
+ (source) => renameFieldContent(source, file, intent),
1331
+ );
1332
+ if (patch) {
1333
+ found = true;
1334
+ patches.push(patch);
1335
+ }
1336
+ }
1337
+ if (!found) {
1338
+ diagnostics.push(
1339
+ diagnostic(
1340
+ "error",
1341
+ "FORGE_REFACTOR_FIELD_NOT_FOUND",
1342
+ `field '${intent.table}.${intent.from.field}' was not found`,
1343
+ ),
1344
+ );
1345
+ }
1346
+ return { patches, diagnostics };
1347
+ }
1348
+
1349
+ function buildRenameTablePlan(workspaceRoot: string, intent: Extract<RefactorIntent, { kind: "renameTable" }>): {
1350
+ patches: PlannedPatch[];
1351
+ diagnostics: Diagnostic[];
1352
+ } {
1353
+ const patches: PlannedPatch[] = [];
1354
+ let found = false;
1355
+ for (const file of walkFiles(workspaceRoot)) {
1356
+ const content = readText(workspaceRoot, file) ?? "";
1357
+ if (!content.includes(intent.from.table)) {
1358
+ continue;
1359
+ }
1360
+ const patch = patchFile(
1361
+ workspaceRoot,
1362
+ file,
1363
+ `Rename table ${intent.from.table} to ${intent.to.table}`,
1364
+ (source) => renameTableContent(source, file, intent),
1365
+ );
1366
+ if (patch) {
1367
+ found = true;
1368
+ patches.push(patch);
1369
+ }
1370
+ }
1371
+ return {
1372
+ patches,
1373
+ diagnostics: found
1374
+ ? []
1375
+ : [diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", `table '${intent.from.table}' was not found`)],
1376
+ };
1377
+ }
1378
+
1379
+ function buildStringRenamePlan(
1380
+ workspaceRoot: string,
1381
+ from: string,
1382
+ to: string,
1383
+ description: string,
1384
+ ): { patches: PlannedPatch[]; diagnostics: Diagnostic[] } {
1385
+ const patches: PlannedPatch[] = [];
1386
+ let found = false;
1387
+ for (const file of walkFiles(workspaceRoot)) {
1388
+ const content = readText(workspaceRoot, file) ?? "";
1389
+ if (!content.includes(from)) {
1390
+ continue;
1391
+ }
1392
+ const patch = patchFile(
1393
+ workspaceRoot,
1394
+ file,
1395
+ description,
1396
+ (source) => source.split(from).join(to),
1397
+ );
1398
+ if (patch) {
1399
+ found = true;
1400
+ patches.push(patch);
1401
+ }
1402
+ }
1403
+ return {
1404
+ patches,
1405
+ diagnostics: found
1406
+ ? []
1407
+ : [diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", `target '${from}' was not found`)],
1408
+ };
1409
+ }
1410
+
1411
+ function buildMoveComponentPlan(workspaceRoot: string, intent: Extract<RefactorIntent, { kind: "moveComponent" }>): {
1412
+ patches: PlannedPatch[];
1413
+ filesToCreate: PlannedFile[];
1414
+ filesToDelete: Array<{ file: string; description: string }>;
1415
+ diagnostics: Diagnostic[];
1416
+ } {
1417
+ const sourceFile = walkFiles(workspaceRoot).find(
1418
+ (file) =>
1419
+ file.endsWith(`${intent.name}.tsx`) ||
1420
+ (file.endsWith(".tsx") && (readText(workspaceRoot, file) ?? "").includes(`function ${intent.name}`)),
1421
+ );
1422
+ if (!sourceFile) {
1423
+ return {
1424
+ patches: [],
1425
+ filesToCreate: [],
1426
+ filesToDelete: [],
1427
+ diagnostics: [
1428
+ diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", `component '${intent.name}' was not found`),
1429
+ ],
1430
+ };
1431
+ }
1432
+ if (isGenerated(intent.toPath)) {
1433
+ return {
1434
+ patches: [],
1435
+ filesToCreate: [],
1436
+ filesToDelete: [],
1437
+ diagnostics: [
1438
+ diagnostic(
1439
+ "error",
1440
+ "FORGE_REFACTOR_GENERATED_FILE_EDIT_BLOCKED",
1441
+ "cannot move component into generated directory",
1442
+ intent.toPath,
1443
+ ),
1444
+ ],
1445
+ };
1446
+ }
1447
+ const content = readText(workspaceRoot, sourceFile) ?? "";
1448
+ const fromNoExt = sourceFile.replace(/\.[tj]sx?$/, "");
1449
+ const toNoExt = intent.toPath.replace(/\.[tj]sx?$/, "");
1450
+ const patches: PlannedPatch[] = [];
1451
+ for (const file of walkFiles(workspaceRoot)) {
1452
+ if (file === sourceFile) {
1453
+ continue;
1454
+ }
1455
+ const patch = patchFile(
1456
+ workspaceRoot,
1457
+ file,
1458
+ `Update imports for moved component ${intent.name}`,
1459
+ (source) => source.split(fromNoExt).join(toNoExt),
1460
+ );
1461
+ if (patch) {
1462
+ patches.push(patch);
1463
+ }
1464
+ }
1465
+ return {
1466
+ patches,
1467
+ filesToCreate: [makeFile(workspaceRoot, intent.toPath, `Move component ${intent.name}`, content)],
1468
+ filesToDelete: [{ file: sourceFile, description: `Remove old component path for ${intent.name}` }],
1469
+ diagnostics: [],
1470
+ };
1471
+ }
1472
+
1473
+ function buildExtractActionPlan(workspaceRoot: string, intent: Extract<RefactorIntent, { kind: "extractAction" }>): {
1474
+ patches: PlannedPatch[];
1475
+ filesToCreate: PlannedFile[];
1476
+ diagnostics: Diagnostic[];
1477
+ } {
1478
+ const commandFile = findCommandFile(workspaceRoot, intent.command);
1479
+ if (!commandFile) {
1480
+ return {
1481
+ patches: [],
1482
+ filesToCreate: [],
1483
+ diagnostics: [
1484
+ diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", `command '${intent.command}' was not found`),
1485
+ ],
1486
+ };
1487
+ }
1488
+ const commandSource = readText(workspaceRoot, commandFile);
1489
+ if (commandSource === null) {
1490
+ return {
1491
+ patches: [],
1492
+ filesToCreate: [],
1493
+ diagnostics: [
1494
+ diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", `command file '${commandFile}' was not found`, commandFile),
1495
+ ],
1496
+ };
1497
+ }
1498
+ const rewrite = rewriteExtractActionCommand(workspaceRoot, commandSource, commandFile, intent);
1499
+ const commandPatch = rewrite.after
1500
+ ? makePatchFromContent(
1501
+ commandFile,
1502
+ `Extract ${intent.packageName} side effect from ${intent.command}`,
1503
+ commandSource,
1504
+ rewrite.after,
1505
+ )
1506
+ : null;
1507
+ const actionFile = `src/actions/${intent.actionName}.ts`;
1508
+ const actionContent = buildExtractedActionContent(intent);
1509
+ return {
1510
+ patches: commandPatch ? [commandPatch] : [],
1511
+ filesToCreate: commandPatch
1512
+ ? [makeFile(workspaceRoot, actionFile, `Create extracted action ${intent.actionName}`, actionContent)]
1513
+ : [],
1514
+ diagnostics: commandPatch
1515
+ ? rewrite.diagnostics
1516
+ : rewrite.diagnostics.length > 0
1517
+ ? rewrite.diagnostics
1518
+ : [diagnostic("error", "FORGE_REFACTOR_PATCH_UNSAFE", `could not safely rewrite command '${intent.command}'`, commandFile)],
1519
+ };
1520
+ }
1521
+
1522
+ function buildReplaceProcessEnvPlan(workspaceRoot: string, intent: Extract<RefactorIntent, { kind: "replaceProcessEnv" }>): {
1523
+ patches: PlannedPatch[];
1524
+ diagnostics: Diagnostic[];
1525
+ } {
1526
+ const patches: PlannedPatch[] = [];
1527
+ const diagnostics: Diagnostic[] = [];
1528
+ let found = false;
1529
+ for (const file of walkFiles(workspaceRoot)) {
1530
+ const content = readText(workspaceRoot, file) ?? "";
1531
+ const needle = `process.env.${intent.name}`;
1532
+ if (!content.includes(needle) && !content.includes(`process.env[${JSON.stringify(intent.name)}]`)) {
1533
+ continue;
1534
+ }
1535
+ found = true;
1536
+ if (file.endsWith(".tsx") || file.includes("/components/") || file.includes("\\components\\")) {
1537
+ diagnostics.push(
1538
+ diagnostic("error", "FORGE_REFACTOR_SECRET_IN_CLIENT", `secret env var used in client-like file ${file}`, file),
1539
+ );
1540
+ continue;
1541
+ }
1542
+ if (!content.includes("ctx")) {
1543
+ diagnostics.push(
1544
+ diagnostic("error", "FORGE_REFACTOR_CTX_NOT_AVAILABLE", `ctx is not available in ${file}`, file),
1545
+ );
1546
+ continue;
1547
+ }
1548
+ const patch = patchFile(
1549
+ workspaceRoot,
1550
+ file,
1551
+ `Replace process.env.${intent.name} with ctx.secrets`,
1552
+ (source) =>
1553
+ source
1554
+ .split(needle)
1555
+ .join(`ctx.secrets.get(${JSON.stringify(intent.name)})`)
1556
+ .split(`process.env[${JSON.stringify(intent.name)}]`)
1557
+ .join(`ctx.secrets.get(${JSON.stringify(intent.name)})`),
1558
+ );
1559
+ if (patch) {
1560
+ patches.push(patch);
1561
+ }
1562
+ }
1563
+ if (!found) {
1564
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", `process.env.${intent.name} was not found`));
1565
+ }
1566
+ return { patches, diagnostics };
1567
+ }
1568
+
1569
+ function planParts(workspaceRoot: string, intent: RefactorIntent): {
1570
+ patches: PlannedPatch[];
1571
+ filesToCreate: PlannedFile[];
1572
+ filesToDelete: Array<{ file: string; description: string }>;
1573
+ diagnostics: Diagnostic[];
1574
+ } {
1575
+ if (intent.kind === "renameField") {
1576
+ const result = buildRenameFieldPlan(workspaceRoot, intent);
1577
+ return { patches: result.patches, filesToCreate: [], filesToDelete: [], diagnostics: result.diagnostics };
1578
+ }
1579
+ if (intent.kind === "renameTable") {
1580
+ const result = buildRenameTablePlan(workspaceRoot, intent);
1581
+ return { patches: result.patches, filesToCreate: [], filesToDelete: [], diagnostics: result.diagnostics };
1582
+ }
1583
+ if (intent.kind === "renamePolicy") {
1584
+ const result = buildStringRenamePlan(workspaceRoot, intent.from, intent.to, `Rename policy ${intent.from} to ${intent.to}`);
1585
+ return { patches: result.patches, filesToCreate: [], filesToDelete: [], diagnostics: result.diagnostics };
1586
+ }
1587
+ if (intent.kind === "renameEvent") {
1588
+ const result = buildStringRenamePlan(workspaceRoot, intent.from, intent.to, `Rename event ${intent.from} to ${intent.to}`);
1589
+ return { patches: result.patches, filesToCreate: [], filesToDelete: [], diagnostics: result.diagnostics };
1590
+ }
1591
+ if (intent.kind === "renameRuntimeEntry") {
1592
+ const result = buildStringRenamePlan(workspaceRoot, intent.from, intent.to, `Rename ${intent.entryKind} ${intent.from} to ${intent.to}`);
1593
+ return { patches: result.patches, filesToCreate: [], filesToDelete: [], diagnostics: result.diagnostics };
1594
+ }
1595
+ if (intent.kind === "moveComponent") {
1596
+ const result = buildMoveComponentPlan(workspaceRoot, intent);
1597
+ return result;
1598
+ }
1599
+ if (intent.kind === "extractAction") {
1600
+ const result = buildExtractActionPlan(workspaceRoot, intent);
1601
+ return { patches: result.patches, filesToCreate: result.filesToCreate, filesToDelete: [], diagnostics: result.diagnostics };
1602
+ }
1603
+ if (intent.kind === "replaceProcessEnv") {
1604
+ const result = buildReplaceProcessEnvPlan(workspaceRoot, intent);
1605
+ return { patches: result.patches, filesToCreate: [], filesToDelete: [], diagnostics: result.diagnostics };
1606
+ }
1607
+ if (intent.kind === "replaceImport") {
1608
+ const result = buildStringRenamePlan(workspaceRoot, `from "${intent.from}"`, `from "${intent.to}"`, `Replace import ${intent.from}`);
1609
+ const single = buildStringRenamePlan(workspaceRoot, `from '${intent.from}'`, `from '${intent.to}'`, `Replace import ${intent.from}`);
1610
+ return { patches: [...result.patches, ...single.patches], filesToCreate: [], filesToDelete: [], diagnostics: [...result.diagnostics, ...single.diagnostics].filter((diag, _, arr) => arr.length < 2 || diag.severity !== "error") };
1611
+ }
1612
+ return {
1613
+ patches: [],
1614
+ filesToCreate: [],
1615
+ filesToDelete: [],
1616
+ diagnostics: [
1617
+ diagnostic("error", "FORGE_REFACTOR_TARGET_NOT_FOUND", "unsupported refactor intent"),
1618
+ ],
1619
+ };
1620
+ }
1621
+
1622
+ function summarizeIntent(intent: RefactorIntent): string {
1623
+ if (intent.kind === "renameField") {
1624
+ return `Rename ${intent.table}.${intent.from.field} to ${intent.table}.${intent.to.field}`;
1625
+ }
1626
+ if (intent.kind === "renameTable") {
1627
+ return `Rename table ${intent.from.table} to ${intent.to.table}`;
1628
+ }
1629
+ if (intent.kind === "renamePolicy") {
1630
+ return `Rename policy ${intent.from} to ${intent.to}`;
1631
+ }
1632
+ if (intent.kind === "renameRuntimeEntry") {
1633
+ return `Rename ${intent.entryKind} ${intent.from} to ${intent.to}`;
1634
+ }
1635
+ if (intent.kind === "renameEvent") {
1636
+ return `Rename event ${intent.from} to ${intent.to}`;
1637
+ }
1638
+ if (intent.kind === "moveComponent") {
1639
+ return `Move component ${intent.name} to ${intent.toPath}`;
1640
+ }
1641
+ if (intent.kind === "extractAction") {
1642
+ return `Extract ${intent.packageName} usage from ${intent.command}`;
1643
+ }
1644
+ if (intent.kind === "replaceProcessEnv") {
1645
+ return `Replace process.env.${intent.name}`;
1646
+ }
1647
+ return `Replace import ${intent.from} to ${intent.to}`;
1648
+ }
1649
+
1650
+ function migrationPlan(intent: RefactorIntent): RefactorPlan["migrationPlan"] {
1651
+ if (intent.kind === "renameField") {
1652
+ return {
1653
+ strategy: "rename-column",
1654
+ sql: [`ALTER TABLE ${intent.table} RENAME COLUMN ${intent.from.field} TO ${intent.to.field};`],
1655
+ };
1656
+ }
1657
+ if (intent.kind === "renameTable") {
1658
+ return {
1659
+ strategy: "rename-table",
1660
+ sql: [`ALTER TABLE ${intent.from.table} RENAME TO ${intent.to.table};`],
1661
+ };
1662
+ }
1663
+ return undefined;
1664
+ }
1665
+
1666
+ function impact(intent: RefactorIntent, patches: PlannedPatch[], creates: PlannedFile[]): RefactorImpact {
1667
+ const result = emptyImpact();
1668
+ for (const patch of patches) {
1669
+ if (patch.file.startsWith(".forge/blueprints/")) {
1670
+ pushUnique(result.blueprints, patch.file);
1671
+ } else if (patch.file.includes("/components/") || patch.file.endsWith(".tsx")) {
1672
+ pushUnique(result.frontend.components, patch.file);
1673
+ } else if (patch.file.includes("/queries/")) {
1674
+ pushUnique(result.runtime.queries, patch.file);
1675
+ } else if (patch.file.includes("/commands/")) {
1676
+ pushUnique(result.runtime.commands, patch.file);
1677
+ } else if (patch.file.includes("/actions/")) {
1678
+ pushUnique(result.runtime.actions, patch.file);
1679
+ } else if (patch.file.includes("/workflows/")) {
1680
+ pushUnique(result.runtime.workflows, patch.file);
1681
+ } else if (patch.file.includes("/tests/")) {
1682
+ pushUnique(result.tests, patch.file);
1683
+ }
1684
+ }
1685
+ for (const file of creates) {
1686
+ if (file.file.includes("/actions/")) {
1687
+ pushUnique(result.runtime.actions, file.file);
1688
+ }
1689
+ if (file.file.endsWith(".tsx")) {
1690
+ pushUnique(result.frontend.components, file.file);
1691
+ }
1692
+ }
1693
+ if (intent.kind === "renameField") {
1694
+ pushUnique(result.data.fields, `${intent.table}.${intent.from.field}`);
1695
+ }
1696
+ if (intent.kind === "renameTable") {
1697
+ pushUnique(result.data.tables, intent.from.table);
1698
+ }
1699
+ if (intent.kind === "renamePolicy") {
1700
+ pushUnique(result.policies, intent.from);
1701
+ }
1702
+ return result;
1703
+ }
1704
+
1705
+ function risk(intent: RefactorIntent): RefactorPlan["risk"] {
1706
+ if (intent.kind === "renameTable") {
1707
+ return { level: "high", reasons: ["table rename requires database migration and client API changes"] };
1708
+ }
1709
+ if (intent.kind === "renameField") {
1710
+ return { level: "medium", reasons: ["field rename may require database migration"] };
1711
+ }
1712
+ if (intent.kind === "renameRuntimeEntry") {
1713
+ return { level: "medium", reasons: ["runtime entry rename changes public API"] };
1714
+ }
1715
+ if (intent.kind === "extractAction") {
1716
+ return { level: "medium", reasons: ["extracts side effect into after-commit action"] };
1717
+ }
1718
+ return { level: "low", reasons: [] };
1719
+ }
1720
+
1721
+ export function buildRefactorPlan(options: RefactorCommandOptions): RefactorResult {
1722
+ const parsed = inferIntent(options);
1723
+ if (!parsed.intent) {
1724
+ return { ok: false, diagnostics: parsed.diagnostics, exitCode: 1 };
1725
+ }
1726
+ const parts = planParts(options.workspaceRoot, parsed.intent);
1727
+ const id = `refactor_${hashStable(canonicalJson(parsed.intent)).slice(0, 12)}`;
1728
+ const trackedFiles = [
1729
+ ...parts.patches.map((patch) => patch.file),
1730
+ ...parts.filesToCreate.map((file) => file.file),
1731
+ ...parts.filesToDelete.map((file) => file.file),
1732
+ ].filter((value, index, array) => array.indexOf(value) === index).sort();
1733
+ const plan: RefactorPlan = {
1734
+ schemaVersion: "0.1.0",
1735
+ refactorVersion: GENERATOR_VERSION,
1736
+ id,
1737
+ intent: parsed.intent,
1738
+ summary: summarizeIntent(parsed.intent),
1739
+ impact: impact(parsed.intent, parts.patches, parts.filesToCreate),
1740
+ filesToModify: parts.patches,
1741
+ filesToCreate: parts.filesToCreate,
1742
+ filesToDelete: parts.filesToDelete,
1743
+ generatedImpacts: emptyImpact().generatedArtifacts,
1744
+ migrationPlan: migrationPlan(parsed.intent),
1745
+ risk: risk(parsed.intent),
1746
+ commandsToRun: ["forge generate", "forge verify --strict"],
1747
+ diagnostics: [...parsed.diagnostics, ...parts.diagnostics],
1748
+ rollback: {
1749
+ trackedFiles,
1750
+ instructions: [`forge refactor rollback ${id}`],
1751
+ },
1752
+ };
1753
+ if (parts.patches.some((patch) => isGenerated(patch.file))) {
1754
+ plan.diagnostics.push(
1755
+ diagnostic(
1756
+ "error",
1757
+ "FORGE_REFACTOR_GENERATED_FILE_EDIT_BLOCKED",
1758
+ "refactor attempted to patch generated files",
1759
+ ),
1760
+ );
1761
+ }
1762
+ const ok = !plan.diagnostics.some((diag) => diag.severity === "error");
1763
+ return { ok, plan, diagnostics: plan.diagnostics, exitCode: ok ? 0 : 1 };
1764
+ }
1765
+
1766
+ function planPath(_workspaceRoot: string, id: string): string {
1767
+ return `${REFACTOR_DIR}/${id}/plan.json`;
1768
+ }
1769
+
1770
+ function snapshotPath(id: string): string {
1771
+ return `${REFACTOR_DIR}/${id}/rollback.json`;
1772
+ }
1773
+
1774
+ function recordPath(id: string): string {
1775
+ return `${REFACTOR_DIR}/${id}/applied.json`;
1776
+ }
1777
+
1778
+ export function writeRefactorPlan(workspaceRoot: string, plan: RefactorPlan): string {
1779
+ const path = planPath(workspaceRoot, plan.id);
1780
+ writeText(workspaceRoot, path, serializeCanonical(plan));
1781
+ writeText(workspaceRoot, `${REFACTOR_DIR}/${plan.id}/plan.md`, renderRefactorMarkdown(plan));
1782
+ return path;
1783
+ }
1784
+
1785
+ export function readRefactorPlan(workspaceRoot: string, idOrPath: string): RefactorPlan | null {
1786
+ const candidates = [idOrPath, planPath(workspaceRoot, idOrPath), `${REFACTOR_DIR}/${idOrPath}/plan.json`];
1787
+ for (const candidate of candidates) {
1788
+ const content = readText(workspaceRoot, candidate);
1789
+ if (content) {
1790
+ return JSON.parse(content) as RefactorPlan;
1791
+ }
1792
+ }
1793
+ return null;
1794
+ }
1795
+
1796
+ function writeSnapshot(workspaceRoot: string, plan: RefactorPlan): void {
1797
+ const files: SnapshotFile[] = [];
1798
+ for (const file of plan.rollback.trackedFiles) {
1799
+ const content = readText(workspaceRoot, file);
1800
+ files.push({ file, existed: content !== null, ...(content !== null ? { content } : {}) });
1801
+ }
1802
+ const snapshot: RefactorSnapshot = { schemaVersion: "0.1.0", id: plan.id, files };
1803
+ writeText(workspaceRoot, snapshotPath(plan.id), serializeCanonical(snapshot));
1804
+ }
1805
+
1806
+ export function rollbackRefactor(workspaceRoot: string, id: string): RefactorResult {
1807
+ const raw = readText(workspaceRoot, snapshotPath(id));
1808
+ const plan = readRefactorPlan(workspaceRoot, id) ?? undefined;
1809
+ if (!raw) {
1810
+ return {
1811
+ ok: false,
1812
+ plan,
1813
+ diagnostics: [diagnostic("error", "FORGE_REFACTOR_ROLLBACK_FAILED", `missing rollback snapshot for ${id}`)],
1814
+ exitCode: 1,
1815
+ };
1816
+ }
1817
+ const snapshot = JSON.parse(raw) as RefactorSnapshot;
1818
+ for (const file of snapshot.files) {
1819
+ if (file.existed) {
1820
+ writeText(workspaceRoot, file.file, file.content ?? "");
1821
+ } else {
1822
+ removeFile(workspaceRoot, file.file);
1823
+ }
1824
+ }
1825
+ const record: RefactorRecord = {
1826
+ schemaVersion: "0.1.0",
1827
+ id,
1828
+ status: "rolled-back",
1829
+ summary: plan?.summary ?? id,
1830
+ filesModified: plan?.filesToModify.map((patch) => patch.file) ?? [],
1831
+ filesCreated: plan?.filesToCreate.map((file) => file.file) ?? [],
1832
+ result: { ok: true },
1833
+ };
1834
+ writeText(workspaceRoot, recordPath(id), serializeCanonical(record));
1835
+ return { ok: true, plan, record, diagnostics: [], explanation: `Rolled back ${id}`, exitCode: 0 };
1836
+ }
1837
+
1838
+ export function applyRefactorPlan(workspaceRoot: string, plan: RefactorPlan, force: boolean): RefactorResult {
1839
+ const diagnostics = [...plan.diagnostics];
1840
+ if (diagnostics.some((diag) => diag.severity === "error")) {
1841
+ return { ok: false, plan, diagnostics, exitCode: 1 };
1842
+ }
1843
+ writeRefactorPlan(workspaceRoot, plan);
1844
+ writeSnapshot(workspaceRoot, plan);
1845
+ for (const file of plan.filesToCreate) {
1846
+ if (file.exists && !force) {
1847
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_TARGET_EXISTS", `file already exists: ${file.file}`, file.file));
1848
+ continue;
1849
+ }
1850
+ writeText(workspaceRoot, file.file, file.content);
1851
+ }
1852
+ for (const patch of plan.filesToModify) {
1853
+ const current = readText(workspaceRoot, patch.file);
1854
+ if (patch.beforeHash && current !== null && hashStable(current) !== patch.beforeHash) {
1855
+ diagnostics.push(diagnostic("error", "FORGE_REFACTOR_PATCH_UNSAFE", `file changed after plan was created: ${patch.file}`, patch.file));
1856
+ continue;
1857
+ }
1858
+ writeText(workspaceRoot, patch.file, patch.afterPreview);
1859
+ }
1860
+ for (const file of plan.filesToDelete) {
1861
+ removeFile(workspaceRoot, file.file);
1862
+ }
1863
+ const ok = !diagnostics.some((diag) => diag.severity === "error");
1864
+ const record: RefactorRecord = {
1865
+ schemaVersion: "0.1.0",
1866
+ id: plan.id,
1867
+ status: ok ? "applied" : "rolled-back",
1868
+ summary: plan.summary,
1869
+ filesModified: plan.filesToModify.map((patch) => patch.file),
1870
+ filesCreated: plan.filesToCreate.map((file) => file.file),
1871
+ result: { ok },
1872
+ };
1873
+ writeText(workspaceRoot, recordPath(plan.id), serializeCanonical(record));
1874
+ return { ok, plan, record, diagnostics, exitCode: ok ? 0 : 1 };
1875
+ }
1876
+
1877
+ export function listRefactors(workspaceRoot: string): RefactorRecord[] {
1878
+ const records: RefactorRecord[] = [];
1879
+ for (const entry of readDirEntries(workspaceRoot, REFACTOR_DIR)) {
1880
+ if (!entry.isDirectory) {
1881
+ continue;
1882
+ }
1883
+ const record = readText(workspaceRoot, `${REFACTOR_DIR}/${entry.name}/applied.json`);
1884
+ if (record) {
1885
+ records.push(JSON.parse(record) as RefactorRecord);
1886
+ }
1887
+ }
1888
+ return records.sort((a, b) => a.id.localeCompare(b.id));
1889
+ }
1890
+
1891
+ export function renderRefactorDiff(plan: RefactorPlan): string {
1892
+ const lines: string[] = [];
1893
+ for (const file of plan.filesToCreate) {
1894
+ lines.push(`diff --forge-refactor ${file.file}`, `+++ ${file.file}`);
1895
+ for (const line of file.content.split(/\r?\n/)) {
1896
+ lines.push(`+${line}`);
1897
+ }
1898
+ }
1899
+ for (const patch of plan.filesToModify) {
1900
+ lines.push(`diff --forge-refactor ${patch.file}`, `--- ${patch.file}`, `+++ ${patch.file}`);
1901
+ for (const line of patch.afterPreview.split(/\r?\n/).slice(0, 120)) {
1902
+ lines.push(`+${line}`);
1903
+ }
1904
+ }
1905
+ for (const file of plan.filesToDelete) {
1906
+ lines.push(`diff --forge-refactor ${file.file}`, `--- ${file.file}`, `deleted file`);
1907
+ }
1908
+ return `${lines.join("\n")}\n`;
1909
+ }
1910
+
1911
+ export function renderRefactorMarkdown(plan: RefactorPlan): string {
1912
+ return `# Refactor plan: ${plan.id}
1913
+
1914
+ ${plan.summary}
1915
+
1916
+ Risk: ${plan.risk.level}
1917
+
1918
+ ## Will modify
1919
+
1920
+ ${plan.filesToModify.map((patch) => `- ${patch.file}`).join("\n") || "- none"}
1921
+
1922
+ ## Will create
1923
+
1924
+ ${plan.filesToCreate.map((file) => `- ${file.file}`).join("\n") || "- none"}
1925
+
1926
+ ## Will delete
1927
+
1928
+ ${plan.filesToDelete.map((file) => `- ${file.file}`).join("\n") || "- none"}
1929
+
1930
+ ## Generated impacts
1931
+
1932
+ ${plan.generatedImpacts.map((file) => `- ${file}`).join("\n") || "- none"}
1933
+
1934
+ ${plan.migrationPlan ? `## Migration hint\n\n${plan.migrationPlan.sql.map((sql) => `\`\`\`sql\n${sql}\n\`\`\``).join("\n")}` : ""}
1935
+ `;
1936
+ }