agentfootprint 18.3.0 → 19.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/CAPABILITIES.md +4 -0
  2. package/dist/answer-account/facts/calls.js +26 -7
  3. package/dist/answer-account/facts/calls.js.map +1 -1
  4. package/dist/core/agent/assessment/gather/stage.js +1 -1
  5. package/dist/core/agent/assessment/gather/stage.js.map +1 -1
  6. package/dist/core/agent/assessment/periodRows.js +7 -11
  7. package/dist/core/agent/assessment/periodRows.js.map +1 -1
  8. package/dist/core/agent/assessment/reasonClasses.js +28 -1
  9. package/dist/core/agent/assessment/reasonClasses.js.map +1 -1
  10. package/dist/core/agent/assessment/resultEvidence.js +8 -5
  11. package/dist/core/agent/assessment/resultEvidence.js.map +1 -1
  12. package/dist/core/agent/assessment/results/coverage/declarations.js +28 -0
  13. package/dist/core/agent/assessment/results/coverage/declarations.js.map +1 -0
  14. package/dist/core/agent/assessment/results/stage.js +1 -1
  15. package/dist/core/agent/assessment/results/stage.js.map +1 -1
  16. package/dist/core/agent/assessment/settledResults.js +4 -2
  17. package/dist/core/agent/assessment/settledResults.js.map +1 -1
  18. package/dist/core/agent/assessment/standingAdmission.js +14 -2
  19. package/dist/core/agent/assessment/standingAdmission.js.map +1 -1
  20. package/dist/core/agent/emptiness.js +6 -1
  21. package/dist/core/agent/emptiness.js.map +1 -1
  22. package/dist/core/agent/recordedCalls/resultDispositions.js +9 -1
  23. package/dist/core/agent/recordedCalls/resultDispositions.js.map +1 -1
  24. package/dist/core/agent/recordedCalls/resultRewriteIndex.js +40 -0
  25. package/dist/core/agent/recordedCalls/resultRewriteIndex.js.map +1 -0
  26. package/dist/core/agent/recordedCalls.js +3 -1
  27. package/dist/core/agent/recordedCalls.js.map +1 -1
  28. package/dist/core/agent/runtime/configuration/configure.js +6 -0
  29. package/dist/core/agent/runtime/configuration/configure.js.map +1 -1
  30. package/dist/core/agent/runtime/configuration/values.js.map +1 -1
  31. package/dist/core/agent/runtime/native/initialize.js +1 -0
  32. package/dist/core/agent/runtime/native/initialize.js.map +1 -1
  33. package/dist/esm/answer-account/facts/calls.js +27 -8
  34. package/dist/esm/answer-account/facts/calls.js.map +1 -1
  35. package/dist/esm/answer-account/types.d.ts +6 -0
  36. package/dist/esm/core/agent/assessment/gather/stage.js +1 -1
  37. package/dist/esm/core/agent/assessment/gather/stage.js.map +1 -1
  38. package/dist/esm/core/agent/assessment/periodRows.d.ts +1 -3
  39. package/dist/esm/core/agent/assessment/periodRows.js +4 -9
  40. package/dist/esm/core/agent/assessment/periodRows.js.map +1 -1
  41. package/dist/esm/core/agent/assessment/reasonClasses.d.ts +9 -0
  42. package/dist/esm/core/agent/assessment/reasonClasses.js +25 -0
  43. package/dist/esm/core/agent/assessment/reasonClasses.js.map +1 -1
  44. package/dist/esm/core/agent/assessment/resultEvidence.d.ts +1 -0
  45. package/dist/esm/core/agent/assessment/resultEvidence.js +9 -6
  46. package/dist/esm/core/agent/assessment/resultEvidence.js.map +1 -1
  47. package/dist/esm/core/agent/assessment/results/coverage/declarations.d.ts +16 -0
  48. package/dist/esm/core/agent/assessment/results/coverage/declarations.js +22 -0
  49. package/dist/esm/core/agent/assessment/results/coverage/declarations.js.map +1 -0
  50. package/dist/esm/core/agent/assessment/results/stage.js +1 -1
  51. package/dist/esm/core/agent/assessment/results/stage.js.map +1 -1
  52. package/dist/esm/core/agent/assessment/settledResults.js +4 -2
  53. package/dist/esm/core/agent/assessment/settledResults.js.map +1 -1
  54. package/dist/esm/core/agent/assessment/standingAdmission.d.ts +1 -0
  55. package/dist/esm/core/agent/assessment/standingAdmission.js +15 -3
  56. package/dist/esm/core/agent/assessment/standingAdmission.js.map +1 -1
  57. package/dist/esm/core/agent/emptiness.d.ts +2 -0
  58. package/dist/esm/core/agent/emptiness.js +4 -0
  59. package/dist/esm/core/agent/emptiness.js.map +1 -1
  60. package/dist/esm/core/agent/recordedCalls/resultDispositions.d.ts +2 -0
  61. package/dist/esm/core/agent/recordedCalls/resultDispositions.js +7 -0
  62. package/dist/esm/core/agent/recordedCalls/resultDispositions.js.map +1 -1
  63. package/dist/esm/core/agent/recordedCalls/resultRewriteIndex.d.ts +11 -0
  64. package/dist/esm/core/agent/recordedCalls/resultRewriteIndex.js +36 -0
  65. package/dist/esm/core/agent/recordedCalls/resultRewriteIndex.js.map +1 -0
  66. package/dist/esm/core/agent/recordedCalls.d.ts +1 -0
  67. package/dist/esm/core/agent/recordedCalls.js +1 -0
  68. package/dist/esm/core/agent/recordedCalls.js.map +1 -1
  69. package/dist/esm/core/agent/runtime/configuration/configure.js +6 -0
  70. package/dist/esm/core/agent/runtime/configuration/configure.js.map +1 -1
  71. package/dist/esm/core/agent/runtime/configuration/types.d.ts +5 -1
  72. package/dist/esm/core/agent/runtime/configuration/values.d.ts +1 -0
  73. package/dist/esm/core/agent/runtime/configuration/values.js.map +1 -1
  74. package/dist/esm/core/agent/runtime/native/initialize.js +1 -0
  75. package/dist/esm/core/agent/runtime/native/initialize.js.map +1 -1
  76. package/dist/esm/core/agent/types/observation.d.ts +34 -1
  77. package/dist/esm/hosting/durability.d.ts +10 -0
  78. package/dist/esm/hosting/durability.js +54 -30
  79. package/dist/esm/hosting/durability.js.map +1 -1
  80. package/dist/esm/hosting/standingAgent/artifacts/accountParts.d.ts +3 -2
  81. package/dist/esm/hosting/standingAgent/artifacts/accountParts.js +9 -4
  82. package/dist/esm/hosting/standingAgent/artifacts/accountParts.js.map +1 -1
  83. package/dist/esm/index.d.ts +1 -0
  84. package/dist/esm/llm-ledger/build.d.ts +77 -0
  85. package/dist/esm/llm-ledger/build.js +344 -0
  86. package/dist/esm/llm-ledger/build.js.map +1 -0
  87. package/dist/esm/llm-ledger/digest.d.ts +24 -0
  88. package/dist/esm/llm-ledger/digest.js +22 -0
  89. package/dist/esm/llm-ledger/digest.js.map +1 -0
  90. package/dist/esm/llm-ledger/entries.d.ts +37 -0
  91. package/dist/esm/llm-ledger/entries.js +291 -0
  92. package/dist/esm/llm-ledger/entries.js.map +1 -0
  93. package/dist/esm/llm-ledger/fileStore.d.ts +12 -0
  94. package/dist/esm/llm-ledger/fileStore.js +571 -0
  95. package/dist/esm/llm-ledger/fileStore.js.map +1 -0
  96. package/dist/esm/llm-ledger/limits.d.ts +65 -0
  97. package/dist/esm/llm-ledger/limits.js +64 -0
  98. package/dist/esm/llm-ledger/limits.js.map +1 -0
  99. package/dist/esm/llm-ledger/memoryStore.d.ts +3 -0
  100. package/dist/esm/llm-ledger/memoryStore.js +44 -0
  101. package/dist/esm/llm-ledger/memoryStore.js.map +1 -0
  102. package/dist/esm/llm-ledger/session.d.ts +6 -0
  103. package/dist/esm/llm-ledger/session.js +43 -0
  104. package/dist/esm/llm-ledger/session.js.map +1 -0
  105. package/dist/esm/llm-ledger/store.d.ts +97 -0
  106. package/dist/esm/llm-ledger/store.js +208 -0
  107. package/dist/esm/llm-ledger/store.js.map +1 -0
  108. package/dist/esm/llm-ledger/tokens.d.ts +30 -0
  109. package/dist/esm/llm-ledger/tokens.js +319 -0
  110. package/dist/esm/llm-ledger/tokens.js.map +1 -0
  111. package/dist/esm/llm-ledger/types.d.ts +173 -0
  112. package/dist/esm/llm-ledger/types.js +2 -0
  113. package/dist/esm/llm-ledger/types.js.map +1 -0
  114. package/dist/esm/llm-ledger/window.d.ts +40 -0
  115. package/dist/esm/llm-ledger/window.js +66 -0
  116. package/dist/esm/llm-ledger/window.js.map +1 -0
  117. package/dist/esm/observe.d.ts +1 -1
  118. package/dist/esm/observe.js.map +1 -1
  119. package/dist/esm/recorders/observability/FlowchartRecorder.d.ts +8 -120
  120. package/dist/esm/recorders/observability/FlowchartRecorder.js +16 -0
  121. package/dist/esm/recorders/observability/FlowchartRecorder.js.map +1 -1
  122. package/dist/esm/recorders/observability/boundaryRecorder/domainEvents.d.ts +9 -0
  123. package/dist/esm/recorders/observability/recordingParts/format.d.ts +87 -12
  124. package/dist/esm/recorders/observability/recordingParts/format.js +136 -17
  125. package/dist/esm/recorders/observability/recordingParts/format.js.map +1 -1
  126. package/dist/esm/recorders/observability/recordingParts/open.d.ts +41 -7
  127. package/dist/esm/recorders/observability/recordingParts/open.js +32 -6
  128. package/dist/esm/recorders/observability/recordingParts/open.js.map +1 -1
  129. package/dist/esm/recorders/observability/recordingParts/partReader.d.ts +25 -2
  130. package/dist/esm/recorders/observability/recordingParts/partReader.js +183 -15
  131. package/dist/esm/recorders/observability/recordingParts/partReader.js.map +1 -1
  132. package/dist/esm/recorders/observability/recordingParts/split.d.ts +13 -0
  133. package/dist/esm/recorders/observability/recordingParts/split.js +135 -26
  134. package/dist/esm/recorders/observability/recordingParts/split.js.map +1 -1
  135. package/dist/esm/recorders/observability/recordingParts/subflowPayloads.d.ts +124 -0
  136. package/dist/esm/recorders/observability/recordingParts/subflowPayloads.js +389 -0
  137. package/dist/esm/recorders/observability/recordingParts/subflowPayloads.js.map +1 -0
  138. package/dist/esm/recorders/observability/stepGraphTypes.d.ts +139 -0
  139. package/dist/esm/recorders/observability/stepGraphTypes.js +10 -0
  140. package/dist/esm/recorders/observability/stepGraphTypes.js.map +1 -0
  141. package/dist/esm/trace-toolpack/traceToolpack/resultRewrite.d.ts +3 -0
  142. package/dist/esm/trace-toolpack/traceToolpack/resultRewrite.js +14 -0
  143. package/dist/esm/trace-toolpack/traceToolpack/resultRewrite.js.map +1 -0
  144. package/dist/esm/trace-toolpack/traceToolpack/toolCallJournal.d.ts +6 -5
  145. package/dist/esm/trace-toolpack/traceToolpack/toolCallJournal.js +12 -17
  146. package/dist/esm/trace-toolpack/traceToolpack/toolCallJournal.js.map +1 -1
  147. package/dist/esm/trace-toolpack/traceToolpack/toolCallOccurrence.d.ts +7 -0
  148. package/dist/esm/trace-toolpack/traceToolpack/toolCallOccurrence.js +19 -0
  149. package/dist/esm/trace-toolpack/traceToolpack/toolCallOccurrence.js.map +1 -1
  150. package/dist/hosting/durability.js +54 -30
  151. package/dist/hosting/durability.js.map +1 -1
  152. package/dist/hosting/standingAgent/artifacts/accountParts.js +9 -4
  153. package/dist/hosting/standingAgent/artifacts/accountParts.js.map +1 -1
  154. package/dist/llm-ledger/build.js +354 -0
  155. package/dist/llm-ledger/build.js.map +1 -0
  156. package/dist/llm-ledger/digest.js +28 -0
  157. package/dist/llm-ledger/digest.js.map +1 -0
  158. package/dist/llm-ledger/entries.js +308 -0
  159. package/dist/llm-ledger/entries.js.map +1 -0
  160. package/dist/llm-ledger/fileStore.js +575 -0
  161. package/dist/llm-ledger/fileStore.js.map +1 -0
  162. package/dist/llm-ledger/limits.js +67 -0
  163. package/dist/llm-ledger/limits.js.map +1 -0
  164. package/dist/llm-ledger/memoryStore.js +48 -0
  165. package/dist/llm-ledger/memoryStore.js.map +1 -0
  166. package/dist/llm-ledger/session.js +49 -0
  167. package/dist/llm-ledger/session.js.map +1 -0
  168. package/dist/llm-ledger/store.js +220 -0
  169. package/dist/llm-ledger/store.js.map +1 -0
  170. package/dist/llm-ledger/tokens.js +327 -0
  171. package/dist/llm-ledger/tokens.js.map +1 -0
  172. package/dist/llm-ledger/types.js +3 -0
  173. package/dist/llm-ledger/types.js.map +1 -0
  174. package/dist/llm-ledger/window.js +72 -0
  175. package/dist/llm-ledger/window.js.map +1 -0
  176. package/dist/observe.js.map +1 -1
  177. package/dist/recorders/observability/FlowchartRecorder.js +16 -0
  178. package/dist/recorders/observability/FlowchartRecorder.js.map +1 -1
  179. package/dist/recorders/observability/recordingParts/format.js +139 -18
  180. package/dist/recorders/observability/recordingParts/format.js.map +1 -1
  181. package/dist/recorders/observability/recordingParts/open.js +30 -4
  182. package/dist/recorders/observability/recordingParts/open.js.map +1 -1
  183. package/dist/recorders/observability/recordingParts/partReader.js +182 -14
  184. package/dist/recorders/observability/recordingParts/partReader.js.map +1 -1
  185. package/dist/recorders/observability/recordingParts/split.js +134 -25
  186. package/dist/recorders/observability/recordingParts/split.js.map +1 -1
  187. package/dist/recorders/observability/recordingParts/subflowPayloads.js +396 -0
  188. package/dist/recorders/observability/recordingParts/subflowPayloads.js.map +1 -0
  189. package/dist/recorders/observability/stepGraphTypes.js +11 -0
  190. package/dist/recorders/observability/stepGraphTypes.js.map +1 -0
  191. package/dist/trace-toolpack/traceToolpack/resultRewrite.js +18 -0
  192. package/dist/trace-toolpack/traceToolpack/resultRewrite.js.map +1 -0
  193. package/dist/trace-toolpack/traceToolpack/toolCallJournal.js +14 -19
  194. package/dist/trace-toolpack/traceToolpack/toolCallJournal.js.map +1 -1
  195. package/dist/trace-toolpack/traceToolpack/toolCallOccurrence.js +22 -1
  196. package/dist/trace-toolpack/traceToolpack/toolCallOccurrence.js.map +1 -1
  197. package/dist/types/answer-account/facts/calls.d.ts.map +1 -1
  198. package/dist/types/answer-account/types.d.ts +6 -0
  199. package/dist/types/answer-account/types.d.ts.map +1 -1
  200. package/dist/types/core/agent/assessment/periodRows.d.ts +1 -3
  201. package/dist/types/core/agent/assessment/periodRows.d.ts.map +1 -1
  202. package/dist/types/core/agent/assessment/reasonClasses.d.ts +9 -0
  203. package/dist/types/core/agent/assessment/reasonClasses.d.ts.map +1 -1
  204. package/dist/types/core/agent/assessment/resultEvidence.d.ts +1 -0
  205. package/dist/types/core/agent/assessment/resultEvidence.d.ts.map +1 -1
  206. package/dist/types/core/agent/assessment/results/coverage/declarations.d.ts +17 -0
  207. package/dist/types/core/agent/assessment/results/coverage/declarations.d.ts.map +1 -0
  208. package/dist/types/core/agent/assessment/settledResults.d.ts.map +1 -1
  209. package/dist/types/core/agent/assessment/standingAdmission.d.ts +1 -0
  210. package/dist/types/core/agent/assessment/standingAdmission.d.ts.map +1 -1
  211. package/dist/types/core/agent/emptiness.d.ts +2 -0
  212. package/dist/types/core/agent/emptiness.d.ts.map +1 -1
  213. package/dist/types/core/agent/recordedCalls/resultDispositions.d.ts +2 -0
  214. package/dist/types/core/agent/recordedCalls/resultDispositions.d.ts.map +1 -1
  215. package/dist/types/core/agent/recordedCalls/resultRewriteIndex.d.ts +12 -0
  216. package/dist/types/core/agent/recordedCalls/resultRewriteIndex.d.ts.map +1 -0
  217. package/dist/types/core/agent/recordedCalls.d.ts +1 -0
  218. package/dist/types/core/agent/recordedCalls.d.ts.map +1 -1
  219. package/dist/types/core/agent/runtime/configuration/configure.d.ts.map +1 -1
  220. package/dist/types/core/agent/runtime/configuration/types.d.ts +5 -1
  221. package/dist/types/core/agent/runtime/configuration/types.d.ts.map +1 -1
  222. package/dist/types/core/agent/runtime/configuration/values.d.ts +1 -0
  223. package/dist/types/core/agent/runtime/configuration/values.d.ts.map +1 -1
  224. package/dist/types/core/agent/runtime/native/initialize.d.ts.map +1 -1
  225. package/dist/types/core/agent/types/observation.d.ts +34 -1
  226. package/dist/types/core/agent/types/observation.d.ts.map +1 -1
  227. package/dist/types/hosting/durability.d.ts +10 -0
  228. package/dist/types/hosting/durability.d.ts.map +1 -1
  229. package/dist/types/hosting/standingAgent/artifacts/accountParts.d.ts +3 -2
  230. package/dist/types/hosting/standingAgent/artifacts/accountParts.d.ts.map +1 -1
  231. package/dist/types/index.d.ts +1 -0
  232. package/dist/types/index.d.ts.map +1 -1
  233. package/dist/types/llm-ledger/build.d.ts +78 -0
  234. package/dist/types/llm-ledger/build.d.ts.map +1 -0
  235. package/dist/types/llm-ledger/digest.d.ts +25 -0
  236. package/dist/types/llm-ledger/digest.d.ts.map +1 -0
  237. package/dist/types/llm-ledger/entries.d.ts +38 -0
  238. package/dist/types/llm-ledger/entries.d.ts.map +1 -0
  239. package/dist/types/llm-ledger/fileStore.d.ts +13 -0
  240. package/dist/types/llm-ledger/fileStore.d.ts.map +1 -0
  241. package/dist/types/llm-ledger/limits.d.ts +66 -0
  242. package/dist/types/llm-ledger/limits.d.ts.map +1 -0
  243. package/dist/types/llm-ledger/memoryStore.d.ts +4 -0
  244. package/dist/types/llm-ledger/memoryStore.d.ts.map +1 -0
  245. package/dist/types/llm-ledger/session.d.ts +7 -0
  246. package/dist/types/llm-ledger/session.d.ts.map +1 -0
  247. package/dist/types/llm-ledger/store.d.ts +98 -0
  248. package/dist/types/llm-ledger/store.d.ts.map +1 -0
  249. package/dist/types/llm-ledger/tokens.d.ts +31 -0
  250. package/dist/types/llm-ledger/tokens.d.ts.map +1 -0
  251. package/dist/types/llm-ledger/types.d.ts +174 -0
  252. package/dist/types/llm-ledger/types.d.ts.map +1 -0
  253. package/dist/types/llm-ledger/window.d.ts +41 -0
  254. package/dist/types/llm-ledger/window.d.ts.map +1 -0
  255. package/dist/types/observe.d.ts +1 -1
  256. package/dist/types/observe.d.ts.map +1 -1
  257. package/dist/types/recorders/observability/FlowchartRecorder.d.ts +8 -120
  258. package/dist/types/recorders/observability/FlowchartRecorder.d.ts.map +1 -1
  259. package/dist/types/recorders/observability/boundaryRecorder/domainEvents.d.ts +9 -0
  260. package/dist/types/recorders/observability/boundaryRecorder/domainEvents.d.ts.map +1 -1
  261. package/dist/types/recorders/observability/recordingParts/format.d.ts +87 -12
  262. package/dist/types/recorders/observability/recordingParts/format.d.ts.map +1 -1
  263. package/dist/types/recorders/observability/recordingParts/open.d.ts +41 -7
  264. package/dist/types/recorders/observability/recordingParts/open.d.ts.map +1 -1
  265. package/dist/types/recorders/observability/recordingParts/partReader.d.ts +25 -2
  266. package/dist/types/recorders/observability/recordingParts/partReader.d.ts.map +1 -1
  267. package/dist/types/recorders/observability/recordingParts/split.d.ts +13 -0
  268. package/dist/types/recorders/observability/recordingParts/split.d.ts.map +1 -1
  269. package/dist/types/recorders/observability/recordingParts/subflowPayloads.d.ts +125 -0
  270. package/dist/types/recorders/observability/recordingParts/subflowPayloads.d.ts.map +1 -0
  271. package/dist/types/recorders/observability/stepGraphTypes.d.ts +140 -0
  272. package/dist/types/recorders/observability/stepGraphTypes.d.ts.map +1 -0
  273. package/dist/types/trace-toolpack/traceToolpack/resultRewrite.d.ts +4 -0
  274. package/dist/types/trace-toolpack/traceToolpack/resultRewrite.d.ts.map +1 -0
  275. package/dist/types/trace-toolpack/traceToolpack/toolCallJournal.d.ts +6 -5
  276. package/dist/types/trace-toolpack/traceToolpack/toolCallJournal.d.ts.map +1 -1
  277. package/dist/types/trace-toolpack/traceToolpack/toolCallOccurrence.d.ts +7 -0
  278. package/dist/types/trace-toolpack/traceToolpack/toolCallOccurrence.d.ts.map +1 -1
  279. package/package.json +1 -1
@@ -0,0 +1,65 @@
1
+ /**
2
+ * llm-ledger/limits — every cap the ledger holds, in ONE frozen table.
3
+ *
4
+ * Pattern: one table, read by every check. A cap written as a number anywhere
5
+ * else in the folder is a second owner, so the builders, the entry
6
+ * checks and both stores read `LEDGER_LIMITS` and nothing else.
7
+ * Role: the closed leaf's own vocabulary. Imports nothing.
8
+ * Emits: N/A.
9
+ *
10
+ * Characters are counted as Unicode code points (what a JSON Schema
11
+ * `maxLength` counts), bytes as UTF-8. A line is one stored entry as the file
12
+ * store writes it (`store.ts` · `lineBytesOf`); the memory store measures the
13
+ * same line, so both stores hold the same caps.
14
+ */
15
+ export declare const LEDGER_LIMITS: Readonly<{
16
+ /** Entries in one session, all kinds counted. */
17
+ readonly maxEntries: 10000;
18
+ /** Bytes of one session's log: the sum of its lines. */
19
+ readonly maxLogBytes: number;
20
+ /** Bytes of one line. */
21
+ readonly maxLineBytes: number;
22
+ /** A receipt's `source`: 1–128 characters, no control characters. */
23
+ readonly minSourceChars: 1;
24
+ readonly maxSourceChars: 128;
25
+ /** A receipt's `query`. */
26
+ readonly maxQueryChars: 1024;
27
+ /** Distinct tokens one receipt or supplied entry keeps. */
28
+ readonly maxTokens: 2048;
29
+ /** A token's length. */
30
+ readonly minTokenChars: 4;
31
+ readonly maxTokenChars: 64;
32
+ /** Bytes of the host's text read for tokens; the rest is counted, never read. */
33
+ readonly maxTextBytes: 1048576;
34
+ /** A receipt's `ref.uri`. */
35
+ readonly maxRefUriChars: 512;
36
+ /** A receipt's `externalId`. */
37
+ readonly maxExternalIdChars: 256;
38
+ /** A note's `text`. */
39
+ readonly maxNoteChars: 2000;
40
+ /** A note's `values`: how many, and each one's length. */
41
+ readonly maxValues: 32;
42
+ readonly maxValueChars: 64;
43
+ /** A note's `cites`. */
44
+ readonly maxCites: 32;
45
+ /** A withdrawal's `why`. */
46
+ readonly maxWhyChars: 500;
47
+ /** A submit's `claims`. */
48
+ readonly maxClaims: 64;
49
+ /** Bytes of a draft (and of a delivered text). */
50
+ readonly maxDraftBytes: number;
51
+ /** Tokens a check lists as `missing`; the rest are counted. */
52
+ readonly maxMissing: 32;
53
+ /** Rules one session runs (`opened.rules`). */
54
+ readonly maxRules: 16;
55
+ /** The file store's key, in bytes. */
56
+ readonly minKeyBytes: 32;
57
+ /** How long an append waits for another writer's lock. */
58
+ readonly lockWaitMs: 2000;
59
+ /** A lock older than this was left by a writer that died; it is taken over. */
60
+ readonly lockStaleMs: 30000;
61
+ /** Sessions one high-water mark remembers; the least recently used is forgotten past it. */
62
+ readonly maxHighWaterSessions: 10000;
63
+ }>;
64
+ /** The cap table's type: every value a literal. */
65
+ export type LedgerLimits = typeof LEDGER_LIMITS;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * llm-ledger/limits — every cap the ledger holds, in ONE frozen table.
3
+ *
4
+ * Pattern: one table, read by every check. A cap written as a number anywhere
5
+ * else in the folder is a second owner, so the builders, the entry
6
+ * checks and both stores read `LEDGER_LIMITS` and nothing else.
7
+ * Role: the closed leaf's own vocabulary. Imports nothing.
8
+ * Emits: N/A.
9
+ *
10
+ * Characters are counted as Unicode code points (what a JSON Schema
11
+ * `maxLength` counts), bytes as UTF-8. A line is one stored entry as the file
12
+ * store writes it (`store.ts` · `lineBytesOf`); the memory store measures the
13
+ * same line, so both stores hold the same caps.
14
+ */
15
+ export const LEDGER_LIMITS = Object.freeze({
16
+ /** Entries in one session, all kinds counted. */
17
+ maxEntries: 10_000,
18
+ /** Bytes of one session's log: the sum of its lines. */
19
+ maxLogBytes: 32 * 1024 * 1024,
20
+ /** Bytes of one line. */
21
+ maxLineBytes: 256 * 1024,
22
+ /** A receipt's `source`: 1–128 characters, no control characters. */
23
+ minSourceChars: 1,
24
+ maxSourceChars: 128,
25
+ /** A receipt's `query`. */
26
+ maxQueryChars: 1_024,
27
+ /** Distinct tokens one receipt or supplied entry keeps. */
28
+ maxTokens: 2_048,
29
+ /** A token's length. */
30
+ minTokenChars: 4,
31
+ maxTokenChars: 64,
32
+ /** Bytes of the host's text read for tokens; the rest is counted, never read. */
33
+ maxTextBytes: 1_048_576,
34
+ /** A receipt's `ref.uri`. */
35
+ maxRefUriChars: 512,
36
+ /** A receipt's `externalId`. */
37
+ maxExternalIdChars: 256,
38
+ /** A note's `text`. */
39
+ maxNoteChars: 2_000,
40
+ /** A note's `values`: how many, and each one's length. */
41
+ maxValues: 32,
42
+ maxValueChars: 64,
43
+ /** A note's `cites`. */
44
+ maxCites: 32,
45
+ /** A withdrawal's `why`. */
46
+ maxWhyChars: 500,
47
+ /** A submit's `claims`. */
48
+ maxClaims: 64,
49
+ /** Bytes of a draft (and of a delivered text). */
50
+ maxDraftBytes: 64 * 1024,
51
+ /** Tokens a check lists as `missing`; the rest are counted. */
52
+ maxMissing: 32,
53
+ /** Rules one session runs (`opened.rules`). */
54
+ maxRules: 16,
55
+ /** The file store's key, in bytes. */
56
+ minKeyBytes: 32,
57
+ /** How long an append waits for another writer's lock. */
58
+ lockWaitMs: 2_000,
59
+ /** A lock older than this was left by a writer that died; it is taken over. */
60
+ lockStaleMs: 30_000,
61
+ /** Sessions one high-water mark remembers; the least recently used is forgotten past it. */
62
+ maxHighWaterSessions: 10_000,
63
+ });
64
+ //# sourceMappingURL=limits.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"limits.js","sourceRoot":"","sources":["../../../src/llm-ledger/limits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC;IACzC,iDAAiD;IACjD,UAAU,EAAE,MAAM;IAClB,wDAAwD;IACxD,WAAW,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI;IAC7B,yBAAyB;IACzB,YAAY,EAAE,GAAG,GAAG,IAAI;IAExB,qEAAqE;IACrE,cAAc,EAAE,CAAC;IACjB,cAAc,EAAE,GAAG;IACnB,2BAA2B;IAC3B,aAAa,EAAE,KAAK;IACpB,2DAA2D;IAC3D,SAAS,EAAE,KAAK;IAChB,wBAAwB;IACxB,aAAa,EAAE,CAAC;IAChB,aAAa,EAAE,EAAE;IACjB,iFAAiF;IACjF,YAAY,EAAE,SAAS;IACvB,6BAA6B;IAC7B,cAAc,EAAE,GAAG;IACnB,gCAAgC;IAChC,kBAAkB,EAAE,GAAG;IAEvB,uBAAuB;IACvB,YAAY,EAAE,KAAK;IACnB,0DAA0D;IAC1D,SAAS,EAAE,EAAE;IACb,aAAa,EAAE,EAAE;IACjB,wBAAwB;IACxB,QAAQ,EAAE,EAAE;IACZ,4BAA4B;IAC5B,WAAW,EAAE,GAAG;IAChB,2BAA2B;IAC3B,SAAS,EAAE,EAAE;IACb,kDAAkD;IAClD,aAAa,EAAE,EAAE,GAAG,IAAI;IACxB,+DAA+D;IAC/D,UAAU,EAAE,EAAE;IACd,+CAA+C;IAC/C,QAAQ,EAAE,EAAE;IAEZ,sCAAsC;IACtC,WAAW,EAAE,EAAE;IACf,0DAA0D;IAC1D,UAAU,EAAE,KAAK;IACjB,+EAA+E;IAC/E,WAAW,EAAE,MAAM;IACnB,4FAA4F;IAC5F,oBAAoB,EAAE,MAAM;CACpB,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { LedgerStore } from './store.js';
2
+ /** A new, empty in-memory store. */
3
+ export declare function memoryLedgerStore(): LedgerStore;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * llm-ledger/memoryStore — the in-process adapter: for tests and a host that keeps the log in memory.
3
+ *
4
+ * Pattern: the port's second adapter, under the file store's caps and the
5
+ * same admission (`store.ts` · `admitBatch`), so one contract suite
6
+ * runs on both and gives the same answers.
7
+ * Role: each session is an array of entries in a `Map`. An entry is kept as
8
+ * the copy admission parsed from its canonical JSON (a clone that
9
+ * also refuses what JSON cannot hold), deep-frozen; a load hands out
10
+ * a frozen array of those entries.
11
+ * Emits: N/A.
12
+ *
13
+ * Nothing survives the process, and there is no MAC: a memory store is as
14
+ * trustworthy as the process that holds it. One `append` runs to completion
15
+ * before the next starts (it awaits nothing between its compare and its
16
+ * write), which is the compare-and-swap within one process.
17
+ */
18
+ import { LedgerStoreError, admitBatch, checkRoom } from './store.js';
19
+ import { isSessionId } from './session.js';
20
+ /** A new, empty in-memory store. */
21
+ export function memoryLedgerStore() {
22
+ const sessions = new Map();
23
+ return {
24
+ async load(session) {
25
+ if (!isSessionId(session))
26
+ throw new LedgerStoreError('malformed', 'session');
27
+ const held = sessions.get(session);
28
+ const entries = held === undefined ? [] : [...held.entries];
29
+ return Object.freeze({ seq: entries.length, entries: Object.freeze(entries) });
30
+ },
31
+ async append(session, expectedSeq, entries) {
32
+ const batch = admitBatch(session, expectedSeq, entries);
33
+ const held = sessions.get(session) ?? { entries: [], bytes: 0 };
34
+ if (held.entries.length !== expectedSeq)
35
+ return 'conflict';
36
+ checkRoom(held.entries.length, held.bytes, batch);
37
+ held.entries.push(...batch.entries);
38
+ held.bytes += batch.bytes;
39
+ sessions.set(session, held);
40
+ return held.entries.length;
41
+ },
42
+ };
43
+ }
44
+ //# sourceMappingURL=memoryStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memoryStore.js","sourceRoot":"","sources":["../../../src/llm-ledger/memoryStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAErE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAG3C,oCAAoC;AACpC,MAAM,UAAU,iBAAiB;IAC/B,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAqD,CAAC;IAC9E,OAAO;QACL,KAAK,CAAC,IAAI,CAAC,OAAe;YACxB,IAAI,CAAC,WAAW,CAAC,OAAO,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
3
+ /** The session id of exactly 16 bytes: `ls_` + 26 characters, most significant bits first. */
4
+ export declare function sessionIdOf(bytes: Uint8Array): string;
5
+ /** A new session id from 128 random bits. */
6
+ export declare function mintSessionId(): string;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * llm-ledger/session — the one spelling of a session id: minted by the host, checked before any path.
3
+ *
4
+ * Pattern: one grammar, one minter. A session id is `ls_` and 26 Crockford
5
+ * base32 characters holding 128 random bits (the first character is
6
+ * 0–7: the two bits above the 128 are zero). The host mints it;
7
+ * nothing accepts an id from the agent.
8
+ * Role: every door that takes a session checks it here FIRST — the file
9
+ * store forms `<dir>/<session>.jsonl` only from an id this file
10
+ * accepts, so no id can name a path outside the store's directory.
11
+ * Emits: N/A.
12
+ */
13
+ import { randomBytes } from './digest.js';
14
+ /** Crockford's base32 alphabet: no I, L, O or U. */
15
+ const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
16
+ const SESSION_ID = /^ls_[0-7][0-9A-HJKMNP-TV-Z]{25}$/;
17
+ /** Whether `value` is a session id as `mintSessionId` writes it. */
18
+ export function isSessionId(value) {
19
+ return typeof value === 'string' && SESSION_ID.test(value);
20
+ }
21
+ /** The session id of exactly 16 bytes: `ls_` + 26 characters, most significant bits first. */
22
+ export function sessionIdOf(bytes) {
23
+ if (!(bytes instanceof Uint8Array) || bytes.length !== 16) {
24
+ throw new RangeError('sessionIdOf: a session id is made of exactly 16 bytes');
25
+ }
26
+ let id = 'ls_';
27
+ // 130 bits, read 5 at a time: two zero bits, then the 128 bits of `bytes`.
28
+ for (let char = 0; char < 26; char++) {
29
+ let value = 0;
30
+ for (let b = 0; b < 5; b++) {
31
+ const bit = char * 5 + b - 2;
32
+ const set = bit < 0 ? 0 : (bytes[bit >> 3] >> (7 - (bit & 7))) & 1;
33
+ value = (value << 1) | set;
34
+ }
35
+ id += CROCKFORD[value];
36
+ }
37
+ return id;
38
+ }
39
+ /** A new session id from 128 random bits. */
40
+ export function mintSessionId() {
41
+ return sessionIdOf(randomBytes(16));
42
+ }
43
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../../src/llm-ledger/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,oDAAoD;AACpD,MAAM,SAAS,GAAG,kCAAkC,CAAC;AAErD,MAAM,UAAU,GAAG,kCAAkC,CAAC;AAEtD,oEAAoE;AACpE,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC7D,CAAC;AAED,8FAA8F;AAC9F,MAAM,UAAU,WAAW,CAAC,KAAiB;IAC3C,IAAI,CAAC,CAAC,KAAK,YAAY,UAAU,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QAC1D,MAAM,IAAI,UAAU,CAAC,uDAAuD,CAAC,CAAC;IAChF,CAAC;IACD,IAAI,EAAE,GAAG,KAAK,CAAC;IACf,2EAA2E;IAC3E,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC;QACrC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC7B,MAAM,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAE,KAAK,CAAC,GAAG,IAAI,CAAC,CAAY,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;YAC/E,KAAK,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,GAAG,CAAC;QAC7B,CAAC;QACD,EAAE,IAAI,SAAS,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,6CAA6C;AAC7C,MAAM,UAAU,aAAa;IAC3B,OAAO,WAAW,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;AACtC,CAAC"}
@@ -0,0 +1,97 @@
1
+ import type { CanonicalJsonOptions } from 'foottrace/canonical-json';
2
+ import type { LedgerEntry } from './types.js';
3
+ /** One session's log as a store holds it. */
4
+ export interface LedgerLog {
5
+ /** The number of entries: the last entry's seq. */
6
+ readonly seq: number;
7
+ readonly entries: readonly LedgerEntry[];
8
+ }
9
+ /** The store port. */
10
+ export interface LedgerStore {
11
+ load(session: string): Promise<LedgerLog>;
12
+ append(session: string, expectedSeq: number, entries: readonly LedgerEntry[]): Promise<number | 'conflict'>;
13
+ }
14
+ /**
15
+ * Why a store refused. `rolled-back` comes only from `withHighWater`; every
16
+ * other code from an adapter. `lock-lost` is the one code that says nothing
17
+ * about the log: the append wrote its batch, but another writer took the lock
18
+ * before it finished, so the batch may or may not stand — the caller must
19
+ * reload, never assume either.
20
+ */
21
+ export type LedgerStoreCode = 'mac-mismatch' | 'torn-line' | 'malformed' | 'unsafe-file' | 'too-large' | 'locked-timeout' | 'lock-lost' | 'rolled-back';
22
+ /** A store's refusal: a closed code, and for `malformed` the field — a fixed word, never a value. */
23
+ export declare class LedgerStoreError extends Error {
24
+ readonly code: LedgerStoreCode;
25
+ readonly field: string | undefined;
26
+ constructor(code: LedgerStoreCode, field?: string);
27
+ }
28
+ /**
29
+ * Bytes a line adds around its entry's canonical JSON, for a batch ending at seq `end`:
30
+ * `{"end":` (7) + the digits of `end` + `,"entry":` (9) + `,"mac":"` (8) + 64 hex + `"}` (2) +
31
+ * the newline (1).
32
+ */
33
+ export declare function lineOverheadBytes(end: number): number;
34
+ /**
35
+ * The canonical-JSON bounds of one stored LINE, `{ end, entry, mac }`. Every bound is stated, because
36
+ * three canonicalisations nest and each must hold what the one inside it held:
37
+ *
38
+ * ENTRY (admission, `entryCanonical`) ⊂ LINE (this) ⊂ MAC INPUT (`fileStore.ts`).
39
+ *
40
+ * An entry admitted at the edge of its bounds must still make a line that reads back, and a line
41
+ * that reads back must still have a MAC input to compute — or a crafted entry at an edge would
42
+ * turn a refusal into a raw error. Kept below foottrace's ceilings (100,000 nodes and fields) so
43
+ * the MAC input, a little larger again, fits under them.
44
+ */
45
+ export declare const LINE_CANONICAL: Readonly<{
46
+ maxBytes: number;
47
+ maxNodes: 99000;
48
+ maxFields: 99000;
49
+ maxDepth: 64;
50
+ maxSortWork: 200000;
51
+ }>;
52
+ /**
53
+ * An entry's own bounds, in a batch ending at `end`: each a little inside its line's (the line
54
+ * adds one object, the batch's end and the MAC).
55
+ */
56
+ export declare function entryCanonical(end: number): CanonicalJsonOptions;
57
+ /** Bytes of the line an entry is stored as, given its canonical JSON and its batch's end. */
58
+ export declare function lineBytesOf(entryText: string, end: number): number;
59
+ /** An admitted entry: its frozen copy, its canonical JSON and its line's size. */
60
+ export interface LedgerAdmitted {
61
+ readonly entry: LedgerEntry;
62
+ readonly text: string;
63
+ readonly lineBytes: number;
64
+ }
65
+ /**
66
+ * Check a plain parsed entry for position `seq`: its shape, its number, and
67
+ * that seq 1 — and only seq 1 — is the session's `opened`.
68
+ */
69
+ export declare function checkEntryAt(entry: unknown, seq: number): LedgerEntry;
70
+ /**
71
+ * Admit one entry for position `seq` of a batch ending at `end`: its canonical
72
+ * JSON (own data only — a getter is never called), a fresh copy parsed from it,
73
+ * its shape, its line's size.
74
+ */
75
+ export declare function admitEntry(value: unknown, seq: number, end?: number): LedgerAdmitted;
76
+ /** A batch ready to append: entries, their canonical JSON, the bytes its lines take, its last seq. */
77
+ export interface LedgerBatch {
78
+ readonly entries: readonly LedgerEntry[];
79
+ readonly texts: readonly string[];
80
+ readonly bytes: number;
81
+ /** The batch's last seq: every one of its lines carries it, so a reader knows the batch is whole. */
82
+ readonly end: number;
83
+ }
84
+ /**
85
+ * Admit an append's arguments: a session id (checked before any path is
86
+ * formed), a whole `expectedSeq`, and one or more entries numbered from
87
+ * `expectedSeq + 1`. Throws `malformed` or `too-large`; reads no store.
88
+ */
89
+ export declare function admitBatch(session: unknown, expectedSeq: unknown, entries: unknown): LedgerBatch;
90
+ /** Refuse a batch that would take a session past a cap (`too-large`). */
91
+ export declare function checkRoom(storedSeq: number, storedBytes: number, batch: LedgerBatch): void;
92
+ /**
93
+ * `store`, refusing a load below the highest seq this instance has seen for that session. An
94
+ * append is admitted first (a malformed batch is `malformed` here as on the store itself), then
95
+ * compared with the mark. At most `maxSessions` marks are kept, least recently used out.
96
+ */
97
+ export declare function withHighWater(store: LedgerStore, maxSessions?: number): LedgerStore;
@@ -0,0 +1,208 @@
1
+ /**
2
+ * llm-ledger/store — the store PORT, its one error, and the admission both adapters share.
3
+ *
4
+ * Pattern: port + adapters. `LedgerStore` is OUR shape: `load` gives the
5
+ * whole session, `append` is a compare-and-swap on its seq. Every
6
+ * adapter admits a batch through `admitBatch` and measures a line
7
+ * with `lineBytesOf`, so the two adapters cannot hold different laws.
8
+ * Role: the trusted side's log. The later stages read it through the port;
9
+ * the log is never seeded as chart state.
10
+ * Emits: N/A.
11
+ *
12
+ * ## The contract (one suite runs it on every adapter)
13
+ *
14
+ * - `seq` is the number of entries. Every kind shares one sequence, 1…n.
15
+ * An unknown session loads as `{ seq: 0, entries: [] }`.
16
+ * - `append(session, expectedSeq, entries)` takes entries numbered
17
+ * `expectedSeq + 1 …`, the first entry of a session (seq 1) an `opened`
18
+ * and no other. It returns the new last seq, or `'conflict'` — writing
19
+ * nothing — when the stored seq is not `expectedSeq`.
20
+ * - Everything else is THROWN, never returned: a `LedgerStoreError` with one
21
+ * closed code. A store that cannot vouch for its bytes has no seq to give;
22
+ * it is refused, never repaired.
23
+ * - Order: a malformed batch is refused before the seq is compared; caps are
24
+ * checked after it (`too-large`).
25
+ *
26
+ * ## The high-water mark
27
+ *
28
+ * `withHighWater(store)` remembers, per session, the highest seq this instance
29
+ * has seen, and refuses a load below it (`rolled-back`). It cannot see a saved
30
+ * earlier copy restored between two processes: that needs a trusted counter,
31
+ * which this release does not have. It remembers at most `maxHighWaterSessions`
32
+ * sessions; past that the least recently used one is forgotten, and a rollback
33
+ * of a forgotten session is no more visible to this instance than to a fresh one.
34
+ */
35
+ import { canonicalizeJson, CanonicalJsonError } from 'foottrace/canonical-json';
36
+ import { deepFreeze, entryFault } from './entries.js';
37
+ import { LEDGER_LIMITS } from './limits.js';
38
+ import { isSessionId } from './session.js';
39
+ import { utf8Bytes } from './tokens.js';
40
+ /** A store's refusal: a closed code, and for `malformed` the field — a fixed word, never a value. */
41
+ export class LedgerStoreError extends Error {
42
+ code;
43
+ field;
44
+ constructor(code, field) {
45
+ super(field === undefined ? `llm-ledger store: ${code}` : `llm-ledger store: ${code} (${field})`);
46
+ this.name = 'LedgerStoreError';
47
+ this.code = code;
48
+ this.field = field;
49
+ }
50
+ }
51
+ /**
52
+ * Bytes a line adds around its entry's canonical JSON, for a batch ending at seq `end`:
53
+ * `{"end":` (7) + the digits of `end` + `,"entry":` (9) + `,"mac":"` (8) + 64 hex + `"}` (2) +
54
+ * the newline (1).
55
+ */
56
+ export function lineOverheadBytes(end) {
57
+ return 91 + String(end).length;
58
+ }
59
+ /**
60
+ * The canonical-JSON bounds of one stored LINE, `{ end, entry, mac }`. Every bound is stated, because
61
+ * three canonicalisations nest and each must hold what the one inside it held:
62
+ *
63
+ * ENTRY (admission, `entryCanonical`) ⊂ LINE (this) ⊂ MAC INPUT (`fileStore.ts`).
64
+ *
65
+ * An entry admitted at the edge of its bounds must still make a line that reads back, and a line
66
+ * that reads back must still have a MAC input to compute — or a crafted entry at an edge would
67
+ * turn a refusal into a raw error. Kept below foottrace's ceilings (100,000 nodes and fields) so
68
+ * the MAC input, a little larger again, fits under them.
69
+ */
70
+ export const LINE_CANONICAL = Object.freeze({
71
+ maxBytes: LEDGER_LIMITS.maxLineBytes,
72
+ maxNodes: 99_000,
73
+ maxFields: 99_000,
74
+ maxDepth: 64,
75
+ maxSortWork: 200_000,
76
+ });
77
+ /**
78
+ * An entry's own bounds, in a batch ending at `end`: each a little inside its line's (the line
79
+ * adds one object, the batch's end and the MAC).
80
+ */
81
+ export function entryCanonical(end) {
82
+ return {
83
+ maxBytes: LINE_CANONICAL.maxBytes - lineOverheadBytes(end),
84
+ maxNodes: LINE_CANONICAL.maxNodes - 10,
85
+ maxFields: LINE_CANONICAL.maxFields - 10,
86
+ maxDepth: LINE_CANONICAL.maxDepth - 1,
87
+ maxSortWork: LINE_CANONICAL.maxSortWork - 10_000,
88
+ };
89
+ }
90
+ /** Bytes of the line an entry is stored as, given its canonical JSON and its batch's end. */
91
+ export function lineBytesOf(entryText, end) {
92
+ return utf8Bytes(entryText) + lineOverheadBytes(end);
93
+ }
94
+ /**
95
+ * Check a plain parsed entry for position `seq`: its shape, its number, and
96
+ * that seq 1 — and only seq 1 — is the session's `opened`.
97
+ */
98
+ export function checkEntryAt(entry, seq) {
99
+ const fault = entryFault(entry);
100
+ if (fault !== undefined)
101
+ throw new LedgerStoreError('malformed', fault);
102
+ const e = entry;
103
+ if (e.seq !== seq)
104
+ throw new LedgerStoreError('malformed', 'seq');
105
+ if ((seq === 1) !== (e.kind === 'opened'))
106
+ throw new LedgerStoreError('malformed', 'kind');
107
+ return e;
108
+ }
109
+ /**
110
+ * Admit one entry for position `seq` of a batch ending at `end`: its canonical
111
+ * JSON (own data only — a getter is never called), a fresh copy parsed from it,
112
+ * its shape, its line's size.
113
+ */
114
+ export function admitEntry(value, seq, end = seq) {
115
+ let text;
116
+ try {
117
+ text = canonicalizeJson(value, entryCanonical(end));
118
+ }
119
+ catch (error) {
120
+ if (error instanceof CanonicalJsonError && error.code === 'output-bytes')
121
+ throw new LedgerStoreError('too-large');
122
+ throw new LedgerStoreError('malformed', 'entry');
123
+ }
124
+ const entry = checkEntryAt(JSON.parse(text), seq);
125
+ return { entry: deepFreeze(entry), text, lineBytes: lineBytesOf(text, end) };
126
+ }
127
+ /**
128
+ * Admit an append's arguments: a session id (checked before any path is
129
+ * formed), a whole `expectedSeq`, and one or more entries numbered from
130
+ * `expectedSeq + 1`. Throws `malformed` or `too-large`; reads no store.
131
+ */
132
+ export function admitBatch(session, expectedSeq, entries) {
133
+ if (!isSessionId(session))
134
+ throw new LedgerStoreError('malformed', 'session');
135
+ if (!Number.isSafeInteger(expectedSeq) || expectedSeq < 0)
136
+ throw new LedgerStoreError('malformed', 'expectedSeq');
137
+ if (!Array.isArray(entries) || entries.length === 0)
138
+ throw new LedgerStoreError('malformed', 'entries');
139
+ if (entries.length > LEDGER_LIMITS.maxEntries)
140
+ throw new LedgerStoreError('too-large');
141
+ const count = entries.length; // read once: a growing array does not change the batch
142
+ const end = expectedSeq + count;
143
+ const admitted = [];
144
+ for (let i = 0; i < count; i++) {
145
+ const item = Object.getOwnPropertyDescriptor(entries, i);
146
+ if (item === undefined || !('value' in item))
147
+ throw new LedgerStoreError('malformed', 'entries');
148
+ admitted.push(admitEntry(item.value, expectedSeq + i + 1, end));
149
+ }
150
+ return Object.freeze({
151
+ entries: Object.freeze(admitted.map((a) => a.entry)),
152
+ texts: Object.freeze(admitted.map((a) => a.text)),
153
+ bytes: admitted.reduce((sum, a) => sum + a.lineBytes, 0),
154
+ end,
155
+ });
156
+ }
157
+ /** Refuse a batch that would take a session past a cap (`too-large`). */
158
+ export function checkRoom(storedSeq, storedBytes, batch) {
159
+ if (storedSeq + batch.entries.length > LEDGER_LIMITS.maxEntries ||
160
+ storedBytes + batch.bytes > LEDGER_LIMITS.maxLogBytes)
161
+ throw new LedgerStoreError('too-large');
162
+ }
163
+ /**
164
+ * `store`, refusing a load below the highest seq this instance has seen for that session. An
165
+ * append is admitted first (a malformed batch is `malformed` here as on the store itself), then
166
+ * compared with the mark. At most `maxSessions` marks are kept, least recently used out.
167
+ */
168
+ export function withHighWater(store, maxSessions = LEDGER_LIMITS.maxHighWaterSessions) {
169
+ if (!Number.isSafeInteger(maxSessions) || maxSessions < 1)
170
+ throw new RangeError('withHighWater: maxSessions must be a whole number, at least 1');
171
+ const seen = new Map(); // insertion order = least recently used first
172
+ const markOf = (session) => {
173
+ const mark = seen.get(session);
174
+ if (mark !== undefined) {
175
+ seen.delete(session); // touched: now the most recently used
176
+ seen.set(session, mark);
177
+ }
178
+ return mark ?? 0;
179
+ };
180
+ const raise = (session, seq) => {
181
+ const prior = seen.get(session) ?? 0;
182
+ seen.delete(session);
183
+ seen.set(session, Math.max(seq, prior));
184
+ if (seen.size > maxSessions)
185
+ seen.delete(seen.keys().next().value);
186
+ };
187
+ return {
188
+ async load(session) {
189
+ const log = await store.load(session);
190
+ if (log.seq < markOf(session))
191
+ throw new LedgerStoreError('rolled-back');
192
+ raise(session, log.seq);
193
+ return log;
194
+ },
195
+ async append(session, expectedSeq, entries) {
196
+ admitBatch(session, expectedSeq, entries);
197
+ // A caller behind what this instance has seen gets the answer an honest store gives —
198
+ // without asking a store that may have been rolled back to exactly that seq.
199
+ if (expectedSeq < markOf(session))
200
+ return 'conflict';
201
+ const result = await store.append(session, expectedSeq, entries);
202
+ if (result !== 'conflict')
203
+ raise(session, result);
204
+ return result;
205
+ },
206
+ };
207
+ }
208
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../../src/llm-ledger/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAGhF,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,O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,EAAE,OAAO,CAAC,CAAC;YACjE,IAAI,MAAM,KAAK,UAAU;gBAAE,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YAClD,OAAO,MAAM,CAAC;QAChB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,30 @@
1
+ /** What a text was read to hold. */
2
+ export interface LedgerTokenReading {
3
+ readonly tokens: readonly string[];
4
+ /** UTF-8 bytes of the whole text. */
5
+ readonly bytes: number;
6
+ readonly cut?: {
7
+ readonly tokensDropped: number;
8
+ readonly bytesUnread: number;
9
+ };
10
+ /** Present only when the part read holds a number written with thousands separators. */
11
+ readonly groupedNumbers?: number;
12
+ /** Present only when the part read holds pieces next to a backslash that read two ways. */
13
+ readonly escapedPieces?: number;
14
+ }
15
+ /** Every distinct token of `text`, in first-seen order, with no cap. */
16
+ export declare function tokensOf(text: string): string[];
17
+ /** Whether `value` is exactly one token, spelled as `tokensOf` writes it. */
18
+ export declare function isToken(value: unknown): value is string;
19
+ /** UTF-8 bytes of `text`, counted without encoding it. */
20
+ export declare function utf8Bytes(text: string): number;
21
+ /**
22
+ * The tokens a receipt keeps of the host's `text`, and what it did not keep.
23
+ * The text itself is never kept: only its byte count and its capped tokens.
24
+ */
25
+ export declare function readTokens(text: string): LedgerTokenReading;
26
+ /**
27
+ * `readTokens` under another byte cap, `maxBytes` (a whole number). The
28
+ * receipt's cap is `maxTextBytes`; a smaller one reads less, by the same law.
29
+ */
30
+ export declare function readTokensUpTo(text: string, maxBytes: number): LedgerTokenReading;