primitive-admin 1.2.0-alpha.13 → 1.2.0-alpha.15

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 (324) hide show
  1. package/README.md +102 -78
  2. package/assets/skill/skills/primitive-platform/SKILL.md +87 -23
  3. package/dist/bin/primitive.js +11 -82
  4. package/dist/bin/primitive.js.map +1 -1
  5. package/dist/src/commands/admins.js +79 -17
  6. package/dist/src/commands/admins.js.map +1 -1
  7. package/dist/src/commands/agent-sessions.js +2 -0
  8. package/dist/src/commands/agent-sessions.js.map +1 -1
  9. package/dist/src/commands/analytics.js +220 -18
  10. package/dist/src/commands/analytics.js.map +1 -1
  11. package/dist/src/commands/apps-children.js +2 -0
  12. package/dist/src/commands/apps-children.js.map +1 -1
  13. package/dist/src/commands/apps.js +19 -2
  14. package/dist/src/commands/apps.js.map +1 -1
  15. package/dist/src/commands/auth.js +11 -1
  16. package/dist/src/commands/auth.js.map +1 -1
  17. package/dist/src/commands/collections.js +13 -0
  18. package/dist/src/commands/collections.js.map +1 -1
  19. package/dist/src/commands/config.d.ts +1 -1
  20. package/dist/src/commands/config.js +40 -9
  21. package/dist/src/commands/config.js.map +1 -1
  22. package/dist/src/commands/connections.js +2 -0
  23. package/dist/src/commands/connections.js.map +1 -1
  24. package/dist/src/commands/database-type-configs.js +5 -3
  25. package/dist/src/commands/database-type-configs.js.map +1 -1
  26. package/dist/src/commands/databases.js +6 -4
  27. package/dist/src/commands/databases.js.map +1 -1
  28. package/dist/src/commands/db3-types.js +2 -0
  29. package/dist/src/commands/db3-types.js.map +1 -1
  30. package/dist/src/commands/db3.d.ts +12 -0
  31. package/dist/src/commands/db3.js +345 -11
  32. package/dist/src/commands/db3.js.map +1 -1
  33. package/dist/src/commands/documents.d.ts +52 -12
  34. package/dist/src/commands/documents.js +120 -29
  35. package/dist/src/commands/documents.js.map +1 -1
  36. package/dist/src/commands/email-templates.js +72 -0
  37. package/dist/src/commands/email-templates.js.map +1 -1
  38. package/dist/src/commands/functions.js +47 -10
  39. package/dist/src/commands/functions.js.map +1 -1
  40. package/dist/src/commands/groups.js +18 -3
  41. package/dist/src/commands/groups.js.map +1 -1
  42. package/dist/src/commands/init.js +7 -1
  43. package/dist/src/commands/init.js.map +1 -1
  44. package/dist/src/commands/integrations.js +80 -47
  45. package/dist/src/commands/integrations.js.map +1 -1
  46. package/dist/src/commands/metadata-category-configs.js +2 -0
  47. package/dist/src/commands/metadata-category-configs.js.map +1 -1
  48. package/dist/src/commands/metadata.js +41 -0
  49. package/dist/src/commands/metadata.js.map +1 -1
  50. package/dist/src/commands/prompts.js +73 -47
  51. package/dist/src/commands/prompts.js.map +1 -1
  52. package/dist/src/commands/rule-sets.js +2 -0
  53. package/dist/src/commands/rule-sets.js.map +1 -1
  54. package/dist/src/commands/schemas.d.ts +15 -0
  55. package/dist/src/commands/schemas.js +122 -0
  56. package/dist/src/commands/schemas.js.map +1 -0
  57. package/dist/src/commands/secrets.js +45 -3
  58. package/dist/src/commands/secrets.js.map +1 -1
  59. package/dist/src/commands/sessions.js +2 -0
  60. package/dist/src/commands/sessions.js.map +1 -1
  61. package/dist/src/commands/sync-schemas.d.ts +202 -0
  62. package/dist/src/commands/sync-schemas.js +629 -0
  63. package/dist/src/commands/sync-schemas.js.map +1 -0
  64. package/dist/src/commands/sync-user-properties.d.ts +118 -0
  65. package/dist/src/commands/sync-user-properties.js +276 -0
  66. package/dist/src/commands/sync-user-properties.js.map +1 -0
  67. package/dist/src/commands/sync.d.ts +243 -12
  68. package/dist/src/commands/sync.js +1474 -314
  69. package/dist/src/commands/sync.js.map +1 -1
  70. package/dist/src/commands/user-properties.d.ts +14 -0
  71. package/dist/src/commands/user-properties.js +79 -0
  72. package/dist/src/commands/user-properties.js.map +1 -0
  73. package/dist/src/commands/users.js +912 -18
  74. package/dist/src/commands/users.js.map +1 -1
  75. package/dist/src/commands/waitlist.js +39 -4
  76. package/dist/src/commands/waitlist.js.map +1 -1
  77. package/dist/src/lib/analytics-query.d.ts +58 -0
  78. package/dist/src/lib/analytics-query.js +134 -0
  79. package/dist/src/lib/analytics-query.js.map +1 -0
  80. package/dist/src/lib/api-client.d.ts +779 -95
  81. package/dist/src/lib/api-client.js +369 -166
  82. package/dist/src/lib/api-client.js.map +1 -1
  83. package/dist/src/lib/block-selector.d.ts +9 -15
  84. package/dist/src/lib/block-selector.js +9 -20
  85. package/dist/src/lib/block-selector.js.map +1 -1
  86. package/dist/src/lib/cli-manifest.d.ts +8 -0
  87. package/dist/src/lib/cli-manifest.js +10 -1
  88. package/dist/src/lib/cli-manifest.js.map +1 -1
  89. package/dist/src/lib/cli-version.d.ts +14 -0
  90. package/dist/src/lib/cli-version.js +38 -0
  91. package/dist/src/lib/cli-version.js.map +1 -0
  92. package/dist/src/lib/codegen-shared/prettierStable.d.ts +41 -0
  93. package/dist/src/lib/codegen-shared/prettierStable.js +146 -24
  94. package/dist/src/lib/codegen-shared/prettierStable.js.map +1 -1
  95. package/dist/src/lib/command-status.d.ts +110 -0
  96. package/dist/src/lib/command-status.js +184 -0
  97. package/dist/src/lib/command-status.js.map +1 -0
  98. package/dist/src/lib/config-object-descriptor.d.ts +6 -0
  99. package/dist/src/lib/config-object-descriptor.js +71 -0
  100. package/dist/src/lib/config-object-descriptor.js.map +1 -1
  101. package/dist/src/lib/config-toml.d.ts +11 -2
  102. package/dist/src/lib/config-toml.js +25 -2
  103. package/dist/src/lib/config-toml.js.map +1 -1
  104. package/dist/src/lib/data-input.d.ts +1 -1
  105. package/dist/src/lib/data-input.js +1 -1
  106. package/dist/src/lib/db-codegen/dbTsTypes.d.ts +1 -1
  107. package/dist/src/lib/db-codegen/dbTsTypes.js +3 -0
  108. package/dist/src/lib/db-codegen/dbTsTypes.js.map +1 -1
  109. package/dist/src/lib/db-codegen/dbTypeIR.d.ts +11 -1
  110. package/dist/src/lib/db-codegen/dbTypeIR.js +11 -5
  111. package/dist/src/lib/db-codegen/dbTypeIR.js.map +1 -1
  112. package/dist/src/lib/db-type-records.d.ts +95 -0
  113. package/dist/src/lib/db-type-records.js +219 -0
  114. package/dist/src/lib/db-type-records.js.map +1 -0
  115. package/dist/src/lib/db3-export.d.ts +65 -9
  116. package/dist/src/lib/db3-export.js +159 -22
  117. package/dist/src/lib/db3-export.js.map +1 -1
  118. package/dist/src/lib/db3-index-output.d.ts +69 -3
  119. package/dist/src/lib/db3-index-output.js +142 -4
  120. package/dist/src/lib/db3-index-output.js.map +1 -1
  121. package/dist/src/lib/db3-type-models.d.ts +39 -7
  122. package/dist/src/lib/db3-type-models.js +138 -27
  123. package/dist/src/lib/db3-type-models.js.map +1 -1
  124. package/dist/src/lib/deprecation.d.ts +2 -2
  125. package/dist/src/lib/deprecation.js +2 -2
  126. package/dist/src/lib/experimental-apis.d.ts +36 -0
  127. package/dist/src/lib/experimental-apis.js +46 -0
  128. package/dist/src/lib/experimental-apis.js.map +1 -0
  129. package/dist/src/lib/file-attachment.d.ts +24 -0
  130. package/dist/src/lib/file-attachment.js +31 -0
  131. package/dist/src/lib/file-attachment.js.map +1 -0
  132. package/dist/src/lib/function-authorize-check.d.ts +1 -1
  133. package/dist/src/lib/function-authorize-check.js +2 -1
  134. package/dist/src/lib/function-authorize-check.js.map +1 -1
  135. package/dist/src/lib/function-authorize-view.js +4 -0
  136. package/dist/src/lib/function-authorize-view.js.map +1 -1
  137. package/dist/src/lib/function-bundle.d.ts +27 -5
  138. package/dist/src/lib/function-bundle.js +106 -7
  139. package/dist/src/lib/function-bundle.js.map +1 -1
  140. package/dist/src/lib/function-collect.d.ts +2 -0
  141. package/dist/src/lib/function-collect.js +36 -7
  142. package/dist/src/lib/function-collect.js.map +1 -1
  143. package/dist/src/lib/function-db-types.d.ts +57 -40
  144. package/dist/src/lib/function-db-types.js +246 -285
  145. package/dist/src/lib/function-db-types.js.map +1 -1
  146. package/dist/src/lib/function-document-types.d.ts +19 -7
  147. package/dist/src/lib/function-document-types.js +24 -12
  148. package/dist/src/lib/function-document-types.js.map +1 -1
  149. package/dist/src/lib/function-run.d.ts +1 -1
  150. package/dist/src/lib/function-run.js +1 -1
  151. package/dist/src/lib/function-run.js.map +1 -1
  152. package/dist/src/lib/function-schema-codegen.d.ts +58 -18
  153. package/dist/src/lib/function-schema-codegen.js +85 -74
  154. package/dist/src/lib/function-schema-codegen.js.map +1 -1
  155. package/dist/src/lib/function-schema-refs.d.ts +98 -0
  156. package/dist/src/lib/function-schema-refs.js +177 -0
  157. package/dist/src/lib/function-schema-refs.js.map +1 -0
  158. package/dist/src/lib/function-sync.d.ts +38 -4
  159. package/dist/src/lib/function-sync.js +83 -14
  160. package/dist/src/lib/function-sync.js.map +1 -1
  161. package/dist/src/lib/function-typecheck.d.ts +52 -0
  162. package/dist/src/lib/function-typecheck.js +171 -54
  163. package/dist/src/lib/function-typecheck.js.map +1 -1
  164. package/dist/src/lib/generated-build-flags.d.ts +13 -0
  165. package/dist/src/lib/generated-build-flags.js +14 -0
  166. package/dist/src/lib/generated-build-flags.js.map +1 -0
  167. package/dist/src/lib/generated-config-surfaces.d.ts +211 -11
  168. package/dist/src/lib/generated-config-surfaces.js +579 -69
  169. package/dist/src/lib/generated-config-surfaces.js.map +1 -1
  170. package/dist/src/lib/generated-sdk-types.d.ts +1 -1
  171. package/dist/src/lib/generated-sdk-types.js +1 -1
  172. package/dist/src/lib/generated-sdk-types.js.map +1 -1
  173. package/dist/src/lib/integration-test-request.d.ts +54 -0
  174. package/dist/src/lib/integration-test-request.js +207 -0
  175. package/dist/src/lib/integration-test-request.js.map +1 -0
  176. package/dist/src/lib/log-inspection.d.ts +3 -0
  177. package/dist/src/lib/log-inspection.js +1 -0
  178. package/dist/src/lib/log-inspection.js.map +1 -1
  179. package/dist/src/lib/metadata-report.d.ts +85 -0
  180. package/dist/src/lib/metadata-report.js +81 -0
  181. package/dist/src/lib/metadata-report.js.map +1 -0
  182. package/dist/src/lib/output.d.ts +1 -1
  183. package/dist/src/lib/output.js +1 -1
  184. package/dist/src/lib/platform-owned.js +5 -0
  185. package/dist/src/lib/platform-owned.js.map +1 -1
  186. package/dist/src/lib/primitive-declaration-names.d.ts +21 -0
  187. package/dist/src/lib/primitive-declaration-names.js +22 -0
  188. package/dist/src/lib/primitive-declaration-names.js.map +1 -0
  189. package/dist/src/lib/primitive-declaration.d.ts +143 -0
  190. package/dist/src/lib/primitive-declaration.js +658 -0
  191. package/dist/src/lib/primitive-declaration.js.map +1 -0
  192. package/dist/src/lib/prompt-schema-codegen.d.ts +36 -12
  193. package/dist/src/lib/prompt-schema-codegen.js +82 -98
  194. package/dist/src/lib/prompt-schema-codegen.js.map +1 -1
  195. package/dist/src/lib/prompt-schema-document.d.ts +35 -0
  196. package/dist/src/lib/prompt-schema-document.js +92 -0
  197. package/dist/src/lib/prompt-schema-document.js.map +1 -0
  198. package/dist/src/lib/prompt-schema-refs.d.ts +164 -0
  199. package/dist/src/lib/prompt-schema-refs.js +452 -0
  200. package/dist/src/lib/prompt-schema-refs.js.map +1 -0
  201. package/dist/src/lib/records-verbs.d.ts +12 -2
  202. package/dist/src/lib/records-verbs.js +7 -2
  203. package/dist/src/lib/records-verbs.js.map +1 -1
  204. package/dist/src/lib/register-commands.d.ts +28 -0
  205. package/dist/src/lib/register-commands.js +101 -0
  206. package/dist/src/lib/register-commands.js.map +1 -0
  207. package/dist/src/lib/retired-imports.d.ts +53 -0
  208. package/dist/src/lib/retired-imports.js +120 -0
  209. package/dist/src/lib/retired-imports.js.map +1 -0
  210. package/dist/src/lib/schema-codegen/compiledSpace.d.ts +62 -0
  211. package/dist/src/lib/schema-codegen/compiledSpace.js +103 -0
  212. package/dist/src/lib/schema-codegen/compiledSpace.js.map +1 -0
  213. package/dist/src/lib/schema-codegen/generated-db3-bridge.d.ts +86 -0
  214. package/dist/src/lib/schema-codegen/generated-db3-bridge.js +221 -0
  215. package/dist/src/lib/schema-codegen/generated-db3-bridge.js.map +1 -0
  216. package/dist/src/lib/schema-codegen/generated-db3-query-capabilities.d.ts +85 -0
  217. package/dist/src/lib/schema-codegen/generated-db3-query-capabilities.js +86 -0
  218. package/dist/src/lib/schema-codegen/generated-db3-query-capabilities.js.map +1 -0
  219. package/dist/src/lib/schema-codegen/generated-field-grammar.d.ts +209 -0
  220. package/dist/src/lib/schema-codegen/generated-field-grammar.js +379 -0
  221. package/dist/src/lib/schema-codegen/generated-field-grammar.js.map +1 -0
  222. package/dist/src/lib/schema-codegen/generated-field-table.d.ts +80 -0
  223. package/dist/src/lib/schema-codegen/generated-field-table.js +542 -0
  224. package/dist/src/lib/schema-codegen/generated-field-table.js.map +1 -0
  225. package/dist/src/lib/schema-codegen/generated-prompt-schemas.d.ts +195 -0
  226. package/dist/src/lib/schema-codegen/generated-prompt-schemas.js +503 -0
  227. package/dist/src/lib/schema-codegen/generated-prompt-schemas.js.map +1 -0
  228. package/dist/src/lib/schema-codegen/generated-schema-descriptor.d.ts +129 -0
  229. package/dist/src/lib/schema-codegen/generated-schema-descriptor.js +154 -1
  230. package/dist/src/lib/schema-codegen/generated-schema-descriptor.js.map +1 -1
  231. package/dist/src/lib/schema-codegen/generated-schema-file.d.ts +129 -0
  232. package/dist/src/lib/schema-codegen/generated-schema-file.js +475 -0
  233. package/dist/src/lib/schema-codegen/generated-schema-file.js.map +1 -0
  234. package/dist/src/lib/schema-codegen/generated-schema-provenance.d.ts +115 -0
  235. package/dist/src/lib/schema-codegen/generated-schema-provenance.js +274 -0
  236. package/dist/src/lib/schema-codegen/generated-schema-provenance.js.map +1 -0
  237. package/dist/src/lib/schema-codegen/generated-schema-refs.d.ts +366 -0
  238. package/dist/src/lib/schema-codegen/generated-schema-refs.js +1789 -0
  239. package/dist/src/lib/schema-codegen/generated-schema-refs.js.map +1 -0
  240. package/dist/src/lib/schema-codegen/namedRefs.d.ts +56 -0
  241. package/dist/src/lib/schema-codegen/namedRefs.js +143 -0
  242. package/dist/src/lib/schema-codegen/namedRefs.js.map +1 -0
  243. package/dist/src/lib/schema-codegen/schemaChanges.d.ts +74 -0
  244. package/dist/src/lib/schema-codegen/schemaChanges.js +277 -0
  245. package/dist/src/lib/schema-codegen/schemaChanges.js.map +1 -0
  246. package/dist/src/lib/schema-codegen/schemaDocs.d.ts +69 -0
  247. package/dist/src/lib/schema-codegen/schemaDocs.js +219 -0
  248. package/dist/src/lib/schema-codegen/schemaDocs.js.map +1 -0
  249. package/dist/src/lib/schema-codegen/schemaIR.d.ts +158 -0
  250. package/dist/src/lib/schema-codegen/schemaIR.js +279 -0
  251. package/dist/src/lib/schema-codegen/schemaIR.js.map +1 -0
  252. package/dist/src/lib/schema-codegen/schemaToTs.d.ts +40 -13
  253. package/dist/src/lib/schema-codegen/schemaToTs.js +124 -160
  254. package/dist/src/lib/schema-codegen/schemaToTs.js.map +1 -1
  255. package/dist/src/lib/schema-codegen/valuesLists.d.ts +35 -0
  256. package/dist/src/lib/schema-codegen/valuesLists.js +128 -0
  257. package/dist/src/lib/schema-codegen/valuesLists.js.map +1 -0
  258. package/dist/src/lib/schema-family-index.d.ts +228 -0
  259. package/dist/src/lib/schema-family-index.js +813 -0
  260. package/dist/src/lib/schema-family-index.js.map +1 -0
  261. package/dist/src/lib/schema-pins.d.ts +82 -0
  262. package/dist/src/lib/schema-pins.js +198 -0
  263. package/dist/src/lib/schema-pins.js.map +1 -0
  264. package/dist/src/lib/schema-preview.d.ts +244 -0
  265. package/dist/src/lib/schema-preview.js +498 -0
  266. package/dist/src/lib/schema-preview.js.map +1 -0
  267. package/dist/src/lib/skill-installer.d.ts +2 -2
  268. package/dist/src/lib/skill-installer.js +2 -21
  269. package/dist/src/lib/skill-installer.js.map +1 -1
  270. package/dist/src/lib/step-run-table.d.ts +1 -2
  271. package/dist/src/lib/step-run-table.js +1 -2
  272. package/dist/src/lib/step-run-table.js.map +1 -1
  273. package/dist/src/lib/swift-codegen/agentGenerator.d.ts +2 -1
  274. package/dist/src/lib/swift-codegen/agentGenerator.js +24 -22
  275. package/dist/src/lib/swift-codegen/agentGenerator.js.map +1 -1
  276. package/dist/src/lib/swift-codegen/banners.d.ts +12 -0
  277. package/dist/src/lib/swift-codegen/banners.js +12 -0
  278. package/dist/src/lib/swift-codegen/banners.js.map +1 -1
  279. package/dist/src/lib/swift-codegen/functionGenerator.d.ts +36 -1
  280. package/dist/src/lib/swift-codegen/functionGenerator.js +167 -50
  281. package/dist/src/lib/swift-codegen/functionGenerator.js.map +1 -1
  282. package/dist/src/lib/swift-codegen/schemaToSwift.d.ts +31 -7
  283. package/dist/src/lib/swift-codegen/schemaToSwift.js +213 -177
  284. package/dist/src/lib/swift-codegen/schemaToSwift.js.map +1 -1
  285. package/dist/src/lib/swift-codegen/sharedGenerator.d.ts +45 -0
  286. package/dist/src/lib/swift-codegen/sharedGenerator.js +120 -0
  287. package/dist/src/lib/swift-codegen/sharedGenerator.js.map +1 -0
  288. package/dist/src/lib/swift-codegen/userPropertiesGenerator.d.ts +30 -0
  289. package/dist/src/lib/swift-codegen/userPropertiesGenerator.js +69 -0
  290. package/dist/src/lib/swift-codegen/userPropertiesGenerator.js.map +1 -0
  291. package/dist/src/lib/sync-resource-types.d.ts +37 -5
  292. package/dist/src/lib/sync-resource-types.js +53 -5
  293. package/dist/src/lib/sync-resource-types.js.map +1 -1
  294. package/dist/src/lib/sync-selectors.d.ts +7 -0
  295. package/dist/src/lib/sync-selectors.js +22 -2
  296. package/dist/src/lib/sync-selectors.js.map +1 -1
  297. package/dist/src/lib/test-runs.d.ts +45 -0
  298. package/dist/src/lib/test-runs.js +150 -0
  299. package/dist/src/lib/test-runs.js.map +1 -0
  300. package/dist/src/lib/test-users.d.ts +25 -0
  301. package/dist/src/lib/test-users.js +36 -0
  302. package/dist/src/lib/test-users.js.map +1 -0
  303. package/dist/src/lib/toml-database-config.d.ts +2 -1
  304. package/dist/src/lib/toml-database-config.js +4 -3
  305. package/dist/src/lib/toml-database-config.js.map +1 -1
  306. package/dist/src/lib/toml-scalar-edit.js +6 -4
  307. package/dist/src/lib/toml-scalar-edit.js.map +1 -1
  308. package/dist/src/lib/user-index.d.ts +80 -0
  309. package/dist/src/lib/user-index.js +166 -0
  310. package/dist/src/lib/user-index.js.map +1 -0
  311. package/dist/src/lib/user-properties/generated-properties-schema.d.ts +159 -0
  312. package/dist/src/lib/user-properties/generated-properties-schema.js +471 -0
  313. package/dist/src/lib/user-properties/generated-properties-schema.js.map +1 -0
  314. package/dist/src/lib/user-properties/user-properties-codegen.d.ts +55 -0
  315. package/dist/src/lib/user-properties/user-properties-codegen.js +129 -0
  316. package/dist/src/lib/user-properties/user-properties-codegen.js.map +1 -0
  317. package/dist/src/lib/user-walk.d.ts +31 -0
  318. package/dist/src/lib/user-walk.js +102 -0
  319. package/dist/src/lib/user-walk.js.map +1 -0
  320. package/dist/src/lib/worker-perf-format.d.ts +22 -0
  321. package/dist/src/lib/worker-perf-format.js +27 -0
  322. package/dist/src/lib/worker-perf-format.js.map +1 -0
  323. package/dist/src/types/index.d.ts +13 -34
  324. package/package.json +5 -3
@@ -0,0 +1,813 @@
1
+ /**
2
+ * The tree's family index (#5533, project `unified-schemas` phase 3): what a
3
+ * function schema's refs to shared defs resolve against in `config push`,
4
+ * `config diff` and the preflight.
5
+ *
6
+ * `loadSchemaFamilyIndex(configDir)` reads every `schemas/<file>.toml`, finds
7
+ * the files each one names through `schemaFamilyRefsIn` (every ref spelling),
8
+ * refuses a cycle of files naming the chain, and compiles the files in
9
+ * dependency order with the vendored server compiler, so a file's refs to
10
+ * another file's defs resolve as the server will resolve them against the
11
+ * versions pushed first. A file that fails keeps its error, and a ref to it is
12
+ * refused naming that error. The keys of the function, prompt and db3-type
13
+ * families are collected for the short-form rules. A file the server holds
14
+ * only as a tombstone, once `rememberRemoteSchemaFiles` has been told, reads
15
+ * as deleted rather than missing.
16
+ *
17
+ * #5540 — the index has a db3 slot: every db3 type of the tree (`db3-types/`
18
+ * and the legacy `database-type-configs/`, `[type] models` resolved, named as
19
+ * the push names them) bridged into both projections of every model, from
20
+ * the `[models]` tables and timestamps the push sends, so a db3 ref compiles
21
+ * to the bytes the server compiles from the stored row. A type only the
22
+ * server holds resolves against its row once `rememberRemoteDb3Types` has
23
+ * been told, and `overrideTreeDb3Types` compiles against the server's row for
24
+ * a type a `--only` push does not select.
25
+ *
26
+ * Memoised per process by the tree's content, so the push loop, the preflight
27
+ * and the diff share one compile.
28
+ */
29
+ import { existsSync, readdirSync, readFileSync } from "fs";
30
+ import { createHash } from "crypto";
31
+ import { join } from "path";
32
+ import { FieldTableError, parseConfigToml, stringifyConfigToml } from "./config-toml.js";
33
+ import { resolveDb3TypeModels } from "./db3-type-models.js";
34
+ import { db3TypeProjections } from "./schema-codegen/generated-db3-bridge.js";
35
+ import { configAuthoringError } from "./platform-owned.js";
36
+ import { isFunctionDeclarationFile } from "./function-document-types.js";
37
+ import { SCHEMA_FILE_MAX_BYTES, compileSchemaFile } from "./schema-codegen/generated-schema-file.js";
38
+ import { schemaFamilyRefsIn, } from "./schema-codegen/generated-schema-refs.js";
39
+ import { agentSchemaSlots, compilePromptSchemas } from "./schema-codegen/generated-prompt-schemas.js";
40
+ import { compileFunctionToml } from "./function-schema-refs.js";
41
+ import { agentServerTools, authoredSchemaValue, promptFileKey, promptSchemaDocumentOf, promptSchemaLocations } from "./prompt-schema-document.js";
42
+ /** The directory of the shared shapes in a tree. */
43
+ const SCHEMAS_DIR = "schemas";
44
+ const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex");
45
+ function tomlStems(configDir, dir, keep = (name) => name.endsWith(".toml")) {
46
+ const path = join(configDir, dir);
47
+ if (!existsSync(path))
48
+ return [];
49
+ return readdirSync(path)
50
+ .filter(keep)
51
+ .map((name) => name.slice(0, -".toml".length))
52
+ .sort();
53
+ }
54
+ /** The server's files a tree was told about, by tree. */
55
+ const remoteFiles = new Map();
56
+ /** #5540 — the server's db3 types a tree was told about, and the ones a push compiles against in place of the tree's. */
57
+ const remoteDb3 = new Map();
58
+ const db3Overrides = new Map();
59
+ const cache = new Map();
60
+ const loaders = new Map();
61
+ /**
62
+ * Tell the tree's index what the server holds, so a ref to a file the tree
63
+ * does not have but the server tombstoned is refused as deleted, naming the
64
+ * tombstone. Files the tree has are always read from the tree.
65
+ */
66
+ export function rememberRemoteSchemaFiles(configDir, files) {
67
+ remoteFiles.set(configDir, new Map(files.map((file) => [file.file, file])));
68
+ }
69
+ /**
70
+ * Tell the tree's index which db3 types the server holds (#5540), so a ref
71
+ * to a type the tree does not have resolves against the server's stored row,
72
+ * and its name counts for the short-form rules. Types the tree has are read
73
+ * from the tree.
74
+ */
75
+ export function rememberRemoteDb3Types(configDir, records) {
76
+ const known = remoteDb3.get(configDir) ?? new Map();
77
+ for (const record of records)
78
+ if (record?.databaseType)
79
+ known.set(record.databaseType, record);
80
+ remoteDb3.set(configDir, known);
81
+ }
82
+ /** The server's db3 types this tree was told about (#5540), by name, or null when it was told nothing. */
83
+ export function rememberedRemoteDb3Types(configDir) {
84
+ return remoteDb3.get(configDir) ?? null;
85
+ }
86
+ /**
87
+ * Compile refs to these db3 types against the server's stored rows rather
88
+ * than the tree's files (#5540): a `--only` push that does not select a type
89
+ * its files ref leaves the server's revision in place, and the server
90
+ * compiles against that one. An empty list lifts every override.
91
+ */
92
+ export function overrideTreeDb3Types(configDir, records) {
93
+ db3Overrides.set(configDir, new Map(records.filter((record) => record?.databaseType).map((record) => [record.databaseType, record])));
94
+ }
95
+ const isTable = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
96
+ /**
97
+ * Every db3 type of the tree, as `config push` names and reads it: `db3-types/`
98
+ * first (named by `[type] name`, a file naming shared models resolved), then
99
+ * `database-type-configs/` (named by `[type] databaseType`), each by its stem
100
+ * when it names none. The first file to claim a name wins, as the push's one
101
+ * namespace does.
102
+ */
103
+ function treeDb3Types(configDir) {
104
+ const byName = new Map();
105
+ const db3Dir = join(configDir, "db3-types");
106
+ const resolved = existsSync(db3Dir) ? resolveDb3TypeModels(db3Dir) : null;
107
+ for (const [dir, key] of [
108
+ ["db3-types", "name"],
109
+ ["database-type-configs", "databaseType"],
110
+ ]) {
111
+ for (const stem of tomlStems(configDir, dir)) {
112
+ const file = `${stem}.toml`;
113
+ const bytes = readFileSync(join(configDir, dir, file));
114
+ let document = null;
115
+ try {
116
+ document = parseConfigToml(bytes.toString("utf-8"));
117
+ }
118
+ catch {
119
+ // Its own preflight reports a file that does not parse.
120
+ }
121
+ if (dir === "db3-types" && resolved?.referencing.has(file))
122
+ document = resolved.documents.get(file) ?? document;
123
+ const declared = document?.type?.[key];
124
+ const name = typeof declared === "string" && declared ? declared : stem;
125
+ if (!byName.has(name))
126
+ byName.set(name, { name, relative: `${dir}/${file}`, document, contentHash: sha256(bytes) });
127
+ }
128
+ }
129
+ return [...byName.values()];
130
+ }
131
+ /** A type's `timestamps` as the server normalizes it: only the stamp names and a non-empty model list, or null. */
132
+ function normalizedTimestamps(value) {
133
+ if (!isTable(value))
134
+ return null;
135
+ const out = {};
136
+ if (value.create !== undefined)
137
+ out.create = value.create;
138
+ if (value.update !== undefined)
139
+ out.update = value.update;
140
+ if (Array.isArray(value.models) && value.models.length > 0)
141
+ out.models = value.models;
142
+ return out.create === undefined && out.update === undefined ? null : out;
143
+ }
144
+ /** A tree's db3 type as the db3 slot holds it: bridged from the `[models]` tables the push sends, re-read as the server reads them. */
145
+ function treeDb3Entry(type) {
146
+ if (!type.document)
147
+ return { models: [], defs: {}, error: `${type.relative} does not parse` };
148
+ let models;
149
+ try {
150
+ models = isTable(type.document.models) ? parseConfigToml(stringifyConfigToml({ models: type.document.models })).models : undefined;
151
+ }
152
+ catch (err) {
153
+ return { models: [], defs: {}, error: `its [models] tables cannot be read: ${String(err?.message ?? err).split("\n")[0]}` };
154
+ }
155
+ return db3TypeProjections({
156
+ name: type.name,
157
+ models,
158
+ timestamps: normalizedTimestamps(type.document.type?.timestamps),
159
+ autoPopulatedFields: isTable(type.document.type?.autoPopulatedFields) ? type.document.type.autoPopulatedFields : null,
160
+ });
161
+ }
162
+ /** A JSON column, as a server listing answers it: an object, or JSON text parsed; null otherwise. */
163
+ function jsonColumn(value) {
164
+ if (typeof value === "string") {
165
+ try {
166
+ const parsed = JSON.parse(value);
167
+ return isTable(parsed) ? parsed : null;
168
+ }
169
+ catch {
170
+ return null;
171
+ }
172
+ }
173
+ return isTable(value) ? value : null;
174
+ }
175
+ /** A server's db3 type as the db3 slot holds it: bridged from its stored row, as the server bridges it. */
176
+ export function remoteDb3Entry(record) {
177
+ if (typeof record.schema !== "string" || record.schema.trim() === "")
178
+ return { models: [], defs: {}, error: "it declares no models" };
179
+ let parsed;
180
+ try {
181
+ parsed = parseConfigToml(record.schema);
182
+ }
183
+ catch (err) {
184
+ return { models: [], defs: {}, error: `its stored schema is not valid TOML: ${String(err?.message ?? err).split("\n")[0]}` };
185
+ }
186
+ return db3TypeProjections({
187
+ name: record.databaseType,
188
+ models: parsed?.models,
189
+ timestamps: jsonColumn(record.timestamps),
190
+ autoPopulatedFields: jsonColumn(record.autoPopulatedFields),
191
+ });
192
+ }
193
+ /** A path from `to` back to `from` over `graph`, or null. */
194
+ function pathBack(graph, from, to) {
195
+ const via = new Map([[to, null]]);
196
+ const queue = [to];
197
+ while (queue.length > 0) {
198
+ const at = queue.shift();
199
+ for (const hop of graph.get(at) ?? []) {
200
+ if (hop.edge.file === from) {
201
+ const hops = [hop];
202
+ for (let step = via.get(at); step; step = via.get(step.from))
203
+ hops.unshift(step);
204
+ return hops;
205
+ }
206
+ if (!via.has(hop.edge.file)) {
207
+ via.set(hop.edge.file, hop);
208
+ queue.push(hop.edge.file);
209
+ }
210
+ }
211
+ }
212
+ return null;
213
+ }
214
+ /** The def a pointer into a parsed `schemas/` file sits in: `/defs/<Name>/…`. */
215
+ function defOfPointer(pointer) {
216
+ return (pointer.split("/")[2] ?? "").replace(/~1/g, "/").replace(/~0/g, "~");
217
+ }
218
+ /** The cycle refusal for a file: the chain of refs, each def where a hop leaves it. */
219
+ function cycleMessage(hops) {
220
+ const ref = (file, def) => `schemas:${file}#/$defs/${def}`;
221
+ const chain = [ref(hops[0].from, defOfPointer(hops[0].edge.pointer))];
222
+ let at = { file: hops[0].from, def: defOfPointer(hops[0].edge.pointer) };
223
+ for (const hop of hops) {
224
+ const fromDef = defOfPointer(hop.edge.pointer);
225
+ if (hop.from !== at.file || fromDef !== at.def)
226
+ chain.push(ref(hop.from, fromDef));
227
+ chain.push(ref(hop.edge.file, hop.edge.def));
228
+ at = { file: hop.edge.file, def: hop.edge.def };
229
+ }
230
+ return (`SCHEMA_REF_CYCLE: the files reference each other: ${chain.join(" → ")}. A file's dependencies are stored before it, ` +
231
+ "so two files that reference each other can never both be pushed. Move the shared def into one of them, or into a third file.");
232
+ }
233
+ /** The index of `configDir`'s `schemas/` files, compiled; see the module note. */
234
+ export function loadSchemaFamilyIndex(configDir) {
235
+ const stems = tomlStems(configDir, SCHEMAS_DIR);
236
+ const raw = stems.map((file) => {
237
+ const bytes = readFileSync(join(configDir, SCHEMAS_DIR, `${file}.toml`));
238
+ return { file, bytes, text: bytes.toString("utf-8") };
239
+ });
240
+ const db3Locals = treeDb3Types(configDir);
241
+ const db3Remote = remoteDb3.get(configDir) ?? new Map();
242
+ const db3Overridden = db3Overrides.get(configDir) ?? new Map();
243
+ const keys = {
244
+ function: tomlStems(configDir, "functions", isFunctionDeclarationFile),
245
+ prompt: tomlStems(configDir, "prompts"),
246
+ // #5540 — both directories, by the name the push gives each, and the server's.
247
+ db3: [...new Set([...db3Locals.map((type) => type.name), ...db3Remote.keys()])].sort(),
248
+ };
249
+ const remote = remoteFiles.get(configDir) ?? new Map();
250
+ const signature = sha256(JSON.stringify([
251
+ raw.map((entry) => [entry.file, sha256(entry.bytes)]),
252
+ keys,
253
+ [...remote.values()].filter((file) => file.tombstone),
254
+ db3Locals.map((type) => [type.name, type.relative, type.contentHash]),
255
+ [...db3Remote.values()],
256
+ [...db3Overridden.values()],
257
+ ]));
258
+ const known = cache.get(configDir);
259
+ if (known && known.signature === signature)
260
+ return known.index;
261
+ // Read and parse each file; a file the push would refuse before compiling keeps its error.
262
+ const locals = [];
263
+ const documents = new Map();
264
+ for (const { file, bytes, text } of raw) {
265
+ const local = {
266
+ file,
267
+ relative: `${SCHEMAS_DIR}/${file}.toml`,
268
+ text,
269
+ contentHash: sha256(bytes),
270
+ sourceHash: sha256(Buffer.from(text, "utf-8")),
271
+ error: null,
272
+ compile: null,
273
+ edges: [],
274
+ db3Edges: [],
275
+ };
276
+ locals.push(local);
277
+ const stem = configAuthoringError("schema", file);
278
+ if (stem) {
279
+ local.error = stem;
280
+ continue;
281
+ }
282
+ const size = Buffer.byteLength(text, "utf-8");
283
+ if (size > SCHEMA_FILE_MAX_BYTES) {
284
+ local.error = `the file is ${size} bytes; at most ${SCHEMA_FILE_MAX_BYTES} are accepted. Split it into more files.`;
285
+ continue;
286
+ }
287
+ try {
288
+ documents.set(file, parseConfigToml(text));
289
+ }
290
+ catch (err) {
291
+ // #5541 — a def written as a field table that does not compile: every problem, at its dotted path.
292
+ if (err instanceof FieldTableError) {
293
+ local.error = err.problems.map((problem) => `${problem.path}: ${problem.message}`).join("; ");
294
+ continue;
295
+ }
296
+ const message = String(err?.message ?? err).split("\n")[0];
297
+ local.error = `invalid TOML: line ${Number(err?.line) || 0}, column ${Number(err?.column) || 0}: ${message}`;
298
+ }
299
+ }
300
+ // #5540 — the db3 types: an overridden one from the server's row, the tree's from its file, the rest the server's.
301
+ const db3 = new Map();
302
+ const db3Tree = db3Locals.map((type) => ({ name: type.name, relative: type.relative, entry: treeDb3Entry(type) }));
303
+ for (const type of db3Tree) {
304
+ const override = db3Overridden.get(type.name);
305
+ db3.set(type.name, override ? remoteDb3Entry(override) : type.entry);
306
+ }
307
+ for (const [name, record] of db3Remote)
308
+ if (!db3.has(name))
309
+ db3.set(name, remoteDb3Entry(record));
310
+ for (const [name, record] of db3Overridden)
311
+ if (!db3.has(name))
312
+ db3.set(name, remoteDb3Entry(record));
313
+ // Each file by its def names, for the short forms; files only the server tombstoned too.
314
+ const files = new Map();
315
+ for (const local of locals) {
316
+ const document = documents.get(local.file);
317
+ const defs = document?.defs;
318
+ files.set(local.file, {
319
+ names: defs !== null && typeof defs === "object" && !Array.isArray(defs) ? Object.keys(defs) : [],
320
+ ...(local.error ? { error: local.error } : {}),
321
+ });
322
+ }
323
+ for (const file of remote.values()) {
324
+ if (file.tombstone && !files.has(file.file))
325
+ files.set(file.file, { version: file.version, tombstone: true, names: [] });
326
+ }
327
+ const namesIndex = { files, keys };
328
+ // The files each file names, and a cycle among them refused on every member.
329
+ const graph = new Map();
330
+ for (const local of locals) {
331
+ if (!documents.has(local.file))
332
+ continue;
333
+ const scan = schemaFamilyRefsIn(documents.get(local.file), namesIndex);
334
+ local.edges = scan.edges.filter((edge) => edge.file !== local.file);
335
+ local.db3Edges = scan.db3;
336
+ graph.set(local.file, local.edges.filter((edge) => documents.has(edge.file)).map((edge) => ({ edge, from: local.file })));
337
+ }
338
+ for (const local of locals) {
339
+ if (local.error)
340
+ continue;
341
+ const hops = (graph.get(local.file) ?? []).map((hop) => ({ hop, back: pathBack(graph, local.file, hop.edge.file) })).find((found) => found.back);
342
+ if (!hops)
343
+ continue;
344
+ local.error = cycleMessage([hops.hop, ...hops.back]);
345
+ files.set(local.file, { ...files.get(local.file), error: local.error });
346
+ }
347
+ // Compile dependencies first, each against the files compiled before it.
348
+ const order = [];
349
+ const state = new Map();
350
+ for (const root of locals.map((local) => local.file)) {
351
+ const stack = [{ file: root, next: 0 }];
352
+ while (stack.length > 0) {
353
+ const frame = stack[stack.length - 1];
354
+ if (state.get(frame.file) === "done") {
355
+ stack.pop();
356
+ continue;
357
+ }
358
+ state.set(frame.file, "open");
359
+ const targets = (graph.get(frame.file) ?? []).map((hop) => hop.edge.file);
360
+ if (frame.next < targets.length) {
361
+ const target = targets[frame.next++];
362
+ if (!state.has(target))
363
+ stack.push({ file: target, next: 0 });
364
+ continue;
365
+ }
366
+ state.set(frame.file, "done");
367
+ order.push(frame.file);
368
+ stack.pop();
369
+ }
370
+ }
371
+ const byFile = new Map(locals.map((local) => [local.file, local]));
372
+ for (const file of order) {
373
+ const local = byFile.get(file);
374
+ if (local.error)
375
+ continue;
376
+ const compile = compileSchemaFile(documents.get(file), { families: { files, keys, db3 }, file });
377
+ local.compile = compile;
378
+ if ("refusal" in compile) {
379
+ const { path, message } = compile.refusal;
380
+ local.error = path && !message.startsWith(path) ? `${path}: ${message}` : message;
381
+ files.set(file, { ...files.get(file), error: local.error });
382
+ continue;
383
+ }
384
+ files.set(file, { names: compile.names, defs: compile.defs, resolved: compile.resolved });
385
+ }
386
+ const index = { files, keys, locals, db3, db3Locals: db3Tree };
387
+ cache.set(configDir, { signature, index });
388
+ return index;
389
+ }
390
+ /**
391
+ * The loader a function compile reads the index through: one per tree for
392
+ * the process, called only when a schema writes a family-spelled ref.
393
+ */
394
+ export function schemaFamilyIndexLoader(configDir) {
395
+ let loader = loaders.get(configDir);
396
+ if (!loader) {
397
+ loader = () => loadSchemaFamilyIndex(configDir);
398
+ loaders.set(configDir, loader);
399
+ }
400
+ return loader;
401
+ }
402
+ const remotePrompts = new Map();
403
+ const remotePromptKeys = new Map();
404
+ const remoteFunctions = new Map();
405
+ const functionOverrides = new Map();
406
+ const promptCache = new Map();
407
+ const promptLoaders = new Map();
408
+ /**
409
+ * Tell the tree's index which prompts the server holds, so a ref to a prompt
410
+ * the tree does not have resolves against the server's stored schemas (and
411
+ * provenance, when the record is a detail), and one the server archived reads
412
+ * as deleted. Prompts the tree has are always read from the tree.
413
+ */
414
+ export function rememberRemotePrompts(configDir, records) {
415
+ const known = remotePrompts.get(configDir) ?? new Map();
416
+ for (const record of records)
417
+ if (record?.promptKey)
418
+ known.set(record.promptKey, record);
419
+ remotePrompts.set(configDir, known);
420
+ }
421
+ /**
422
+ * Tell the tree's index every prompt key the server holds, archived ones
423
+ * included, as the server judges a short ref (`<key>.output`) against them:
424
+ * a server-only key then resolves to the prompt family, and a key in two
425
+ * families reads as ambiguous. A key only gives the short form its family;
426
+ * what it resolves to comes from `rememberRemotePrompts`.
427
+ */
428
+ export function rememberRemotePromptKeys(configDir, keys) {
429
+ const known = remotePromptKeys.get(configDir) ?? new Set();
430
+ for (const key of keys)
431
+ if (key)
432
+ known.add(key);
433
+ remotePromptKeys.set(configDir, known);
434
+ }
435
+ /** As `rememberRemotePrompts`, for functions. */
436
+ export function rememberRemoteFunctions(configDir, records) {
437
+ const known = remoteFunctions.get(configDir) ?? new Map();
438
+ for (const record of records)
439
+ if (record?.functionKey)
440
+ known.set(record.functionKey, record);
441
+ remoteFunctions.set(configDir, known);
442
+ }
443
+ /**
444
+ * Compile refs to these functions against the server's stored schemas rather
445
+ * than the tree's files: a `--only` push that does not select a function its
446
+ * prompts ref leaves the server's version in place, and the server compiles
447
+ * against that one.
448
+ */
449
+ export function overrideTreeFunctions(configDir, records) {
450
+ functionOverrides.set(configDir, new Map(records.filter((record) => record?.functionKey).map((record) => [record.functionKey, record])));
451
+ }
452
+ /** A server function record as the function slot holds it. */
453
+ function remoteFunctionEntry(record) {
454
+ if (record.status === "archived")
455
+ return { tombstone: true };
456
+ const input = schemaObject(record.inputSchema);
457
+ const output = schemaObject(record.outputSchema);
458
+ return { ...(input ? { input } : {}), ...(output ? { output } : {}) };
459
+ }
460
+ /** A stored schema value as an object: JSON text parsed; undefined for anything else. */
461
+ function schemaObject(value) {
462
+ const parsed = authoredSchemaValue(value);
463
+ return parsed !== null && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : undefined;
464
+ }
465
+ /** A stored provenance map's entries under `prefix`, rebased to it. */
466
+ function provenanceUnder(provenance, prefix) {
467
+ const out = {};
468
+ if (provenance === null || typeof provenance !== "object")
469
+ return out;
470
+ for (const [pointer, entry] of Object.entries(provenance)) {
471
+ if (prefix !== "" && pointer !== prefix && !pointer.startsWith(`${prefix}/`))
472
+ continue;
473
+ Object.defineProperty(out, pointer.slice(prefix.length), { value: entry, enumerable: true, writable: true, configurable: true });
474
+ }
475
+ return out;
476
+ }
477
+ /** A prompt the server holds, as the slot reads it: its targets, a server tool's from the function slot. */
478
+ function remotePromptEntry(record, functions) {
479
+ if (record.status === "archived")
480
+ return { tombstone: true, targets: new Map() };
481
+ const targets = new Map();
482
+ const unresolvedTargets = new Map();
483
+ const provenance = {};
484
+ for (const [field, target] of [
485
+ ["inputSchema", "input"],
486
+ ["outputSchema", "output"],
487
+ ]) {
488
+ const schema = schemaObject(record[field]);
489
+ if (!schema)
490
+ continue;
491
+ targets.set(target, schema);
492
+ provenance[target] = provenanceUnder(record.provenance?.[field], "");
493
+ }
494
+ for (const slot of agentSchemaSlots(schemaObject(record.agent) ?? null)) {
495
+ if (!slot.target || targets.has(slot.target) || !schemaObject(slot.schema))
496
+ continue;
497
+ targets.set(slot.target, slot.schema);
498
+ provenance[slot.target] = provenanceUnder(record.provenance?.agent, slot.pointer);
499
+ }
500
+ for (const [name, fn] of agentServerTools(record.agent)) {
501
+ const entry = functions.get(fn);
502
+ for (const part of ["input", "output"]) {
503
+ const target = `tools/${name}/${part}`;
504
+ if (targets.has(target))
505
+ continue;
506
+ const schema = entry?.[part];
507
+ if (!entry || entry.error)
508
+ unresolvedTargets.set(target, entry?.error ?? `its server tool's function ${fn} is not in this tree; pull functions/${fn}.toml first`);
509
+ else if (entry.tombstone)
510
+ unresolvedTargets.set(target, `its server tool's function ${fn} is archived`);
511
+ else if (schema === undefined)
512
+ unresolvedTargets.set(target, `its server tool's function ${fn} declares no ${part}Schema`);
513
+ else
514
+ targets.set(target, schema);
515
+ }
516
+ }
517
+ return { targets, ...(unresolvedTargets.size > 0 ? { unresolvedTargets } : {}), provenance };
518
+ }
519
+ /** The refs a prompt file's schemas make, any spelling, judged against `names`; an annotation's value is not a ref. */
520
+ function promptFileEdges(key, document, names) {
521
+ const promptEdges = [];
522
+ const selfEdges = [];
523
+ const functionEdges = [];
524
+ const schemaEdges = [];
525
+ const db3Edges = [];
526
+ for (const { location, document: schema } of promptSchemaLocations(document)) {
527
+ const scan = schemaFamilyRefsIn(schema, names, { schemaPositions: true });
528
+ for (const edge of scan.prompts) {
529
+ if (edge.key !== key)
530
+ promptEdges.push({ location, key: edge.key, target: edge.target });
531
+ else
532
+ selfEdges.push({ location, target: edge.target });
533
+ }
534
+ for (const edge of scan.functions)
535
+ functionEdges.push({ location, key: edge.key, part: edge.part });
536
+ for (const edge of scan.edges)
537
+ schemaEdges.push({ location, file: edge.file });
538
+ for (const edge of scan.db3)
539
+ db3Edges.push({ location, type: edge.type });
540
+ }
541
+ return { promptEdges, selfEdges, functionEdges, schemaEdges, db3Edges };
542
+ }
543
+ /**
544
+ * The functions behind the server tools a tree's prompt files ref, through
545
+ * another prompt's `tools.<name>` target or their own: what such a target
546
+ * resolves to.
547
+ */
548
+ export function promptToolFunctionsReffed(promptLocals) {
549
+ const byKey = new Map(promptLocals.map((local) => [local.key, local]));
550
+ const functions = new Set();
551
+ for (const local of promptLocals) {
552
+ const edges = [...local.promptEdges, ...local.selfEdges.map((edge) => ({ ...edge, key: local.key }))];
553
+ for (const edge of edges) {
554
+ const [kind, name] = edge.target.split("/");
555
+ if (kind !== "tools")
556
+ continue;
557
+ const fn = agentServerTools(byKey.get(edge.key)?.parsed?.prompt?.agent).get(name);
558
+ if (fn)
559
+ functions.add(fn);
560
+ }
561
+ }
562
+ return [...functions].sort();
563
+ }
564
+ /** The cycle refusal for a prompt file: the chain of refs, each location where a hop leaves it, in explicit form. */
565
+ function promptCycleMessage(hops) {
566
+ const ref = (key, target) => `prompt:${key}#/$defs/${target}`;
567
+ const chain = [ref(hops[0].from, hops[0].location)];
568
+ let at = { key: hops[0].from, target: hops[0].location };
569
+ for (const hop of hops) {
570
+ if (hop.from !== at.key || hop.location !== at.target)
571
+ chain.push(ref(hop.from, hop.location));
572
+ chain.push(ref(hop.key, hop.target));
573
+ at = { key: hop.key, target: hop.target };
574
+ }
575
+ return (`PROMPT_SCHEMA_REF_CYCLE: the prompts reference each other: ${chain.join(" → ")}. A prompt's dependencies are stored before it, ` +
576
+ "so two prompts that reference each other can never both be pushed. Give the shared shape its own def in a schemas/ file and ref that from both.");
577
+ }
578
+ /**
579
+ * The tree's index with a prompt slot and a function slot (#5534,
580
+ * DSO-5534-009): every `functions/*.toml` compiled (the `schemas/` files
581
+ * resolved), and every `prompts/*.toml` compiled in dependency order, each
582
+ * against the prompts compiled before it, with the vendored server compiler.
583
+ * A cycle among prompts is an error on every member, naming the chain; a file
584
+ * that cannot be compiled keeps its error, and a ref to it is refused naming
585
+ * it. A prompt or function the tree lacks resolves against the server's
586
+ * records once `rememberRemotePrompts` / `rememberRemoteFunctions` have been
587
+ * told (an archived one reads as deleted), and is otherwise an error naming
588
+ * the file to pull.
589
+ *
590
+ * Memoised per process by the tree's content and what the server was said to hold.
591
+ */
592
+ export function loadPromptFamilyIndex(configDir) {
593
+ const base = loadSchemaFamilyIndex(configDir);
594
+ const promptFiles = existsSync(join(configDir, "prompts")) ? readdirSync(join(configDir, "prompts")).filter((name) => name.endsWith(".toml")).sort() : [];
595
+ const functionFiles = existsSync(join(configDir, "functions")) ? readdirSync(join(configDir, "functions")).filter(isFunctionDeclarationFile).sort() : [];
596
+ const raw = (dir, names) => names.map((name) => ({ name, bytes: readFileSync(join(configDir, dir, name)) }));
597
+ const promptRaw = raw("prompts", promptFiles);
598
+ const functionRaw = raw("functions", functionFiles);
599
+ const remoteP = remotePrompts.get(configDir) ?? new Map();
600
+ const remotePKeys = remotePromptKeys.get(configDir) ?? new Set();
601
+ const remoteF = remoteFunctions.get(configDir) ?? new Map();
602
+ const overrides = functionOverrides.get(configDir) ?? new Map();
603
+ const signature = sha256(JSON.stringify([
604
+ base.locals.map((local) => [local.file, local.contentHash, local.error]),
605
+ [...base.files.entries()].filter(([, entry]) => entry.tombstone).map(([file, entry]) => [file, entry.version]),
606
+ promptRaw.map((entry) => [entry.name, sha256(entry.bytes)]),
607
+ functionRaw.map((entry) => [entry.name, sha256(entry.bytes)]),
608
+ [...remoteP.values()],
609
+ [...remotePKeys].sort(),
610
+ [...remoteF.values()],
611
+ [...overrides.values()],
612
+ // #5540 — what the db3 slot holds.
613
+ [...base.db3.entries()],
614
+ ]));
615
+ const known = promptCache.get(configDir);
616
+ if (known && known.signature === signature)
617
+ return known.index;
618
+ // The function slot: each local function file compiled against the schemas/ files.
619
+ const functions = new Map();
620
+ const functionLocals = [];
621
+ for (const { name, bytes } of functionRaw) {
622
+ const relative = `functions/${name}`;
623
+ let parsed;
624
+ try {
625
+ parsed = parseConfigToml(bytes.toString("utf-8"));
626
+ }
627
+ catch {
628
+ functionLocals.push({ key: name.slice(0, -".toml".length), relative, error: "the file does not parse" });
629
+ continue;
630
+ }
631
+ const key = typeof parsed?.function?.key === "string" && parsed.function.key ? parsed.function.key : name.slice(0, -".toml".length);
632
+ const override = overrides.get(key);
633
+ if (override) {
634
+ functionLocals.push({ key, relative, error: null });
635
+ functions.set(key, remoteFunctionEntry(override));
636
+ continue;
637
+ }
638
+ const compiled = compileFunctionToml(relative, parsed, base);
639
+ functionLocals.push({ key, relative, error: compiled.refusal });
640
+ functions.set(key, compiled.refusal
641
+ ? { error: compiled.refusal }
642
+ : {
643
+ ...(compiled.schemas.inputSchema ? { input: compiled.schemas.inputSchema } : {}),
644
+ ...(compiled.schemas.outputSchema ? { output: compiled.schemas.outputSchema } : {}),
645
+ });
646
+ }
647
+ // Each local prompt file: its key, document and edges.
648
+ const promptLocals = [];
649
+ for (const { name, bytes } of promptRaw) {
650
+ const relative = `prompts/${name}`;
651
+ let parsed = null;
652
+ try {
653
+ parsed = parseConfigToml(bytes.toString("utf-8"));
654
+ }
655
+ catch {
656
+ // Reported by the prompt collectors; the file declares nothing here.
657
+ }
658
+ promptLocals.push({
659
+ key: parsed ? promptFileKey(name, parsed) : name.slice(0, -".toml".length),
660
+ file: name,
661
+ relative,
662
+ parsed,
663
+ error: null,
664
+ compile: null,
665
+ promptEdges: [],
666
+ selfEdges: [],
667
+ functionEdges: [],
668
+ schemaEdges: [],
669
+ db3Edges: [],
670
+ });
671
+ }
672
+ const localKeys = new Set(promptLocals.map((local) => local.key));
673
+ const functionKeys = new Set(functions.keys());
674
+ const keys = {
675
+ function: [...new Set([...base.keys.function, ...functionKeys, ...remoteF.keys()])].sort(),
676
+ prompt: [...new Set([...base.keys.prompt, ...localKeys, ...remoteP.keys(), ...remotePKeys])].sort(),
677
+ db3: base.keys.db3,
678
+ };
679
+ const names = { files: base.files, keys, db3: base.db3 };
680
+ for (const local of promptLocals) {
681
+ if (!local.parsed)
682
+ continue;
683
+ Object.assign(local, promptFileEdges(local.key, promptSchemaDocumentOf(local.parsed), names));
684
+ }
685
+ // Functions and prompts the tree lacks: the server's, or an error naming the file to pull.
686
+ for (const local of promptLocals) {
687
+ for (const edge of local.functionEdges) {
688
+ if (functions.has(edge.key))
689
+ continue;
690
+ const remote = remoteF.get(edge.key);
691
+ if (!remote) {
692
+ functions.set(edge.key, { error: `functions/${edge.key}.toml is not in this tree; pull it first` });
693
+ continue;
694
+ }
695
+ functions.set(edge.key, remoteFunctionEntry(remote));
696
+ }
697
+ }
698
+ // A server tool of a local agent whose function the tree lacks reads the server's.
699
+ for (const local of promptLocals) {
700
+ for (const fn of agentServerTools(local.parsed?.prompt?.agent).values()) {
701
+ const remote = remoteF.get(fn);
702
+ if (!functions.has(fn) && remote)
703
+ functions.set(fn, remoteFunctionEntry(remote));
704
+ }
705
+ }
706
+ const prompts = new Map();
707
+ for (const local of promptLocals) {
708
+ for (const edge of local.promptEdges) {
709
+ if (localKeys.has(edge.key) || prompts.has(edge.key))
710
+ continue;
711
+ const remote = remoteP.get(edge.key);
712
+ if (remote) {
713
+ // A server tool of a server-only agent reads its function the same way.
714
+ for (const fn of agentServerTools(remote.agent).values()) {
715
+ if (functions.has(fn))
716
+ continue;
717
+ const remoteFn = remoteF.get(fn);
718
+ if (remoteFn)
719
+ functions.set(fn, remoteFunctionEntry(remoteFn));
720
+ }
721
+ prompts.set(edge.key, remotePromptEntry(remote, functions));
722
+ }
723
+ else {
724
+ prompts.set(edge.key, { error: `prompts/${edge.key}.toml is not in this tree; pull it first`, targets: new Map() });
725
+ }
726
+ }
727
+ }
728
+ // A cycle among local prompts is an error on every member.
729
+ const byKey = new Map(promptLocals.map((local) => [local.key, local]));
730
+ const graph = new Map();
731
+ for (const local of promptLocals) {
732
+ graph.set(local.key, local.promptEdges.filter((edge) => byKey.has(edge.key)).map((edge) => ({ from: local.key, ...edge })));
733
+ }
734
+ for (const local of promptLocals) {
735
+ for (const hop of graph.get(local.key) ?? []) {
736
+ const via = new Map([[hop.key, null]]);
737
+ const queue = [hop.key];
738
+ let back = null;
739
+ while (queue.length > 0 && !back) {
740
+ const at = queue.shift();
741
+ for (const next of graph.get(at) ?? []) {
742
+ if (next.key === local.key) {
743
+ back = [next];
744
+ for (let step = via.get(at); step; step = via.get(step.from))
745
+ back.unshift(step);
746
+ break;
747
+ }
748
+ if (!via.has(next.key)) {
749
+ via.set(next.key, next);
750
+ queue.push(next.key);
751
+ }
752
+ }
753
+ }
754
+ if (back) {
755
+ local.error = promptCycleMessage([hop, ...back]);
756
+ break;
757
+ }
758
+ }
759
+ }
760
+ // Compile dependencies first, each against the prompts compiled before it.
761
+ const state = new Map();
762
+ const order = [];
763
+ const visit = (local) => {
764
+ if (state.has(local.key))
765
+ return;
766
+ state.set(local.key, "open");
767
+ for (const edge of local.promptEdges) {
768
+ const target = byKey.get(edge.key);
769
+ if (target && !state.has(target.key))
770
+ visit(target);
771
+ }
772
+ state.set(local.key, "done");
773
+ order.push(local);
774
+ };
775
+ for (const local of promptLocals)
776
+ visit(local);
777
+ for (const local of order) {
778
+ if (!local.parsed)
779
+ continue;
780
+ if (local.error) {
781
+ prompts.set(local.key, { error: local.error, targets: new Map() });
782
+ continue;
783
+ }
784
+ const compile = compilePromptSchemas(promptSchemaDocumentOf(local.parsed), {
785
+ families: { files: base.files, keys, prompts, functions, db3: base.db3 },
786
+ selfKey: local.key,
787
+ });
788
+ local.compile = compile;
789
+ if (compile.refusal) {
790
+ const where = compile.refusal.config !== undefined ? `[[configs]] "${compile.refusal.config}": ` : "";
791
+ local.error = `${compile.refusal.code}: ${where}${compile.refusal.message}`;
792
+ prompts.set(local.key, { error: local.error, targets: new Map() });
793
+ continue;
794
+ }
795
+ prompts.set(local.key, compile.self);
796
+ }
797
+ const index = { ...base, keys, prompts, functions, promptLocals, functionLocals };
798
+ promptCache.set(configDir, { signature, index });
799
+ return index;
800
+ }
801
+ /**
802
+ * The loader a prompt compile reads the index through: one per tree for the
803
+ * process, called only when a schema writes a family-spelled ref.
804
+ */
805
+ export function promptFamilyIndexLoader(configDir) {
806
+ let loader = promptLoaders.get(configDir);
807
+ if (!loader) {
808
+ loader = () => loadPromptFamilyIndex(configDir);
809
+ promptLoaders.set(configDir, loader);
810
+ }
811
+ return loader;
812
+ }
813
+ //# sourceMappingURL=schema-family-index.js.map