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,74 @@
1
+ import type { AuthContext } from "./types.ts";
2
+
3
+ export interface AuthHeaderInput {
4
+ userId?: string;
5
+ tenantId?: string;
6
+ role?: string;
7
+ }
8
+
9
+ export interface CliAuthInput {
10
+ userId?: string;
11
+ tenantId?: string;
12
+ role?: string;
13
+ }
14
+
15
+ export function resolveAuthFromHeaders(input: AuthHeaderInput): AuthContext {
16
+ const { userId, tenantId, role } = input;
17
+ if (userId && role) {
18
+ return {
19
+ kind: "user",
20
+ userId,
21
+ ...(tenantId ? { tenantId } : {}),
22
+ role,
23
+ roles: [role],
24
+ permissions: [],
25
+ token: {
26
+ issuer: "dev-headers",
27
+ audience: "dev",
28
+ subject: userId,
29
+ authProvider: "dev-headers",
30
+ },
31
+ claims: {},
32
+ };
33
+ }
34
+ return { kind: "anonymous" };
35
+ }
36
+
37
+ export function resolveAuthFromCli(input: CliAuthInput): AuthContext {
38
+ return resolveAuthFromHeaders(input);
39
+ }
40
+
41
+ export function parseAuthHeaders(headers: Headers): AuthContext {
42
+ return resolveAuthFromHeaders({
43
+ userId: headers.get("x-forge-user-id") ?? undefined,
44
+ tenantId: headers.get("x-forge-tenant-id") ?? undefined,
45
+ role: headers.get("x-forge-role") ?? undefined,
46
+ });
47
+ }
48
+
49
+ export function systemAuthFromSnapshot(
50
+ snapshot: AuthContext,
51
+ tenantId?: string,
52
+ ): AuthContext {
53
+ if (snapshot.kind === "user") {
54
+ return {
55
+ kind: "system",
56
+ tenantId: tenantId ?? snapshot.tenantId,
57
+ triggeredBy: snapshot,
58
+ };
59
+ }
60
+
61
+ if (snapshot.kind === "system") {
62
+ return {
63
+ kind: "system",
64
+ tenantId: tenantId ?? snapshot.tenantId,
65
+ triggeredBy: snapshot.triggeredBy ?? snapshot,
66
+ };
67
+ }
68
+
69
+ return {
70
+ kind: "system",
71
+ ...(tenantId ? { tenantId } : {}),
72
+ triggeredBy: { kind: "anonymous" },
73
+ };
74
+ }
@@ -0,0 +1,87 @@
1
+ export type AuthContext =
2
+ | {
3
+ kind: "user";
4
+ userId: string;
5
+ tenantId?: string;
6
+ role?: string;
7
+ roles?: string[];
8
+ permissions?: string[];
9
+ email?: string;
10
+ name?: string;
11
+ token?: {
12
+ issuer: string;
13
+ audience: string | string[];
14
+ subject: string;
15
+ expiresAt?: number;
16
+ issuedAt?: number;
17
+ authProvider: string;
18
+ };
19
+ claims?: Record<string, unknown>;
20
+ }
21
+ | {
22
+ kind: "system";
23
+ tenantId?: string;
24
+ triggeredBy?: AuthSnapshot;
25
+ }
26
+ | { kind: "anonymous" };
27
+
28
+ export type AuthSnapshot =
29
+ | {
30
+ kind: "user";
31
+ userId: string;
32
+ tenantId?: string;
33
+ role?: string;
34
+ roles?: string[];
35
+ permissions?: string[];
36
+ email?: string;
37
+ name?: string;
38
+ token?: {
39
+ issuer: string;
40
+ audience: string | string[];
41
+ subject: string;
42
+ expiresAt?: number;
43
+ issuedAt?: number;
44
+ authProvider: string;
45
+ };
46
+ claims?: Record<string, unknown>;
47
+ }
48
+ | {
49
+ kind: "system";
50
+ tenantId?: string;
51
+ triggeredBy?: AuthSnapshot;
52
+ }
53
+ | { kind: "anonymous" };
54
+
55
+ export function isUserAuth(auth: AuthContext): auth is Extract<AuthContext, { kind: "user" }> {
56
+ return auth.kind === "user";
57
+ }
58
+
59
+ export function isSystemAuth(auth: AuthContext): auth is Extract<AuthContext, { kind: "system" }> {
60
+ return auth.kind === "system";
61
+ }
62
+
63
+ export function snapshotAuth(auth: AuthContext): AuthSnapshot {
64
+ if (auth.kind === "user") {
65
+ return {
66
+ kind: "user",
67
+ userId: auth.userId,
68
+ ...(auth.tenantId ? { tenantId: auth.tenantId } : {}),
69
+ ...(auth.role ? { role: auth.role } : {}),
70
+ ...(auth.roles ? { roles: auth.roles } : {}),
71
+ ...(auth.permissions ? { permissions: auth.permissions } : {}),
72
+ ...(auth.email ? { email: auth.email } : {}),
73
+ ...(auth.name ? { name: auth.name } : {}),
74
+ ...(auth.token ? { token: auth.token } : {}),
75
+ };
76
+ }
77
+
78
+ if (auth.kind === "system") {
79
+ return {
80
+ kind: "system",
81
+ ...(auth.tenantId ? { tenantId: auth.tenantId } : {}),
82
+ ...(auth.triggeredBy ? { triggeredBy: auth.triggeredBy } : {}),
83
+ };
84
+ }
85
+
86
+ return { kind: "anonymous" };
87
+ }
@@ -0,0 +1,138 @@
1
+ import { createRemoteJWKSet, errors, jwtVerify, type JWTPayload } from "jose";
2
+ import {
3
+ FORGE_AUTH_INVALID_AUDIENCE,
4
+ FORGE_AUTH_INVALID_ISSUER,
5
+ FORGE_AUTH_INVALID_TOKEN,
6
+ FORGE_AUTH_JWKS_FAILED,
7
+ FORGE_AUTH_TOKEN_EXPIRED,
8
+ } from "../../compiler/diagnostics/codes.ts";
9
+ import type { ForgeAuthConfig } from "./config.ts";
10
+ import { ForgeAuthError } from "./errors.ts";
11
+ import type { VerifiedTokenMetadata } from "./claims.ts";
12
+
13
+ const jwksCache = new Map<string, ReturnType<typeof createRemoteJWKSet>>();
14
+ const oidcDiscoveryCache = new Map<string, Promise<string>>();
15
+
16
+ function getRemoteJwks(jwksUri: string): ReturnType<typeof createRemoteJWKSet> {
17
+ const cached = jwksCache.get(jwksUri);
18
+ if (cached) {
19
+ return cached;
20
+ }
21
+ const jwks = createRemoteJWKSet(new URL(jwksUri));
22
+ jwksCache.set(jwksUri, jwks);
23
+ return jwks;
24
+ }
25
+
26
+ function oidcMetadataUrl(issuer: string): string {
27
+ return `${issuer.replace(/\/+$/, "")}/.well-known/openid-configuration`;
28
+ }
29
+
30
+ export async function discoverOidcJwksUri(issuer: string): Promise<string> {
31
+ const cached = oidcDiscoveryCache.get(issuer);
32
+ if (cached) {
33
+ return cached;
34
+ }
35
+
36
+ const discovered = (async () => {
37
+ const response = await fetch(oidcMetadataUrl(issuer));
38
+ if (!response.ok) {
39
+ throw new ForgeAuthError(
40
+ FORGE_AUTH_JWKS_FAILED,
41
+ `OIDC discovery failed with status ${response.status}`,
42
+ );
43
+ }
44
+ const metadata = (await response.json()) as { jwks_uri?: unknown };
45
+ if (typeof metadata.jwks_uri !== "string" || metadata.jwks_uri.length === 0) {
46
+ throw new ForgeAuthError(
47
+ FORGE_AUTH_JWKS_FAILED,
48
+ "OIDC discovery did not include jwks_uri",
49
+ );
50
+ }
51
+ return metadata.jwks_uri;
52
+ })();
53
+
54
+ oidcDiscoveryCache.set(issuer, discovered);
55
+ return discovered;
56
+ }
57
+
58
+ function classifyJoseError(error: unknown): ForgeAuthError {
59
+ if (error instanceof ForgeAuthError) {
60
+ return error;
61
+ }
62
+ if (error instanceof errors.JWTExpired) {
63
+ return new ForgeAuthError(FORGE_AUTH_TOKEN_EXPIRED, "JWT token is expired", {
64
+ cause: error,
65
+ });
66
+ }
67
+ if (error instanceof errors.JWTClaimValidationFailed) {
68
+ const claim = error.claim;
69
+ if (claim === "iss") {
70
+ return new ForgeAuthError(
71
+ FORGE_AUTH_INVALID_ISSUER,
72
+ "JWT issuer did not match Forge auth config",
73
+ { cause: error },
74
+ );
75
+ }
76
+ if (claim === "aud") {
77
+ return new ForgeAuthError(
78
+ FORGE_AUTH_INVALID_AUDIENCE,
79
+ "JWT audience did not match Forge auth config",
80
+ { cause: error },
81
+ );
82
+ }
83
+ }
84
+ if (error instanceof errors.JWKSNoMatchingKey || error instanceof errors.JOSEError) {
85
+ return new ForgeAuthError(
86
+ FORGE_AUTH_INVALID_TOKEN,
87
+ "JWT token could not be verified",
88
+ { cause: error },
89
+ );
90
+ }
91
+ return new ForgeAuthError(FORGE_AUTH_INVALID_TOKEN, "JWT token is invalid", {
92
+ cause: error,
93
+ });
94
+ }
95
+
96
+ function jwtPayloadToRecord(payload: JWTPayload): Record<string, unknown> {
97
+ return { ...payload };
98
+ }
99
+
100
+ export async function verifyJwtToken(
101
+ token: string,
102
+ config: ForgeAuthConfig,
103
+ ): Promise<{ payload: Record<string, unknown>; token: VerifiedTokenMetadata }> {
104
+ try {
105
+ const jwksUri =
106
+ config.mode === "oidc"
107
+ ? await discoverOidcJwksUri(config.issuer ?? "")
108
+ : config.jwksUri;
109
+
110
+ if (!jwksUri) {
111
+ throw new ForgeAuthError(
112
+ FORGE_AUTH_JWKS_FAILED,
113
+ "FORGE_AUTH_JWKS_URI is required for JWT auth",
114
+ );
115
+ }
116
+
117
+ const verified = await jwtVerify(token, getRemoteJwks(jwksUri), {
118
+ issuer: config.issuer,
119
+ audience: config.audience,
120
+ algorithms: config.algorithms,
121
+ });
122
+ const payload = jwtPayloadToRecord(verified.payload);
123
+
124
+ return {
125
+ payload,
126
+ token: {
127
+ issuer: verified.payload.iss,
128
+ audience: verified.payload.aud,
129
+ subject: verified.payload.sub,
130
+ expiresAt: verified.payload.exp,
131
+ issuedAt: verified.payload.iat,
132
+ authProvider: config.mode,
133
+ },
134
+ };
135
+ } catch (error) {
136
+ throw classifyJoseError(error);
137
+ }
138
+ }
@@ -0,0 +1,204 @@
1
+ import type { DbClient } from "../db/generated-client.ts";
2
+ import type { DbTransaction } from "../db/adapter.ts";
3
+ import type { ActionSubscription } from "../../compiler/types/action-subscriptions.ts";
4
+ import type { TelemetryContext } from "../telemetry/types.ts";
5
+ import type { AuthContext } from "../auth/types.ts";
6
+ import type { RuntimeContext } from "../../compiler/types/runtime.ts";
7
+ import { snapshotAuth } from "../auth/types.ts";
8
+ import { createNoopTelemetryContext } from "../telemetry/context.ts";
9
+ import { insertOutbox } from "../db/outbox.ts";
10
+ import type { ConfigContext, SecretsContext } from "../secrets/types.ts";
11
+ import type { RuntimeEnvStore } from "../secrets/types.ts";
12
+ import { loadEnvFiles } from "../secrets/env-loader.ts";
13
+ import { loadEnvSchema, loadSecretRegistry } from "../secrets/check.ts";
14
+ import { createRuntimeSecretsBundle } from "../secrets/runtime-bundle.ts";
15
+ import { createAiContext } from "../ai/context.ts";
16
+ import type { AiContext } from "../ai/types.ts";
17
+ import { isMockAiEnabled } from "../ai/state.ts";
18
+ import { currentReleaseInfo, type RuntimeReleaseInfo } from "../release/runtime.ts";
19
+
20
+ export interface ForgeContext {
21
+ db: DbClient;
22
+ emit: (eventType: string, payload: unknown) => Promise<void>;
23
+ env: Record<string, string | undefined>;
24
+ telemetry: TelemetryContext;
25
+ auth: AuthContext;
26
+ secrets: SecretsContext;
27
+ config: ConfigContext;
28
+ ai: AiContext;
29
+ release: RuntimeReleaseInfo;
30
+ }
31
+
32
+ let sharedEnvStore: RuntimeEnvStore | null = null;
33
+ let sharedEnvWorkspaceRoot: string | null = null;
34
+
35
+ export function initializeRuntimeEnv(
36
+ workspaceRoot: string,
37
+ envFiles?: string[],
38
+ ): RuntimeEnvStore {
39
+ const { store } = loadEnvFiles({ workspaceRoot, envFiles });
40
+ sharedEnvStore = store;
41
+ sharedEnvWorkspaceRoot = workspaceRoot;
42
+ return store;
43
+ }
44
+
45
+ export function getRuntimeEnvStore(workspaceRoot?: string): RuntimeEnvStore {
46
+ if (sharedEnvStore && (!workspaceRoot || workspaceRoot === sharedEnvWorkspaceRoot)) {
47
+ return sharedEnvStore;
48
+ }
49
+
50
+ if (workspaceRoot) {
51
+ return initializeRuntimeEnv(workspaceRoot);
52
+ }
53
+
54
+ return {
55
+ loadedFiles: [],
56
+ resolve(name: string): string | undefined {
57
+ return process.env[name];
58
+ },
59
+ snapshot(): Record<string, string | undefined> {
60
+ return { ...process.env };
61
+ },
62
+ };
63
+ }
64
+
65
+ function buildSecretsConfigAndAi(
66
+ workspaceRoot: string | undefined,
67
+ runtimeKind: RuntimeContext,
68
+ store: RuntimeEnvStore,
69
+ telemetry: TelemetryContext,
70
+ options?: { mockAi?: boolean },
71
+ ): {
72
+ secrets: SecretsContext;
73
+ config: ConfigContext;
74
+ ai: AiContext;
75
+ env: Record<string, string | undefined>;
76
+ } {
77
+ const registry = workspaceRoot ? loadSecretRegistry(workspaceRoot) : null;
78
+ const envSchema = workspaceRoot ? loadEnvSchema(workspaceRoot) : null;
79
+ const bundle = createRuntimeSecretsBundle({
80
+ store,
81
+ registry,
82
+ envSchema,
83
+ runtimeKind,
84
+ });
85
+
86
+ const ai = createAiContext({
87
+ secrets: bundle.secrets,
88
+ telemetry,
89
+ runtimeKind,
90
+ mockAi: options?.mockAi ?? isMockAiEnabled(),
91
+ envelope: {
92
+ traceId: telemetry.traceId,
93
+ tenantId:
94
+ undefined,
95
+ },
96
+ });
97
+
98
+ return {
99
+ secrets: bundle.secrets,
100
+ config: bundle.config,
101
+ ai,
102
+ env: store.snapshot(),
103
+ };
104
+ }
105
+
106
+ export function createForgeContext(
107
+ tx: DbTransaction,
108
+ db: DbClient,
109
+ subscriptions: ActionSubscription[],
110
+ telemetry: TelemetryContext,
111
+ auth: AuthContext,
112
+ options?: {
113
+ env?: Record<string, string | undefined>;
114
+ runtimeKind?: RuntimeContext;
115
+ workspaceRoot?: string;
116
+ store?: RuntimeEnvStore;
117
+ mockAi?: boolean;
118
+ },
119
+ ): ForgeContext {
120
+ const runtimeKind = options?.runtimeKind ?? "command";
121
+ const store =
122
+ options?.store ??
123
+ (options?.workspaceRoot
124
+ ? getRuntimeEnvStore(options.workspaceRoot)
125
+ : getRuntimeEnvStore());
126
+ const { secrets, config, ai, env } = buildSecretsConfigAndAi(
127
+ options?.workspaceRoot,
128
+ runtimeKind,
129
+ store,
130
+ telemetry,
131
+ { mockAi: options?.mockAi },
132
+ );
133
+
134
+ return {
135
+ db,
136
+ env: options?.env ?? env,
137
+ telemetry,
138
+ auth,
139
+ secrets,
140
+ config,
141
+ ai,
142
+ release: currentReleaseInfo(),
143
+ emit: async (eventType, payload) => {
144
+ const enriched =
145
+ payload && typeof payload === "object" && !Array.isArray(payload)
146
+ ? { ...(payload as Record<string, unknown>), traceId: telemetry.traceId }
147
+ : { value: payload, traceId: telemetry.traceId };
148
+
149
+ const result = await insertOutbox(
150
+ tx,
151
+ eventType,
152
+ enriched,
153
+ subscriptions,
154
+ snapshotAuth(auth),
155
+ );
156
+ if (!result.ok) {
157
+ throw new Error(result.diagnostic.message);
158
+ }
159
+ },
160
+ };
161
+ }
162
+
163
+ export function createActionContext(
164
+ db: DbClient,
165
+ telemetry: TelemetryContext,
166
+ auth: AuthContext,
167
+ options?: {
168
+ env?: Record<string, string | undefined>;
169
+ workspaceRoot?: string;
170
+ store?: RuntimeEnvStore;
171
+ runtimeKind?: RuntimeContext;
172
+ mockAi?: boolean;
173
+ },
174
+ ): ForgeContext {
175
+ const runtimeKind = options?.runtimeKind ?? "action";
176
+ const store =
177
+ options?.store ??
178
+ (options?.workspaceRoot
179
+ ? getRuntimeEnvStore(options.workspaceRoot)
180
+ : getRuntimeEnvStore());
181
+ const { secrets, config, ai, env } = buildSecretsConfigAndAi(
182
+ options?.workspaceRoot,
183
+ runtimeKind,
184
+ store,
185
+ telemetry,
186
+ { mockAi: options?.mockAi },
187
+ );
188
+
189
+ return {
190
+ db,
191
+ env: options?.env ?? env,
192
+ telemetry,
193
+ auth,
194
+ secrets,
195
+ config,
196
+ ai,
197
+ release: currentReleaseInfo(),
198
+ emit: async () => {
199
+ /* actions invoked by outbox worker do not emit */
200
+ },
201
+ };
202
+ }
203
+
204
+ export { createNoopTelemetryContext };
@@ -0,0 +1,34 @@
1
+ import type { TelemetryContext } from "../telemetry/types.ts";
2
+ import type { AuthContext } from "../auth/types.ts";
3
+ import type { ReadOnlyDbClient } from "../db/read-only-client.ts";
4
+ import { assertQueryContextForbidden } from "../db/read-only-client.ts";
5
+
6
+ export interface QueryContext {
7
+ db: ReadOnlyDbClient;
8
+ telemetry: TelemetryContext;
9
+ auth: AuthContext;
10
+ emit: never;
11
+ secrets: never;
12
+ ai: never;
13
+ }
14
+
15
+ export function createQueryContext(
16
+ db: ReadOnlyDbClient,
17
+ telemetry: TelemetryContext,
18
+ auth: AuthContext,
19
+ ): QueryContext {
20
+ return {
21
+ db,
22
+ telemetry,
23
+ auth,
24
+ get emit(): never {
25
+ return assertQueryContextForbidden("emit");
26
+ },
27
+ get secrets(): never {
28
+ return assertQueryContextForbidden("secrets");
29
+ },
30
+ get ai(): never {
31
+ return assertQueryContextForbidden("ai");
32
+ },
33
+ };
34
+ }
@@ -0,0 +1,31 @@
1
+ export interface DbQueryResult {
2
+ rows: Record<string, unknown>[];
3
+ rowCount: number;
4
+ }
5
+
6
+ export interface DbTransaction {
7
+ query(sql: string, params?: unknown[]): Promise<DbQueryResult>;
8
+ commit(): Promise<void>;
9
+ rollback(): Promise<void>;
10
+ }
11
+
12
+ export type DbAdapterKind = "pglite" | "postgres" | "memory";
13
+
14
+ export interface DbAdapter {
15
+ kind: DbAdapterKind;
16
+ query(sql: string, params?: unknown[]): Promise<DbQueryResult>;
17
+ begin(): Promise<DbTransaction>;
18
+ close(): Promise<void>;
19
+ }
20
+
21
+ export function adapterAsTransaction(adapter: DbAdapter): DbTransaction {
22
+ return {
23
+ query: (sql, params) => adapter.query(sql, params),
24
+ commit: async () => {
25
+ /* non-transactional */
26
+ },
27
+ rollback: async () => {
28
+ /* non-transactional */
29
+ },
30
+ };
31
+ }
@@ -0,0 +1,83 @@
1
+ import { join } from "node:path";
2
+ import { createDiagnostic } from "../../compiler/diagnostics/create.ts";
3
+ import { FORGE_DB_CONNECT_FAILED } from "../../compiler/diagnostics/codes.ts";
4
+ import type { Diagnostic } from "../../compiler/types/diagnostic.ts";
5
+ import type { DbAdapter, DbAdapterKind } from "./adapter.ts";
6
+ import { createMemoryAdapter } from "./memory-adapter.ts";
7
+ import { createPgliteAdapter } from "./pglite-adapter.ts";
8
+ import { createPostgresAdapter } from "./postgres-adapter.ts";
9
+
10
+ export interface CreateDbAdapterOptions {
11
+ kind: DbAdapterKind;
12
+ workspaceRoot?: string;
13
+ databaseUrl?: string;
14
+ dataDir?: string;
15
+ }
16
+
17
+ export interface CreateDbAdapterResult {
18
+ adapter: DbAdapter | null;
19
+ diagnostics: Diagnostic[];
20
+ }
21
+
22
+ const DEFAULT_PGLITE_DIR = ".forge/pglite";
23
+
24
+ export async function createDbAdapter(
25
+ options: CreateDbAdapterOptions,
26
+ ): Promise<CreateDbAdapterResult> {
27
+ const diagnostics: Diagnostic[] = [];
28
+
29
+ try {
30
+ if (options.kind === "memory") {
31
+ return { adapter: createMemoryAdapter(), diagnostics };
32
+ }
33
+
34
+ if (options.kind === "pglite") {
35
+ const workspaceRoot = options.workspaceRoot ?? process.cwd();
36
+ const dataDir = options.dataDir ?? join(workspaceRoot, DEFAULT_PGLITE_DIR);
37
+ const adapter = await createPgliteAdapter(dataDir);
38
+ return { adapter, diagnostics };
39
+ }
40
+
41
+ if (options.kind === "postgres") {
42
+ const databaseUrl = options.databaseUrl ?? process.env.DATABASE_URL;
43
+ if (!databaseUrl) {
44
+ diagnostics.push(
45
+ createDiagnostic({
46
+ severity: "error",
47
+ code: FORGE_DB_CONNECT_FAILED,
48
+ message: "postgres adapter requires --database-url or DATABASE_URL",
49
+ }),
50
+ );
51
+ return { adapter: null, diagnostics };
52
+ }
53
+
54
+ const result = await createPostgresAdapter(databaseUrl);
55
+ if (!result.adapter) {
56
+ diagnostics.push(result.diagnostic);
57
+ return { adapter: null, diagnostics };
58
+ }
59
+
60
+ await result.adapter.query("SELECT 1");
61
+ return { adapter: result.adapter, diagnostics };
62
+ }
63
+
64
+ diagnostics.push(
65
+ createDiagnostic({
66
+ severity: "error",
67
+ code: FORGE_DB_CONNECT_FAILED,
68
+ message: `unsupported db adapter kind '${options.kind as string}'`,
69
+ }),
70
+ );
71
+ return { adapter: null, diagnostics };
72
+ } catch (error) {
73
+ const message = error instanceof Error ? error.message : "database connection failed";
74
+ diagnostics.push(
75
+ createDiagnostic({
76
+ severity: "error",
77
+ code: FORGE_DB_CONNECT_FAILED,
78
+ message,
79
+ }),
80
+ );
81
+ return { adapter: null, diagnostics };
82
+ }
83
+ }