@astrosheep/keiyaku 4.0.0 → 4.0.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 (308) hide show
  1. package/README.md +133 -8
  2. package/build/integrations/marketplace/plugins/keiyaku/.claude-plugin/plugin.json +2 -2
  3. package/build/integrations/marketplace/plugins/keiyaku/.codex-plugin/plugin.json +4 -4
  4. package/build/integrations/marketplace/plugins/keiyaku/opencode.js +2 -2
  5. package/build/integrations/marketplace/plugins/keiyaku/package.json +2 -2
  6. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku/SKILL.md +13 -5
  7. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-akuma/SKILL.md +91 -32
  8. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-bind/SKILL.md +134 -0
  9. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-bind/agents/openai.yaml +4 -0
  10. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-task/SKILL.md +41 -6
  11. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-workflow/SKILL.md +262 -46
  12. package/build/src/akuma/abort.d.ts +2 -0
  13. package/build/src/akuma/abort.js +50 -0
  14. package/build/src/akuma/akuma.d.ts +50 -33
  15. package/build/src/akuma/akuma.js +175 -127
  16. package/build/src/akuma/archetype.d.ts +14 -5
  17. package/build/src/akuma/archetype.js +85 -39
  18. package/build/src/akuma/body.d.ts +3 -5
  19. package/build/src/akuma/body.js +490 -183
  20. package/build/src/akuma/coordinate.d.ts +9 -0
  21. package/build/src/akuma/coordinate.js +22 -0
  22. package/build/src/akuma/heart/facts.d.ts +88 -45
  23. package/build/src/akuma/heart/facts.js +19 -8
  24. package/build/src/akuma/heart/index.d.ts +64 -55
  25. package/build/src/akuma/heart/index.js +172 -252
  26. package/build/src/akuma/heart/rows.d.ts +38 -83
  27. package/build/src/akuma/heart/rows.js +117 -155
  28. package/build/src/akuma/heart/schema.d.ts +1 -1
  29. package/build/src/akuma/heart/schema.js +61 -22
  30. package/build/src/akuma/heart/soul.d.ts +8 -0
  31. package/build/src/akuma/heart/soul.js +136 -0
  32. package/build/src/akuma/heart/storage.d.ts +47 -0
  33. package/build/src/akuma/heart/storage.js +224 -0
  34. package/build/src/akuma/heart/tells.d.ts +12 -0
  35. package/build/src/akuma/heart/tells.js +79 -0
  36. package/build/src/akuma/heart/timeline.d.ts +15 -0
  37. package/build/src/akuma/heart/timeline.js +86 -0
  38. package/build/src/akuma/identity.d.ts +2 -2
  39. package/build/src/akuma/identity.js +9 -9
  40. package/build/src/akuma/index.d.ts +10 -3
  41. package/build/src/akuma/index.js +7 -1
  42. package/build/src/akuma/projection.d.ts +176 -0
  43. package/build/src/akuma/projection.js +256 -0
  44. package/build/src/akuma/provider-recipe.d.ts +24 -0
  45. package/build/src/akuma/provider-recipe.js +100 -0
  46. package/build/src/akuma/provider.d.ts +67 -13
  47. package/build/src/akuma/provider.js +172 -105
  48. package/build/src/akuma/providers/acp/core.d.ts +19 -0
  49. package/build/src/akuma/providers/acp/core.js +159 -0
  50. package/build/src/akuma/providers/acp/events.d.ts +20 -0
  51. package/build/src/akuma/providers/acp/events.js +73 -0
  52. package/build/src/akuma/providers/acp/index.d.ts +12 -0
  53. package/build/src/akuma/providers/acp/index.js +94 -0
  54. package/build/src/akuma/providers/{claude.d.ts → claude/events.d.ts} +9 -41
  55. package/build/src/akuma/providers/claude/events.js +238 -0
  56. package/build/src/akuma/providers/claude/index.d.ts +47 -0
  57. package/build/src/akuma/providers/claude/index.js +279 -0
  58. package/build/src/akuma/providers/claude/input.d.ts +13 -0
  59. package/build/src/akuma/providers/claude/input.js +109 -0
  60. package/build/src/akuma/providers/codex-app-server/events.d.ts +1 -1
  61. package/build/src/akuma/providers/codex-app-server/events.js +15 -12
  62. package/build/src/akuma/providers/codex-app-server/index.d.ts +15 -13
  63. package/build/src/akuma/providers/codex-app-server/index.js +77 -33
  64. package/build/src/akuma/providers/grok-build/index.d.ts +4 -0
  65. package/build/src/akuma/providers/grok-build/index.js +70 -0
  66. package/build/src/akuma/providers/index.d.ts +5 -3
  67. package/build/src/akuma/providers/index.js +35 -56
  68. package/build/src/akuma/providers/opencode-sdk/events.d.ts +54 -0
  69. package/build/src/akuma/providers/opencode-sdk/events.js +235 -0
  70. package/build/src/akuma/providers/opencode-sdk/index.d.ts +33 -0
  71. package/build/src/akuma/providers/opencode-sdk/index.js +308 -0
  72. package/build/src/akuma/providers/opencode-sdk/session.d.ts +26 -0
  73. package/build/src/akuma/providers/opencode-sdk/session.js +63 -0
  74. package/build/src/akuma/providers/pi/events.d.ts +33 -0
  75. package/build/src/akuma/providers/pi/events.js +166 -0
  76. package/build/src/akuma/providers/pi/index.d.ts +11 -0
  77. package/build/src/akuma/providers/pi/index.js +191 -0
  78. package/build/src/akuma/publication.d.ts +3 -3
  79. package/build/src/akuma/publication.js +28 -40
  80. package/build/src/akuma/requests.d.ts +9 -6
  81. package/build/src/akuma/requests.js +95 -65
  82. package/build/src/alias/index.d.ts +4 -3
  83. package/build/src/alias/index.js +12 -13
  84. package/build/src/body/decode.js +7 -1
  85. package/build/src/body/region.d.ts +2 -0
  86. package/build/src/body/region.js +77 -7
  87. package/build/src/body/render.js +2 -1
  88. package/build/src/body/shape.js +7 -1
  89. package/build/src/body/verification.js +19 -4
  90. package/build/src/cli/accepted.d.ts +18 -12
  91. package/build/src/cli/accepted.js +73 -34
  92. package/build/src/cli/actor.js +3 -3
  93. package/build/src/cli/commands/akuma-invoke.d.ts +44 -19
  94. package/build/src/cli/commands/akuma-invoke.js +101 -52
  95. package/build/src/cli/commands/akuma.d.ts +27 -9
  96. package/build/src/cli/commands/akuma.js +151 -80
  97. package/build/src/cli/commands/contract.d.ts +155 -0
  98. package/build/src/cli/commands/contract.js +20 -0
  99. package/build/src/cli/commands/install.js +2 -0
  100. package/build/src/cli/commands/task-invoke.d.ts +19 -4
  101. package/build/src/cli/commands/task-invoke.js +65 -17
  102. package/build/src/cli/commands/task-query.d.ts +5 -0
  103. package/build/src/cli/commands/task-query.js +247 -0
  104. package/build/src/cli/commands/task.d.ts +3 -1
  105. package/build/src/cli/commands/task.js +52 -14
  106. package/build/src/cli/coordinates.d.ts +18 -0
  107. package/build/src/cli/coordinates.js +120 -0
  108. package/build/src/cli/draft.d.ts +8 -0
  109. package/build/src/cli/draft.js +96 -0
  110. package/build/src/cli/invoke.d.ts +9 -3
  111. package/build/src/cli/invoke.js +287 -108
  112. package/build/src/cli/main.js +54 -7
  113. package/build/src/cli/parse.d.ts +21 -117
  114. package/build/src/cli/parse.js +79 -84
  115. package/build/src/cli/render/akuma-tool-command.d.ts +6 -0
  116. package/build/src/cli/render/akuma-tool-command.js +144 -0
  117. package/build/src/cli/render/akuma-tool.d.ts +4 -2
  118. package/build/src/cli/render/akuma-tool.js +35 -5
  119. package/build/src/cli/render/akuma.d.ts +3 -1
  120. package/build/src/cli/render/akuma.js +247 -212
  121. package/build/src/cli/render/audit.d.ts +3 -0
  122. package/build/src/cli/render/audit.js +104 -0
  123. package/build/src/cli/render/catalog.d.ts +2 -0
  124. package/build/src/cli/render/catalog.js +16 -0
  125. package/build/src/cli/render/contract.d.ts +3 -3
  126. package/build/src/cli/render/contract.js +200 -54
  127. package/build/src/cli/render/kanshi.js +272 -129
  128. package/build/src/cli/render/receipt.d.ts +19 -0
  129. package/build/src/cli/render/receipt.js +106 -0
  130. package/build/src/cli/render/refusal.d.ts +16 -2
  131. package/build/src/cli/render/refusal.js +88 -3
  132. package/build/src/cli/render/region.d.ts +2 -0
  133. package/build/src/cli/render/region.js +17 -0
  134. package/build/src/cli/render/task.d.ts +2 -1
  135. package/build/src/cli/render/task.js +223 -60
  136. package/build/src/cli/render/terminal.d.ts +20 -0
  137. package/build/src/cli/render/terminal.js +121 -0
  138. package/build/src/cli/render/text.js +11 -3
  139. package/build/src/cli/result.d.ts +126 -19
  140. package/build/src/cli/usage.d.ts +1 -0
  141. package/build/src/cli/usage.js +3 -0
  142. package/build/src/contract-worktree.d.ts +44 -0
  143. package/build/src/contract-worktree.js +251 -0
  144. package/build/src/coordination/durable-file.d.ts +5 -1
  145. package/build/src/coordination/durable-file.js +57 -10
  146. package/build/src/coordination/sqlite-transaction-lock.d.ts +4 -0
  147. package/build/src/coordination/sqlite-transaction-lock.js +22 -5
  148. package/build/src/core/facts/codec.js +23 -4
  149. package/build/src/core/facts/eligibility.d.ts +1 -10
  150. package/build/src/core/facts/eligibility.js +0 -49
  151. package/build/src/core/facts/fold.js +1 -6
  152. package/build/src/core/facts/gate.d.ts +1 -0
  153. package/build/src/core/facts/gate.js +1 -0
  154. package/build/src/core/facts/observation.d.ts +3 -0
  155. package/build/src/core/facts/observation.js +12 -0
  156. package/build/src/core/facts/types.d.ts +10 -3
  157. package/build/src/core/subject.js +2 -2
  158. package/build/src/core/verbs/amend.d.ts +1 -1
  159. package/build/src/core/verbs/amend.js +3 -18
  160. package/build/src/core/verbs/bind.js +2 -16
  161. package/build/src/core/verbs/deliver.d.ts +1 -1
  162. package/build/src/core/verbs/deliver.js +14 -4
  163. package/build/src/core/verbs/placement.d.ts +1 -1
  164. package/build/src/core/verbs/placement.js +7 -9
  165. package/build/src/dispatch/index.d.ts +5 -3
  166. package/build/src/dispatch/index.js +30 -21
  167. package/build/src/duration.d.ts +10 -0
  168. package/build/src/duration.js +22 -0
  169. package/build/src/git/admission.d.ts +2 -2
  170. package/build/src/git/admission.js +33 -23
  171. package/build/src/git/hooks.d.ts +15 -0
  172. package/build/src/git/hooks.js +70 -18
  173. package/build/src/git/identity.d.ts +5 -1
  174. package/build/src/git/identity.js +23 -4
  175. package/build/src/git/integration.d.ts +48 -0
  176. package/build/src/git/integration.js +250 -0
  177. package/build/src/git/observe.d.ts +29 -15
  178. package/build/src/git/observe.js +185 -76
  179. package/build/src/git/read-observation.d.ts +41 -0
  180. package/build/src/git/read-observation.js +361 -0
  181. package/build/src/git/reconcile.d.ts +17 -5
  182. package/build/src/git/reconcile.js +305 -109
  183. package/build/src/git/repository.d.ts +41 -18
  184. package/build/src/git/repository.js +220 -114
  185. package/build/src/git/scratch.d.ts +17 -0
  186. package/build/src/git/scratch.js +73 -0
  187. package/build/src/git/target-placement.d.ts +92 -0
  188. package/build/src/git/target-placement.js +375 -0
  189. package/build/src/git/tender.d.ts +49 -0
  190. package/build/src/git/tender.js +63 -0
  191. package/build/src/git/terminal-seal.d.ts +24 -0
  192. package/build/src/git/terminal-seal.js +89 -0
  193. package/build/src/git/tree.d.ts +2 -2
  194. package/build/src/git/tree.js +4 -0
  195. package/build/src/git/workspace.d.ts +53 -0
  196. package/build/src/git/workspace.js +139 -0
  197. package/build/src/identity/selector.d.ts +6 -0
  198. package/build/src/identity/selector.js +19 -0
  199. package/build/src/index.d.ts +4 -2
  200. package/build/src/index.js +2 -1
  201. package/build/src/kanshi/index.d.ts +2 -2
  202. package/build/src/kanshi/index.js +1 -1
  203. package/build/src/kanshi/read.d.ts +7 -3
  204. package/build/src/kanshi/read.js +245 -45
  205. package/build/src/kanshi/report.d.ts +81 -6
  206. package/build/src/kanshi/select.d.ts +5 -0
  207. package/build/src/kanshi/select.js +33 -1
  208. package/build/src/library/address.d.ts +57 -0
  209. package/build/src/library/address.js +165 -0
  210. package/build/src/library/akuma-creation.d.ts +16 -7
  211. package/build/src/library/akuma-creation.js +96 -22
  212. package/build/src/library/audit.d.ts +18 -0
  213. package/build/src/library/audit.js +44 -0
  214. package/build/src/library/bind.d.ts +21 -0
  215. package/build/src/library/bind.js +56 -0
  216. package/build/src/library/catalog.d.ts +58 -0
  217. package/build/src/library/catalog.js +69 -0
  218. package/build/src/library/configuration.d.ts +6 -15
  219. package/build/src/library/configuration.js +14 -17
  220. package/build/src/library/contract.d.ts +23 -49
  221. package/build/src/library/contract.js +171 -185
  222. package/build/src/library/delivery.d.ts +24 -0
  223. package/build/src/library/delivery.js +21 -0
  224. package/build/src/library/fleet.d.ts +65 -0
  225. package/build/src/library/fleet.js +153 -0
  226. package/build/src/library/input.d.ts +3 -0
  227. package/build/src/library/input.js +19 -0
  228. package/build/src/library/keiyaku.d.ts +18 -3
  229. package/build/src/library/keiyaku.js +12 -1
  230. package/build/src/library/mutation.d.ts +39 -0
  231. package/build/src/library/mutation.js +37 -0
  232. package/build/src/library/reconcile.d.ts +29 -0
  233. package/build/src/library/reconcile.js +179 -0
  234. package/build/src/library/refusal.d.ts +21 -0
  235. package/build/src/library/refusal.js +29 -0
  236. package/build/src/library/region.d.ts +14 -1
  237. package/build/src/library/region.js +18 -4
  238. package/build/src/library/repo.d.ts +4 -13
  239. package/build/src/library/repo.js +14 -24
  240. package/build/src/protocol/attempt.d.ts +16 -3
  241. package/build/src/protocol/attempt.js +41 -10
  242. package/build/src/protocol/bind.d.ts +9 -2
  243. package/build/src/protocol/bind.js +48 -40
  244. package/build/src/protocol/intent.d.ts +57 -20
  245. package/build/src/protocol/intent.js +55 -79
  246. package/build/src/protocol/operations.d.ts +152 -43
  247. package/build/src/protocol/operations.js +482 -166
  248. package/build/src/protocol/outcome.d.ts +14 -3
  249. package/build/src/protocol/outcome.js +11 -3
  250. package/build/src/protocol/placement.d.ts +41 -0
  251. package/build/src/protocol/placement.js +147 -0
  252. package/build/src/protocol/read/documents.d.ts +2 -2
  253. package/build/src/protocol/read/documents.js +4 -4
  254. package/build/src/protocol/read/status.d.ts +18 -5
  255. package/build/src/protocol/read/status.js +73 -13
  256. package/build/src/protocol/run.d.ts +49 -7
  257. package/build/src/protocol/run.js +53 -25
  258. package/build/src/runtime/proc/line-rpc.d.ts +4 -8
  259. package/build/src/runtime/proc/line-rpc.js +22 -28
  260. package/build/src/runtime/proc/run.d.ts +23 -21
  261. package/build/src/runtime/proc/run.js +123 -89
  262. package/build/src/runtime/proc/stdio.d.ts +18 -0
  263. package/build/src/runtime/proc/stdio.js +58 -0
  264. package/build/src/settings.d.ts +7 -1
  265. package/build/src/settings.js +32 -17
  266. package/build/src/settlement/fence.d.ts +5 -0
  267. package/build/src/settlement/fence.js +14 -0
  268. package/build/src/settlement/holder.d.ts +35 -4
  269. package/build/src/settlement/holder.js +127 -28
  270. package/build/src/settlement/settle.d.ts +9 -1
  271. package/build/src/settlement/settle.js +74 -37
  272. package/build/src/task/board.d.ts +23 -5
  273. package/build/src/task/board.js +17 -10
  274. package/build/src/task/compose.d.ts +2 -2
  275. package/build/src/task/compose.js +18 -10
  276. package/build/src/task/context.d.ts +3 -3
  277. package/build/src/task/context.js +15 -20
  278. package/build/src/task/document.d.ts +2 -1
  279. package/build/src/task/document.js +15 -5
  280. package/build/src/task/index.d.ts +27 -15
  281. package/build/src/task/index.js +57 -35
  282. package/build/src/task/operations.d.ts +38 -13
  283. package/build/src/task/operations.js +80 -46
  284. package/build/src/task/query.d.ts +67 -0
  285. package/build/src/task/query.js +279 -0
  286. package/build/src/task/store.d.ts +5 -7
  287. package/build/src/task/store.js +43 -28
  288. package/build/src/verification/declaration.d.ts +1 -0
  289. package/build/src/verification/execution.d.ts +51 -0
  290. package/build/src/verification/execution.js +108 -0
  291. package/build/src/workspace-place.d.ts +41 -0
  292. package/build/src/workspace-place.js +386 -0
  293. package/build/src/world.d.ts +24 -0
  294. package/build/src/world.js +136 -0
  295. package/package.json +8 -4
  296. package/build/src/akuma/activity.d.ts +0 -66
  297. package/build/src/akuma/activity.js +0 -139
  298. package/build/src/akuma/providers/claude.js +0 -305
  299. package/build/src/context-root.d.ts +0 -4
  300. package/build/src/context-root.js +0 -15
  301. package/build/src/git/delivery.d.ts +0 -20
  302. package/build/src/git/delivery.js +0 -109
  303. package/build/src/git/verification.d.ts +0 -14
  304. package/build/src/git/verification.js +0 -56
  305. package/build/src/protocol/read/audit.d.ts +0 -25
  306. package/build/src/protocol/read/audit.js +0 -39
  307. package/build/src/verification/producer.d.ts +0 -23
  308. package/build/src/verification/producer.js +0 -54
@@ -1,185 +1,85 @@
1
- import { existsSync } from "node:fs";
2
- import { DatabaseSync } from "node:sqlite";
3
- import { HEART_SCHEMA, LEASH_SCHEMA, assertHeartSchemaVersion, assertLeashSchemaVersion, } from "./schema.js";
4
- import { activityFactSlice, answeredTurnFact, deathExists, deathFact, deleteAbandonedControls, deletePauseControl, endBodyFact, finishBodyFact, historyFacts, insertActivityFact, insertAnsweredTurnFact, insertBodyFact, insertDeathFact, insertFailedTurnFact, insertPauseControl, insertRequestFact, insertSealFact, insertSessionFact, insertSoulFact, insertStopControl, insertTellFact, latestBodyFact, latestSessionFact, latestTurnFact, lastAnsweredTurnFact, nonterminalRequestFacts, pauseExists, pendingTellFacts, pruneActivityFacts, requestFact, sealExists, sealFact, sessionFactForCoordinate, soulFact, stopExists, tellState, updateRequestRefused, updateRequestReserved, updateRequestServed, updateRequestVoided, updateTellState, voidRequestsByDeath, voidTellsByDeath, } from "./rows.js";
5
- export { life } from "./facts.js";
1
+ import { answeredTurnFact, endBodyFact, finishBodyFact, insertActivityFact, insertTurnEndFact, insertTurnStartFact, insertPauseControl, insertRequestFact, insertSessionFact, insertStopControl, killFactForBody, latestBodyFact, latestKillFact, latestSessionFact, lastAnsweredTurnFact, nonterminalRequestFacts, pauseFact, requestFact, sessionFactForCoordinate, stopFact, updateRequestRefused, updateRequestReserved, updateRequestServed, updateRequestVoided, } from "./rows.js";
2
+ import { insertTellDeliveryFact, insertTellFact, insertTellReceiptFact, pendingTellFacts, tellFact, tellIdsForFence, } from "./tells.js";
3
+ import { activityFactSlice, pruneActivityFacts, } from "./timeline.js";
4
+ import { isHeartAbsent, readSealFromLeash, transaction, withHeart } from "./storage.js";
5
+ import { soulFact } from "./soul.js";
6
+ export { HeartAbsentError, HeldAkumaLeash, initializeHeart, isHeartAbsent, probeLeash } from "./storage.js";
7
+ export { life, lifeAt } from "./facts.js";
6
8
  const ACTIVITY_LIMIT = 5_000;
7
- function isBusy(error) {
8
- const value = error;
9
- const message = String(value?.message ?? "").toLowerCase();
10
- return value?.code === "ERR_SQLITE_BUSY" || value?.code === "ERR_SQLITE_LOCKED"
11
- || value?.errcode === 5 || message.includes("database is locked") || message.includes("database is busy");
12
- }
13
- function openHeart(path, verify = true) {
14
- const database = new DatabaseSync(path);
15
- database.exec("PRAGMA foreign_keys=ON; PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL");
16
- if (verify)
17
- assertHeartSchemaVersion(database);
18
- return database;
19
- }
20
- function withHeart(paths, body) {
21
- const database = openHeart(paths.heart);
9
+ export async function readSoul(paths) {
22
10
  try {
23
- return body(database);
24
- }
25
- finally {
26
- database.close();
27
- }
28
- }
29
- function transaction(database, body) {
30
- database.exec("BEGIN IMMEDIATE");
31
- try {
32
- const result = body();
33
- database.exec("COMMIT");
34
- return result;
11
+ return await withHeart(paths, soulFact);
35
12
  }
36
13
  catch (error) {
37
- try {
38
- database.exec("ROLLBACK");
39
- }
40
- catch { /* preserve the adjudication failure */ }
14
+ if (isHeartAbsent(error))
15
+ return null;
41
16
  throw error;
42
17
  }
43
18
  }
44
- export function initializeHeart(paths) {
45
- const heart = openHeart(paths.heart, false);
46
- try {
47
- heart.exec(HEART_SCHEMA);
48
- assertHeartSchemaVersion(heart);
49
- }
50
- finally {
51
- heart.close();
52
- }
53
- const leash = new DatabaseSync(paths.leash);
19
+ export async function heartExists(paths) {
54
20
  try {
55
- leash.exec(LEASH_SCHEMA);
56
- assertLeashSchemaVersion(leash);
57
- }
58
- finally {
59
- leash.close();
60
- }
61
- }
62
- export class HeldAkumaLeash {
63
- database;
64
- closed = false;
65
- constructor(database) {
66
- this.database = database;
67
- }
68
- static try(paths) {
69
- const database = new DatabaseSync(paths.leash, { timeout: 0 });
70
- try {
71
- assertLeashSchemaVersion(database);
72
- database.exec("PRAGMA busy_timeout=0; BEGIN EXCLUSIVE");
73
- return new HeldAkumaLeash(database);
74
- }
75
- catch (error) {
76
- database.close();
77
- if (isBusy(error))
78
- return null;
79
- throw error;
80
- }
21
+ return await withHeart(paths, () => true);
81
22
  }
82
- birth(paths, soul, session) {
83
- if (sealExists(this.database))
84
- return "sealed";
85
- return withHeart(paths, (heart) => transaction(heart, () => {
86
- if (soulFact(heart) !== null)
87
- return "already-born";
88
- insertSoulFact(heart, soul);
89
- if (session !== undefined)
90
- insertSessionFact(heart, session);
91
- return "born";
92
- }));
93
- }
94
- sealIfUnborn(paths, input) {
95
- if (withHeart(paths, (heart) => soulFact(heart)) !== null)
96
- return "born";
97
- insertSealFact(this.database, input);
98
- this.database.exec("COMMIT");
99
- this.closed = true;
100
- this.database.close();
101
- return "sealed";
102
- }
103
- clearPause(paths) { withHeart(paths, deletePauseControl); }
104
- release() {
105
- if (this.closed)
106
- return;
107
- this.closed = true;
108
- try {
109
- this.database.exec("ROLLBACK");
110
- }
111
- finally {
112
- this.database.close();
113
- }
23
+ catch (error) {
24
+ if (isHeartAbsent(error))
25
+ return false;
26
+ throw error;
114
27
  }
115
28
  }
116
- export function probeLeash(paths) {
117
- const claim = HeldAkumaLeash.try(paths);
118
- if (claim === null)
119
- return "held";
120
- claim.release();
121
- return "free";
122
- }
123
- export function readSoul(paths) {
124
- if (!existsSync(paths.heart))
125
- return null;
126
- return withHeart(paths, soulFact);
127
- }
128
- export function heartExists(paths) { return existsSync(paths.heart); }
129
- export function readSeal(paths) {
130
- if (!existsSync(paths.leash))
131
- return null;
132
- const leash = new DatabaseSync(paths.leash);
29
+ export async function readSeal(paths) {
133
30
  try {
134
- assertLeashSchemaVersion(leash);
135
- return sealFact(leash);
31
+ return await readSealFromLeash(paths);
136
32
  }
137
- finally {
138
- leash.close();
33
+ catch (error) {
34
+ if (isHeartAbsent(error))
35
+ return null;
36
+ throw error;
139
37
  }
140
38
  }
141
- export function recordBody(paths, input) {
142
- return withHeart(paths, (heart) => {
143
- return { sequence: insertBodyFact(heart, input), collar: input.collar, leashTakenAt: input.leashTakenAt };
144
- });
145
- }
146
- export function recordSession(paths, input) {
147
- return withHeart(paths, (heart) => ({ sequence: insertSessionFact(heart, input), ...input }));
39
+ export async function recordSession(paths, input) {
40
+ return await withHeart(paths, (heart) => ({ sequence: insertSessionFact(heart, input), ...input }));
148
41
  }
149
- export function appendActivity(paths, input) {
150
- return withHeart(paths, (heart) => transaction(heart, () => {
42
+ export async function appendActivity(paths, input) {
43
+ return await withHeart(paths, (heart) => transaction(heart, () => {
151
44
  const sequence = insertActivityFact(heart, input);
152
45
  pruneActivityFacts(heart, ACTIVITY_LIMIT);
153
46
  return sequence;
154
47
  }));
155
48
  }
156
- export function activitySlice(paths, input = {}) {
49
+ export async function activitySlice(paths, input = {}) {
157
50
  const limit = input.limit ?? ACTIVITY_LIMIT;
158
- return withHeart(paths, (heart) => activityFactSlice(heart, { ...input, limit }));
51
+ return await withHeart(paths, (heart) => activityFactSlice(heart, { ...input, limit }));
159
52
  }
160
- export function recordTell(paths, tell) {
161
- return withHeart(paths, (heart) => transaction(heart, () => {
162
- if (deathExists(heart))
163
- return "dead";
164
- insertTellFact(heart, tell);
165
- return "recorded";
53
+ export async function recordTell(paths, tell) {
54
+ return await withHeart(paths, (heart) => transaction(heart, () => {
55
+ if (soulFact(heart) === null)
56
+ return { kind: "not-born" };
57
+ const sequence = insertTellFact(heart, tell);
58
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
59
+ return { kind: "recorded", tell: { sequence, ...tell, state: "pending", deliveries: [] } };
166
60
  }));
167
61
  }
168
- const TELL_ORDER = {
169
- recorded: 0,
170
- delivered: 1,
171
- seen: 2,
172
- consumed: 3,
173
- "voided-by-death": 3,
174
- };
175
- export function advanceTell(paths, id, state) {
176
- withHeart(paths, (heart) => transaction(heart, () => {
177
- const current = tellState(heart, id);
178
- if (current === null)
179
- throw new Error(`unknown tell ${id}`);
180
- if (TELL_ORDER[state] < TELL_ORDER[current] || current === "voided-by-death")
181
- return;
182
- updateTellState(heart, id, state);
62
+ export async function recordTellDeliveries(paths, inputs) {
63
+ await withHeart(paths, (heart) => transaction(heart, () => {
64
+ for (const input of inputs) {
65
+ const current = tellFact(heart, input.tellId);
66
+ if (current === null)
67
+ throw new Error(`unknown tell ${input.tellId}`);
68
+ }
69
+ for (const input of inputs)
70
+ insertTellDeliveryFact(heart, input);
71
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
72
+ }));
73
+ }
74
+ export async function recordTellReceipt(paths, input) {
75
+ await withHeart(paths, (heart) => transaction(heart, () => {
76
+ const tellIds = input.evidence === "exact"
77
+ ? [input.tellId]
78
+ : tellIdsForFence(heart, input.turnSequence, input.fence);
79
+ if (tellIds.length === 0)
80
+ throw new Error("tell receipt has no delivery mapping");
81
+ insertTellReceiptFact(heart, input);
82
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
183
83
  }));
184
84
  }
185
85
  function sameRequestInput(fact, input) {
@@ -190,8 +90,8 @@ function sameRequestInput(fact, input) {
190
90
  && fact.world === input.world
191
91
  && JSON.stringify(fact.recipe) === JSON.stringify(input.recipe);
192
92
  }
193
- export function admitRequest(paths, input) {
194
- return withHeart(paths, (heart) => transaction(heart, () => {
93
+ export async function admitRequest(paths, input) {
94
+ return await withHeart(paths, (heart) => transaction(heart, () => {
195
95
  insertRequestFact(heart, input);
196
96
  const fact = requestFact(heart, input.id);
197
97
  if (fact === null)
@@ -201,8 +101,8 @@ export function admitRequest(paths, input) {
201
101
  return fact;
202
102
  }));
203
103
  }
204
- export function reserveRequest(paths, id, child) {
205
- return withHeart(paths, (heart) => transaction(heart, () => {
104
+ export async function reserveRequest(paths, id, child) {
105
+ return await withHeart(paths, (heart) => transaction(heart, () => {
206
106
  const before = requestFact(heart, id);
207
107
  if (before === null)
208
108
  throw new Error(`unknown Akuma request ${id}`);
@@ -214,8 +114,8 @@ export function reserveRequest(paths, id, child) {
214
114
  return requestFact(heart, id);
215
115
  }));
216
116
  }
217
- export function serveRequest(paths, id, child) {
218
- return withHeart(paths, (heart) => transaction(heart, () => {
117
+ export async function serveRequest(paths, id, child) {
118
+ return await withHeart(paths, (heart) => transaction(heart, () => {
219
119
  const before = requestFact(heart, id);
220
120
  if (before === null)
221
121
  throw new Error(`unknown Akuma request ${id}`);
@@ -227,8 +127,8 @@ export function serveRequest(paths, id, child) {
227
127
  return requestFact(heart, id);
228
128
  }));
229
129
  }
230
- export function refuseRequest(paths, id, diagnostic) {
231
- return withHeart(paths, (heart) => transaction(heart, () => {
130
+ export async function refuseRequest(paths, id, diagnostic) {
131
+ return await withHeart(paths, (heart) => transaction(heart, () => {
232
132
  const before = requestFact(heart, id);
233
133
  if (before === null)
234
134
  throw new Error(`unknown Akuma request ${id}`);
@@ -240,8 +140,8 @@ export function refuseRequest(paths, id, diagnostic) {
240
140
  return requestFact(heart, id);
241
141
  }));
242
142
  }
243
- export function voidRequest(paths, id, evidence) {
244
- return withHeart(paths, (heart) => transaction(heart, () => {
143
+ export async function voidRequest(paths, id, evidence) {
144
+ return await withHeart(paths, (heart) => transaction(heart, () => {
245
145
  const before = requestFact(heart, id);
246
146
  if (before === null)
247
147
  throw new Error(`unknown Akuma request ${id}`);
@@ -253,65 +153,78 @@ export function voidRequest(paths, id, evidence) {
253
153
  return requestFact(heart, id);
254
154
  }));
255
155
  }
256
- export function readRequest(paths, id) {
257
- return withHeart(paths, (heart) => requestFact(heart, id));
258
- }
259
- export function readNonterminalRequests(paths) {
260
- return withHeart(paths, nonterminalRequestFacts);
261
- }
262
- export function requestStop(paths, at) {
263
- return withHeart(paths, (heart) => transaction(heart, () => {
264
- if (deathExists(heart))
265
- return "dead";
266
- insertStopControl(heart, at);
267
- return "requested";
156
+ export async function readRequest(paths, id) {
157
+ return await withHeart(paths, (heart) => requestFact(heart, id));
158
+ }
159
+ export async function readNonterminalRequests(paths) {
160
+ return await withHeart(paths, nonterminalRequestFacts);
161
+ }
162
+ export async function readKill(paths, bodySequence) {
163
+ return await withHeart(paths, (heart) => killFactForBody(heart, bodySequence));
164
+ }
165
+ export async function requestStop(paths, at) {
166
+ return await withHeart(paths, (heart) => transaction(heart, () => {
167
+ const body = latestBodyFact(heart);
168
+ if (body === null)
169
+ throw new Error("Akuma has no Body to kill");
170
+ if (latestKillFact(heart)?.bodySequence === body.sequence)
171
+ return { kind: "already-killed", body };
172
+ if (body.end !== undefined)
173
+ return { kind: "already-stopped", body };
174
+ const existing = stopFact(heart);
175
+ if (existing !== null && existing.bodySequence !== body.sequence) {
176
+ throw new Error("Akuma stop target is not the latest Body");
177
+ }
178
+ insertStopControl(heart, body.sequence, at);
179
+ return { kind: "requested", body };
268
180
  }));
269
181
  }
270
- export function requestPause(paths, at) {
271
- return withHeart(paths, (heart) => transaction(heart, () => {
272
- if (deathExists(heart))
273
- return "dead";
182
+ export async function requestPause(paths, at) {
183
+ return await withHeart(paths, (heart) => transaction(heart, () => {
184
+ if (soulFact(heart) === null)
185
+ return { kind: "not-born" };
186
+ const body = latestBodyFact(heart);
187
+ if (body === null)
188
+ throw new Error("Akuma has no Body to interrupt");
274
189
  insertPauseControl(heart, at);
275
- return "requested";
190
+ return { kind: "requested", body };
276
191
  }));
277
192
  }
278
- export function stopRequested(paths) { return withHeart(paths, stopExists); }
279
- export function pauseRequested(paths) { return withHeart(paths, pauseExists); }
280
- export function clearAbandonedControl(paths) {
281
- // A control row without settlement belongs to a vanished caller; the next leash holder revokes it.
282
- withHeart(paths, deleteAbandonedControls);
193
+ export async function stopRequested(paths, bodySequence) {
194
+ return await withHeart(paths, (heart) => {
195
+ const target = stopFact(heart);
196
+ return target !== null && (bodySequence === undefined || target.bodySequence === bodySequence);
197
+ });
283
198
  }
284
- export function recordDeath(paths, death) {
285
- return withHeart(paths, (heart) => transaction(heart, () => {
286
- if (deathExists(heart))
287
- return "already-dead";
288
- insertDeathFact(heart, death);
289
- voidTellsByDeath(heart);
290
- voidRequestsByDeath(heart, `death:${death.evidence}`);
291
- return "recorded";
292
- }));
199
+ export async function pauseRequested(paths, bodySequence) {
200
+ return await withHeart(paths, (heart) => {
201
+ const target = pauseFact(heart);
202
+ return target !== null && (bodySequence === undefined || target.bodySequence === bodySequence);
203
+ });
293
204
  }
294
- export function breakBody(paths, input) {
295
- withHeart(paths, (heart) => endBodyFact(heart, input));
205
+ export async function breakBody(paths, input) {
206
+ await withHeart(paths, (heart) => endBodyFact(heart, input));
296
207
  }
297
- export function recordTurn(paths, input) {
298
- return withHeart(paths, (heart) => {
299
- const sequence = input.outcome.kind === "answered"
300
- ? insertAnsweredTurnFact(heart, {
301
- bodySequence: input.bodySequence,
302
- outcome: input.outcome,
303
- completedAt: input.completedAt,
304
- })
305
- : insertFailedTurnFact(heart, {
306
- bodySequence: input.bodySequence,
307
- outcome: input.outcome,
308
- completedAt: input.completedAt,
309
- });
310
- return { sequence, ...input };
311
- });
208
+ export async function beginTurn(paths, input) {
209
+ return await withHeart(paths, (heart) => transaction(heart, () => {
210
+ const fact = insertTurnStartFact(heart, input);
211
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
212
+ return fact;
213
+ }));
214
+ }
215
+ export async function endTurn(paths, input) {
216
+ return await withHeart(paths, (heart) => transaction(heart, () => {
217
+ const fact = insertTurnEndFact(heart, { kind: "turn-end", ...input });
218
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
219
+ return fact;
220
+ }));
312
221
  }
313
- export function finishBodyIfIdle(paths, input) {
314
- return withHeart(paths, (heart) => transaction(heart, () => {
222
+ export async function finishBodyIfIdle(paths, input) {
223
+ return await withHeart(paths, (heart) => transaction(heart, () => {
224
+ if (stopFact(heart)?.bodySequence === input.sequence || pauseFact(heart)?.bodySequence === input.sequence) {
225
+ endBodyFact(heart, { ...input, end: "put-down" });
226
+ return { kind: "controlled" };
227
+ }
315
228
  const pending = pendingTellFacts(heart);
316
229
  if (pending.length > 0)
317
230
  return { kind: "pending", tells: pending.map((tell) => tell.id) };
@@ -319,43 +232,50 @@ export function finishBodyIfIdle(paths, input) {
319
232
  return { kind: "finished" };
320
233
  }));
321
234
  }
322
- export function readHeart(paths) {
323
- if (!existsSync(paths.heart)) {
324
- return { soul: null, latestBody: null, latestSession: null, pending: [], death: null };
235
+ export async function readHeart(paths) {
236
+ try {
237
+ return await withHeart(paths, (heart) => ({
238
+ soul: soulFact(heart),
239
+ latestBody: latestBodyFact(heart),
240
+ latestSession: latestSessionFact(heart),
241
+ pending: pendingTellFacts(heart),
242
+ latestKill: latestKillFact(heart),
243
+ stop: stopFact(heart),
244
+ pause: pauseFact(heart),
245
+ }));
246
+ }
247
+ catch (error) {
248
+ if (isHeartAbsent(error)) {
249
+ return { soul: null, latestBody: null, latestSession: null, pending: [], latestKill: null, stop: null, pause: null };
250
+ }
251
+ throw error;
325
252
  }
326
- return withHeart(paths, (heart) => ({
327
- soul: soulFact(heart),
328
- latestBody: latestBodyFact(heart),
329
- latestSession: latestSessionFact(heart),
330
- pending: pendingTellFacts(heart),
331
- death: deathFact(heart),
332
- }));
333
- }
334
- export function readTurns(paths) {
335
- return withHeart(paths, historyFacts);
336
- }
337
- export function readLastAnsweredTurn(paths) {
338
- return withHeart(paths, lastAnsweredTurnFact);
339
253
  }
340
- export function readCurrentTurn(paths) {
341
- return withHeart(paths, latestTurnFact);
254
+ export async function readLastAnsweredTurn(paths) {
255
+ return await withHeart(paths, lastAnsweredTurnFact);
342
256
  }
343
- export function readForkPoint(paths, historyId) {
344
- if (!existsSync(paths.heart))
345
- return null;
346
- return withHeart(paths, (heart) => {
347
- const turn = answeredTurnFact(heart, historyId);
348
- if (turn?.outcome.kind !== "answered")
257
+ export async function readForkPoint(paths, historyId) {
258
+ try {
259
+ return await withHeart(paths, (heart) => {
260
+ const turn = answeredTurnFact(heart, historyId);
261
+ const outcome = turn?.end?.outcome;
262
+ if (outcome?.kind !== "answered" || outcome.historyId === undefined)
263
+ return null;
264
+ const recipe = sessionFactForCoordinate(heart, outcome.session);
265
+ if (recipe === null)
266
+ throw new Error(`Akuma fork point ${historyId} has no session recipe`);
267
+ return {
268
+ historyId: outcome.historyId,
269
+ session: outcome.session,
270
+ provider: recipe.provider,
271
+ cwd: recipe.cwd,
272
+ options: recipe.options,
273
+ };
274
+ });
275
+ }
276
+ catch (error) {
277
+ if (isHeartAbsent(error))
349
278
  return null;
350
- const recipe = sessionFactForCoordinate(heart, turn.outcome.session);
351
- if (recipe === null)
352
- throw new Error(`Akuma fork point ${historyId} has no session recipe`);
353
- return {
354
- historyId: turn.outcome.historyId,
355
- session: turn.outcome.session,
356
- provider: recipe.provider,
357
- cwd: recipe.cwd,
358
- options: recipe.options,
359
- };
360
- });
279
+ throw error;
280
+ }
361
281
  }