attention-manifest-protocol 0.4.0 → 0.4.2

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 (627) hide show
  1. package/CHANGELOG.md +250 -226
  2. package/CHANGELOG.zh-CN.md +63 -39
  3. package/README.md +22 -1
  4. package/README.zh-CN.md +22 -1
  5. package/docs/probes/calibration-imported.json +141 -141
  6. package/docs/probes/eval-recorded-replay.json +597 -597
  7. package/docs/probes/eval-replay.json +124 -124
  8. package/docs/probes/eval-summary.json +42 -42
  9. package/docs/probes/runtime-eval.json +4230 -4230
  10. package/lib/attention/attention.d.ts +153 -0
  11. package/lib/attention/attention.js +209 -0
  12. package/lib/attention/budget.d.ts +31 -0
  13. package/lib/attention/budget.js +55 -0
  14. package/lib/attention/cognition.d.ts +230 -0
  15. package/lib/attention/cognition.js +245 -0
  16. package/lib/attention/index.d.ts +8 -0
  17. package/lib/attention/index.js +7 -0
  18. package/lib/attention/interrupts.d.ts +97 -0
  19. package/lib/attention/interrupts.js +151 -0
  20. package/lib/attention/resource.d.ts +167 -0
  21. package/lib/attention/resource.js +148 -0
  22. package/lib/attention/social.d.ts +74 -0
  23. package/lib/attention/social.js +153 -0
  24. package/lib/attention/socialTypes.d.ts +102 -0
  25. package/lib/attention/socialTypes.js +1 -0
  26. package/lib/authz/authz.d.ts +75 -0
  27. package/lib/authz/authz.js +176 -0
  28. package/lib/authz/index.d.ts +1 -0
  29. package/lib/authz/index.js +1 -0
  30. package/lib/bayes/calibration.d.ts +67 -0
  31. package/lib/bayes/calibration.js +114 -0
  32. package/lib/bayes/calibrationRegistry.d.ts +39 -0
  33. package/lib/bayes/calibrationRegistry.js +61 -0
  34. package/lib/bayes/calibrationSelection.d.ts +40 -0
  35. package/lib/bayes/calibrationSelection.js +89 -0
  36. package/lib/bayes/engine.d.ts +163 -0
  37. package/lib/bayes/engine.js +354 -0
  38. package/lib/bayes/index.d.ts +6 -0
  39. package/lib/bayes/index.js +6 -0
  40. package/lib/bayes/reliability.d.ts +27 -0
  41. package/lib/bayes/reliability.js +38 -0
  42. package/lib/bayes/scopedPriors.d.ts +34 -0
  43. package/lib/bayes/scopedPriors.js +70 -0
  44. package/lib/boundary/frame.d.ts +63 -0
  45. package/lib/boundary/frame.js +239 -0
  46. package/lib/boundary/ids.d.ts +8 -0
  47. package/lib/boundary/ids.js +12 -0
  48. package/lib/boundary/index.d.ts +12 -0
  49. package/lib/boundary/index.js +8 -0
  50. package/lib/boundary/refTable.d.ts +49 -0
  51. package/lib/boundary/refTable.js +92 -0
  52. package/lib/boundary/toolCall.d.ts +33 -0
  53. package/lib/boundary/toolCall.js +173 -0
  54. package/lib/concept/concept.d.ts +67 -0
  55. package/lib/concept/concept.js +1 -0
  56. package/lib/concept/conceptBudget.d.ts +56 -0
  57. package/lib/concept/conceptBudget.js +281 -0
  58. package/lib/concept/conceptBudgetTypes.d.ts +74 -0
  59. package/lib/concept/conceptBudgetTypes.js +1 -0
  60. package/lib/concept/index.d.ts +3 -0
  61. package/lib/concept/index.js +3 -0
  62. package/lib/concurrency/idempotency.d.ts +16 -0
  63. package/lib/concurrency/idempotency.js +21 -0
  64. package/lib/concurrency/index.d.ts +5 -0
  65. package/lib/concurrency/index.js +5 -0
  66. package/lib/concurrency/mutex.d.ts +13 -0
  67. package/lib/concurrency/mutex.js +26 -0
  68. package/lib/concurrency/namedLocks.d.ts +7 -0
  69. package/lib/concurrency/namedLocks.js +31 -0
  70. package/lib/concurrency/retry.d.ts +23 -0
  71. package/lib/concurrency/retry.js +38 -0
  72. package/lib/concurrency/worker.d.ts +42 -0
  73. package/lib/concurrency/worker.js +67 -0
  74. package/lib/context/agentContext.d.ts +58 -0
  75. package/lib/context/agentContext.js +197 -0
  76. package/lib/context/ast.d.ts +98 -0
  77. package/lib/context/ast.js +414 -0
  78. package/lib/context/contentStore.d.ts +16 -0
  79. package/lib/context/contentStore.js +61 -0
  80. package/lib/context/context.d.ts +15 -0
  81. package/lib/context/context.js +39 -0
  82. package/lib/context/contextIndex.d.ts +40 -0
  83. package/lib/context/contextIndex.js +64 -0
  84. package/lib/context/document.d.ts +102 -0
  85. package/lib/context/document.js +22 -0
  86. package/lib/context/fileStorePath.d.ts +1 -0
  87. package/lib/context/fileStorePath.js +13 -0
  88. package/lib/context/index.d.ts +22 -0
  89. package/lib/context/index.js +11 -0
  90. package/lib/context/lifecycle.d.ts +91 -0
  91. package/lib/context/lifecycle.js +163 -0
  92. package/lib/context/projections.d.ts +20 -0
  93. package/lib/context/projections.js +127 -0
  94. package/lib/context/workspace.d.ts +111 -0
  95. package/lib/context/workspace.js +318 -0
  96. package/lib/context/workspaceProtocol.d.ts +30 -0
  97. package/lib/context/workspaceProtocol.js +49 -0
  98. package/lib/context/workspaceService.d.ts +63 -0
  99. package/lib/context/workspaceService.js +352 -0
  100. package/lib/cooperation/board.d.ts +47 -0
  101. package/lib/cooperation/board.js +66 -0
  102. package/lib/cooperation/collaboration.d.ts +55 -0
  103. package/lib/cooperation/collaboration.js +1 -0
  104. package/lib/cooperation/index.d.ts +6 -0
  105. package/lib/cooperation/index.js +6 -0
  106. package/lib/cooperation/merge.d.ts +69 -0
  107. package/lib/cooperation/merge.js +109 -0
  108. package/lib/cooperation/metrics.d.ts +39 -0
  109. package/lib/cooperation/metrics.js +17 -0
  110. package/lib/cooperation/session.d.ts +75 -0
  111. package/lib/cooperation/session.js +74 -0
  112. package/lib/cooperation/unified.d.ts +38 -0
  113. package/lib/cooperation/unified.js +40 -0
  114. package/lib/database/adapter/adapters.d.ts +24 -0
  115. package/lib/database/adapter/adapters.js +31 -0
  116. package/lib/database/adapter/index.d.ts +4 -0
  117. package/lib/database/adapter/index.js +4 -0
  118. package/lib/database/adapter/runtimeSink.d.ts +20 -0
  119. package/lib/database/adapter/runtimeSink.js +50 -0
  120. package/lib/database/adapter/sessionStore.d.ts +11 -0
  121. package/lib/database/adapter/sessionStore.js +23 -0
  122. package/lib/database/adapter/turnStore.d.ts +10 -0
  123. package/lib/database/adapter/turnStore.js +15 -0
  124. package/lib/database/entity/agent.d.ts +133 -0
  125. package/lib/database/entity/agent.js +61 -0
  126. package/lib/database/entity/entity.d.ts +715 -0
  127. package/lib/database/entity/entity.js +372 -0
  128. package/lib/database/entity/handle.d.ts +31 -0
  129. package/lib/database/entity/handle.js +53 -0
  130. package/lib/database/entity/index.d.ts +32 -0
  131. package/lib/database/entity/index.js +22 -0
  132. package/lib/database/entity/message.d.ts +27 -0
  133. package/lib/database/entity/message.js +52 -0
  134. package/lib/database/entity/meta.d.ts +11 -0
  135. package/lib/database/entity/meta.js +1 -0
  136. package/lib/database/entity/object.d.ts +23 -0
  137. package/lib/database/entity/object.js +24 -0
  138. package/lib/database/entity/outbox.d.ts +9 -0
  139. package/lib/database/entity/outbox.js +5 -0
  140. package/lib/database/entity/process.d.ts +30 -0
  141. package/lib/database/entity/process.js +40 -0
  142. package/lib/database/entity/registry.d.ts +81 -0
  143. package/lib/database/entity/registry.js +91 -0
  144. package/lib/database/entity/runtimeApproval.d.ts +23 -0
  145. package/lib/database/entity/runtimeApproval.js +5 -0
  146. package/lib/database/entity/runtimeSession.d.ts +7 -0
  147. package/lib/database/entity/runtimeSession.js +5 -0
  148. package/lib/database/entity/runtimeTurn.d.ts +9 -0
  149. package/lib/database/entity/runtimeTurn.js +5 -0
  150. package/lib/database/entity/thread.d.ts +35 -0
  151. package/lib/database/entity/thread.js +55 -0
  152. package/lib/database/entity/window.d.ts +39 -0
  153. package/lib/database/entity/window.js +56 -0
  154. package/lib/database/entity/worker.d.ts +42 -0
  155. package/lib/database/entity/worker.js +58 -0
  156. package/lib/database/index.d.ts +6 -0
  157. package/lib/database/index.js +6 -0
  158. package/lib/database/repository/addressSpaceRepository.d.ts +18 -0
  159. package/lib/database/repository/addressSpaceRepository.js +40 -0
  160. package/lib/database/repository/agentRepository.d.ts +41 -0
  161. package/lib/database/repository/agentRepository.js +398 -0
  162. package/lib/database/repository/base.d.ts +31 -0
  163. package/lib/database/repository/base.js +23 -0
  164. package/lib/database/repository/executionRepository.d.ts +64 -0
  165. package/lib/database/repository/executionRepository.js +379 -0
  166. package/lib/database/repository/handleRepository.d.ts +35 -0
  167. package/lib/database/repository/handleRepository.js +57 -0
  168. package/lib/database/repository/index.d.ts +20 -0
  169. package/lib/database/repository/index.js +17 -0
  170. package/lib/database/repository/messageRepository.d.ts +33 -0
  171. package/lib/database/repository/messageRepository.js +45 -0
  172. package/lib/database/repository/objectRepository.d.ts +43 -0
  173. package/lib/database/repository/objectRepository.js +60 -0
  174. package/lib/database/repository/outboxRepository.d.ts +15 -0
  175. package/lib/database/repository/outboxRepository.js +51 -0
  176. package/lib/database/repository/processRepository.d.ts +31 -0
  177. package/lib/database/repository/processRepository.js +48 -0
  178. package/lib/database/repository/registryRepository.d.ts +72 -0
  179. package/lib/database/repository/registryRepository.js +95 -0
  180. package/lib/database/repository/repository.d.ts +322 -0
  181. package/lib/database/repository/repository.js +332 -0
  182. package/lib/database/repository/runtimeApprovalRepository.d.ts +14 -0
  183. package/lib/database/repository/runtimeApprovalRepository.js +35 -0
  184. package/lib/database/repository/runtimeSessionRepository.d.ts +9 -0
  185. package/lib/database/repository/runtimeSessionRepository.js +21 -0
  186. package/lib/database/repository/runtimeTurnRepository.d.ts +10 -0
  187. package/lib/database/repository/runtimeTurnRepository.js +32 -0
  188. package/lib/database/repository/socialRepository.d.ts +22 -0
  189. package/lib/database/repository/socialRepository.js +155 -0
  190. package/lib/database/repository/threadRepository.d.ts +29 -0
  191. package/lib/database/repository/threadRepository.js +48 -0
  192. package/lib/database/repository/windowRepository.d.ts +46 -0
  193. package/lib/database/repository/windowRepository.js +68 -0
  194. package/lib/database/repository/workerRepository.d.ts +38 -0
  195. package/lib/database/repository/workerRepository.js +61 -0
  196. package/lib/database/service/database.d.ts +55 -0
  197. package/lib/database/service/database.js +77 -0
  198. package/lib/database/service/handleService.d.ts +20 -0
  199. package/lib/database/service/handleService.js +34 -0
  200. package/lib/database/service/index.d.ts +16 -0
  201. package/lib/database/service/index.js +11 -0
  202. package/lib/database/service/messageService.d.ts +26 -0
  203. package/lib/database/service/messageService.js +40 -0
  204. package/lib/database/service/objectService.d.ts +32 -0
  205. package/lib/database/service/objectService.js +31 -0
  206. package/lib/database/service/processService.d.ts +22 -0
  207. package/lib/database/service/processService.js +45 -0
  208. package/lib/database/service/registryServices.d.ts +43 -0
  209. package/lib/database/service/registryServices.js +60 -0
  210. package/lib/database/service/service.d.ts +131 -0
  211. package/lib/database/service/service.js +153 -0
  212. package/lib/database/service/stepService.d.ts +23 -0
  213. package/lib/database/service/stepService.js +35 -0
  214. package/lib/database/service/threadService.d.ts +23 -0
  215. package/lib/database/service/threadService.js +48 -0
  216. package/lib/database/service/windowService.d.ts +15 -0
  217. package/lib/database/service/windowService.js +22 -0
  218. package/lib/database/service/workerService.d.ts +28 -0
  219. package/lib/database/service/workerService.js +34 -0
  220. package/lib/database/sql/index.d.ts +3 -0
  221. package/lib/database/sql/index.js +2 -0
  222. package/lib/database/sql/schema.d.ts +5 -0
  223. package/lib/database/sql/schema.js +416 -0
  224. package/lib/database/sql/sql.d.ts +290 -0
  225. package/lib/database/sql/sql.js +430 -0
  226. package/lib/database/store/database.d.ts +75 -0
  227. package/lib/database/store/database.js +143 -0
  228. package/lib/database/store/index.d.ts +3 -0
  229. package/lib/database/store/index.js +2 -0
  230. package/lib/database/store/store.d.ts +145 -0
  231. package/lib/database/store/store.js +192 -0
  232. package/lib/delegate/index.d.ts +2 -0
  233. package/lib/delegate/index.js +2 -0
  234. package/lib/delegate/team.d.ts +92 -0
  235. package/lib/delegate/team.js +200 -0
  236. package/lib/delegate/teamTypes.d.ts +43 -0
  237. package/lib/delegate/teamTypes.js +1 -0
  238. package/lib/diagnosis/checklist.d.ts +16 -0
  239. package/lib/diagnosis/checklist.js +61 -0
  240. package/lib/diagnosis/context.d.ts +19 -0
  241. package/lib/diagnosis/context.js +79 -0
  242. package/lib/diagnosis/frame.d.ts +34 -0
  243. package/lib/diagnosis/frame.js +230 -0
  244. package/lib/diagnosis/index.d.ts +5 -0
  245. package/lib/diagnosis/index.js +5 -0
  246. package/lib/diagnosis/router.d.ts +22 -0
  247. package/lib/diagnosis/router.js +47 -0
  248. package/lib/diagnosis/types.d.ts +78 -0
  249. package/lib/diagnosis/types.js +2 -0
  250. package/lib/error/agentError.d.ts +9 -0
  251. package/lib/error/agentError.js +13 -0
  252. package/lib/error/ampError.d.ts +14 -0
  253. package/lib/error/ampError.js +19 -0
  254. package/lib/error/authzError.d.ts +10 -0
  255. package/lib/error/authzError.js +11 -0
  256. package/lib/error/boundaryRejection.d.ts +16 -0
  257. package/lib/error/boundaryRejection.js +14 -0
  258. package/lib/error/index.d.ts +7 -0
  259. package/lib/error/index.js +7 -0
  260. package/lib/error/kernelError.d.ts +11 -0
  261. package/lib/error/kernelError.js +12 -0
  262. package/lib/error/mailboxError.d.ts +5 -0
  263. package/lib/error/mailboxError.js +6 -0
  264. package/lib/error/windowError.d.ts +8 -0
  265. package/lib/error/windowError.js +9 -0
  266. package/lib/evaluation/agreement.d.ts +22 -0
  267. package/lib/evaluation/agreement.js +82 -0
  268. package/lib/evaluation/index.d.ts +2 -0
  269. package/lib/evaluation/index.js +2 -0
  270. package/lib/evaluation/labels.d.ts +56 -0
  271. package/lib/evaluation/labels.js +206 -0
  272. package/lib/evidence/evidenceGraph.d.ts +37 -0
  273. package/lib/evidence/evidenceGraph.js +165 -0
  274. package/lib/evidence/index.d.ts +4 -0
  275. package/lib/evidence/index.js +2 -0
  276. package/lib/evidence/revalidation.d.ts +70 -0
  277. package/lib/evidence/revalidation.js +146 -0
  278. package/lib/execution/exec.d.ts +151 -0
  279. package/lib/execution/exec.js +282 -0
  280. package/lib/execution/handles.d.ts +25 -0
  281. package/lib/execution/handles.js +45 -0
  282. package/lib/execution/index.d.ts +3 -0
  283. package/lib/execution/index.js +3 -0
  284. package/lib/execution/pin.d.ts +60 -0
  285. package/lib/execution/pin.js +103 -0
  286. package/lib/frame/frame.d.ts +61 -0
  287. package/lib/frame/frame.js +1 -0
  288. package/lib/frame/index.d.ts +1 -0
  289. package/lib/frame/index.js +1 -0
  290. package/lib/goal/goal.d.ts +19 -0
  291. package/lib/goal/goal.js +21 -0
  292. package/lib/goal/index.d.ts +2 -0
  293. package/lib/goal/index.js +1 -0
  294. package/lib/goal/plan.d.ts +125 -0
  295. package/lib/goal/plan.js +60 -0
  296. package/lib/governance/attentionManifest.d.ts +279 -0
  297. package/lib/governance/attentionManifest.js +467 -0
  298. package/lib/governance/audit.d.ts +123 -0
  299. package/lib/governance/audit.js +263 -0
  300. package/lib/governance/budget.d.ts +219 -0
  301. package/lib/governance/budget.js +424 -0
  302. package/lib/governance/context.d.ts +159 -0
  303. package/lib/governance/context.js +229 -0
  304. package/lib/governance/control.d.ts +65 -0
  305. package/lib/governance/control.js +89 -0
  306. package/lib/governance/cooperation.d.ts +163 -0
  307. package/lib/governance/cooperation.js +350 -0
  308. package/lib/governance/credential.d.ts +117 -0
  309. package/lib/governance/credential.js +229 -0
  310. package/lib/governance/httpServer.d.ts +18 -0
  311. package/lib/governance/httpServer.js +74 -0
  312. package/lib/governance/index.d.ts +32 -0
  313. package/lib/governance/index.js +32 -0
  314. package/lib/governance/legal.d.ts +222 -0
  315. package/lib/governance/legal.js +371 -0
  316. package/lib/governance/matrix.d.ts +170 -0
  317. package/lib/governance/matrix.js +365 -0
  318. package/lib/governance/perspectives.d.ts +98 -0
  319. package/lib/governance/perspectives.js +340 -0
  320. package/lib/governance/signing.d.ts +131 -0
  321. package/lib/governance/signing.js +241 -0
  322. package/lib/governance/store.d.ts +33 -0
  323. package/lib/governance/store.js +89 -0
  324. package/lib/governance/switching.d.ts +183 -0
  325. package/lib/governance/switching.js +257 -0
  326. package/lib/governance/toolPolicy.d.ts +37 -0
  327. package/lib/governance/toolPolicy.js +42 -0
  328. package/lib/governance/transport.d.ts +76 -0
  329. package/lib/governance/transport.js +124 -0
  330. package/lib/governance/types.d.ts +218 -0
  331. package/lib/governance/types.js +72 -0
  332. package/lib/governance/workflow.d.ts +174 -0
  333. package/lib/governance/workflow.js +612 -0
  334. package/lib/index.d.ts +11 -0
  335. package/lib/index.js +11 -0
  336. package/lib/interaction/index.d.ts +1 -0
  337. package/lib/interaction/index.js +1 -0
  338. package/lib/interaction/interaction.d.ts +83 -0
  339. package/lib/interaction/interaction.js +256 -0
  340. package/lib/ipc/index.d.ts +4 -0
  341. package/lib/ipc/index.js +3 -0
  342. package/lib/ipc/mailbox.d.ts +37 -0
  343. package/lib/ipc/mailbox.js +77 -0
  344. package/lib/ipc/memorySocialStore.d.ts +14 -0
  345. package/lib/ipc/memorySocialStore.js +83 -0
  346. package/lib/ipc/social.d.ts +80 -0
  347. package/lib/ipc/social.js +1 -0
  348. package/lib/kernel/index.d.ts +2 -0
  349. package/lib/kernel/index.js +1 -0
  350. package/lib/kernel/scheduler.d.ts +195 -0
  351. package/lib/kernel/scheduler.js +334 -0
  352. package/lib/ledger/checkpoint.d.ts +116 -0
  353. package/lib/ledger/checkpoint.js +223 -0
  354. package/lib/ledger/index.d.ts +4 -0
  355. package/lib/ledger/index.js +4 -0
  356. package/lib/ledger/ledger.d.ts +21 -0
  357. package/lib/ledger/ledger.js +23 -0
  358. package/lib/ledger/persistence.d.ts +46 -0
  359. package/lib/ledger/persistence.js +106 -0
  360. package/lib/ledger/share.d.ts +29 -0
  361. package/lib/ledger/share.js +39 -0
  362. package/lib/lock/index.d.ts +2 -0
  363. package/lib/lock/index.js +1 -0
  364. package/lib/lock/lease.d.ts +23 -0
  365. package/lib/lock/lease.js +42 -0
  366. package/lib/manifest/agentCard.d.ts +35 -0
  367. package/lib/manifest/agentCard.js +38 -0
  368. package/lib/manifest/cardSign.d.ts +25 -0
  369. package/lib/manifest/cardSign.js +70 -0
  370. package/lib/manifest/federation.d.ts +64 -0
  371. package/lib/manifest/federation.js +187 -0
  372. package/lib/manifest/federationTypes.d.ts +51 -0
  373. package/lib/manifest/federationTypes.js +1 -0
  374. package/lib/manifest/index.d.ts +8 -0
  375. package/lib/manifest/index.js +8 -0
  376. package/lib/manifest/keys.d.ts +221 -0
  377. package/lib/manifest/keys.js +484 -0
  378. package/lib/manifest/manifest.d.ts +102 -0
  379. package/lib/manifest/manifest.js +1 -0
  380. package/lib/manifest/sign.d.ts +38 -0
  381. package/lib/manifest/sign.js +90 -0
  382. package/lib/manifest/trust.d.ts +54 -0
  383. package/lib/manifest/trust.js +87 -0
  384. package/lib/mission/index.d.ts +2 -0
  385. package/lib/mission/index.js +1 -0
  386. package/lib/mission/missionQueue.d.ts +19 -0
  387. package/lib/mission/missionQueue.js +62 -0
  388. package/lib/module/index.d.ts +9 -0
  389. package/lib/module/index.js +9 -0
  390. package/lib/module/llm/deepseek.d.ts +43 -0
  391. package/lib/module/llm/deepseek.js +162 -0
  392. package/lib/module/llm/llm.d.ts +64 -0
  393. package/lib/module/llm/llm.js +16 -0
  394. package/lib/module/mcp/client.d.ts +32 -0
  395. package/lib/module/mcp/client.js +84 -0
  396. package/lib/module/mcp/http.d.ts +32 -0
  397. package/lib/module/mcp/http.js +83 -0
  398. package/lib/module/mcp/protocol.d.ts +62 -0
  399. package/lib/module/mcp/protocol.js +20 -0
  400. package/lib/module/mcp/server.d.ts +22 -0
  401. package/lib/module/mcp/server.js +138 -0
  402. package/lib/module/mcp/transport.d.ts +19 -0
  403. package/lib/module/mcp/transport.js +16 -0
  404. package/lib/module/mcp/workspaceServer.d.ts +20 -0
  405. package/lib/module/mcp/workspaceServer.js +90 -0
  406. package/lib/module/sandbox/sandbox.d.ts +24 -0
  407. package/lib/module/sandbox/sandbox.js +5 -0
  408. package/lib/module/tool/mcpExecutor.d.ts +18 -0
  409. package/lib/module/tool/mcpExecutor.js +41 -0
  410. package/lib/module/tool/policy.d.ts +30 -0
  411. package/lib/module/tool/policy.js +23 -0
  412. package/lib/module/tool/redact.d.ts +9 -0
  413. package/lib/module/tool/redact.js +19 -0
  414. package/lib/module/tool/tool.d.ts +26 -0
  415. package/lib/module/tool/tool.js +24 -0
  416. package/lib/module/tool/workspaceExecutor.d.ts +12 -0
  417. package/lib/module/tool/workspaceExecutor.js +25 -0
  418. package/lib/policy/guard.d.ts +115 -0
  419. package/lib/policy/guard.js +154 -0
  420. package/lib/policy/index.d.ts +2 -0
  421. package/lib/policy/index.js +2 -0
  422. package/lib/policy/policy.d.ts +249 -0
  423. package/lib/policy/policy.js +364 -0
  424. package/lib/process/agent.d.ts +295 -0
  425. package/lib/process/agent.js +1 -0
  426. package/lib/process/agentService.d.ts +87 -0
  427. package/lib/process/agentService.js +128 -0
  428. package/lib/process/agentStore.d.ts +35 -0
  429. package/lib/process/agentStore.js +328 -0
  430. package/lib/process/executionStore.d.ts +79 -0
  431. package/lib/process/executionStore.js +71 -0
  432. package/lib/process/index.d.ts +5 -0
  433. package/lib/process/index.js +5 -0
  434. package/lib/process/process.d.ts +120 -0
  435. package/lib/process/process.js +111 -0
  436. package/lib/process/transitions.d.ts +11 -0
  437. package/lib/process/transitions.js +8 -0
  438. package/lib/progress/index.d.ts +2 -0
  439. package/lib/progress/index.js +1 -0
  440. package/lib/progress/progress.d.ts +26 -0
  441. package/lib/progress/progress.js +42 -0
  442. package/lib/protocol/a2a.d.ts +51 -0
  443. package/lib/protocol/a2a.js +79 -0
  444. package/lib/protocol/channel.d.ts +25 -0
  445. package/lib/protocol/channel.js +27 -0
  446. package/lib/protocol/envelope.d.ts +180 -0
  447. package/lib/protocol/envelope.js +495 -0
  448. package/lib/protocol/httpChannel.d.ts +33 -0
  449. package/lib/protocol/httpChannel.js +59 -0
  450. package/lib/protocol/index.d.ts +6 -0
  451. package/lib/protocol/index.js +6 -0
  452. package/lib/protocol/protocol.d.ts +120 -0
  453. package/lib/protocol/protocol.js +1 -0
  454. package/lib/protocol/wire.d.ts +12 -0
  455. package/lib/protocol/wire.js +55 -0
  456. package/lib/receipt/index.d.ts +1 -0
  457. package/lib/receipt/index.js +1 -0
  458. package/lib/receipt/receipt.d.ts +43 -0
  459. package/lib/receipt/receipt.js +1 -0
  460. package/lib/registry/content.d.ts +10 -0
  461. package/lib/registry/content.js +58 -0
  462. package/lib/registry/index.d.ts +4 -0
  463. package/lib/registry/index.js +2 -0
  464. package/lib/registry/registry.d.ts +109 -0
  465. package/lib/registry/registry.js +299 -0
  466. package/lib/runtime/action.d.ts +46 -0
  467. package/lib/runtime/action.js +39 -0
  468. package/lib/runtime/behavior.d.ts +110 -0
  469. package/lib/runtime/behavior.js +179 -0
  470. package/lib/runtime/behaviorTuning.d.ts +59 -0
  471. package/lib/runtime/behaviorTuning.js +95 -0
  472. package/lib/runtime/cognitiveRuntime.d.ts +189 -0
  473. package/lib/runtime/cognitiveRuntime.js +490 -0
  474. package/lib/runtime/escalation.d.ts +22 -0
  475. package/lib/runtime/escalation.js +15 -0
  476. package/lib/runtime/governedTask.d.ts +57 -0
  477. package/lib/runtime/governedTask.js +150 -0
  478. package/lib/runtime/index.d.ts +15 -0
  479. package/lib/runtime/index.js +15 -0
  480. package/lib/runtime/kernelRuntime.d.ts +33 -0
  481. package/lib/runtime/kernelRuntime.js +105 -0
  482. package/lib/runtime/operation.d.ts +30 -0
  483. package/lib/runtime/operation.js +1 -0
  484. package/lib/runtime/replay.d.ts +24 -0
  485. package/lib/runtime/replay.js +38 -0
  486. package/lib/runtime/routing.d.ts +12 -0
  487. package/lib/runtime/routing.js +44 -0
  488. package/lib/runtime/safety.d.ts +15 -0
  489. package/lib/runtime/safety.js +28 -0
  490. package/lib/runtime/session.d.ts +147 -0
  491. package/lib/runtime/session.js +214 -0
  492. package/lib/runtime/sessionStore.d.ts +24 -0
  493. package/lib/runtime/sessionStore.js +20 -0
  494. package/lib/runtime/telemetry.d.ts +24 -0
  495. package/lib/runtime/telemetry.js +33 -0
  496. package/lib/runtime/turnStore.d.ts +16 -0
  497. package/lib/runtime/turnStore.js +15 -0
  498. package/lib/scenario/index.d.ts +1 -0
  499. package/lib/scenario/index.js +1 -0
  500. package/lib/scenario/scenario.d.ts +52 -0
  501. package/lib/scenario/scenario.js +210 -0
  502. package/lib/semantic/canonical.d.ts +6 -0
  503. package/lib/semantic/canonical.js +22 -0
  504. package/lib/semantic/index.d.ts +2 -0
  505. package/lib/semantic/index.js +2 -0
  506. package/lib/semantic/sha256.d.ts +1 -0
  507. package/lib/semantic/sha256.js +66 -0
  508. package/lib/storage/appendLog.d.ts +145 -0
  509. package/lib/storage/appendLog.js +299 -0
  510. package/lib/storage/fileAppendSink.d.ts +7 -0
  511. package/lib/storage/fileAppendSink.js +34 -0
  512. package/lib/storage/index.d.ts +2 -0
  513. package/lib/storage/index.js +2 -0
  514. package/lib/store/bindCache.d.ts +9 -0
  515. package/lib/store/bindCache.js +18 -0
  516. package/lib/store/cache.d.ts +48 -0
  517. package/lib/store/cache.js +162 -0
  518. package/lib/store/context.d.ts +10 -0
  519. package/lib/store/context.js +27 -0
  520. package/lib/store/handleStore.d.ts +9 -0
  521. package/lib/store/handleStore.js +25 -0
  522. package/lib/store/index.d.ts +9 -0
  523. package/lib/store/index.js +8 -0
  524. package/lib/store/messageStore.d.ts +9 -0
  525. package/lib/store/messageStore.js +25 -0
  526. package/lib/store/objectStore.d.ts +9 -0
  527. package/lib/store/objectStore.js +25 -0
  528. package/lib/store/processStore.d.ts +9 -0
  529. package/lib/store/processStore.js +25 -0
  530. package/lib/store/threadStore.d.ts +9 -0
  531. package/lib/store/threadStore.js +25 -0
  532. package/lib/store/windowStore.d.ts +9 -0
  533. package/lib/store/windowStore.js +25 -0
  534. package/lib/structure/abstractSyntaxTree.d.ts +181 -0
  535. package/lib/structure/abstractSyntaxTree.js +400 -0
  536. package/lib/structure/avlTree.d.ts +125 -0
  537. package/lib/structure/avlTree.js +369 -0
  538. package/lib/structure/binarySearchTree.d.ts +125 -0
  539. package/lib/structure/binarySearchTree.js +306 -0
  540. package/lib/structure/binaryTree.d.ts +174 -0
  541. package/lib/structure/binaryTree.js +495 -0
  542. package/lib/structure/bloomFilter.d.ts +126 -0
  543. package/lib/structure/bloomFilter.js +177 -0
  544. package/lib/structure/compare.d.ts +55 -0
  545. package/lib/structure/compare.js +96 -0
  546. package/lib/structure/cuckooFilter.d.ts +149 -0
  547. package/lib/structure/cuckooFilter.js +267 -0
  548. package/lib/structure/deque.d.ts +117 -0
  549. package/lib/structure/deque.js +205 -0
  550. package/lib/structure/filterKey.d.ts +19 -0
  551. package/lib/structure/filterKey.js +28 -0
  552. package/lib/structure/hash.d.ts +129 -0
  553. package/lib/structure/hash.js +241 -0
  554. package/lib/structure/heap.d.ts +152 -0
  555. package/lib/structure/heap.js +227 -0
  556. package/lib/structure/index.d.ts +20 -0
  557. package/lib/structure/index.js +20 -0
  558. package/lib/structure/iterator.d.ts +6 -0
  559. package/lib/structure/iterator.js +16 -0
  560. package/lib/structure/lru.d.ts +235 -0
  561. package/lib/structure/lru.js +524 -0
  562. package/lib/structure/ordered.d.ts +89 -0
  563. package/lib/structure/ordered.js +336 -0
  564. package/lib/structure/priority.d.ts +499 -0
  565. package/lib/structure/priority.js +756 -0
  566. package/lib/structure/queue.d.ts +90 -0
  567. package/lib/structure/queue.js +154 -0
  568. package/lib/structure/redBlackTree.d.ts +142 -0
  569. package/lib/structure/redBlackTree.js +644 -0
  570. package/lib/structure/segmentTree.d.ts +129 -0
  571. package/lib/structure/segmentTree.js +205 -0
  572. package/lib/structure/stack.d.ts +77 -0
  573. package/lib/structure/stack.js +107 -0
  574. package/lib/structure/unordered.d.ts +99 -0
  575. package/lib/structure/unordered.js +341 -0
  576. package/lib/thread/index.d.ts +2 -0
  577. package/lib/thread/index.js +2 -0
  578. package/lib/thread/thread.d.ts +107 -0
  579. package/lib/thread/thread.js +81 -0
  580. package/lib/thread/transitions.d.ts +8 -0
  581. package/lib/thread/transitions.js +9 -0
  582. package/lib/types/common.d.ts +80 -0
  583. package/lib/types/common.js +5 -0
  584. package/lib/types/index.d.ts +23 -0
  585. package/lib/types/index.js +1 -0
  586. package/lib/warrant/epistemic.d.ts +141 -0
  587. package/lib/warrant/epistemic.js +1 -0
  588. package/lib/warrant/index.d.ts +3 -0
  589. package/lib/warrant/index.js +3 -0
  590. package/lib/warrant/inquiry.d.ts +69 -0
  591. package/lib/warrant/inquiry.js +119 -0
  592. package/lib/warrant/warrant.d.ts +46 -0
  593. package/lib/warrant/warrant.js +153 -0
  594. package/lib/window/compaction.d.ts +57 -0
  595. package/lib/window/compaction.js +334 -0
  596. package/lib/window/compactionTypes.d.ts +113 -0
  597. package/lib/window/compactionTypes.js +1 -0
  598. package/lib/window/index.d.ts +7 -0
  599. package/lib/window/index.js +3 -0
  600. package/lib/window/page.d.ts +16 -0
  601. package/lib/window/page.js +2 -0
  602. package/lib/window/window.d.ts +68 -0
  603. package/lib/window/window.js +194 -0
  604. package/package.json +22 -1
  605. package/src/authz/index.ts +1 -0
  606. package/src/concept/index.ts +3 -0
  607. package/src/concurrency/index.ts +5 -0
  608. package/src/cooperation/index.ts +6 -0
  609. package/src/delegate/index.ts +2 -0
  610. package/src/error/index.ts +7 -0
  611. package/src/evaluation/index.ts +2 -0
  612. package/src/execution/index.ts +3 -0
  613. package/src/frame/index.ts +1 -0
  614. package/src/index.ts +11 -0
  615. package/src/ledger/index.ts +4 -0
  616. package/src/manifest/index.ts +8 -0
  617. package/src/policy/index.ts +2 -0
  618. package/src/protocol/index.ts +6 -0
  619. package/src/receipt/index.ts +1 -0
  620. package/src/semantic/index.ts +2 -0
  621. package/src/storage/index.ts +2 -0
  622. package/src/structure/index.ts +20 -0
  623. package/src/thread/index.ts +2 -0
  624. package/src/warrant/index.ts +3 -0
  625. package/src/window/index.ts +7 -4
  626. package/tools/rewrite-declaration-extensions.ts +98 -0
  627. package/tsconfig.build.json +14 -0
@@ -0,0 +1,756 @@
1
+ /**
2
+ * Priority-ordered containers.
3
+ *
4
+ * Every container in this module orders values by a {@link Comparator} and then
5
+ * by arrival: when two values compare equal, the one enqueued first is served
6
+ * first. The queues are therefore stable, and re-inserting an equal value never
7
+ * reorders them.
8
+ *
9
+ * `order: 'max'` is the default and serves the greatest value first, matching
10
+ * the priority convention used elsewhere in this project where a larger number
11
+ * is more urgent; `order: 'min'` serves the smallest first. The comparator
12
+ * decides priority only — it never changes which values are stored.
13
+ *
14
+ * Costs, with `n` entries and `b` live integer priorities:
15
+ *
16
+ * | Container | enqueue | dequeue | peek | re-prioritise |
17
+ * | --- | --- | --- | --- | --- |
18
+ * | {@link PriorityQueue} | O(log n) | O(log n) | O(1) | — |
19
+ * | {@link IndexedPriorityQueue} | O(log n) | O(log n) | O(1) | O(log n) |
20
+ * | {@link BucketQueue} | O(1) | O(1) amortised | O(1) | — |
21
+ * | {@link TopK} | O(log k) | — | O(1) | — |
22
+ *
23
+ * `toArray`, iteration and `drain` expose **dequeue order**, not internal
24
+ * storage order: the contract of a priority queue is the order in which it will
25
+ * be served. This is the deliberate difference from {@link BinaryHeap}, whose
26
+ * snapshots expose heap order. `drain` is the destructive version of `toArray`.
27
+ */
28
+ import { compareNatural } from "./compare.js";
29
+ import { BinaryHeap } from "./heap.js";
30
+ import { generatorFrom } from "./iterator.js";
31
+ /**
32
+ * Sign multiplier that turns a comparator into the serving order of a heap:
33
+ * the smallest result is served first, so `'max'` inverts the comparator.
34
+ */
35
+ const servingDirection = (order) => {
36
+ return order === "max" ? -1 : 1;
37
+ };
38
+ /**
39
+ * Sign multiplier for "better than": a positive result means the left value is
40
+ * better, so `'max'` keeps the comparator as it is.
41
+ */
42
+ const qualityDirection = (order) => {
43
+ return order === "max" ? 1 : -1;
44
+ };
45
+ const entryComparator = (compare, direction) => {
46
+ return (left, right) => {
47
+ const byPriority = direction * compare(left.value, right.value);
48
+ if (byPriority !== 0) {
49
+ return byPriority;
50
+ }
51
+ return left.sequence - right.sequence;
52
+ };
53
+ };
54
+ /**
55
+ * Reject a capacity that cannot bound a container.
56
+ */
57
+ const requireCapacity = (capacity) => {
58
+ if (!Number.isSafeInteger(capacity) || capacity <= 0) {
59
+ throw new RangeError("capacity must be a positive safe integer");
60
+ }
61
+ };
62
+ /**
63
+ * A stable priority queue backed by a binary heap.
64
+ *
65
+ * The value that compares greatest under `compare` is served first by default;
66
+ * `order: 'min'` serves the least. Ties are broken by arrival, so two entries
67
+ * with the same priority always leave in the order they were enqueued.
68
+ *
69
+ * `enqueue` and `dequeue` are O(log n), and `peek` is O(1). `toArray`,
70
+ * iteration and `drain` return **dequeue order**; `drain` removes everything.
71
+ *
72
+ * `dequeue` and `peek` return `undefined` when the queue is empty. Because
73
+ * `undefined` is also a valid stored value, check {@link size} or
74
+ * {@link isEmpty} when presence matters.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const queue = new PriorityQueue<number>({ values: [3, 1, 4, 1] });
79
+ * queue.enqueue(2);
80
+ * queue.dequeue(); // 4
81
+ * [...queue]; // [3, 2, 1, 1]
82
+ * queue.drain(); // [3, 2, 1, 1]
83
+ * ```
84
+ */
85
+ export class PriorityQueue {
86
+ heap;
87
+ compareFn;
88
+ sequence = 0;
89
+ /**
90
+ * Create a queue, optionally seeded with values and a custom ordering.
91
+ *
92
+ * Seeded values are enqueued in iteration order, so among equal priorities
93
+ * the first seeded value is served first.
94
+ *
95
+ * @param options - Comparator, order, and initial values.
96
+ */
97
+ constructor(options) {
98
+ const compare = options?.compare ?? compareNatural;
99
+ const order = options?.order ?? "max";
100
+ this.compareFn = entryComparator(compare, servingDirection(order));
101
+ this.heap = new BinaryHeap({ compare: this.compareFn });
102
+ if (options?.values !== (void 0)) {
103
+ this.enqueueAll(options.values);
104
+ }
105
+ }
106
+ /**
107
+ * Build a queue from `values` without repeating the `values` option.
108
+ *
109
+ * @param values - Initial values to enqueue in iteration order.
110
+ * @param options - Comparator and order options.
111
+ * @returns A new priority queue containing every supplied value.
112
+ */
113
+ static from(values, options) {
114
+ const queueOptions = { values };
115
+ if (options?.compare !== (void 0)) {
116
+ queueOptions.compare = options.compare;
117
+ }
118
+ if (options?.order !== (void 0)) {
119
+ queueOptions.order = options.order;
120
+ }
121
+ return new PriorityQueue(queueOptions);
122
+ }
123
+ /**
124
+ * Number of values currently in the queue.
125
+ */
126
+ get size() {
127
+ return this.heap.size;
128
+ }
129
+ /**
130
+ * Whether the queue contains no values.
131
+ */
132
+ get isEmpty() {
133
+ return this.heap.isEmpty;
134
+ }
135
+ /**
136
+ * Add a value at its priority, behind any equal value already queued.
137
+ *
138
+ * @param value - Value to enqueue.
139
+ * @returns This queue, for chaining.
140
+ */
141
+ enqueue(value) {
142
+ const entry = { value, sequence: this.sequence };
143
+ this.sequence += 1;
144
+ this.heap.push(entry);
145
+ return this;
146
+ }
147
+ /**
148
+ * Enqueue every value in iteration order.
149
+ *
150
+ * @param values - Values to enqueue.
151
+ * @returns This queue, for chaining.
152
+ */
153
+ enqueueAll(values) {
154
+ const items = [...values];
155
+ for (let index = 0; index < items.length; index += 1) {
156
+ this.enqueue(items[index]);
157
+ }
158
+ return this;
159
+ }
160
+ /**
161
+ * Remove and return the value with the highest priority.
162
+ *
163
+ * @returns The served value, or `undefined` if the queue is empty.
164
+ */
165
+ dequeue() {
166
+ const entry = this.heap.pop();
167
+ return entry === (void 0) ? undefined : entry.value;
168
+ }
169
+ /**
170
+ * Return the value that would be served next, without removing it.
171
+ *
172
+ * @returns The next value, or `undefined` if the queue is empty.
173
+ */
174
+ peek() {
175
+ const entry = this.heap.peek();
176
+ return entry === (void 0) ? undefined : entry.value;
177
+ }
178
+ /**
179
+ * Remove every value and reset the arrival counter.
180
+ */
181
+ clear() {
182
+ this.heap.clear();
183
+ this.sequence = 0;
184
+ }
185
+ /**
186
+ * Snapshot the values in dequeue order without changing the queue.
187
+ *
188
+ * The cost is O(n log n): the heap is copied, then sorted by the same
189
+ * comparator the queue uses.
190
+ *
191
+ * @returns A new array whose first item is served first.
192
+ */
193
+ toArray() {
194
+ const entries = this.heap.toArray();
195
+ entries.sort(this.compareFn);
196
+ return entries.map((entry) => entry.value);
197
+ }
198
+ /**
199
+ * Remove and return every value in dequeue order.
200
+ *
201
+ * @returns A new array in the order the values would have been served.
202
+ */
203
+ drain() {
204
+ const values = [];
205
+ while (!this.heap.isEmpty) {
206
+ const entry = this.heap.pop();
207
+ if (entry !== (void 0)) {
208
+ values.push(entry.value);
209
+ }
210
+ }
211
+ return values;
212
+ }
213
+ /**
214
+ * Iterate in dequeue order over a snapshot taken when the iterator is
215
+ * created.
216
+ */
217
+ [Symbol.iterator]() {
218
+ return generatorFrom(this.toArray());
219
+ }
220
+ }
221
+ /**
222
+ * A priority queue that can be addressed by key.
223
+ *
224
+ * A plain {@link PriorityQueue} cannot re-prioritise or remove an entry it has
225
+ * already accepted, because finding it costs a scan. This queue keeps a map
226
+ * from key to heap position, so `set`, `delete`, `get` and `has` are O(log n)
227
+ * or O(1) — the shape Dijkstra's algorithm and task schedulers need.
228
+ *
229
+ * `set` inserts a new key or replaces the value stored under an existing one.
230
+ * A replaced entry keeps its original arrival sequence, so re-setting an entry
231
+ * at an unchanged priority does not move it behind entries that arrived later.
232
+ * Ties are still served in arrival order.
233
+ *
234
+ * `toArray` and `keys` are O(n log n) snapshots in dequeue order; `dequeue`,
235
+ * `peek`, `get` and `delete` do not scan.
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * const queue = new IndexedPriorityQueue<string, Task>({
240
+ * keyOf: (task: Task): string => task.id,
241
+ * compare: (left: Task, right: Task): number => left.rank - right.rank,
242
+ * });
243
+ * queue.set({ id: "a", rank: 2 }).set({ id: "b", rank: 5 });
244
+ * queue.peek()?.id; // "b"
245
+ * queue.set({ id: "a", rank: 9 });
246
+ * queue.dequeue()?.id; // "a"
247
+ * queue.delete("b"); // true
248
+ * ```
249
+ */
250
+ export class IndexedPriorityQueue {
251
+ entries = [];
252
+ positions = new Map();
253
+ keyOf;
254
+ compareFn;
255
+ sequence = 0;
256
+ /**
257
+ * Create an empty keyed queue.
258
+ *
259
+ * @param options - Key selector, comparator and order.
260
+ */
261
+ constructor(options) {
262
+ const compare = options.compare ?? compareNatural;
263
+ const order = options.order ?? "max";
264
+ this.keyOf = options.keyOf;
265
+ this.compareFn = entryComparator(compare, servingDirection(order));
266
+ }
267
+ /**
268
+ * Number of entries currently in the queue.
269
+ */
270
+ get size() {
271
+ return this.entries.length;
272
+ }
273
+ /**
274
+ * Whether the queue contains no entries.
275
+ */
276
+ get isEmpty() {
277
+ return this.entries.length === 0;
278
+ }
279
+ /**
280
+ * Insert a value, or replace the value stored under its key.
281
+ *
282
+ * A new key is appended behind entries with equal priority. An existing key
283
+ * keeps its arrival sequence and only moves if its priority changed.
284
+ *
285
+ * @param value - Value to insert or replace.
286
+ * @returns This queue, for chaining.
287
+ */
288
+ set(value) {
289
+ const key = this.keyOf(value);
290
+ const index = this.positions.get(key);
291
+ if (index === (void 0)) {
292
+ const entry = { key, value, sequence: this.sequence };
293
+ this.sequence += 1;
294
+ this.positions.set(key, this.entries.length);
295
+ this.entries.push(entry);
296
+ this.siftUp(this.entries.length - 1);
297
+ return this;
298
+ }
299
+ const entry = this.entries[index];
300
+ entry.value = value;
301
+ const moved = this.siftUp(index);
302
+ this.siftDown(moved);
303
+ return this;
304
+ }
305
+ /**
306
+ * Remove the entry stored under `key`.
307
+ *
308
+ * @param key - Key to remove.
309
+ * @returns `true` if an entry was removed, `false` if the key was absent.
310
+ */
311
+ delete(key) {
312
+ const index = this.positions.get(key);
313
+ if (index === (void 0)) {
314
+ return false;
315
+ }
316
+ this.removeAt(index);
317
+ return true;
318
+ }
319
+ /**
320
+ * Read the value stored under `key` without changing the queue.
321
+ *
322
+ * @param key - Key to read.
323
+ * @returns The stored value, or `undefined` if the key is absent.
324
+ */
325
+ get(key) {
326
+ const index = this.positions.get(key);
327
+ return index === (void 0) ? undefined : this.entries[index].value;
328
+ }
329
+ /**
330
+ * Whether an entry is stored under `key`.
331
+ *
332
+ * @param key - Key to test.
333
+ */
334
+ has(key) {
335
+ return this.positions.has(key);
336
+ }
337
+ /**
338
+ * Remove and return the value with the highest priority.
339
+ *
340
+ * @returns The served value, or `undefined` if the queue is empty.
341
+ */
342
+ dequeue() {
343
+ if (this.entries.length === 0) {
344
+ return undefined;
345
+ }
346
+ return this.removeAt(0).value;
347
+ }
348
+ /**
349
+ * Return the value that would be served next, without removing it.
350
+ *
351
+ * @returns The next value, or `undefined` if the queue is empty.
352
+ */
353
+ peek() {
354
+ return this.entries.length === 0 ? undefined : this.entries[0].value;
355
+ }
356
+ /**
357
+ * Return the key that would be served next, without removing it.
358
+ *
359
+ * @returns The next key, or `undefined` if the queue is empty.
360
+ */
361
+ peekKey() {
362
+ return this.entries.length === 0 ? undefined : this.entries[0].key;
363
+ }
364
+ /**
365
+ * Remove every entry.
366
+ */
367
+ clear() {
368
+ this.entries.length = 0;
369
+ this.positions.clear();
370
+ this.sequence = 0;
371
+ }
372
+ /**
373
+ * Snapshot the values in dequeue order.
374
+ *
375
+ * @returns A new array whose first item is served first.
376
+ */
377
+ toArray() {
378
+ const ordered = this.entries.slice();
379
+ ordered.sort(this.compareFn);
380
+ return ordered.map((entry) => entry.value);
381
+ }
382
+ /**
383
+ * Snapshot the keys in dequeue order.
384
+ *
385
+ * @returns A new array whose first key is served first.
386
+ */
387
+ keys() {
388
+ const ordered = this.entries.slice();
389
+ ordered.sort(this.compareFn);
390
+ return ordered.map((entry) => entry.key);
391
+ }
392
+ /**
393
+ * Iterate in dequeue order over a snapshot taken when the iterator is
394
+ * created.
395
+ */
396
+ [Symbol.iterator]() {
397
+ return generatorFrom(this.toArray());
398
+ }
399
+ /**
400
+ * Remove the entry at `index` and return it.
401
+ *
402
+ * The last entry fills the hole, then moves either up or down to restore
403
+ * the heap property; the key index follows both moves.
404
+ */
405
+ removeAt(index) {
406
+ const removed = this.entries[index];
407
+ const lastIndex = this.entries.length - 1;
408
+ const last = this.entries[lastIndex];
409
+ this.entries.pop();
410
+ this.positions.delete(removed.key);
411
+ if (index !== lastIndex) {
412
+ this.entries[index] = last;
413
+ this.positions.set(last.key, index);
414
+ const moved = this.siftUp(index);
415
+ this.siftDown(moved);
416
+ }
417
+ return removed;
418
+ }
419
+ /**
420
+ * Move an entry toward the root until its parent has priority.
421
+ *
422
+ * @returns The index the entry ended up at.
423
+ */
424
+ siftUp(index) {
425
+ let child = index;
426
+ while (child > 0) {
427
+ const parent = Math.floor((child - 1) / 2);
428
+ if (this.compareFn(this.entries[child], this.entries[parent]) >= 0) {
429
+ return child;
430
+ }
431
+ this.swap(child, parent);
432
+ child = parent;
433
+ }
434
+ return child;
435
+ }
436
+ /**
437
+ * Move an entry toward the leaves until both children have lower priority.
438
+ */
439
+ siftDown(index) {
440
+ let parent = index;
441
+ while (true) {
442
+ const left = parent * 2 + 1;
443
+ if (left >= this.entries.length) {
444
+ return;
445
+ }
446
+ const right = left + 1;
447
+ let first = left;
448
+ if (right < this.entries.length && this.compareFn(this.entries[right], this.entries[left]) < 0) {
449
+ first = right;
450
+ }
451
+ if (this.compareFn(this.entries[first], this.entries[parent]) >= 0) {
452
+ return;
453
+ }
454
+ this.swap(parent, first);
455
+ parent = first;
456
+ }
457
+ }
458
+ /**
459
+ * Exchange two entries and keep the key index consistent.
460
+ */
461
+ swap(a, b) {
462
+ const left = this.entries[a];
463
+ const right = this.entries[b];
464
+ this.entries[a] = right;
465
+ this.entries[b] = left;
466
+ this.positions.set(right.key, a);
467
+ this.positions.set(left.key, b);
468
+ }
469
+ }
470
+ /**
471
+ * A priority queue for integer priorities, with one FIFO bucket per priority.
472
+ *
473
+ * Values sharing a priority wait in a single bucket, so serving the next entry
474
+ * costs O(1) until the active bucket empties; switching to the next live bucket
475
+ * costs O(log b) once, where `b` is the number of non-empty buckets. This is
476
+ * the right shape when priorities come from a small domain — scheduler ticks,
477
+ * T-shirt sizes, the 0–100 behaviour priorities used in this project — and the
478
+ * per-entry generality of a comparison heap is wasted.
479
+ *
480
+ * Priorities must be safe integers; anything else raises a `RangeError`, since
481
+ * a bucket index cannot represent it. Negative priorities are fine, and the
482
+ * live priorities need not be contiguous: only buckets that hold values are
483
+ * tracked.
484
+ *
485
+ * @example
486
+ * ```ts
487
+ * const queue = new BucketQueue<string>();
488
+ * queue.enqueue("cold", 0).enqueue("warm", 1).enqueue("hot", 9);
489
+ * queue.dequeue(); // "hot"
490
+ * queue.peekPriority(); // 1
491
+ * queue.toArray(); // ["warm", "cold"]
492
+ * ```
493
+ */
494
+ export class BucketQueue {
495
+ buckets = new Map();
496
+ active;
497
+ order;
498
+ count = 0;
499
+ /**
500
+ * Create an empty bucket queue.
501
+ *
502
+ * @param options - Which end is served first.
503
+ */
504
+ constructor(options) {
505
+ this.order = options?.order ?? "max";
506
+ this.active = new BinaryHeap({ order: this.order });
507
+ }
508
+ /**
509
+ * Number of values currently in the queue.
510
+ */
511
+ get size() {
512
+ return this.count;
513
+ }
514
+ /**
515
+ * Whether the queue contains no values.
516
+ */
517
+ get isEmpty() {
518
+ return this.count === 0;
519
+ }
520
+ /**
521
+ * Append a value to the bucket for `priority`.
522
+ *
523
+ * Values with the same priority leave in enqueue order.
524
+ *
525
+ * @param value - Value to enqueue.
526
+ * @param priority - Integer priority; greater is served first by default.
527
+ * @returns This queue, for chaining.
528
+ */
529
+ enqueue(value, priority) {
530
+ if (!Number.isSafeInteger(priority)) {
531
+ throw new RangeError("priority must be a safe integer");
532
+ }
533
+ let bucket = this.buckets.get(priority);
534
+ if (bucket === (void 0)) {
535
+ bucket = { values: [], head: 0 };
536
+ this.buckets.set(priority, bucket);
537
+ this.active.push(priority);
538
+ }
539
+ bucket.values.push(value);
540
+ this.count += 1;
541
+ return this;
542
+ }
543
+ /**
544
+ * Remove and return the value with the highest priority.
545
+ *
546
+ * @returns The served value, or `undefined` if the queue is empty.
547
+ */
548
+ dequeue() {
549
+ const priority = this.peekPriority();
550
+ if (priority === (void 0)) {
551
+ return undefined;
552
+ }
553
+ const bucket = this.buckets.get(priority);
554
+ if (bucket === (void 0)) {
555
+ return undefined;
556
+ }
557
+ const value = bucket.values[bucket.head];
558
+ bucket.head += 1;
559
+ this.count -= 1;
560
+ if (bucket.head === bucket.values.length) {
561
+ this.buckets.delete(priority);
562
+ this.active.pop();
563
+ }
564
+ else if (bucket.head > 32 && bucket.head * 2 >= bucket.values.length) {
565
+ bucket.values = bucket.values.slice(bucket.head);
566
+ bucket.head = 0;
567
+ }
568
+ return value;
569
+ }
570
+ /**
571
+ * Return the value that would be served next, without removing it.
572
+ *
573
+ * @returns The next value, or `undefined` if the queue is empty.
574
+ */
575
+ peek() {
576
+ const priority = this.peekPriority();
577
+ if (priority === (void 0)) {
578
+ return undefined;
579
+ }
580
+ const bucket = this.buckets.get(priority);
581
+ return bucket === (void 0) ? undefined : bucket.values[bucket.head];
582
+ }
583
+ /**
584
+ * Priority of the next entry to be served, without removing it.
585
+ *
586
+ * @returns The next priority, or `undefined` if the queue is empty.
587
+ */
588
+ peekPriority() {
589
+ while (!this.active.isEmpty) {
590
+ const priority = this.active.peek();
591
+ if (priority !== (void 0) && this.buckets.has(priority)) {
592
+ return priority;
593
+ }
594
+ this.active.pop();
595
+ }
596
+ return undefined;
597
+ }
598
+ /**
599
+ * Remove every value.
600
+ */
601
+ clear() {
602
+ this.buckets.clear();
603
+ this.active.clear();
604
+ this.count = 0;
605
+ }
606
+ /**
607
+ * Snapshot the values in dequeue order without changing the queue.
608
+ *
609
+ * @returns A new array whose first item is served first.
610
+ */
611
+ toArray() {
612
+ const priorities = [...this.buckets.keys()];
613
+ priorities.sort((left, right) => {
614
+ return this.order === "max" ? right - left : left - right;
615
+ });
616
+ const values = [];
617
+ for (let priorityIndex = 0; priorityIndex < priorities.length; priorityIndex += 1) {
618
+ const priority = priorities[priorityIndex];
619
+ const bucket = this.buckets.get(priority);
620
+ if (bucket === (void 0)) {
621
+ continue;
622
+ }
623
+ for (let index = bucket.head; index < bucket.values.length; index += 1) {
624
+ values.push(bucket.values[index]);
625
+ }
626
+ }
627
+ return values;
628
+ }
629
+ /**
630
+ * Iterate in dequeue order over a snapshot taken when the iterator is
631
+ * created.
632
+ */
633
+ [Symbol.iterator]() {
634
+ return generatorFrom(this.toArray());
635
+ }
636
+ }
637
+ /**
638
+ * The `k` best values seen so far, in a bounded buffer.
639
+ *
640
+ * `offer` keeps the best `capacity` values under the comparator and discards
641
+ * the rest, in O(log k): the retained values live in a heap whose root is the
642
+ * current worst, so a new value only displaces that root. A value equal to the
643
+ * worst is refused rather than swapping equal entries, which keeps the buffer
644
+ * stable and its contents independent of later arrivals.
645
+ *
646
+ * The buffer is useful when a full sort is wasteful — for example keeping the
647
+ * `k` most salient candidates out of a stream that never ends.
648
+ *
649
+ * @example
650
+ * ```ts
651
+ * const best = new TopK<number>(3);
652
+ * for (const value of [5, 1, 9, 3, 7]) {
653
+ * best.offer(value);
654
+ * }
655
+ * best.toArray(); // [9, 7, 5]
656
+ * best.worst; // 5
657
+ * ```
658
+ */
659
+ export class TopK {
660
+ /**
661
+ * Maximum number of values retained.
662
+ */
663
+ capacity;
664
+ retained;
665
+ compareFn;
666
+ quality;
667
+ /**
668
+ * Create an empty buffer for the `capacity` best values.
669
+ *
670
+ * @param capacity - Maximum number of values retained; positive integer.
671
+ * @param options - Comparator and which end is best.
672
+ */
673
+ constructor(capacity, options) {
674
+ requireCapacity(capacity);
675
+ this.capacity = capacity;
676
+ this.compareFn = options?.compare ?? compareNatural;
677
+ this.quality = qualityDirection(options?.order ?? "max");
678
+ this.retained = new BinaryHeap({
679
+ compare: (left, right) => this.signedCompare(left, right),
680
+ });
681
+ }
682
+ /**
683
+ * Number of values currently retained.
684
+ */
685
+ get size() {
686
+ return this.retained.size;
687
+ }
688
+ /**
689
+ * Whether the buffer holds `capacity` values.
690
+ */
691
+ get isFull() {
692
+ return this.retained.size >= this.capacity;
693
+ }
694
+ /**
695
+ * The weakest retained value: the next one to be displaced.
696
+ *
697
+ * @returns The current worst value, or `undefined` if nothing is retained.
698
+ */
699
+ get worst() {
700
+ return this.retained.peek();
701
+ }
702
+ /**
703
+ * Offer a value to the buffer.
704
+ *
705
+ * A value is retained while there is room; once full, only a value that
706
+ * strictly outranks {@link worst} is kept and the worst is displaced. A tie
707
+ * keeps the value that arrived first.
708
+ *
709
+ * @param value - Value to offer.
710
+ * @returns `true` if the value is retained afterwards, `false` otherwise.
711
+ */
712
+ offer(value) {
713
+ if (!this.isFull) {
714
+ this.retained.push(value);
715
+ return true;
716
+ }
717
+ const worst = this.retained.peek();
718
+ if (worst === (void 0)) {
719
+ return false;
720
+ }
721
+ if (this.signedCompare(value, worst) <= 0) {
722
+ return false;
723
+ }
724
+ this.retained.replace(value);
725
+ return true;
726
+ }
727
+ /**
728
+ * Discard every retained value.
729
+ */
730
+ clear() {
731
+ this.retained.clear();
732
+ }
733
+ /**
734
+ * Snapshot the retained values, best first.
735
+ *
736
+ * @returns A new array in dequeue order.
737
+ */
738
+ toArray() {
739
+ const values = this.retained.toArray();
740
+ values.sort((left, right) => this.signedCompare(right, left));
741
+ return values;
742
+ }
743
+ /**
744
+ * Iterate best first over a snapshot taken when the iterator is created.
745
+ */
746
+ [Symbol.iterator]() {
747
+ return generatorFrom(this.toArray());
748
+ }
749
+ /**
750
+ * Compare two values so that a positive result means the left one is
751
+ * better, whichever order is in effect.
752
+ */
753
+ signedCompare(left, right) {
754
+ return this.quality * this.compareFn(left, right);
755
+ }
756
+ }