@proposit/proposit-core 0.8.9

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 (429) hide show
  1. package/README.md +1032 -0
  2. package/dist/cli/commands/analysis.d.ts +3 -0
  3. package/dist/cli/commands/analysis.d.ts.map +1 -0
  4. package/dist/cli/commands/analysis.js +504 -0
  5. package/dist/cli/commands/analysis.js.map +1 -0
  6. package/dist/cli/commands/arguments.d.ts +3 -0
  7. package/dist/cli/commands/arguments.d.ts.map +1 -0
  8. package/dist/cli/commands/arguments.js +187 -0
  9. package/dist/cli/commands/arguments.js.map +1 -0
  10. package/dist/cli/commands/claims.d.ts +3 -0
  11. package/dist/cli/commands/claims.d.ts.map +1 -0
  12. package/dist/cli/commands/claims.js +120 -0
  13. package/dist/cli/commands/claims.js.map +1 -0
  14. package/dist/cli/commands/diff.d.ts +3 -0
  15. package/dist/cli/commands/diff.d.ts.map +1 -0
  16. package/dist/cli/commands/diff.js +61 -0
  17. package/dist/cli/commands/diff.js.map +1 -0
  18. package/dist/cli/commands/expressions.d.ts +3 -0
  19. package/dist/cli/commands/expressions.d.ts.map +1 -0
  20. package/dist/cli/commands/expressions.js +344 -0
  21. package/dist/cli/commands/expressions.js.map +1 -0
  22. package/dist/cli/commands/graph.d.ts +13 -0
  23. package/dist/cli/commands/graph.d.ts.map +1 -0
  24. package/dist/cli/commands/graph.js +382 -0
  25. package/dist/cli/commands/graph.js.map +1 -0
  26. package/dist/cli/commands/meta.d.ts +3 -0
  27. package/dist/cli/commands/meta.d.ts.map +1 -0
  28. package/dist/cli/commands/meta.js +14 -0
  29. package/dist/cli/commands/meta.js.map +1 -0
  30. package/dist/cli/commands/parse.d.ts +3 -0
  31. package/dist/cli/commands/parse.d.ts.map +1 -0
  32. package/dist/cli/commands/parse.js +171 -0
  33. package/dist/cli/commands/parse.js.map +1 -0
  34. package/dist/cli/commands/premises.d.ts +3 -0
  35. package/dist/cli/commands/premises.d.ts.map +1 -0
  36. package/dist/cli/commands/premises.js +261 -0
  37. package/dist/cli/commands/premises.js.map +1 -0
  38. package/dist/cli/commands/render.d.ts +3 -0
  39. package/dist/cli/commands/render.d.ts.map +1 -0
  40. package/dist/cli/commands/render.js +109 -0
  41. package/dist/cli/commands/render.js.map +1 -0
  42. package/dist/cli/commands/repair.d.ts +3 -0
  43. package/dist/cli/commands/repair.d.ts.map +1 -0
  44. package/dist/cli/commands/repair.js +53 -0
  45. package/dist/cli/commands/repair.js.map +1 -0
  46. package/dist/cli/commands/roles.d.ts +3 -0
  47. package/dist/cli/commands/roles.d.ts.map +1 -0
  48. package/dist/cli/commands/roles.js +64 -0
  49. package/dist/cli/commands/roles.js.map +1 -0
  50. package/dist/cli/commands/sources.d.ts +3 -0
  51. package/dist/cli/commands/sources.d.ts.map +1 -0
  52. package/dist/cli/commands/sources.js +103 -0
  53. package/dist/cli/commands/sources.js.map +1 -0
  54. package/dist/cli/commands/validate.d.ts +3 -0
  55. package/dist/cli/commands/validate.d.ts.map +1 -0
  56. package/dist/cli/commands/validate.js +27 -0
  57. package/dist/cli/commands/validate.js.map +1 -0
  58. package/dist/cli/commands/variables.d.ts +3 -0
  59. package/dist/cli/commands/variables.d.ts.map +1 -0
  60. package/dist/cli/commands/variables.js +206 -0
  61. package/dist/cli/commands/variables.js.map +1 -0
  62. package/dist/cli/commands/version-show.d.ts +3 -0
  63. package/dist/cli/commands/version-show.d.ts.map +1 -0
  64. package/dist/cli/commands/version-show.js +31 -0
  65. package/dist/cli/commands/version-show.js.map +1 -0
  66. package/dist/cli/config.d.ts +8 -0
  67. package/dist/cli/config.d.ts.map +1 -0
  68. package/dist/cli/config.js +24 -0
  69. package/dist/cli/config.js.map +1 -0
  70. package/dist/cli/engine.d.ts +19 -0
  71. package/dist/cli/engine.d.ts.map +1 -0
  72. package/dist/cli/engine.js +173 -0
  73. package/dist/cli/engine.js.map +1 -0
  74. package/dist/cli/import.d.ts +22 -0
  75. package/dist/cli/import.d.ts.map +1 -0
  76. package/dist/cli/import.js +242 -0
  77. package/dist/cli/import.js.map +1 -0
  78. package/dist/cli/llm/index.d.ts +6 -0
  79. package/dist/cli/llm/index.d.ts.map +1 -0
  80. package/dist/cli/llm/index.js +26 -0
  81. package/dist/cli/llm/index.js.map +1 -0
  82. package/dist/cli/llm/openai.d.ts +4 -0
  83. package/dist/cli/llm/openai.d.ts.map +1 -0
  84. package/dist/cli/llm/openai.js +44 -0
  85. package/dist/cli/llm/openai.js.map +1 -0
  86. package/dist/cli/llm/types.d.ts +14 -0
  87. package/dist/cli/llm/types.d.ts.map +1 -0
  88. package/dist/cli/llm/types.js +2 -0
  89. package/dist/cli/llm/types.js.map +1 -0
  90. package/dist/cli/logging.d.ts +8 -0
  91. package/dist/cli/logging.d.ts.map +1 -0
  92. package/dist/cli/logging.js +23 -0
  93. package/dist/cli/logging.js.map +1 -0
  94. package/dist/cli/output/diff-renderer.d.ts +4 -0
  95. package/dist/cli/output/diff-renderer.d.ts.map +1 -0
  96. package/dist/cli/output/diff-renderer.js +89 -0
  97. package/dist/cli/output/diff-renderer.js.map +1 -0
  98. package/dist/cli/output.d.ts +6 -0
  99. package/dist/cli/output.d.ts.map +1 -0
  100. package/dist/cli/output.js +41 -0
  101. package/dist/cli/output.js.map +1 -0
  102. package/dist/cli/router.d.ts +14 -0
  103. package/dist/cli/router.d.ts.map +1 -0
  104. package/dist/cli/router.js +59 -0
  105. package/dist/cli/router.js.map +1 -0
  106. package/dist/cli/schemata.d.ts +74 -0
  107. package/dist/cli/schemata.d.ts.map +1 -0
  108. package/dist/cli/schemata.js +89 -0
  109. package/dist/cli/schemata.js.map +1 -0
  110. package/dist/cli/storage/analysis.d.ts +9 -0
  111. package/dist/cli/storage/analysis.d.ts.map +1 -0
  112. package/dist/cli/storage/analysis.js +108 -0
  113. package/dist/cli/storage/analysis.js.map +1 -0
  114. package/dist/cli/storage/arguments.d.ts +12 -0
  115. package/dist/cli/storage/arguments.d.ts.map +1 -0
  116. package/dist/cli/storage/arguments.js +80 -0
  117. package/dist/cli/storage/arguments.js.map +1 -0
  118. package/dist/cli/storage/libraries.d.ts +14 -0
  119. package/dist/cli/storage/libraries.d.ts.map +1 -0
  120. package/dist/cli/storage/libraries.js +80 -0
  121. package/dist/cli/storage/libraries.js.map +1 -0
  122. package/dist/cli/storage/premises.d.ts +9 -0
  123. package/dist/cli/storage/premises.d.ts.map +1 -0
  124. package/dist/cli/storage/premises.js +67 -0
  125. package/dist/cli/storage/premises.js.map +1 -0
  126. package/dist/cli/storage/roles.d.ts +4 -0
  127. package/dist/cli/storage/roles.d.ts.map +1 -0
  128. package/dist/cli/storage/roles.js +26 -0
  129. package/dist/cli/storage/roles.js.map +1 -0
  130. package/dist/cli/storage/variables.d.ts +4 -0
  131. package/dist/cli/storage/variables.d.ts.map +1 -0
  132. package/dist/cli/storage/variables.js +36 -0
  133. package/dist/cli/storage/variables.js.map +1 -0
  134. package/dist/cli.d.ts +3 -0
  135. package/dist/cli.d.ts.map +1 -0
  136. package/dist/cli.js +65 -0
  137. package/dist/cli.js.map +1 -0
  138. package/dist/extensions/basics/argument-parser.d.ts +12 -0
  139. package/dist/extensions/basics/argument-parser.d.ts.map +1 -0
  140. package/dist/extensions/basics/argument-parser.js +27 -0
  141. package/dist/extensions/basics/argument-parser.js.map +1 -0
  142. package/dist/extensions/basics/index.d.ts +4 -0
  143. package/dist/extensions/basics/index.d.ts.map +1 -0
  144. package/dist/extensions/basics/index.js +3 -0
  145. package/dist/extensions/basics/index.js.map +1 -0
  146. package/dist/extensions/basics/schemata.d.ts +35 -0
  147. package/dist/extensions/basics/schemata.d.ts.map +1 -0
  148. package/dist/extensions/basics/schemata.js +55 -0
  149. package/dist/extensions/basics/schemata.js.map +1 -0
  150. package/dist/extensions/ieee/formatting.d.ts +18 -0
  151. package/dist/extensions/ieee/formatting.d.ts.map +1 -0
  152. package/dist/extensions/ieee/formatting.js +57 -0
  153. package/dist/extensions/ieee/formatting.js.map +1 -0
  154. package/dist/extensions/ieee/index.d.ts +6 -0
  155. package/dist/extensions/ieee/index.d.ts.map +1 -0
  156. package/dist/extensions/ieee/index.js +6 -0
  157. package/dist/extensions/ieee/index.js.map +1 -0
  158. package/dist/extensions/ieee/references.d.ts +1379 -0
  159. package/dist/extensions/ieee/references.d.ts.map +1 -0
  160. package/dist/extensions/ieee/references.js +929 -0
  161. package/dist/extensions/ieee/references.js.map +1 -0
  162. package/dist/extensions/ieee/relaxed.d.ts +1371 -0
  163. package/dist/extensions/ieee/relaxed.d.ts.map +1 -0
  164. package/dist/extensions/ieee/relaxed.js +160 -0
  165. package/dist/extensions/ieee/relaxed.js.map +1 -0
  166. package/dist/extensions/ieee/segment-builder.d.ts +9 -0
  167. package/dist/extensions/ieee/segment-builder.d.ts.map +1 -0
  168. package/dist/extensions/ieee/segment-builder.js +98 -0
  169. package/dist/extensions/ieee/segment-builder.js.map +1 -0
  170. package/dist/extensions/ieee/segment-templates.d.ts +58 -0
  171. package/dist/extensions/ieee/segment-templates.d.ts.map +1 -0
  172. package/dist/extensions/ieee/segment-templates.js +1618 -0
  173. package/dist/extensions/ieee/segment-templates.js.map +1 -0
  174. package/dist/extensions/ieee/source.d.ts +434 -0
  175. package/dist/extensions/ieee/source.d.ts.map +1 -0
  176. package/dist/extensions/ieee/source.js +12 -0
  177. package/dist/extensions/ieee/source.js.map +1 -0
  178. package/dist/index.d.ts +10 -0
  179. package/dist/index.d.ts.map +1 -0
  180. package/dist/index.js +10 -0
  181. package/dist/index.js.map +1 -0
  182. package/dist/lib/consts.d.ts +21 -0
  183. package/dist/lib/consts.d.ts.map +1 -0
  184. package/dist/lib/consts.js +117 -0
  185. package/dist/lib/consts.js.map +1 -0
  186. package/dist/lib/core/argument-engine.d.ts +181 -0
  187. package/dist/lib/core/argument-engine.d.ts.map +1 -0
  188. package/dist/lib/core/argument-engine.js +1294 -0
  189. package/dist/lib/core/argument-engine.js.map +1 -0
  190. package/dist/lib/core/argument-library.d.ts +84 -0
  191. package/dist/lib/core/argument-library.d.ts.map +1 -0
  192. package/dist/lib/core/argument-library.js +122 -0
  193. package/dist/lib/core/argument-library.js.map +1 -0
  194. package/dist/lib/core/argument-validation.d.ts +74 -0
  195. package/dist/lib/core/argument-validation.d.ts.map +1 -0
  196. package/dist/lib/core/argument-validation.js +315 -0
  197. package/dist/lib/core/argument-validation.js.map +1 -0
  198. package/dist/lib/core/change-collector.d.ts +24 -0
  199. package/dist/lib/core/change-collector.d.ts.map +1 -0
  200. package/dist/lib/core/change-collector.js +63 -0
  201. package/dist/lib/core/change-collector.js.map +1 -0
  202. package/dist/lib/core/checksum.d.ts +15 -0
  203. package/dist/lib/core/checksum.d.ts.map +1 -0
  204. package/dist/lib/core/checksum.js +43 -0
  205. package/dist/lib/core/checksum.js.map +1 -0
  206. package/dist/lib/core/claim-library.d.ts +23 -0
  207. package/dist/lib/core/claim-library.d.ts.map +1 -0
  208. package/dist/lib/core/claim-library.js +21 -0
  209. package/dist/lib/core/claim-library.js.map +1 -0
  210. package/dist/lib/core/claim-source-library.d.ts +32 -0
  211. package/dist/lib/core/claim-source-library.d.ts.map +1 -0
  212. package/dist/lib/core/claim-source-library.js +193 -0
  213. package/dist/lib/core/claim-source-library.js.map +1 -0
  214. package/dist/lib/core/diff.d.ts +20 -0
  215. package/dist/lib/core/diff.d.ts.map +1 -0
  216. package/dist/lib/core/diff.js +260 -0
  217. package/dist/lib/core/diff.js.map +1 -0
  218. package/dist/lib/core/evaluation/argument-evaluation.d.ts +53 -0
  219. package/dist/lib/core/evaluation/argument-evaluation.d.ts.map +1 -0
  220. package/dist/lib/core/evaluation/argument-evaluation.js +535 -0
  221. package/dist/lib/core/evaluation/argument-evaluation.js.map +1 -0
  222. package/dist/lib/core/evaluation/grading.d.ts +28 -0
  223. package/dist/lib/core/evaluation/grading.d.ts.map +1 -0
  224. package/dist/lib/core/evaluation/grading.js +44 -0
  225. package/dist/lib/core/evaluation/grading.js.map +1 -0
  226. package/dist/lib/core/evaluation/kleene.d.ts +12 -0
  227. package/dist/lib/core/evaluation/kleene.d.ts.map +1 -0
  228. package/dist/lib/core/evaluation/kleene.js +29 -0
  229. package/dist/lib/core/evaluation/kleene.js.map +1 -0
  230. package/dist/lib/core/evaluation/validation.d.ts +10 -0
  231. package/dist/lib/core/evaluation/validation.d.ts.map +1 -0
  232. package/dist/lib/core/evaluation/validation.js +28 -0
  233. package/dist/lib/core/evaluation/validation.js.map +1 -0
  234. package/dist/lib/core/expression-manager.d.ts +278 -0
  235. package/dist/lib/core/expression-manager.d.ts.map +1 -0
  236. package/dist/lib/core/expression-manager.js +1622 -0
  237. package/dist/lib/core/expression-manager.js.map +1 -0
  238. package/dist/lib/core/fork-library.d.ts +26 -0
  239. package/dist/lib/core/fork-library.d.ts.map +1 -0
  240. package/dist/lib/core/fork-library.js +71 -0
  241. package/dist/lib/core/fork-library.js.map +1 -0
  242. package/dist/lib/core/fork-namespace.d.ts +32 -0
  243. package/dist/lib/core/fork-namespace.d.ts.map +1 -0
  244. package/dist/lib/core/fork-namespace.js +99 -0
  245. package/dist/lib/core/fork-namespace.js.map +1 -0
  246. package/dist/lib/core/fork.d.ts +30 -0
  247. package/dist/lib/core/fork.d.ts.map +1 -0
  248. package/dist/lib/core/fork.js +125 -0
  249. package/dist/lib/core/fork.js.map +1 -0
  250. package/dist/lib/core/interfaces/argument-engine.interfaces.d.ts +366 -0
  251. package/dist/lib/core/interfaces/argument-engine.interfaces.d.ts.map +1 -0
  252. package/dist/lib/core/interfaces/argument-engine.interfaces.js +2 -0
  253. package/dist/lib/core/interfaces/argument-engine.interfaces.js.map +1 -0
  254. package/dist/lib/core/interfaces/index.d.ts +5 -0
  255. package/dist/lib/core/interfaces/index.d.ts.map +1 -0
  256. package/dist/lib/core/interfaces/index.js +2 -0
  257. package/dist/lib/core/interfaces/index.js.map +1 -0
  258. package/dist/lib/core/interfaces/library.interfaces.d.ts +347 -0
  259. package/dist/lib/core/interfaces/library.interfaces.d.ts.map +1 -0
  260. package/dist/lib/core/interfaces/library.interfaces.js +2 -0
  261. package/dist/lib/core/interfaces/library.interfaces.js.map +1 -0
  262. package/dist/lib/core/interfaces/premise-engine.interfaces.d.ts +401 -0
  263. package/dist/lib/core/interfaces/premise-engine.interfaces.d.ts.map +1 -0
  264. package/dist/lib/core/interfaces/premise-engine.interfaces.js +2 -0
  265. package/dist/lib/core/interfaces/premise-engine.interfaces.js.map +1 -0
  266. package/dist/lib/core/interfaces/shared.interfaces.d.ts +28 -0
  267. package/dist/lib/core/interfaces/shared.interfaces.d.ts.map +1 -0
  268. package/dist/lib/core/interfaces/shared.interfaces.js +2 -0
  269. package/dist/lib/core/interfaces/shared.interfaces.js.map +1 -0
  270. package/dist/lib/core/invariant-violation-error.d.ts +10 -0
  271. package/dist/lib/core/invariant-violation-error.d.ts.map +1 -0
  272. package/dist/lib/core/invariant-violation-error.js +16 -0
  273. package/dist/lib/core/invariant-violation-error.js.map +1 -0
  274. package/dist/lib/core/parser/formula-gen.js +923 -0
  275. package/dist/lib/core/parser/formula.d.ts +24 -0
  276. package/dist/lib/core/parser/formula.d.ts.map +1 -0
  277. package/dist/lib/core/parser/formula.js +8 -0
  278. package/dist/lib/core/parser/formula.js.map +1 -0
  279. package/dist/lib/core/premise-engine.d.ts +122 -0
  280. package/dist/lib/core/premise-engine.d.ts.map +1 -0
  281. package/dist/lib/core/premise-engine.js +1362 -0
  282. package/dist/lib/core/premise-engine.js.map +1 -0
  283. package/dist/lib/core/proposit-core.d.ts +111 -0
  284. package/dist/lib/core/proposit-core.d.ts.map +1 -0
  285. package/dist/lib/core/proposit-core.js +365 -0
  286. package/dist/lib/core/proposit-core.js.map +1 -0
  287. package/dist/lib/core/relationships.d.ts +15 -0
  288. package/dist/lib/core/relationships.d.ts.map +1 -0
  289. package/dist/lib/core/relationships.js +319 -0
  290. package/dist/lib/core/relationships.js.map +1 -0
  291. package/dist/lib/core/source-library.d.ts +23 -0
  292. package/dist/lib/core/source-library.d.ts.map +1 -0
  293. package/dist/lib/core/source-library.js +21 -0
  294. package/dist/lib/core/source-library.js.map +1 -0
  295. package/dist/lib/core/variable-manager.d.ts +68 -0
  296. package/dist/lib/core/variable-manager.d.ts.map +1 -0
  297. package/dist/lib/core/variable-manager.js +200 -0
  298. package/dist/lib/core/variable-manager.js.map +1 -0
  299. package/dist/lib/core/versioned-library.d.ts +52 -0
  300. package/dist/lib/core/versioned-library.d.ts.map +1 -0
  301. package/dist/lib/core/versioned-library.js +192 -0
  302. package/dist/lib/core/versioned-library.js.map +1 -0
  303. package/dist/lib/index.d.ts +54 -0
  304. package/dist/lib/index.d.ts.map +1 -0
  305. package/dist/lib/index.js +39 -0
  306. package/dist/lib/index.js.map +1 -0
  307. package/dist/lib/parsing/argument-parser.d.ts +49 -0
  308. package/dist/lib/parsing/argument-parser.d.ts.map +1 -0
  309. package/dist/lib/parsing/argument-parser.js +403 -0
  310. package/dist/lib/parsing/argument-parser.js.map +1 -0
  311. package/dist/lib/parsing/clamp-max-lengths.d.ts +11 -0
  312. package/dist/lib/parsing/clamp-max-lengths.d.ts.map +1 -0
  313. package/dist/lib/parsing/clamp-max-lengths.js +56 -0
  314. package/dist/lib/parsing/clamp-max-lengths.js.map +1 -0
  315. package/dist/lib/parsing/index.d.ts +7 -0
  316. package/dist/lib/parsing/index.d.ts.map +1 -0
  317. package/dist/lib/parsing/index.js +4 -0
  318. package/dist/lib/parsing/index.js.map +1 -0
  319. package/dist/lib/parsing/prompt-builder.d.ts +11 -0
  320. package/dist/lib/parsing/prompt-builder.d.ts.map +1 -0
  321. package/dist/lib/parsing/prompt-builder.js +229 -0
  322. package/dist/lib/parsing/prompt-builder.js.map +1 -0
  323. package/dist/lib/parsing/schemata.d.ts +79 -0
  324. package/dist/lib/parsing/schemata.d.ts.map +1 -0
  325. package/dist/lib/parsing/schemata.js +92 -0
  326. package/dist/lib/parsing/schemata.js.map +1 -0
  327. package/dist/lib/parsing/types.d.ts +24 -0
  328. package/dist/lib/parsing/types.d.ts.map +1 -0
  329. package/dist/lib/parsing/types.js +2 -0
  330. package/dist/lib/parsing/types.js.map +1 -0
  331. package/dist/lib/schemata/analysis.d.ts +9 -0
  332. package/dist/lib/schemata/analysis.d.ts.map +1 -0
  333. package/dist/lib/schemata/analysis.js +13 -0
  334. package/dist/lib/schemata/analysis.js.map +1 -0
  335. package/dist/lib/schemata/argument.d.ts +14 -0
  336. package/dist/lib/schemata/argument.d.ts.map +1 -0
  337. package/dist/lib/schemata/argument.js +24 -0
  338. package/dist/lib/schemata/argument.js.map +1 -0
  339. package/dist/lib/schemata/claim.d.ts +9 -0
  340. package/dist/lib/schemata/claim.d.ts.map +1 -0
  341. package/dist/lib/schemata/claim.js +18 -0
  342. package/dist/lib/schemata/claim.js.map +1 -0
  343. package/dist/lib/schemata/fork.d.ts +76 -0
  344. package/dist/lib/schemata/fork.d.ts.map +1 -0
  345. package/dist/lib/schemata/fork.js +55 -0
  346. package/dist/lib/schemata/fork.js.map +1 -0
  347. package/dist/lib/schemata/import.d.ts +33 -0
  348. package/dist/lib/schemata/import.d.ts.map +1 -0
  349. package/dist/lib/schemata/import.js +18 -0
  350. package/dist/lib/schemata/import.js.map +1 -0
  351. package/dist/lib/schemata/index.d.ts +8 -0
  352. package/dist/lib/schemata/index.d.ts.map +1 -0
  353. package/dist/lib/schemata/index.js +8 -0
  354. package/dist/lib/schemata/index.js.map +1 -0
  355. package/dist/lib/schemata/propositional.d.ts +142 -0
  356. package/dist/lib/schemata/propositional.d.ts.map +1 -0
  357. package/dist/lib/schemata/propositional.js +120 -0
  358. package/dist/lib/schemata/propositional.js.map +1 -0
  359. package/dist/lib/schemata/shared.d.ts +41 -0
  360. package/dist/lib/schemata/shared.d.ts.map +1 -0
  361. package/dist/lib/schemata/shared.js +66 -0
  362. package/dist/lib/schemata/shared.js.map +1 -0
  363. package/dist/lib/schemata/source.d.ts +18 -0
  364. package/dist/lib/schemata/source.d.ts.map +1 -0
  365. package/dist/lib/schemata/source.js +35 -0
  366. package/dist/lib/schemata/source.js.map +1 -0
  367. package/dist/lib/types/checksum.d.ts +20 -0
  368. package/dist/lib/types/checksum.d.ts.map +1 -0
  369. package/dist/lib/types/checksum.js +2 -0
  370. package/dist/lib/types/checksum.js.map +1 -0
  371. package/dist/lib/types/diff.d.ts +60 -0
  372. package/dist/lib/types/diff.d.ts.map +1 -0
  373. package/dist/lib/types/diff.js +2 -0
  374. package/dist/lib/types/diff.js.map +1 -0
  375. package/dist/lib/types/evaluation.d.ts +164 -0
  376. package/dist/lib/types/evaluation.d.ts.map +1 -0
  377. package/dist/lib/types/evaluation.js +2 -0
  378. package/dist/lib/types/evaluation.js.map +1 -0
  379. package/dist/lib/types/fork.d.ts +25 -0
  380. package/dist/lib/types/fork.d.ts.map +1 -0
  381. package/dist/lib/types/fork.js +2 -0
  382. package/dist/lib/types/fork.js.map +1 -0
  383. package/dist/lib/types/grammar.d.ts +38 -0
  384. package/dist/lib/types/grammar.d.ts.map +1 -0
  385. package/dist/lib/types/grammar.js +11 -0
  386. package/dist/lib/types/grammar.js.map +1 -0
  387. package/dist/lib/types/mutation.d.ts +31 -0
  388. package/dist/lib/types/mutation.d.ts.map +1 -0
  389. package/dist/lib/types/mutation.js +2 -0
  390. package/dist/lib/types/mutation.js.map +1 -0
  391. package/dist/lib/types/reactive.d.ts +14 -0
  392. package/dist/lib/types/reactive.d.ts.map +1 -0
  393. package/dist/lib/types/reactive.js +2 -0
  394. package/dist/lib/types/reactive.js.map +1 -0
  395. package/dist/lib/types/relationships.d.ts +36 -0
  396. package/dist/lib/types/relationships.d.ts.map +1 -0
  397. package/dist/lib/types/relationships.js +2 -0
  398. package/dist/lib/types/relationships.js.map +1 -0
  399. package/dist/lib/types/validation.d.ts +47 -0
  400. package/dist/lib/types/validation.d.ts.map +1 -0
  401. package/dist/lib/types/validation.js +43 -0
  402. package/dist/lib/types/validation.js.map +1 -0
  403. package/dist/lib/utils/changeset.d.ts +124 -0
  404. package/dist/lib/utils/changeset.d.ts.map +1 -0
  405. package/dist/lib/utils/changeset.js +221 -0
  406. package/dist/lib/utils/changeset.js.map +1 -0
  407. package/dist/lib/utils/collections.d.ts +12 -0
  408. package/dist/lib/utils/collections.d.ts.map +1 -0
  409. package/dist/lib/utils/collections.js +24 -0
  410. package/dist/lib/utils/collections.js.map +1 -0
  411. package/dist/lib/utils/default-map.d.ts +17 -0
  412. package/dist/lib/utils/default-map.d.ts.map +1 -0
  413. package/dist/lib/utils/default-map.js +33 -0
  414. package/dist/lib/utils/default-map.js.map +1 -0
  415. package/dist/lib/utils/lookup.d.ts +47 -0
  416. package/dist/lib/utils/lookup.d.ts.map +1 -0
  417. package/dist/lib/utils/lookup.js +62 -0
  418. package/dist/lib/utils/lookup.js.map +1 -0
  419. package/dist/lib/utils/position.d.ts +12 -0
  420. package/dist/lib/utils/position.d.ts.map +1 -0
  421. package/dist/lib/utils/position.js +13 -0
  422. package/dist/lib/utils/position.js.map +1 -0
  423. package/package.json +82 -0
  424. package/skills/proposit-core/SKILL.md +35 -0
  425. package/skills/proposit-core/docs/api-usage.md +442 -0
  426. package/skills/proposit-core/docs/architecture.md +256 -0
  427. package/skills/proposit-core/docs/cli.md +304 -0
  428. package/skills/proposit-core/docs/testing.md +281 -0
  429. package/skills/proposit-core/docs/types-schemas.md +648 -0
@@ -0,0 +1,1294 @@
1
+ import { isClaimBound, isPremiseBound, } from "../schemata/index.js";
2
+ import { DEFAULT_GRAMMAR_CONFIG, PERMISSIVE_GRAMMAR_CONFIG, } from "../types/grammar.js";
3
+ import { DEFAULT_CHECKSUM_CONFIG, normalizeChecksumConfig, serializeChecksumConfig, } from "../consts.js";
4
+ import { ChangeCollector } from "./change-collector.js";
5
+ import { canonicalSerialize, computeHash, entityChecksum } from "./checksum.js";
6
+ import { evaluateArgument as evaluateArgumentStandalone, checkArgumentValidity as checkArgumentValidityStandalone, } from "./evaluation/argument-evaluation.js";
7
+ import { validateArgument as validateArgumentStandalone, validateArgumentAfterPremiseMutation as validateAfterPremiseMutationStandalone, validateArgumentEvaluability as validateArgumentEvaluabilityStandalone, collectArgumentReferencedVariables as collectArgumentReferencedVariablesStandalone, } from "./argument-validation.js";
8
+ import { InvariantViolationError } from "./invariant-violation-error.js";
9
+ import { PremiseEngine } from "./premise-engine.js";
10
+ import { VariableManager } from "./variable-manager.js";
11
+ /** Default ID generator using the Web Crypto API (Node.js 20+, all modern browsers). */
12
+ export const defaultGenerateId = () => globalThis.crypto.randomUUID();
13
+ /**
14
+ * Manages a propositional logic argument composed of premises, variable
15
+ * assignments, and logical roles (supporting premises and a conclusion).
16
+ *
17
+ * Provides premise CRUD, role management, evaluation of individual
18
+ * assignments, and exhaustive validity checking via truth-table enumeration.
19
+ */
20
+ export class ArgumentEngine {
21
+ argument;
22
+ premises;
23
+ variables;
24
+ claimLibrary;
25
+ sourceLibrary;
26
+ claimSourceLibrary;
27
+ conclusionPremiseId;
28
+ checksumConfig;
29
+ positionConfig;
30
+ grammarConfig;
31
+ generateId;
32
+ restoringFromSnapshot = false;
33
+ checksumDirty = true;
34
+ cachedMetaChecksum;
35
+ cachedDescendantChecksum;
36
+ cachedCombinedChecksum;
37
+ cachedPremisesCollectionChecksum;
38
+ cachedVariablesCollectionChecksum;
39
+ expressionIndex;
40
+ listeners = new Set();
41
+ reactiveDirty = {
42
+ argument: true,
43
+ variables: true,
44
+ roles: true,
45
+ premiseIds: new Set(),
46
+ allPremises: true,
47
+ };
48
+ cachedReactiveSnapshot;
49
+ constructor(argument, claimLibrary, sourceLibrary, claimSourceLibrary, options) {
50
+ this.argument = { ...argument };
51
+ this.claimLibrary = claimLibrary;
52
+ this.sourceLibrary = sourceLibrary;
53
+ this.claimSourceLibrary = claimSourceLibrary;
54
+ this.premises = new Map();
55
+ this.checksumConfig = options?.checksumConfig;
56
+ this.positionConfig = options?.positionConfig;
57
+ this.grammarConfig = options?.grammarConfig;
58
+ this.generateId = options?.generateId ?? defaultGenerateId;
59
+ this.variables = new VariableManager({
60
+ checksumConfig: this.checksumConfig,
61
+ positionConfig: this.positionConfig,
62
+ generateId: this.generateId,
63
+ });
64
+ this.expressionIndex = new Map();
65
+ this.conclusionPremiseId = undefined;
66
+ }
67
+ createCircularityCheck() {
68
+ return (variableId, targetPremiseId) => {
69
+ return this.wouldCreateCycle(variableId, targetPremiseId, new Set());
70
+ };
71
+ }
72
+ wouldCreateCycle(variableId, targetPremiseId, visited) {
73
+ const variable = this.variables.getVariable(variableId);
74
+ if (!variable)
75
+ return false;
76
+ if (!isPremiseBound(variable))
77
+ return false;
78
+ const bound = variable;
79
+ if (bound.boundPremiseId === targetPremiseId)
80
+ return true;
81
+ if (visited.size >= this.premises.size) {
82
+ throw new Error(`Circularity check depth limit exceeded (visited ${visited.size} premises).`);
83
+ }
84
+ if (visited.has(bound.boundPremiseId))
85
+ return false;
86
+ visited.add(bound.boundPremiseId);
87
+ const boundPremise = this.premises.get(bound.boundPremiseId);
88
+ if (!boundPremise)
89
+ return false;
90
+ for (const expr of boundPremise.getExpressions()) {
91
+ if (expr.type === "variable") {
92
+ if (this.wouldCreateCycle(expr.variableId, targetPremiseId, visited)) {
93
+ return true;
94
+ }
95
+ }
96
+ }
97
+ return false;
98
+ }
99
+ wireCircularityCheck(pm) {
100
+ pm.setCircularityCheck(this.createCircularityCheck());
101
+ }
102
+ wireEmptyBoundPremiseCheck(pm) {
103
+ pm.setEmptyBoundPremiseCheck((variableId) => {
104
+ const v = this.variables.getVariable(variableId);
105
+ if (!v ||
106
+ !isPremiseBound(v))
107
+ return false;
108
+ const boundPremise = this.premises.get(v.boundPremiseId);
109
+ return !boundPremise?.getRootExpressionId();
110
+ });
111
+ }
112
+ generateUniqueSymbol() {
113
+ let n = this.premises.size - 1;
114
+ let candidate = `P${n}`;
115
+ while (this.variables.getVariableBySymbol(candidate) !== undefined) {
116
+ n++;
117
+ candidate = `P${n}`;
118
+ }
119
+ return candidate;
120
+ }
121
+ subscribe = (listener) => {
122
+ this.listeners.add(listener);
123
+ return () => {
124
+ this.listeners.delete(listener);
125
+ };
126
+ };
127
+ notifySubscribers() {
128
+ for (const listener of this.listeners) {
129
+ listener();
130
+ }
131
+ }
132
+ finalizeChanges(collector) {
133
+ this.markDirty();
134
+ const changes = collector.toChangeset();
135
+ this.markReactiveDirty(changes);
136
+ this.notifySubscribers();
137
+ return changes;
138
+ }
139
+ static skipValidationResult = {
140
+ ok: true,
141
+ violations: [],
142
+ };
143
+ suppressPremiseValidation() {
144
+ for (const pe of this.premises.values()) {
145
+ pe.setArgumentValidateCallback(() => ArgumentEngine.skipValidationResult);
146
+ }
147
+ }
148
+ restorePremiseValidation() {
149
+ for (const pe of this.premises.values()) {
150
+ pe.setArgumentValidateCallback(() => this.validateAfterPremiseMutation());
151
+ }
152
+ }
153
+ withValidation(fn) {
154
+ if (this.restoringFromSnapshot) {
155
+ return fn();
156
+ }
157
+ const snap = this.snapshot();
158
+ // Suppress PremiseEngine-level validation during ArgumentEngine
159
+ // mutations. The ArgumentEngine will do its own validation at the end.
160
+ this.suppressPremiseValidation();
161
+ try {
162
+ const result = fn();
163
+ const validation = this.validate();
164
+ if (!validation.ok) {
165
+ this.rollbackInternal(snap);
166
+ throw new InvariantViolationError(validation.violations);
167
+ }
168
+ return result;
169
+ }
170
+ catch (e) {
171
+ if (!(e instanceof InvariantViolationError)) {
172
+ this.rollbackInternal(snap);
173
+ }
174
+ throw e;
175
+ }
176
+ finally {
177
+ this.restorePremiseValidation();
178
+ }
179
+ }
180
+ getSnapshot = () => {
181
+ return this.buildReactiveSnapshot();
182
+ };
183
+ buildReactiveSnapshot() {
184
+ const dirty = this.reactiveDirty;
185
+ const prev = this.cachedReactiveSnapshot;
186
+ if (prev &&
187
+ !dirty.argument &&
188
+ !dirty.variables &&
189
+ !dirty.roles &&
190
+ dirty.premiseIds.size === 0 &&
191
+ !dirty.allPremises) {
192
+ return prev;
193
+ }
194
+ const argument = dirty.argument || !prev ? this.getArgument() : prev.argument;
195
+ const variables = dirty.variables || !prev
196
+ ? this.buildVariablesRecord()
197
+ : prev.variables;
198
+ const roles = dirty.roles || !prev ? { ...this.getRoleState() } : prev.roles;
199
+ let premises;
200
+ if (dirty.allPremises || !prev) {
201
+ premises = this.buildAllPremisesRecord();
202
+ }
203
+ else {
204
+ premises = { ...prev.premises };
205
+ // Remove premises that no longer exist
206
+ for (const id of Object.keys(premises)) {
207
+ if (!this.premises.has(id)) {
208
+ delete premises[id];
209
+ }
210
+ }
211
+ // Rebuild dirty premises
212
+ for (const id of dirty.premiseIds) {
213
+ const pm = this.premises.get(id);
214
+ if (pm) {
215
+ premises[id] = this.buildPremiseRecord(pm);
216
+ }
217
+ else {
218
+ delete premises[id];
219
+ }
220
+ }
221
+ // Add any new premises not yet in snapshot
222
+ for (const [id, pm] of this.premises) {
223
+ if (!(id in premises)) {
224
+ premises[id] = this.buildPremiseRecord(pm);
225
+ }
226
+ }
227
+ }
228
+ const snapshot = {
229
+ argument,
230
+ variables,
231
+ premises,
232
+ roles,
233
+ };
234
+ this.cachedReactiveSnapshot = snapshot;
235
+ this.reactiveDirty = {
236
+ argument: false,
237
+ variables: false,
238
+ roles: false,
239
+ premiseIds: new Set(),
240
+ allPremises: false,
241
+ };
242
+ return snapshot;
243
+ }
244
+ buildVariablesRecord() {
245
+ const result = {};
246
+ for (const v of this.variables.toArray()) {
247
+ result[v.id] = v;
248
+ }
249
+ return result;
250
+ }
251
+ buildAllPremisesRecord() {
252
+ const result = {};
253
+ for (const [id, pm] of this.premises) {
254
+ result[id] = this.buildPremiseRecord(pm);
255
+ }
256
+ return result;
257
+ }
258
+ buildPremiseRecord(pm) {
259
+ const expressions = {};
260
+ for (const expr of pm.getExpressions()) {
261
+ expressions[expr.id] = expr;
262
+ }
263
+ return {
264
+ premise: pm.toPremiseData(),
265
+ expressions,
266
+ rootExpressionId: pm.getRootExpressionId(),
267
+ };
268
+ }
269
+ markReactiveDirty(changes) {
270
+ if (changes.argument) {
271
+ this.reactiveDirty.argument = true;
272
+ }
273
+ if (changes.variables) {
274
+ this.reactiveDirty.variables = true;
275
+ }
276
+ if (changes.roles) {
277
+ this.reactiveDirty.roles = true;
278
+ }
279
+ if (changes.expressions) {
280
+ const allExprs = [
281
+ ...changes.expressions.added,
282
+ ...changes.expressions.modified,
283
+ ...changes.expressions.removed,
284
+ ];
285
+ for (const expr of allExprs) {
286
+ this.reactiveDirty.premiseIds.add(expr.premiseId);
287
+ }
288
+ }
289
+ if (changes.premises) {
290
+ for (const p of [
291
+ ...changes.premises.added,
292
+ ...changes.premises.modified,
293
+ ...changes.premises.removed,
294
+ ]) {
295
+ this.reactiveDirty.premiseIds.add(p.id);
296
+ }
297
+ }
298
+ }
299
+ getArgument() {
300
+ this.flushChecksums();
301
+ return {
302
+ ...this.argument,
303
+ checksum: this.cachedMetaChecksum,
304
+ descendantChecksum: this.cachedDescendantChecksum,
305
+ combinedChecksum: this.cachedCombinedChecksum,
306
+ };
307
+ }
308
+ toDisplayString() {
309
+ const lines = [];
310
+ const arg = this.getArgument();
311
+ lines.push(`Argument: ${arg.id} (v${arg.version})`);
312
+ lines.push("");
313
+ const supportingIds = new Set(this.listSupportingPremises().map((pe) => pe.getId()));
314
+ for (const pe of this.listPremises()) {
315
+ let role;
316
+ if (pe.getId() === this.conclusionPremiseId) {
317
+ role = "Conclusion";
318
+ }
319
+ else if (supportingIds.has(pe.getId())) {
320
+ role = "Supporting";
321
+ }
322
+ else {
323
+ role = "Constraint";
324
+ }
325
+ const display = pe.toDisplayString() || "(empty)";
326
+ lines.push(`[${role}] ${display}`);
327
+ }
328
+ return lines.join("\n");
329
+ }
330
+ createPremise(extras, symbol) {
331
+ return this.createPremiseWithId(this.generateId(), extras, symbol);
332
+ }
333
+ createPremiseWithId(id, extras, symbol) {
334
+ return this.withValidation(() => {
335
+ if (this.premises.has(id)) {
336
+ throw new Error(`Premise "${id}" already exists.`);
337
+ }
338
+ const premiseData = {
339
+ ...extras,
340
+ id,
341
+ argumentId: this.argument.id,
342
+ argumentVersion: this.argument.version,
343
+ };
344
+ const pm = new PremiseEngine(premiseData, {
345
+ argument: this.argument,
346
+ variables: this.variables,
347
+ expressionIndex: this.expressionIndex,
348
+ }, {
349
+ checksumConfig: this.checksumConfig,
350
+ positionConfig: this.positionConfig,
351
+ grammarConfig: this.grammarConfig,
352
+ generateId: this.generateId,
353
+ });
354
+ this.premises.set(id, pm);
355
+ this.wireCircularityCheck(pm);
356
+ this.wireEmptyBoundPremiseCheck(pm);
357
+ pm.setVariableIdsCallback(() => new Set(this.variables.toArray().map((v) => v.id)));
358
+ pm.setArgumentValidateCallback(() => this.validateAfterPremiseMutation());
359
+ pm.setOnMutate(() => {
360
+ this.markDirty();
361
+ this.reactiveDirty.premiseIds.add(id);
362
+ this.notifySubscribers();
363
+ });
364
+ const collector = new ChangeCollector();
365
+ collector.addedPremise(pm.toPremiseData());
366
+ this.markDirty();
367
+ if (this.conclusionPremiseId === undefined) {
368
+ this.conclusionPremiseId = id;
369
+ collector.setRoles(this.getRoleState());
370
+ }
371
+ // Auto-create a premise-bound variable for this premise
372
+ if (!this.restoringFromSnapshot) {
373
+ const autoSymbol = symbol ?? this.generateUniqueSymbol();
374
+ const autoVariable = {
375
+ id: this.generateId(),
376
+ argumentId: this.argument.id,
377
+ argumentVersion: this.argument.version,
378
+ symbol: autoSymbol,
379
+ boundPremiseId: id,
380
+ boundArgumentId: this.argument.id,
381
+ boundArgumentVersion: this.argument.version,
382
+ };
383
+ const withChecksum = this.attachVariableChecksum({
384
+ ...autoVariable,
385
+ });
386
+ this.variables.addVariable(withChecksum);
387
+ collector.addedVariable(withChecksum);
388
+ this.markAllPremisesDirty();
389
+ }
390
+ const changes = this.finalizeChanges(collector);
391
+ return {
392
+ result: pm,
393
+ changes,
394
+ };
395
+ });
396
+ }
397
+ removePremise(premiseId) {
398
+ return this.withValidation(() => {
399
+ const pm = this.premises.get(premiseId);
400
+ if (!pm)
401
+ return { result: undefined, changes: {} };
402
+ const data = pm.toPremiseData();
403
+ const collector = new ChangeCollector();
404
+ // Clean up expression index for removed premise's expressions
405
+ for (const expr of pm.getExpressions()) {
406
+ this.expressionIndex.delete(expr.id);
407
+ }
408
+ this.premises.delete(premiseId);
409
+ collector.removedPremise(data);
410
+ if (this.conclusionPremiseId === premiseId) {
411
+ this.conclusionPremiseId = undefined;
412
+ collector.setRoles(this.getRoleState());
413
+ }
414
+ // Cascade: remove variables bound to the deleted premise
415
+ const boundVars = this.getVariablesBoundToPremise(premiseId);
416
+ for (const v of boundVars) {
417
+ const removeResult = this.removeVariableCore(v.id);
418
+ if (removeResult.changes.variables) {
419
+ for (const rv of removeResult.changes.variables.removed) {
420
+ collector.removedVariable(rv);
421
+ }
422
+ }
423
+ if (removeResult.changes.expressions) {
424
+ for (const re of removeResult.changes.expressions.removed) {
425
+ collector.removedExpression(re);
426
+ }
427
+ }
428
+ }
429
+ const changes = this.finalizeChanges(collector);
430
+ return {
431
+ result: data,
432
+ changes,
433
+ };
434
+ });
435
+ }
436
+ getPremise(premiseId) {
437
+ return this.premises.get(premiseId);
438
+ }
439
+ hasPremise(premiseId) {
440
+ return this.premises.has(premiseId);
441
+ }
442
+ listPremiseIds() {
443
+ return Array.from(this.premises.keys()).sort((a, b) => a.localeCompare(b));
444
+ }
445
+ listPremises() {
446
+ return this.listPremiseIds()
447
+ .map((id) => this.premises.get(id))
448
+ .filter((pm) => pm !== undefined);
449
+ }
450
+ addVariable(variable) {
451
+ return this.withValidation(() => {
452
+ // Only claim-bound variables may be added via addVariable.
453
+ // Premise-bound variables must use bindVariableToPremise.
454
+ if (!isClaimBound(variable)) {
455
+ throw new Error("addVariable only accepts claim-bound variables. Use bindVariableToPremise for premise-bound variables.");
456
+ }
457
+ if (variable.argumentId !== this.argument.id) {
458
+ throw new Error(`Variable argumentId "${variable.argumentId}" does not match engine argument ID "${this.argument.id}".`);
459
+ }
460
+ if (variable.argumentVersion !== this.argument.version) {
461
+ throw new Error(`Variable argumentVersion "${variable.argumentVersion}" does not match engine argument version "${this.argument.version}".`);
462
+ }
463
+ // Validate claim reference
464
+ if (!this.claimLibrary.get(variable.claimId, variable.claimVersion)) {
465
+ throw new Error(`Claim "${variable.claimId}" version ${variable.claimVersion} does not exist in the claim library.`);
466
+ }
467
+ const withChecksum = this.attachVariableChecksum({
468
+ ...variable,
469
+ });
470
+ this.variables.addVariable(withChecksum);
471
+ const collector = new ChangeCollector();
472
+ collector.addedVariable(withChecksum);
473
+ this.markAllPremisesDirty();
474
+ const changes = this.finalizeChanges(collector);
475
+ return {
476
+ result: withChecksum,
477
+ changes,
478
+ };
479
+ });
480
+ }
481
+ bindVariableToPremise(variable) {
482
+ return this.withValidation(() => {
483
+ if (variable.argumentId !== this.argument.id) {
484
+ throw new Error(`Variable argumentId "${variable.argumentId}" does not match engine argument ID "${this.argument.id}".`);
485
+ }
486
+ if (variable.argumentVersion !== this.argument.version) {
487
+ throw new Error(`Variable argumentVersion "${variable.argumentVersion}" does not match engine argument version "${this.argument.version}".`);
488
+ }
489
+ if (variable.boundArgumentId !== this.argument.id) {
490
+ throw new Error(`Cross-argument bindings are not supported. boundArgumentId "${variable.boundArgumentId}" does not match engine argument ID "${this.argument.id}".`);
491
+ }
492
+ if (!this.premises.has(variable.boundPremiseId)) {
493
+ throw new Error(`Bound premise "${variable.boundPremiseId}" does not exist in this argument.`);
494
+ }
495
+ const withChecksum = this.attachVariableChecksum({
496
+ ...variable,
497
+ });
498
+ this.variables.addVariable(withChecksum);
499
+ const collector = new ChangeCollector();
500
+ collector.addedVariable(withChecksum);
501
+ this.markAllPremisesDirty();
502
+ const changes = this.finalizeChanges(collector);
503
+ return {
504
+ result: withChecksum,
505
+ changes,
506
+ };
507
+ });
508
+ }
509
+ /** Adds a premise-bound variable that references a premise in a different argument. */
510
+ bindVariableToExternalPremise(variable) {
511
+ return this.withValidation(() => {
512
+ if (variable.argumentId !== this.argument.id) {
513
+ throw new Error(`Variable argumentId "${variable.argumentId}" does not match engine argument ID "${this.argument.id}".`);
514
+ }
515
+ if (variable.argumentVersion !== this.argument.version) {
516
+ throw new Error(`Variable argumentVersion "${variable.argumentVersion}" does not match engine argument version "${this.argument.version}".`);
517
+ }
518
+ if (variable.boundArgumentId === this.argument.id) {
519
+ throw new Error(`boundArgumentId matches this engine's argument — use bindVariableToPremise for internal bindings.`);
520
+ }
521
+ if (!this.canBind(variable.boundArgumentId, variable.boundArgumentVersion)) {
522
+ throw new Error(`Binding to argument "${variable.boundArgumentId}" version ${variable.boundArgumentVersion} is not allowed.`);
523
+ }
524
+ const withChecksum = this.attachVariableChecksum({
525
+ ...variable,
526
+ });
527
+ this.variables.addVariable(withChecksum);
528
+ const collector = new ChangeCollector();
529
+ collector.addedVariable(withChecksum);
530
+ this.markAllPremisesDirty();
531
+ const changes = this.finalizeChanges(collector);
532
+ return {
533
+ result: withChecksum,
534
+ changes,
535
+ };
536
+ });
537
+ }
538
+ /** Adds a premise-bound variable that references another argument's conclusion premise. */
539
+ bindVariableToArgument(variable, conclusionPremiseId) {
540
+ return this.bindVariableToExternalPremise({
541
+ ...variable,
542
+ boundPremiseId: conclusionPremiseId,
543
+ });
544
+ }
545
+ updateVariable(variableId, updates) {
546
+ return this.withValidation(() => {
547
+ const existing = this.variables.getVariable(variableId);
548
+ if (!existing) {
549
+ return { result: undefined, changes: {} };
550
+ }
551
+ const existingVar = existing;
552
+ const updatesObj = updates;
553
+ // Reject binding-type conversion
554
+ if (isClaimBound(existingVar)) {
555
+ const premiseBoundFields = [
556
+ "boundPremiseId",
557
+ "boundArgumentId",
558
+ "boundArgumentVersion",
559
+ ];
560
+ for (const f of premiseBoundFields) {
561
+ if (updatesObj[f] !== undefined) {
562
+ throw new Error(`Cannot set "${f}" on a claim-bound variable. Delete and re-create to change binding type.`);
563
+ }
564
+ }
565
+ // Validate: claimId and claimVersion must be provided together
566
+ const hasClaimId = updatesObj.claimId !== undefined;
567
+ const hasClaimVersion = updatesObj.claimVersion !== undefined;
568
+ if (hasClaimId !== hasClaimVersion) {
569
+ throw new Error("claimId and claimVersion must be provided together.");
570
+ }
571
+ // Validate claim reference if provided
572
+ if (hasClaimId && hasClaimVersion) {
573
+ if (!this.claimLibrary.get(updatesObj.claimId, updatesObj.claimVersion)) {
574
+ throw new Error(`Claim "${String(updatesObj.claimId)}" version ${String(updatesObj.claimVersion)} does not exist in the claim library.`);
575
+ }
576
+ }
577
+ }
578
+ else if (isPremiseBound(existingVar)) {
579
+ const claimBoundFields = ["claimId", "claimVersion"];
580
+ for (const f of claimBoundFields) {
581
+ if (updatesObj[f] !== undefined) {
582
+ throw new Error(`Cannot set "${f}" on a premise-bound variable. Delete and re-create to change binding type.`);
583
+ }
584
+ }
585
+ // Validate boundPremiseId if provided
586
+ if (updatesObj.boundPremiseId !== undefined) {
587
+ const newPremiseId = updatesObj.boundPremiseId;
588
+ if (!this.premises.has(newPremiseId)) {
589
+ throw new Error(`Bound premise "${newPremiseId}" does not exist in this argument.`);
590
+ }
591
+ }
592
+ }
593
+ const updated = this.variables.updateVariable(variableId, updates);
594
+ const collector = new ChangeCollector();
595
+ if (updated) {
596
+ const withChecksum = this.attachVariableChecksum({
597
+ ...updated,
598
+ });
599
+ // Re-store with updated checksum so VariableManager always holds
600
+ // variables with correct checksums.
601
+ this.variables.removeVariable(variableId);
602
+ this.variables.addVariable(withChecksum);
603
+ collector.modifiedVariable(withChecksum);
604
+ this.markAllPremisesDirty();
605
+ const changes = this.finalizeChanges(collector);
606
+ return {
607
+ result: withChecksum,
608
+ changes,
609
+ };
610
+ }
611
+ return {
612
+ result: undefined,
613
+ changes: collector.toChangeset(),
614
+ };
615
+ });
616
+ }
617
+ removeVariableCore(variableId) {
618
+ const variable = this.variables.getVariable(variableId);
619
+ if (!variable) {
620
+ return { result: undefined, changes: {} };
621
+ }
622
+ const collector = new ChangeCollector();
623
+ // Cascade: delete referencing expressions in every premise
624
+ for (const pm of this.listPremises()) {
625
+ const { changes } = pm.deleteExpressionsUsingVariable(variableId);
626
+ if (changes.expressions) {
627
+ for (const e of changes.expressions.removed) {
628
+ collector.removedExpression(e);
629
+ }
630
+ }
631
+ }
632
+ this.variables.removeVariable(variableId);
633
+ collector.removedVariable(variable);
634
+ this.markAllPremisesDirty();
635
+ const changes = this.finalizeChanges(collector);
636
+ return {
637
+ result: variable,
638
+ changes,
639
+ };
640
+ }
641
+ removeVariable(variableId) {
642
+ return this.withValidation(() => {
643
+ return this.removeVariableCore(variableId);
644
+ });
645
+ }
646
+ getVariables() {
647
+ return this.variables.toArray();
648
+ }
649
+ getVariable(variableId) {
650
+ return this.variables.getVariable(variableId);
651
+ }
652
+ hasVariable(variableId) {
653
+ return this.variables.hasVariable(variableId);
654
+ }
655
+ getVariableBySymbol(symbol) {
656
+ return this.variables.getVariableBySymbol(symbol);
657
+ }
658
+ buildVariableIndex(keyFn) {
659
+ const map = new Map();
660
+ for (const v of this.variables.toArray()) {
661
+ map.set(keyFn(v), v);
662
+ }
663
+ return map;
664
+ }
665
+ getVariablesBoundToPremise(premiseId) {
666
+ return this.variables.toArray().filter((v) => {
667
+ const base = v;
668
+ return isPremiseBound(base) && base.boundPremiseId === premiseId;
669
+ });
670
+ }
671
+ getExpression(expressionId) {
672
+ const premiseId = this.expressionIndex.get(expressionId);
673
+ if (premiseId === undefined)
674
+ return undefined;
675
+ return this.premises.get(premiseId)?.getExpression(expressionId);
676
+ }
677
+ hasExpression(expressionId) {
678
+ return this.expressionIndex.has(expressionId);
679
+ }
680
+ getExpressionPremiseId(expressionId) {
681
+ return this.expressionIndex.get(expressionId);
682
+ }
683
+ findPremiseByExpressionId(expressionId) {
684
+ const premiseId = this.expressionIndex.get(expressionId);
685
+ if (premiseId === undefined)
686
+ return undefined;
687
+ return this.premises.get(premiseId);
688
+ }
689
+ getAllExpressions() {
690
+ const all = [];
691
+ for (const pe of this.listPremises()) {
692
+ all.push(...pe.getExpressions());
693
+ }
694
+ return all.sort((a, b) => a.id.localeCompare(b.id));
695
+ }
696
+ getExpressionsByVariableId(variableId) {
697
+ const result = [];
698
+ for (const pe of this.listPremises()) {
699
+ const refIds = pe.getReferencedVariableIds();
700
+ if (!refIds.has(variableId))
701
+ continue;
702
+ for (const expr of pe.getExpressions()) {
703
+ if (expr.type === "variable" &&
704
+ expr.variableId === variableId) {
705
+ result.push(expr);
706
+ }
707
+ }
708
+ }
709
+ return result;
710
+ }
711
+ listRootExpressions() {
712
+ const roots = [];
713
+ for (const pe of this.listPremises()) {
714
+ const root = pe.getRootExpression();
715
+ if (root)
716
+ roots.push(root);
717
+ }
718
+ return roots;
719
+ }
720
+ /**
721
+ * Normalizes expression trees across all premises. Collapses unjustified
722
+ * formulas, operators with 0/1 children, and inserts formula buffers where
723
+ * needed. Works regardless of `autoNormalize` setting.
724
+ */
725
+ normalizeAllExpressions() {
726
+ const merged = {};
727
+ for (const pe of this.premises.values()) {
728
+ const { changes } = pe.normalizeExpressions();
729
+ if (changes.expressions) {
730
+ merged.expressions ??= { added: [], modified: [], removed: [] };
731
+ merged.expressions.added.push(...changes.expressions.added);
732
+ merged.expressions.modified.push(...changes.expressions.modified);
733
+ merged.expressions.removed.push(...changes.expressions.removed);
734
+ }
735
+ if (changes.premises) {
736
+ merged.premises ??= { added: [], modified: [], removed: [] };
737
+ merged.premises.modified.push(...changes.premises.modified);
738
+ }
739
+ }
740
+ return { result: undefined, changes: merged };
741
+ }
742
+ getRoleState() {
743
+ return {
744
+ ...(this.conclusionPremiseId !== undefined
745
+ ? { conclusionPremiseId: this.conclusionPremiseId }
746
+ : {}),
747
+ };
748
+ }
749
+ setConclusionPremise(premiseId) {
750
+ return this.withValidation(() => {
751
+ const premise = this.premises.get(premiseId);
752
+ if (!premise) {
753
+ throw new Error(`Premise "${premiseId}" does not exist.`);
754
+ }
755
+ this.conclusionPremiseId = premiseId;
756
+ const roles = this.getRoleState();
757
+ const collector = new ChangeCollector();
758
+ collector.setRoles(roles);
759
+ const changes = this.finalizeChanges(collector);
760
+ return {
761
+ result: roles,
762
+ changes,
763
+ };
764
+ });
765
+ }
766
+ clearConclusionPremise() {
767
+ return this.withValidation(() => {
768
+ this.conclusionPremiseId = undefined;
769
+ const roles = this.getRoleState();
770
+ const collector = new ChangeCollector();
771
+ collector.setRoles(roles);
772
+ const changes = this.finalizeChanges(collector);
773
+ return {
774
+ result: roles,
775
+ changes,
776
+ };
777
+ });
778
+ }
779
+ getConclusionPremise() {
780
+ if (this.conclusionPremiseId === undefined) {
781
+ return undefined;
782
+ }
783
+ return this.premises.get(this.conclusionPremiseId);
784
+ }
785
+ listSupportingPremises() {
786
+ return this.listPremises().filter((pm) => pm.isInference() && pm.getId() !== this.conclusionPremiseId);
787
+ }
788
+ snapshot() {
789
+ this.flushChecksums();
790
+ return {
791
+ argument: {
792
+ ...this.argument,
793
+ checksum: this.cachedMetaChecksum,
794
+ descendantChecksum: this.cachedDescendantChecksum,
795
+ combinedChecksum: this.cachedCombinedChecksum,
796
+ },
797
+ variables: this.variables.snapshot(),
798
+ premises: this.listPremises().map((pe) => pe.snapshot()),
799
+ ...(this.conclusionPremiseId !== undefined
800
+ ? { conclusionPremiseId: this.conclusionPremiseId }
801
+ : {}),
802
+ config: {
803
+ checksumConfig: serializeChecksumConfig(this.checksumConfig),
804
+ positionConfig: this.positionConfig,
805
+ grammarConfig: this.grammarConfig,
806
+ },
807
+ };
808
+ }
809
+ /** Creates a new ArgumentEngine from a previously captured snapshot. */
810
+ static fromSnapshot(snapshot, claimLibrary, sourceLibrary, claimSourceLibrary, grammarConfig, checksumVerification, generateId) {
811
+ const engine = new ArgumentEngine(snapshot.argument, claimLibrary, sourceLibrary, claimSourceLibrary, snapshot.config
812
+ ? {
813
+ ...snapshot.config,
814
+ checksumConfig: normalizeChecksumConfig(snapshot.config.checksumConfig),
815
+ generateId: generateId ?? snapshot.config.generateId,
816
+ }
817
+ : generateId
818
+ ? { generateId }
819
+ : undefined);
820
+ engine.restoringFromSnapshot = true;
821
+ // Restore premises first (premise-bound variables reference them)
822
+ for (const premiseSnap of snapshot.premises) {
823
+ const pe = PremiseEngine.fromSnapshot(premiseSnap, snapshot.argument, engine.variables, engine.expressionIndex, grammarConfig, generateId);
824
+ engine.premises.set(pe.getId(), pe);
825
+ engine.wireCircularityCheck(pe);
826
+ engine.wireEmptyBoundPremiseCheck(pe);
827
+ pe.setVariableIdsCallback(() => new Set(engine.variables.toArray().map((v) => v.id)));
828
+ pe.setArgumentValidateCallback(() => engine.validateAfterPremiseMutation());
829
+ const premiseId = pe.getId();
830
+ pe.setOnMutate(() => {
831
+ engine.markDirty();
832
+ engine.reactiveDirty.premiseIds.add(premiseId);
833
+ engine.notifySubscribers();
834
+ });
835
+ }
836
+ // Restore claim-bound variables first, then premise-bound variables
837
+ for (const v of snapshot.variables.variables) {
838
+ if (isClaimBound(v)) {
839
+ engine.addVariable(v);
840
+ }
841
+ }
842
+ for (const v of snapshot.variables.variables) {
843
+ if (isPremiseBound(v)) {
844
+ const pbv = v;
845
+ if (pbv.boundArgumentId === engine.argument.id) {
846
+ engine.bindVariableToPremise(v);
847
+ }
848
+ else {
849
+ engine.bindVariableToExternalPremise(v);
850
+ }
851
+ }
852
+ }
853
+ // Restore conclusion role (don't use setConclusionPremise to avoid auto-assign logic)
854
+ engine.conclusionPremiseId = snapshot.conclusionPremiseId;
855
+ engine.restoringFromSnapshot = false;
856
+ // Post-load normalization: collapse unjustified formulas if the
857
+ // caller's grammar config requests it.
858
+ const restoredGrammarConfig = grammarConfig ?? DEFAULT_GRAMMAR_CONFIG;
859
+ if (restoredGrammarConfig.autoNormalize) {
860
+ for (const pe of engine.premises.values()) {
861
+ pe.normalizeExpressions();
862
+ }
863
+ }
864
+ if (checksumVerification === "strict") {
865
+ engine.flushChecksums();
866
+ ArgumentEngine.verifySnapshotChecksums(engine, snapshot);
867
+ }
868
+ const validation = engine.validate();
869
+ if (!validation.ok) {
870
+ throw new InvariantViolationError(validation.violations);
871
+ }
872
+ return engine;
873
+ }
874
+ /**
875
+ * Creates a new ArgumentEngine from flat arrays of entities, as typically
876
+ * stored in a relational database. Expressions are grouped by their
877
+ * `premiseId` field and loaded in BFS order (roots first, then children
878
+ * of already-added nodes) to satisfy parent-existence requirements.
879
+ */
880
+ static fromData(argument, claimLibrary, sourceLibrary, claimSourceLibrary, variables, premises, expressions, roles, config, grammarConfig, checksumVerification) {
881
+ const loadingGrammarConfig = grammarConfig ?? config?.grammarConfig ?? DEFAULT_GRAMMAR_CONFIG;
882
+ const normalizedConfig = config
883
+ ? {
884
+ ...config,
885
+ checksumConfig: normalizeChecksumConfig(config.checksumConfig),
886
+ }
887
+ : undefined;
888
+ const loadingConfig = {
889
+ ...normalizedConfig,
890
+ grammarConfig: loadingGrammarConfig,
891
+ };
892
+ const engine = new ArgumentEngine(argument, claimLibrary, sourceLibrary, claimSourceLibrary, loadingConfig);
893
+ engine.restoringFromSnapshot = true;
894
+ // Register claim-bound variables first (no dependencies)
895
+ for (const v of variables) {
896
+ if (isClaimBound(v)) {
897
+ engine.addVariable(v);
898
+ }
899
+ }
900
+ // Create premises (premise-bound variables reference them)
901
+ const premiseEngines = new Map();
902
+ for (const premise of premises) {
903
+ const { id: _id, argumentId: _argumentId, argumentVersion: _argumentVersion, checksum: _checksum, ...extras } = premise;
904
+ const { result: pe } = engine.createPremiseWithId(premise.id, extras);
905
+ premiseEngines.set(premise.id, pe);
906
+ }
907
+ // Register premise-bound variables (depend on premises)
908
+ for (const v of variables) {
909
+ if (isPremiseBound(v)) {
910
+ const pbv = v;
911
+ if (pbv.boundArgumentId === engine.argument.id) {
912
+ engine.bindVariableToPremise(v);
913
+ }
914
+ else {
915
+ engine.bindVariableToExternalPremise(v);
916
+ }
917
+ }
918
+ }
919
+ // Group expressions by premiseId
920
+ const exprsByPremise = new Map();
921
+ for (const expr of expressions) {
922
+ const premiseId = expr
923
+ .premiseId;
924
+ let group = exprsByPremise.get(premiseId);
925
+ if (!group) {
926
+ group = [];
927
+ exprsByPremise.set(premiseId, group);
928
+ }
929
+ group.push(expr);
930
+ }
931
+ // Add expressions via loadExpressions (bypasses nesting check for legacy data)
932
+ for (const [premiseId, pe] of premiseEngines) {
933
+ const premiseExprs = exprsByPremise.get(premiseId) ?? [];
934
+ pe.loadExpressions(premiseExprs);
935
+ }
936
+ // Set roles (override auto-assignment)
937
+ if (roles.conclusionPremiseId !== undefined) {
938
+ engine.setConclusionPremise(roles.conclusionPremiseId);
939
+ }
940
+ // After loading: restore the caller's intended grammar config
941
+ engine.grammarConfig = config?.grammarConfig;
942
+ engine.restoringFromSnapshot = false;
943
+ // Post-load normalization: collapse unjustified formulas and apply
944
+ // grammar rules to loaded data. Runs after all expressions are in place.
945
+ const restoredGrammarConfig = config?.grammarConfig ?? DEFAULT_GRAMMAR_CONFIG;
946
+ if (restoredGrammarConfig.autoNormalize) {
947
+ for (const pe of engine.premises.values()) {
948
+ pe.normalizeExpressions();
949
+ }
950
+ }
951
+ if (checksumVerification === "strict") {
952
+ engine.flushChecksums();
953
+ ArgumentEngine.verifyDataChecksums(engine, argument, variables, premises);
954
+ }
955
+ const validation = engine.validate();
956
+ if (!validation.ok) {
957
+ throw new InvariantViolationError(validation.violations);
958
+ }
959
+ return engine;
960
+ }
961
+ /**
962
+ * Verifies that all checksum fields in the snapshot match the recomputed
963
+ * checksums on the restored engine. Throws on the first mismatch.
964
+ */
965
+ static verifySnapshotChecksums(engine, snapshot) {
966
+ const checksumFields = [
967
+ "checksum",
968
+ "descendantChecksum",
969
+ "combinedChecksum",
970
+ ];
971
+ // Verify expression checksums
972
+ for (const pe of engine.listPremises()) {
973
+ for (const expr of pe.getExpressions()) {
974
+ const premiseSnap = snapshot.premises.find((ps) => ps.premise.id === pe.getId());
975
+ const exprSnap = premiseSnap?.expressions.expressions.find((e) => e.id === expr.id);
976
+ if (exprSnap) {
977
+ for (const field of checksumFields) {
978
+ const stored = String(exprSnap[field]);
979
+ const computed = String(expr[field]);
980
+ if (stored !== "undefined" && stored !== computed) {
981
+ throw new Error(`Checksum mismatch on expression "${expr.id}" field "${field}": stored="${stored}", computed="${computed}"`);
982
+ }
983
+ }
984
+ }
985
+ }
986
+ }
987
+ // Verify variable checksums
988
+ for (const v of engine.getVariables()) {
989
+ const varSnap = snapshot.variables.variables.find((sv) => sv.id === v.id);
990
+ const storedVarChecksum = varSnap
991
+ ? String(varSnap.checksum)
992
+ : undefined;
993
+ if (storedVarChecksum && storedVarChecksum !== "undefined") {
994
+ if (storedVarChecksum !== v.checksum) {
995
+ throw new Error(`Checksum mismatch on variable "${v.id}": stored="${storedVarChecksum}", computed="${v.checksum}"`);
996
+ }
997
+ }
998
+ }
999
+ // Verify premise checksums
1000
+ for (const pe of engine.listPremises()) {
1001
+ const premiseSnap = snapshot.premises.find((ps) => ps.premise.id === pe.getId());
1002
+ if (premiseSnap?.premise) {
1003
+ const sp = premiseSnap.premise;
1004
+ for (const field of checksumFields) {
1005
+ const stored = String(sp[field]);
1006
+ const computed = pe[field]();
1007
+ if (stored !== "undefined" && stored !== computed) {
1008
+ throw new Error(`Checksum mismatch on premise "${pe.getId()}" field "${field}": stored="${stored}", computed="${computed}"`);
1009
+ }
1010
+ }
1011
+ }
1012
+ }
1013
+ // Verify argument checksums
1014
+ const sa = snapshot.argument;
1015
+ for (const field of checksumFields) {
1016
+ const stored = String(sa[field]);
1017
+ const computed = engine[field]();
1018
+ if (stored !== "undefined" && stored !== computed) {
1019
+ throw new Error(`Checksum mismatch on argument "${engine.getArgument().id}" field "${field}": stored="${stored}", computed="${computed}"`);
1020
+ }
1021
+ }
1022
+ }
1023
+ /**
1024
+ * Verifies that all checksum fields in the input data match the recomputed
1025
+ * checksums on the restored engine. Throws on the first mismatch.
1026
+ */
1027
+ static verifyDataChecksums(engine, argument, variables, premises) {
1028
+ const checksumFields = [
1029
+ "checksum",
1030
+ "descendantChecksum",
1031
+ "combinedChecksum",
1032
+ ];
1033
+ // Verify variable checksums
1034
+ for (const v of engine.getVariables()) {
1035
+ const inputVar = variables.find((iv) => iv.id === v.id);
1036
+ const storedVarChecksum = inputVar
1037
+ ? String(inputVar.checksum)
1038
+ : undefined;
1039
+ if (storedVarChecksum && storedVarChecksum !== "undefined") {
1040
+ if (storedVarChecksum !== v.checksum) {
1041
+ throw new Error(`Checksum mismatch on variable "${v.id}": stored="${storedVarChecksum}", computed="${v.checksum}"`);
1042
+ }
1043
+ }
1044
+ }
1045
+ // Verify premise checksums
1046
+ for (const pe of engine.listPremises()) {
1047
+ const inputPremise = premises.find((p) => p.id === pe.getId());
1048
+ if (inputPremise) {
1049
+ const sp = inputPremise;
1050
+ for (const field of checksumFields) {
1051
+ const stored = String(sp[field]);
1052
+ const computed = pe[field]();
1053
+ if (stored !== "undefined" && stored !== computed) {
1054
+ throw new Error(`Checksum mismatch on premise "${pe.getId()}" field "${field}": stored="${stored}", computed="${computed}"`);
1055
+ }
1056
+ }
1057
+ }
1058
+ }
1059
+ // Verify argument checksums
1060
+ const sa = argument;
1061
+ for (const field of checksumFields) {
1062
+ const stored = String(sa[field]);
1063
+ const computed = engine[field]();
1064
+ if (stored !== "undefined" && stored !== computed) {
1065
+ throw new Error(`Checksum mismatch on argument "${engine.getArgument().id}" field "${field}": stored="${stored}", computed="${computed}"`);
1066
+ }
1067
+ }
1068
+ }
1069
+ rollback(snapshot) {
1070
+ const preRollbackSnap = this.snapshot();
1071
+ this.rollbackInternal(snapshot);
1072
+ const validation = this.validate();
1073
+ if (!validation.ok) {
1074
+ this.rollbackInternal(preRollbackSnap);
1075
+ throw new InvariantViolationError(validation.violations);
1076
+ }
1077
+ }
1078
+ rollbackInternal(snapshot) {
1079
+ this.argument = { ...snapshot.argument };
1080
+ this.checksumConfig = normalizeChecksumConfig(snapshot.config?.checksumConfig);
1081
+ this.positionConfig = snapshot.config?.positionConfig;
1082
+ this.grammarConfig = snapshot.config?.grammarConfig;
1083
+ this.variables = VariableManager.fromSnapshot(snapshot.variables);
1084
+ this.premises = new Map();
1085
+ this.expressionIndex = new Map();
1086
+ for (const premiseSnap of snapshot.premises) {
1087
+ const pe = PremiseEngine.fromSnapshot(premiseSnap, this.argument, this.variables, this.expressionIndex, PERMISSIVE_GRAMMAR_CONFIG);
1088
+ this.premises.set(pe.getId(), pe);
1089
+ }
1090
+ this.conclusionPremiseId = snapshot.conclusionPremiseId;
1091
+ for (const pe of this.premises.values()) {
1092
+ this.wireCircularityCheck(pe);
1093
+ this.wireEmptyBoundPremiseCheck(pe);
1094
+ pe.setVariableIdsCallback(() => new Set(this.variables.toArray().map((v) => v.id)));
1095
+ pe.setArgumentValidateCallback(() => this.validateAfterPremiseMutation());
1096
+ const premiseId = pe.getId();
1097
+ pe.setOnMutate(() => {
1098
+ this.markDirty();
1099
+ this.reactiveDirty.premiseIds.add(premiseId);
1100
+ this.notifySubscribers();
1101
+ });
1102
+ }
1103
+ this.markDirty();
1104
+ this.reactiveDirty = {
1105
+ argument: true,
1106
+ variables: true,
1107
+ roles: true,
1108
+ premiseIds: new Set(),
1109
+ allPremises: true,
1110
+ };
1111
+ this.notifySubscribers();
1112
+ }
1113
+ checksum() {
1114
+ if (this.checksumDirty || this.cachedMetaChecksum === undefined) {
1115
+ this.flushChecksums();
1116
+ }
1117
+ return this.cachedMetaChecksum;
1118
+ }
1119
+ descendantChecksum() {
1120
+ if (this.checksumDirty || this.cachedDescendantChecksum === undefined) {
1121
+ this.flushChecksums();
1122
+ }
1123
+ return this.cachedDescendantChecksum;
1124
+ }
1125
+ combinedChecksum() {
1126
+ if (this.checksumDirty || this.cachedCombinedChecksum === undefined) {
1127
+ this.flushChecksums();
1128
+ }
1129
+ return this.cachedCombinedChecksum;
1130
+ }
1131
+ getCollectionChecksum(name) {
1132
+ if (this.checksumDirty) {
1133
+ this.flushChecksums();
1134
+ }
1135
+ return name === "premises"
1136
+ ? this.cachedPremisesCollectionChecksum
1137
+ : this.cachedVariablesCollectionChecksum;
1138
+ }
1139
+ flushChecksums() {
1140
+ const config = this.checksumConfig;
1141
+ // 1. Flush all premise checksums (which flush expression checksums)
1142
+ for (const pe of this.listPremises()) {
1143
+ pe.flushChecksums();
1144
+ }
1145
+ // 2. Compute argument meta checksum (entity fields + role state MERGED)
1146
+ const argumentFields = config?.argumentFields ?? DEFAULT_CHECKSUM_CONFIG.argumentFields;
1147
+ const roleFields = config?.roleFields ?? DEFAULT_CHECKSUM_CONFIG.roleFields;
1148
+ const mergedFields = new Set([...argumentFields, ...roleFields]);
1149
+ const mergedEntity = {
1150
+ ...this.argument,
1151
+ ...this.getRoleState(),
1152
+ };
1153
+ this.cachedMetaChecksum = entityChecksum(mergedEntity, mergedFields);
1154
+ // 3. Compute collection checksums
1155
+ const premiseEntries = this.listPremises();
1156
+ if (premiseEntries.length > 0) {
1157
+ const premiseMap = {};
1158
+ for (const pe of premiseEntries) {
1159
+ premiseMap[pe.getId()] = pe.combinedChecksum();
1160
+ }
1161
+ this.cachedPremisesCollectionChecksum = computeHash(canonicalSerialize(premiseMap));
1162
+ }
1163
+ else {
1164
+ this.cachedPremisesCollectionChecksum = null;
1165
+ }
1166
+ const vars = this.variables.toArray();
1167
+ if (vars.length > 0) {
1168
+ const varMap = {};
1169
+ for (const v of vars) {
1170
+ varMap[v.id] = v.checksum;
1171
+ }
1172
+ this.cachedVariablesCollectionChecksum = computeHash(canonicalSerialize(varMap));
1173
+ }
1174
+ else {
1175
+ this.cachedVariablesCollectionChecksum = null;
1176
+ }
1177
+ // 4. Compute descendant checksum (exclude null collections)
1178
+ const collectionMap = {};
1179
+ if (this.cachedPremisesCollectionChecksum !== null) {
1180
+ collectionMap.premises = this.cachedPremisesCollectionChecksum;
1181
+ }
1182
+ if (this.cachedVariablesCollectionChecksum !== null) {
1183
+ collectionMap.variables = this.cachedVariablesCollectionChecksum;
1184
+ }
1185
+ this.cachedDescendantChecksum =
1186
+ Object.keys(collectionMap).length > 0
1187
+ ? computeHash(canonicalSerialize(collectionMap))
1188
+ : null;
1189
+ // 5. Compute combined checksum
1190
+ this.cachedCombinedChecksum =
1191
+ this.cachedDescendantChecksum === null
1192
+ ? this.cachedMetaChecksum
1193
+ : computeHash(this.cachedMetaChecksum + this.cachedDescendantChecksum);
1194
+ this.checksumDirty = false;
1195
+ }
1196
+ markDirty() {
1197
+ this.checksumDirty = true;
1198
+ this.cachedMetaChecksum = undefined;
1199
+ this.cachedDescendantChecksum = undefined;
1200
+ this.cachedCombinedChecksum = undefined;
1201
+ this.cachedPremisesCollectionChecksum = undefined;
1202
+ this.cachedVariablesCollectionChecksum = undefined;
1203
+ }
1204
+ /** Invalidate all premise checksums (e.g. after variable changes). */
1205
+ markAllPremisesDirty() {
1206
+ for (const pm of this.listPremises()) {
1207
+ pm.markDirty();
1208
+ }
1209
+ }
1210
+ attachVariableChecksum(v) {
1211
+ const fields = this.checksumConfig?.variableFields ??
1212
+ DEFAULT_CHECKSUM_CONFIG.variableFields;
1213
+ return {
1214
+ ...v,
1215
+ checksum: entityChecksum(v, fields),
1216
+ };
1217
+ }
1218
+ collectReferencedVariables() {
1219
+ return collectArgumentReferencedVariablesStandalone(this.asValidationContext());
1220
+ }
1221
+ validateAfterPremiseMutation() {
1222
+ return validateAfterPremiseMutationStandalone(this.asValidationContext());
1223
+ }
1224
+ validate() {
1225
+ return validateArgumentStandalone(this.asValidationContext());
1226
+ }
1227
+ validateEvaluability() {
1228
+ return validateArgumentEvaluabilityStandalone(this.asValidationContext());
1229
+ }
1230
+ asValidationContext() {
1231
+ return {
1232
+ argumentId: this.argument.id,
1233
+ argumentVersion: this.argument.version,
1234
+ conclusionPremiseId: this.conclusionPremiseId,
1235
+ getArgument: () => this.getArgument(),
1236
+ getVariables: () => this.variables.toArray(),
1237
+ listPremises: () => this.listPremises(),
1238
+ hasPremise: (premiseId) => this.premises.has(premiseId),
1239
+ lookupClaim: (claimId, claimVersion) => this.claimLibrary.get(claimId, claimVersion),
1240
+ flushAndGetChecksumDeltas: () => {
1241
+ const savedMeta = this.cachedMetaChecksum;
1242
+ const savedDescendant = this.cachedDescendantChecksum;
1243
+ const savedCombined = this.cachedCombinedChecksum;
1244
+ this.flushChecksums();
1245
+ return {
1246
+ savedMeta,
1247
+ savedDescendant,
1248
+ savedCombined,
1249
+ currentMeta: this.cachedMetaChecksum,
1250
+ currentDescendant: this.cachedDescendantChecksum,
1251
+ currentCombined: this.cachedCombinedChecksum,
1252
+ };
1253
+ },
1254
+ validateVariables: () => this.variables.validate(),
1255
+ wouldCreateCycle: (variableId, premiseId, visited) => this.wouldCreateCycle(variableId, premiseId, visited),
1256
+ };
1257
+ }
1258
+ asEvaluationContext() {
1259
+ return {
1260
+ argumentId: this.argument.id,
1261
+ conclusionPremiseId: this.conclusionPremiseId,
1262
+ getConclusionPremise: () => this.getConclusionPremise(),
1263
+ listSupportingPremises: () => this.listSupportingPremises(),
1264
+ listPremises: () => this.listPremises(),
1265
+ getVariable: (id) => this.variables.getVariable(id),
1266
+ getPremise: (id) => this.premises.get(id),
1267
+ validateEvaluability: () => this.validateEvaluability(),
1268
+ };
1269
+ }
1270
+ evaluate(assignment, options) {
1271
+ return evaluateArgumentStandalone(this.asEvaluationContext(), assignment, options);
1272
+ }
1273
+ checkValidity(options) {
1274
+ return checkArgumentValidityStandalone(this.asEvaluationContext(), options);
1275
+ }
1276
+ // -----------------------------------------------------------------
1277
+ // Forking
1278
+ // -----------------------------------------------------------------
1279
+ /**
1280
+ * Override point for subclasses to prevent forking. When this returns
1281
+ * `false`, `forkArgument` will throw.
1282
+ */
1283
+ canFork() {
1284
+ return true;
1285
+ }
1286
+ /**
1287
+ * Override point for subclasses to restrict cross-argument bindings.
1288
+ * When this returns `false`, `bindVariableToExternalPremise` will throw.
1289
+ */
1290
+ canBind(_boundArgumentId, _boundArgumentVersion) {
1291
+ return true;
1292
+ }
1293
+ }
1294
+ //# sourceMappingURL=argument-engine.js.map