@astrosheep/keiyaku 4.5.11 → 4.5.13

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 (272) hide show
  1. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-workflow/SKILL.md +135 -377
  2. package/build/integrations/pi/keiyaku.ts +11 -116
  3. package/build/src/akuma/akuma-errors.d.ts +1 -0
  4. package/build/src/akuma/akuma-errors.js +1 -0
  5. package/build/src/akuma/akuma-handle.d.ts +1 -0
  6. package/build/src/akuma/akuma-handle.js +6 -7
  7. package/build/src/akuma/akuma-product.d.ts +6 -8
  8. package/build/src/akuma/akuma-product.js +141 -57
  9. package/build/src/akuma/akuma.d.ts +5 -7
  10. package/build/src/akuma/akuma.js +1 -22
  11. package/build/src/akuma/archetype.d.ts +6 -5
  12. package/build/src/akuma/archetype.js +137 -38
  13. package/build/src/akuma/body.d.ts +5 -5
  14. package/build/src/akuma/body.js +223 -63
  15. package/build/src/akuma/call-input.d.ts +4 -0
  16. package/build/src/akuma/call-input.js +21 -0
  17. package/build/src/akuma/call-request.d.ts +5 -8
  18. package/build/src/akuma/call-request.js +28 -73
  19. package/build/src/akuma/fleet-execution.d.ts +26 -0
  20. package/build/src/akuma/fleet-execution.js +109 -0
  21. package/build/src/{library/fleet-result.d.ts → akuma/fleet-observation.d.ts} +13 -57
  22. package/build/src/{library/fleet-result.js → akuma/fleet-observation.js} +4 -25
  23. package/build/src/akuma/fleet-request.d.ts +122 -0
  24. package/build/src/akuma/fleet-request.js +168 -0
  25. package/build/src/akuma/heart/facts.d.ts +23 -0
  26. package/build/src/akuma/heart/facts.js +7 -0
  27. package/build/src/akuma/heart/index.d.ts +43 -4
  28. package/build/src/akuma/heart/index.js +107 -7
  29. package/build/src/akuma/heart/request-authority.d.ts +2 -0
  30. package/build/src/akuma/heart/request-authority.js +28 -3
  31. package/build/src/akuma/heart/request-rows.d.ts +2 -0
  32. package/build/src/akuma/heart/request-rows.js +24 -5
  33. package/build/src/akuma/heart/rows.d.ts +4 -1
  34. package/build/src/akuma/heart/rows.js +46 -19
  35. package/build/src/akuma/heart/schema.d.ts +1 -1
  36. package/build/src/akuma/heart/schema.js +28 -6
  37. package/build/src/akuma/heart/storage.d.ts +1 -1
  38. package/build/src/akuma/heart/tells.d.ts +29 -0
  39. package/build/src/akuma/heart/tells.js +136 -5
  40. package/build/src/akuma/heart/timeline.js +9 -3
  41. package/build/src/akuma/projection-read.js +36 -3
  42. package/build/src/akuma/projection.js +3 -4
  43. package/build/src/akuma/provider.d.ts +4 -0
  44. package/build/src/akuma/provider.js +56 -3
  45. package/build/src/akuma/providers/claude/index.js +3 -0
  46. package/build/src/akuma/providers/codex-app-server/index.js +14 -12
  47. package/build/src/akuma/providers/pi/index.js +7 -1
  48. package/build/src/akuma/publication.js +65 -49
  49. package/build/src/akuma/request-lifecycle.d.ts +5 -4
  50. package/build/src/akuma/request-lifecycle.js +29 -16
  51. package/build/src/akuma/request-rendezvous.d.ts +15 -12
  52. package/build/src/akuma/request-rendezvous.js +80 -51
  53. package/build/src/akuma/request-serve.d.ts +1 -2
  54. package/build/src/akuma/request-serve.js +94 -39
  55. package/build/src/akuma/request-wire.d.ts +57 -26
  56. package/build/src/akuma/request-wire.js +64 -25
  57. package/build/src/akuma/turn-drive.d.ts +5 -4
  58. package/build/src/akuma/turn-drive.js +27 -11
  59. package/build/src/akuma-body.d.ts +4 -6
  60. package/build/src/akuma-body.js +19 -17
  61. package/build/src/bounded-list.d.ts +8 -0
  62. package/build/src/bounded-list.js +14 -0
  63. package/build/src/cli/accepted.d.ts +1 -1
  64. package/build/src/cli/accepted.js +1 -0
  65. package/build/src/cli/commands/akuma-invoke.js +1 -25
  66. package/build/src/cli/commands/contract-help.d.ts +5 -2
  67. package/build/src/cli/commands/contract-help.js +62 -12
  68. package/build/src/cli/commands/contract-invoke.d.ts +5 -0
  69. package/build/src/cli/commands/contract-invoke.js +26 -1
  70. package/build/src/cli/commands/contract.js +50 -18
  71. package/build/src/cli/commands/task-invoke.js +10 -2
  72. package/build/src/cli/commands/task-query.js +3 -4
  73. package/build/src/cli/coordinates.d.ts +13 -0
  74. package/build/src/cli/coordinates.js +38 -24
  75. package/build/src/cli/draft.d.ts +1 -1
  76. package/build/src/cli/invoke.js +14 -1
  77. package/build/src/cli/render/akuma-activity.d.ts +0 -1
  78. package/build/src/cli/render/akuma-activity.js +5 -38
  79. package/build/src/cli/render/akuma.js +4 -1
  80. package/build/src/cli/render/audit.js +4 -1
  81. package/build/src/cli/render/catalog.js +13 -39
  82. package/build/src/cli/render/contract.js +4 -14
  83. package/build/src/cli/render/kanshi-akuma.js +3 -8
  84. package/build/src/cli/render/kanshi.js +8 -34
  85. package/build/src/cli/render/nuke.js +7 -3
  86. package/build/src/cli/render/receipt.d.ts +1 -0
  87. package/build/src/cli/render/receipt.js +8 -0
  88. package/build/src/cli/render/refusal.js +4 -3
  89. package/build/src/cli/render/task.js +4 -37
  90. package/build/src/cli/render/terminal.d.ts +2 -6
  91. package/build/src/cli/render/terminal.js +4 -9
  92. package/build/src/cli/result.d.ts +2 -1
  93. package/build/src/contract-guidance.js +13 -3
  94. package/build/src/contract-worktree.d.ts +1 -0
  95. package/build/src/contract-worktree.js +26 -2
  96. package/build/src/core/facts/codec.d.ts +2 -1
  97. package/build/src/core/facts/codec.js +3 -0
  98. package/build/src/core/facts/gate.d.ts +2 -1
  99. package/build/src/core/facts/gate.js +63 -0
  100. package/build/src/core/verbs/abandon.d.ts +2 -1
  101. package/build/src/core/verbs/abandon.js +16 -0
  102. package/build/src/core/verbs/amend.d.ts +2 -1
  103. package/build/src/core/verbs/amend.js +20 -0
  104. package/build/src/core/verbs/arc.d.ts +2 -1
  105. package/build/src/core/verbs/arc.js +16 -0
  106. package/build/src/core/verbs/attestation.d.ts +2 -1
  107. package/build/src/core/verbs/attestation.js +16 -0
  108. package/build/src/core/verbs/bind.d.ts +2 -1
  109. package/build/src/core/verbs/bind.js +16 -0
  110. package/build/src/core/verbs/deliver.d.ts +2 -1
  111. package/build/src/core/verbs/deliver.js +16 -0
  112. package/build/src/core/verbs/placement.d.ts +2 -1
  113. package/build/src/core/verbs/placement.js +58 -1
  114. package/build/src/dispatch/association.d.ts +13 -0
  115. package/build/src/dispatch/association.js +20 -0
  116. package/build/src/dispatch/index.d.ts +6 -0
  117. package/build/src/dispatch/index.js +27 -3
  118. package/build/src/git/hooks.d.ts +9 -1
  119. package/build/src/git/hooks.js +79 -1
  120. package/build/src/git/nuke.d.ts +2 -1
  121. package/build/src/git/nuke.js +6 -11
  122. package/build/src/git/private-state-seat.d.ts +26 -1
  123. package/build/src/git/private-state-seat.js +78 -13
  124. package/build/src/git/process.d.ts +2 -0
  125. package/build/src/git/reconcile.d.ts +1 -2
  126. package/build/src/git/reconcile.js +0 -6
  127. package/build/src/git/repository.d.ts +0 -2
  128. package/build/src/git/repository.js +27 -25
  129. package/build/src/git/result-codec.d.ts +29 -0
  130. package/build/src/git/result-codec.js +306 -0
  131. package/build/src/git/target-placement.js +2 -8
  132. package/build/src/git/workspace.d.ts +2 -0
  133. package/build/src/git/workspace.js +4 -0
  134. package/build/src/index.d.ts +1 -0
  135. package/build/src/kanshi/fleet.js +1 -1
  136. package/build/src/kanshi/read.js +55 -28
  137. package/build/src/kanshi/report.d.ts +4 -3
  138. package/build/src/kanshi/select.js +14 -1
  139. package/build/src/library/address.d.ts +1 -0
  140. package/build/src/library/address.js +4 -7
  141. package/build/src/library/akuma-creation.d.ts +5 -3
  142. package/build/src/library/akuma-creation.js +90 -33
  143. package/build/src/library/audit.js +2 -1
  144. package/build/src/library/catalog.d.ts +10 -2
  145. package/build/src/library/catalog.js +65 -25
  146. package/build/src/library/composition.d.ts +1 -0
  147. package/build/src/library/composition.js +1 -0
  148. package/build/src/library/configuration.d.ts +4 -21
  149. package/build/src/library/configuration.js +2 -158
  150. package/build/src/library/continuation.d.ts +20 -1
  151. package/build/src/library/continuation.js +36 -0
  152. package/build/src/library/contract-bind.d.ts +1 -0
  153. package/build/src/library/contract-bind.js +16 -5
  154. package/build/src/library/contract-forwarding.d.ts +2 -2
  155. package/build/src/library/contract-forwarding.js +18 -4
  156. package/build/src/library/contract-handle.js +4 -3
  157. package/build/src/library/contract-operations.d.ts +5 -9
  158. package/build/src/library/contract-operations.js +34 -47
  159. package/build/src/library/contract.d.ts +11 -2
  160. package/build/src/library/contract.js +21 -1
  161. package/build/src/library/delivery.d.ts +4 -1
  162. package/build/src/library/delivery.js +47 -0
  163. package/build/src/library/fleet.d.ts +4 -136
  164. package/build/src/library/fleet.js +31 -315
  165. package/build/src/library/keiyaku.d.ts +7 -3
  166. package/build/src/library/keiyaku.js +1 -0
  167. package/build/src/library/mutation.d.ts +431 -3
  168. package/build/src/library/mutation.js +192 -19
  169. package/build/src/library/nuke.d.ts +7 -1
  170. package/build/src/library/nuke.js +18 -4
  171. package/build/src/library/reconcile.d.ts +14 -0
  172. package/build/src/library/reconcile.js +22 -8
  173. package/build/src/library/refusal.d.ts +9 -1
  174. package/build/src/library/refusal.js +76 -0
  175. package/build/src/library/region.d.ts +0 -8
  176. package/build/src/library/region.js +1 -29
  177. package/build/src/library/result-codec.d.ts +2 -0
  178. package/build/src/library/result-codec.js +12 -0
  179. package/build/src/markdown/lex.js +9 -2
  180. package/build/src/markdown/types.d.ts +2 -1
  181. package/build/src/markdown/types.js +3 -1
  182. package/build/src/plugin/akuma-signals.d.ts +12 -0
  183. package/build/src/plugin/akuma-signals.js +14 -0
  184. package/build/src/plugin/public.d.ts +51 -0
  185. package/build/src/plugin/public.js +1 -0
  186. package/build/src/plugin/runtime.d.ts +14 -0
  187. package/build/src/plugin/runtime.js +357 -0
  188. package/build/src/protocol/amend.js +3 -3
  189. package/build/src/protocol/attempt.d.ts +2 -1
  190. package/build/src/protocol/attempt.js +1 -1
  191. package/build/src/protocol/audit.d.ts +1 -1
  192. package/build/src/protocol/audit.js +1 -1
  193. package/build/src/protocol/bind.d.ts +3 -1
  194. package/build/src/protocol/bind.js +26 -0
  195. package/build/src/protocol/completion.js +0 -1
  196. package/build/src/protocol/deliver.d.ts +1 -1
  197. package/build/src/protocol/deliver.js +136 -57
  198. package/build/src/protocol/intent.d.ts +0 -1
  199. package/build/src/protocol/intent.js +0 -1
  200. package/build/src/protocol/operations.d.ts +9 -2
  201. package/build/src/protocol/operations.js +16 -1
  202. package/build/src/protocol/outcome.d.ts +3 -0
  203. package/build/src/protocol/outcome.js +17 -1
  204. package/build/src/protocol/placement.js +10 -3
  205. package/build/src/protocol/read/status.d.ts +8 -0
  206. package/build/src/protocol/read/status.js +49 -0
  207. package/build/src/protocol/reintegrate.js +10 -3
  208. package/build/src/protocol/result-codec.d.ts +22 -0
  209. package/build/src/protocol/result-codec.js +442 -0
  210. package/build/src/protocol/review.d.ts +1 -1
  211. package/build/src/protocol/review.js +81 -46
  212. package/build/src/protocol/run.d.ts +22 -2
  213. package/build/src/protocol/run.js +119 -29
  214. package/build/src/runtime/proc/lifecycle.js +1 -1
  215. package/build/src/runtime/proc/line-rpc.d.ts +7 -1
  216. package/build/src/runtime/proc/line-rpc.js +49 -9
  217. package/build/src/runtime/proc/run.d.ts +0 -1
  218. package/build/src/runtime/proc/run.js +1 -11
  219. package/build/src/runtime/proc/termination.d.ts +1 -0
  220. package/build/src/runtime/proc/termination.js +63 -18
  221. package/build/src/runtime/proc/windows-launch.exe +0 -0
  222. package/build/src/settings.d.ts +10 -0
  223. package/build/src/settings.js +88 -2
  224. package/build/src/settlement/result-codec.d.ts +2 -0
  225. package/build/src/settlement/result-codec.js +56 -0
  226. package/build/src/settlement/settle.d.ts +5 -12
  227. package/build/src/settlement/settle.js +60 -55
  228. package/build/src/task/board.d.ts +3 -0
  229. package/build/src/task/board.js +22 -4
  230. package/build/src/task/catalog.d.ts +13 -2
  231. package/build/src/task/catalog.js +59 -5
  232. package/build/src/task/compose.d.ts +4 -2
  233. package/build/src/task/compose.js +56 -30
  234. package/build/src/task/created-observation.d.ts +39 -0
  235. package/build/src/task/created-observation.js +26 -0
  236. package/build/src/task/identity.d.ts +6 -0
  237. package/build/src/task/identity.js +48 -7
  238. package/build/src/task/index.d.ts +4 -3
  239. package/build/src/task/index.js +19 -16
  240. package/build/src/task/input.d.ts +3 -1
  241. package/build/src/task/input.js +7 -4
  242. package/build/src/task/lifecycle-operations.d.ts +6 -0
  243. package/build/src/task/lifecycle-operations.js +116 -0
  244. package/build/src/task/mutation-result.d.ts +762 -20
  245. package/build/src/task/mutation-result.js +34 -9
  246. package/build/src/task/mutation.d.ts +4 -9
  247. package/build/src/task/mutation.js +17 -52
  248. package/build/src/task/operation-types.d.ts +114 -0
  249. package/build/src/task/operation-types.js +1 -0
  250. package/build/src/task/operations.d.ts +6 -113
  251. package/build/src/task/operations.js +75 -117
  252. package/build/src/task/query.d.ts +8 -11
  253. package/build/src/task/query.js +7 -14
  254. package/build/src/task/store.d.ts +11 -1
  255. package/build/src/task/store.js +169 -55
  256. package/build/src/task/view.d.ts +4 -0
  257. package/build/src/task/view.js +7 -0
  258. package/build/src/verification/declaration.d.ts +1 -0
  259. package/build/src/verification/declaration.js +18 -1
  260. package/build/src/verification/execution.d.ts +0 -1
  261. package/build/src/verification/execution.js +5 -5
  262. package/build/src/world.d.ts +0 -2
  263. package/build/src/world.js +3 -7
  264. package/package.json +17 -5
  265. package/build/src/cli/square-edge.d.ts +0 -7
  266. package/build/src/cli/square-edge.js +0 -159
  267. package/build/src/git/ref-migration.d.ts +0 -22
  268. package/build/src/git/ref-migration.js +0 -55
  269. package/build/src/library/contract-forwarding-reconciliation-result.d.ts +0 -102
  270. package/build/src/library/contract-forwarding-reconciliation-result.js +0 -92
  271. package/build/src/library/contract-forwarding-result.d.ts +0 -4270
  272. package/build/src/library/contract-forwarding-result.js +0 -521
@@ -58,15 +58,23 @@ export type TurnOutcome = Readonly<{
58
58
  historyId?: string;
59
59
  session: ResumeCoordinate;
60
60
  answer: string;
61
+ answerJson?: string;
61
62
  }> | Readonly<{
62
63
  kind: "failed";
63
64
  diagnostic: string;
65
+ }> | Readonly<{
66
+ kind: "invalid-output";
67
+ diagnostic: string;
68
+ answer: string;
69
+ historyId?: string;
70
+ session?: ResumeCoordinate;
64
71
  }>;
65
72
  export type TurnStartFact = Readonly<{
66
73
  kind: "turn-start";
67
74
  sequence: number;
68
75
  bodySequence: number;
69
76
  startedAt: string;
77
+ schemaJson?: string;
70
78
  }>;
71
79
  export type TurnEndFact = Readonly<{
72
80
  kind: "turn-end";
@@ -91,14 +99,20 @@ export type TellDelivery = Readonly<{
91
99
  receipt?: "unavailable" | "required";
92
100
  deliveredAt: string;
93
101
  }>;
102
+ export type TellBinding = Readonly<{
103
+ turnSequence: number;
104
+ boundAt: string;
105
+ }>;
94
106
  export type TellFact = Readonly<{
95
107
  kind: "tell";
96
108
  sequence: number;
97
109
  id: string;
98
110
  body: string;
111
+ schemaJson?: string;
99
112
  recordedAt: string;
100
113
  state: "pending" | "told";
101
114
  deliveries: readonly TellDelivery[];
115
+ binding?: TellBinding;
102
116
  }>;
103
117
  export type TellRow = Readonly<{
104
118
  kind: "tell";
@@ -110,6 +124,10 @@ export type TellRow = Readonly<{
110
124
  deliveries: readonly TellDelivery[];
111
125
  }>;
112
126
  export declare function projectTell(fact: TellFact): TellRow;
127
+ export declare class AkumaBusyError extends Error {
128
+ readonly kind = "akuma-busy";
129
+ constructor();
130
+ }
113
131
  export type TellDeliveryInput = Readonly<{
114
132
  tellId: string;
115
133
  turnSequence: number;
@@ -146,6 +164,8 @@ export type RequestFact = (AdmittedRequest & Readonly<{
146
164
  }>) | (AdmittedRequest & Readonly<{
147
165
  state: "reserved";
148
166
  child: AkuId;
167
+ }>) | (AdmittedRequest & Readonly<{
168
+ state: "begun";
149
169
  }>) | (AdmittedRequest & Readonly<{
150
170
  state: "served";
151
171
  child: AkuId;
@@ -155,6 +175,9 @@ export type RequestFact = (AdmittedRequest & Readonly<{
155
175
  }>) | (AdmittedRequest & Readonly<{
156
176
  state: "refused";
157
177
  diagnostic: string;
178
+ }>) | (AdmittedRequest & Readonly<{
179
+ state: "unproven";
180
+ evidence: string;
158
181
  }>) | (AdmittedRequest & Readonly<{
159
182
  state: "voided";
160
183
  evidence: string;
@@ -9,6 +9,13 @@ export function projectTell(fact) {
9
9
  deliveries: fact.deliveries,
10
10
  };
11
11
  }
12
+ export class AkumaBusyError extends Error {
13
+ kind = "akuma-busy";
14
+ constructor() {
15
+ super("Akuma is busy");
16
+ this.name = "AkumaBusyError";
17
+ }
18
+ }
12
19
  export function life(input) {
13
20
  if (input.body?.hung !== undefined)
14
21
  return "hung";
@@ -4,9 +4,10 @@ export type { CallFact } from "./facts.js";
4
4
  import type { ActivityFact } from "./rows.js";
5
5
  import { type ActivityFactSlice } from "./timeline.js";
6
6
  export { HeartAbsentError, HeldAkumaLeash, classifyHeartSchema, initializeHeart, isHeartAbsent, probeLeash, } from "./storage.js";
7
- export { admitRequest, isRequestInputConflict, readNonterminalRequests, readRequest, refuseRequest, reserveRequest, serveRequest, serveUpstreamRequest, voidRequest, } from "./request-authority.js";
8
- export { life, lifeAt, projectTell } from "./facts.js";
9
- export type { AkumaLife, AkumaOrigin, BodyEnd, BodyFact, HeartSnapshot, ForkPoint, KillEvidence, KillFact, LeashProbe, PauseFact, RequestFact, RequestInput, RequestRecipe, SealFact, SessionFact, Soul, StopFact, TellFact, TellRow, TellDelivery, TellDeliveryInput, TellReceiptInput, TurnEndFact, TurnFact, TurnOutcome, TurnStartFact, } from "./facts.js";
7
+ export { admitRequest, beginRequest, isRequestInputConflict, readNonterminalRequests, readRequest, refuseRequest, reserveRequest, serveRequest, serveUpstreamRequest, unproveRequest, voidRequest, } from "./request-authority.js";
8
+ export { AkumaBusyError, life, lifeAt, projectTell } from "./facts.js";
9
+ export { drainPendingTells } from "./tells.js";
10
+ export type { AkumaLife, AkumaOrigin, BodyEnd, BodyFact, HeartSnapshot, ForkPoint, KillEvidence, KillFact, LeashProbe, PauseFact, RequestFact, RequestInput, RequestRecipe, SealFact, SessionFact, Soul, StopFact, TellBinding, TellFact, TellRow, TellDelivery, TellDeliveryInput, TellReceiptInput, TurnEndFact, TurnFact, TurnOutcome, TurnStartFact, } from "./facts.js";
10
11
  export type { ResumeCoordinate } from "../coordinate.js";
11
12
  export declare function readSoul(paths: AkumaPaths): Promise<Soul | null>;
12
13
  export declare function heartExists(paths: AkumaPaths): Promise<boolean>;
@@ -21,7 +22,9 @@ export type ActivitySlice = ActivityFactSlice;
21
22
  export type { ActivityFact };
22
23
  export type { TimelineFact } from "./timeline.js";
23
24
  export declare function activitySlice(paths: AkumaPaths): Promise<ActivitySlice>;
24
- export declare function recordTell(paths: AkumaPaths, tell: Omit<TellFact, "sequence" | "state" | "deliveries">): Promise<Readonly<{
25
+ export declare function recordTell(paths: AkumaPaths, tell: Omit<TellFact, "sequence" | "state" | "deliveries" | "binding">, options?: Readonly<{
26
+ interrupt?: boolean;
27
+ }>): Promise<Readonly<{
25
28
  kind: "not-born";
26
29
  } | {
27
30
  kind: "recorded";
@@ -30,6 +33,30 @@ export declare function recordTell(paths: AkumaPaths, tell: Omit<TellFact, "sequ
30
33
  export declare function readTell(paths: AkumaPaths, id: string): Promise<TellFact | null>;
31
34
  export declare function recordTellDeliveries(paths: AkumaPaths, inputs: readonly TellDeliveryInput[]): Promise<void>;
32
35
  export declare function recordTellReceipt(paths: AkumaPaths, input: TellReceiptInput): Promise<void>;
36
+ export type PendingTellDisposition = Readonly<{
37
+ bodySequence: number;
38
+ tellIds: readonly string[];
39
+ }>;
40
+ export declare function decidePendingTellDisposition(paths: AkumaPaths, input: Readonly<{
41
+ bodySequence: number;
42
+ at: string;
43
+ handoff: boolean;
44
+ }>): Promise<PendingTellDisposition | null>;
45
+ export declare function readOpenPendingTellDisposition(paths: AkumaPaths): Promise<PendingTellDisposition | null>;
46
+ export declare function resolvePendingTellDisposition(paths: AkumaPaths, bodySequence: number, at: string): Promise<void>;
47
+ /**
48
+ * Heart-owned disposition custody proof. Sequence growth, spawn resolution, and
49
+ * an unqualified held leash are never proof. Returns the successor Body sequence
50
+ * when that exact Body took the frozen Tell-id snapshot by delivery, or true when
51
+ * the snapshot is already fully settled (no longer pending).
52
+ */
53
+ export declare function provePendingTellDispositionCustody(paths: AkumaPaths, disposition: PendingTellDisposition): Promise<Readonly<{
54
+ kind: "proven";
55
+ successorBodySequence?: number;
56
+ } | {
57
+ kind: "unproven";
58
+ }>>;
59
+ export declare function recordUndeliveredPendingTells(paths: AkumaPaths, at: string, tellIds: readonly string[]): Promise<void>;
33
60
  export declare function readKill(paths: AkumaPaths, bodySequence: number): Promise<KillFact | null>;
34
61
  export declare function requestStop(paths: AkumaPaths, at: string): Promise<Readonly<{
35
62
  kind: "requested";
@@ -55,7 +82,19 @@ export declare function beginTurn(paths: AkumaPaths, input: Readonly<{
55
82
  bodySequence: number;
56
83
  startedAt: string;
57
84
  call?: string;
85
+ schemaJson?: string;
58
86
  }>): Promise<TurnStartFact>;
87
+ export declare function bindTellsToTurn(paths: AkumaPaths, input: Readonly<{
88
+ turnSequence: number;
89
+ tellIds: readonly string[];
90
+ boundAt: string;
91
+ }>): Promise<void>;
92
+ export declare function failOpenBoundTurns(paths: AkumaPaths, input: Readonly<{
93
+ bodySequence: number;
94
+ diagnostic: string;
95
+ completedAt: string;
96
+ }>): Promise<readonly number[]>;
97
+ export declare function readTurn(paths: AkumaPaths, sequence: number): Promise<TurnFact | null>;
59
98
  export declare function endTurn(paths: AkumaPaths, input: Readonly<{
60
99
  turnSequence: number;
61
100
  outcome: TurnOutcome;
@@ -1,12 +1,14 @@
1
1
  import { parsePublicHistoryId } from "../identity.js";
2
- import { answeredTurnFact, endBodyFact, finishBodyFact, insertActivityFact, insertTurnEndFact, insertTurnStartFact, insertPauseControl, insertSessionFact, insertStopControl, killFactForBody, latestBodyFact, latestKillFact, latestSessionFact, lastAnsweredTurnFact, pauseFact, sessionFactForCoordinate, stopFact, } from "./rows.js";
3
- import { insertTellDeliveryFact, insertTellFact, insertTellReceiptFact, pendingTellFacts, tellFact, tellIdsForFence, } from "./tells.js";
2
+ import { answeredTurnFact, decodeTurnRow, endBodyFact, finishBodyFact, insertActivityFact, insertTurnEndFact, insertTurnStartFact, insertPauseControl, insertSessionFact, insertStopControl, killFactForBody, latestBodyFact, latestKillFact, latestSessionFact, lastAnsweredTurnFact, pauseFact, sessionFactForCoordinate, stopFact, } from "./rows.js";
3
+ import { insertTellDeliveryFact, dispositionSnapshotProven, insertTellBindingFact, insertTellDispositionSnapshot, insertTellFact, insertTellReceiptFact, insertUndeliveredTellReceipts, latestOpenTellDisposition, openBoundTurns, openTellDispositionIds, pendingTellFacts, resolveTellDispositionSnapshot, successorBodyHoldingDisposition, tellFact, tellIdsForFence, } from "./tells.js";
4
4
  import { activityFactSlice, lastActivityAt as readLastActivityAt, pruneActivityFacts, } from "./timeline.js";
5
5
  import { isHeartAbsent, readSealFromLeash, readTransaction, transaction, withHeart, withReadOnlyHeart, } from "./storage.js";
6
6
  import { soulFact } from "./soul.js";
7
+ import { AkumaBusyError } from "./facts.js";
7
8
  export { HeartAbsentError, HeldAkumaLeash, classifyHeartSchema, initializeHeart, isHeartAbsent, probeLeash, } from "./storage.js";
8
- export { admitRequest, isRequestInputConflict, readNonterminalRequests, readRequest, refuseRequest, reserveRequest, serveRequest, serveUpstreamRequest, voidRequest, } from "./request-authority.js";
9
- export { life, lifeAt, projectTell } from "./facts.js";
9
+ export { admitRequest, beginRequest, isRequestInputConflict, readNonterminalRequests, readRequest, refuseRequest, reserveRequest, serveRequest, serveUpstreamRequest, unproveRequest, voidRequest, } from "./request-authority.js";
10
+ export { AkumaBusyError, life, lifeAt, projectTell } from "./facts.js";
11
+ export { drainPendingTells } from "./tells.js";
10
12
  const ACTIVITY_LIMIT = 5_000;
11
13
  export async function readSoul(paths) {
12
14
  try {
@@ -51,17 +53,23 @@ export async function appendActivity(paths, input) {
51
53
  export async function activitySlice(paths) {
52
54
  return await withHeart(paths, (heart) => readTransaction(heart, () => activityFactSlice(heart)));
53
55
  }
54
- export async function recordTell(paths, tell) {
56
+ function sameTellInput(existing, tell) {
57
+ return existing.body === tell.body && existing.recordedAt === tell.recordedAt && existing.schemaJson === tell.schemaJson;
58
+ }
59
+ export async function recordTell(paths, tell, options = {}) {
55
60
  return await withHeart(paths, (heart) => transaction(heart, () => {
56
61
  if (soulFact(heart) === null)
57
62
  return { kind: "not-born" };
58
63
  const existing = tellFact(heart, tell.id);
59
64
  if (existing !== null) {
60
- if (existing.body !== tell.body || existing.recordedAt !== tell.recordedAt) {
65
+ if (!sameTellInput(existing, tell))
61
66
  throw new Error(`tell ${tell.id} reused different input`);
62
- }
63
67
  return { kind: "recorded", tell: existing };
64
68
  }
69
+ const body = latestBodyFact(heart);
70
+ const running = body !== null && body.end === undefined && body.hung === undefined;
71
+ if (tell.schemaJson !== undefined && running && options.interrupt !== true)
72
+ throw new AkumaBusyError();
65
73
  const sequence = insertTellFact(heart, tell);
66
74
  pruneActivityFacts(heart, ACTIVITY_LIMIT);
67
75
  return { kind: "recorded", tell: { sequence, ...tell, state: "pending", deliveries: [] } };
@@ -91,6 +99,64 @@ export async function recordTellReceipt(paths, input) {
91
99
  pruneActivityFacts(heart, ACTIVITY_LIMIT);
92
100
  }));
93
101
  }
102
+ export async function decidePendingTellDisposition(paths, input) {
103
+ return await withHeart(paths, (heart) => transaction(heart, () => {
104
+ const body = latestBodyFact(heart);
105
+ if (body === null || body.sequence !== input.bodySequence)
106
+ return null;
107
+ const existing = openTellDispositionIds(heart, input.bodySequence);
108
+ if (existing !== null)
109
+ return { bodySequence: input.bodySequence, tellIds: existing };
110
+ const pending = pendingTellFacts(heart);
111
+ const tellIds = pending.map((tell) => tell.id);
112
+ if (tellIds.length === 0)
113
+ return null;
114
+ if (body.end === undefined) {
115
+ if (!input.handoff)
116
+ return null;
117
+ endBodyFact(heart, { sequence: input.bodySequence, end: "put-down", at: input.at });
118
+ }
119
+ else if (body.end === "broke-off") {
120
+ return null;
121
+ }
122
+ insertTellDispositionSnapshot(heart, input.bodySequence, tellIds, input.at);
123
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
124
+ return { bodySequence: input.bodySequence, tellIds };
125
+ }));
126
+ }
127
+ export async function readOpenPendingTellDisposition(paths) {
128
+ return await withHeart(paths, (heart) => readTransaction(heart, () => latestOpenTellDisposition(heart)));
129
+ }
130
+ export async function resolvePendingTellDisposition(paths, bodySequence, at) {
131
+ await withHeart(paths, (heart) => transaction(heart, () => {
132
+ resolveTellDispositionSnapshot(heart, bodySequence, at);
133
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
134
+ }));
135
+ }
136
+ /**
137
+ * Heart-owned disposition custody proof. Sequence growth, spawn resolution, and
138
+ * an unqualified held leash are never proof. Returns the successor Body sequence
139
+ * when that exact Body took the frozen Tell-id snapshot by delivery, or true when
140
+ * the snapshot is already fully settled (no longer pending).
141
+ */
142
+ export async function provePendingTellDispositionCustody(paths, disposition) {
143
+ return await withHeart(paths, (heart) => readTransaction(heart, () => {
144
+ if (!dispositionSnapshotProven(heart, disposition))
145
+ return { kind: "unproven" };
146
+ const successorBodySequence = successorBodyHoldingDisposition(heart, disposition);
147
+ return successorBodySequence === null
148
+ ? { kind: "proven" }
149
+ : { kind: "proven", successorBodySequence };
150
+ }));
151
+ }
152
+ export async function recordUndeliveredPendingTells(paths, at, tellIds) {
153
+ if (tellIds.length === 0)
154
+ return;
155
+ await withHeart(paths, (heart) => transaction(heart, () => {
156
+ insertUndeliveredTellReceipts(heart, tellIds, at);
157
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
158
+ }));
159
+ }
94
160
  export async function readKill(paths, bodySequence) {
95
161
  return await withHeart(paths, (heart) => killFactForBody(heart, bodySequence));
96
162
  }
@@ -144,6 +210,40 @@ export async function beginTurn(paths, input) {
144
210
  return fact;
145
211
  }));
146
212
  }
213
+ export async function bindTellsToTurn(paths, input) {
214
+ await withHeart(paths, (heart) => transaction(heart, () => {
215
+ for (const tellId of input.tellIds) {
216
+ if (tellFact(heart, tellId) === null)
217
+ throw new Error(`unknown tell ${tellId}`);
218
+ insertTellBindingFact(heart, { tellId, turnSequence: input.turnSequence, boundAt: input.boundAt });
219
+ }
220
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
221
+ }));
222
+ }
223
+ export async function failOpenBoundTurns(paths, input) {
224
+ return await withHeart(paths, (heart) => transaction(heart, () => {
225
+ const sequences = openBoundTurns(heart, input.bodySequence);
226
+ for (const turnSequence of sequences) {
227
+ insertTurnEndFact(heart, {
228
+ kind: "turn-end",
229
+ turnSequence,
230
+ outcome: { kind: "failed", diagnostic: input.diagnostic },
231
+ completedAt: input.completedAt,
232
+ });
233
+ }
234
+ pruneActivityFacts(heart, ACTIVITY_LIMIT);
235
+ return sequences;
236
+ }));
237
+ }
238
+ export async function readTurn(paths, sequence) {
239
+ return await withHeart(paths, (heart) => readTransaction(heart, () => {
240
+ const row = heart
241
+ .prepare(`SELECT sequence, body_sequence, started_at, end_sequence, outcome, history_id,
242
+ session_json, answer, answer_json, schema_json, diagnostic, completed_at FROM turns WHERE sequence = ?`)
243
+ .get(sequence);
244
+ return row === undefined ? null : decodeTurnRow(row);
245
+ }));
246
+ }
147
247
  export async function endTurn(paths, input) {
148
248
  return await withHeart(paths, (heart) => transaction(heart, () => {
149
249
  const fact = insertTurnEndFact(heart, { kind: "turn-end", ...input });
@@ -10,5 +10,7 @@ export declare function serveRequest(paths: AkumaPaths, id: string, child: strin
10
10
  export declare function serveUpstreamRequest(paths: AkumaPaths, id: string, serviceJson: string): Promise<RequestFact>;
11
11
  export declare function refuseRequest(paths: AkumaPaths, id: string, diagnostic: string): Promise<RequestFact>;
12
12
  export declare function voidRequest(paths: AkumaPaths, id: string, evidence: string): Promise<RequestFact>;
13
+ export declare function beginRequest(paths: AkumaPaths, id: string): Promise<RequestFact>;
14
+ export declare function unproveRequest(paths: AkumaPaths, id: string, evidence: string): Promise<RequestFact>;
13
15
  export declare function readRequest(paths: AkumaPaths, id: string): Promise<RequestFact | null>;
14
16
  export declare function readNonterminalRequests(paths: AkumaPaths): Promise<readonly RequestFact[]>;
@@ -1,5 +1,5 @@
1
1
  import { parseAkuId } from "../identity.js";
2
- import { insertRequestFact, nonterminalRequestFacts, requestFact, updateRequestRefused, updateRequestReserved, updateRequestServed, updateRequestVoided, updateUpstreamRequestServed, } from "./request-rows.js";
2
+ import { insertRequestFact, nonterminalRequestFacts, requestFact, updateRequestRefused, updateRequestBegun, updateRequestReserved, updateRequestServed, updateRequestUnproven, updateRequestVoided, updateUpstreamRequestServed, } from "./request-rows.js";
3
3
  import { transaction, withHeart } from "./storage.js";
4
4
  import { soulFact } from "./soul.js";
5
5
  import { requestPayloadJson } from "./request-rows.js";
@@ -72,7 +72,7 @@ export async function serveUpstreamRequest(paths, id, serviceJson) {
72
72
  const before = requestFact(heart, id);
73
73
  if (before === null)
74
74
  throw new Error(`unknown Akuma request ${id}`);
75
- if (before.state === "admitted")
75
+ if (before.state === "begun")
76
76
  updateUpstreamRequestServed(heart, id, serviceJson);
77
77
  else if (before.state !== "served" || !("serviceJson" in before) || before.serviceJson !== serviceJson) {
78
78
  throw new Error(`Akuma request ${id} cannot be served with this service reference`);
@@ -98,7 +98,7 @@ export async function voidRequest(paths, id, evidence) {
98
98
  const before = requestFact(heart, id);
99
99
  if (before === null)
100
100
  throw new Error(`unknown Akuma request ${id}`);
101
- if (before.state === "admitted" || before.state === "reserved") {
101
+ if (before.state === "admitted" || before.state === "reserved" || before.state === "begun") {
102
102
  updateRequestVoided(heart, id, evidence);
103
103
  }
104
104
  else if (before.state !== "voided" || before.evidence !== evidence) {
@@ -107,6 +107,31 @@ export async function voidRequest(paths, id, evidence) {
107
107
  return requestFact(heart, id);
108
108
  }));
109
109
  }
110
+ export async function beginRequest(paths, id) {
111
+ return await withHeart(paths, (heart) => transaction(heart, () => {
112
+ const before = requestFact(heart, id);
113
+ if (before === null)
114
+ throw new Error(`unknown Akuma request ${id}`);
115
+ if (before.state === "admitted")
116
+ updateRequestBegun(heart, id);
117
+ else if (before.state !== "begun")
118
+ throw new Error(`Akuma request ${id} cannot begin`);
119
+ return requestFact(heart, id);
120
+ }));
121
+ }
122
+ export async function unproveRequest(paths, id, evidence) {
123
+ return await withHeart(paths, (heart) => transaction(heart, () => {
124
+ const before = requestFact(heart, id);
125
+ if (before === null)
126
+ throw new Error(`unknown Akuma request ${id}`);
127
+ if (before.state === "begun")
128
+ updateRequestUnproven(heart, id, evidence);
129
+ else if (before.state !== "unproven" || before.evidence !== evidence) {
130
+ throw new Error(`Akuma request ${id} cannot be marked unproven`);
131
+ }
132
+ return requestFact(heart, id);
133
+ }));
134
+ }
110
135
  export async function readRequest(paths, id) {
111
136
  return await withHeart(paths, (heart) => requestFact(heart, id));
112
137
  }
@@ -11,6 +11,8 @@ export declare function requestFact(database: DatabaseSync, id: string): Request
11
11
  export declare function nonterminalRequestFacts(database: DatabaseSync): readonly RequestFact[];
12
12
  export declare function updateRequestReserved(database: DatabaseSync, id: string, child: AkuId): void;
13
13
  export declare function updateRequestServed(database: DatabaseSync, id: string, child: AkuId): void;
14
+ export declare function updateRequestBegun(database: DatabaseSync, id: string): void;
14
15
  export declare function updateUpstreamRequestServed(database: DatabaseSync, id: string, service: unknown): void;
15
16
  export declare function updateRequestRefused(database: DatabaseSync, id: string, diagnostic: string): void;
16
17
  export declare function updateRequestVoided(database: DatabaseSync, id: string, evidence: string): void;
18
+ export declare function updateRequestUnproven(database: DatabaseSync, id: string, evidence: string): void;
@@ -21,6 +21,12 @@ function decodeRequestRow(row) {
21
21
  }
22
22
  return { ...input, state: row.state, child: row.child };
23
23
  }
24
+ if (row.state === "begun") {
25
+ if (row.child !== null || row.service_json !== null || row.diagnostic !== null || row.evidence !== null) {
26
+ throw new Error("Akuma authority contains an invalid begun request");
27
+ }
28
+ return { ...input, state: row.state };
29
+ }
24
30
  if (row.state === "served") {
25
31
  if (row.child !== null && row.service_json === null) {
26
32
  return { ...input, state: row.state, child: row.child };
@@ -30,11 +36,15 @@ function decodeRequestRow(row) {
30
36
  }
31
37
  throw new Error("Akuma authority contains an invalid served request");
32
38
  }
39
+ if (row.state === "admitted")
40
+ return { ...input, state: row.state };
33
41
  if (row.state === "refused")
34
42
  return { ...input, state: row.state, diagnostic: row.diagnostic };
43
+ if (row.state === "unproven")
44
+ return { ...input, state: row.state, evidence: row.evidence };
35
45
  if (row.state === "voided")
36
46
  return { ...input, state: row.state, evidence: row.evidence };
37
- return { ...input, state: "admitted" };
47
+ throw new Error(`Akuma authority contains an unknown request state: ${row.state}`);
38
48
  }
39
49
  const REQUEST_COLUMNS = `sequence, id, requester, action, payload_json, admitted_at,
40
50
  state, child, service_json, diagnostic, evidence`;
@@ -53,7 +63,7 @@ export function requestFact(database, id) {
53
63
  export function nonterminalRequestFacts(database) {
54
64
  const rows = database
55
65
  .prepare(`SELECT ${REQUEST_COLUMNS} FROM requests
56
- WHERE state IN ('admitted', 'reserved') ORDER BY sequence`)
66
+ WHERE state IN ('admitted', 'reserved', 'begun') ORDER BY sequence`)
57
67
  .all();
58
68
  return rows.map(decodeRequestRow);
59
69
  }
@@ -68,10 +78,13 @@ export function updateRequestServed(database, id, child) {
68
78
  WHERE id = ? AND state = 'reserved'`)
69
79
  .run(child, id);
70
80
  }
81
+ export function updateRequestBegun(database, id) {
82
+ database.prepare("UPDATE requests SET state = 'begun' WHERE id = ? AND state = 'admitted'").run(id);
83
+ }
71
84
  export function updateUpstreamRequestServed(database, id, service) {
72
85
  database
73
86
  .prepare(`UPDATE requests SET state = 'served', service_json = ?
74
- WHERE id = ? AND state = 'admitted'`)
87
+ WHERE id = ? AND state = 'begun'`)
75
88
  .run(assertJsonText(service), id);
76
89
  }
77
90
  export function updateRequestRefused(database, id, diagnostic) {
@@ -82,7 +95,13 @@ export function updateRequestRefused(database, id, diagnostic) {
82
95
  }
83
96
  export function updateRequestVoided(database, id, evidence) {
84
97
  database
85
- .prepare(`UPDATE requests SET state = 'voided', child = NULL, service_json = NULL, evidence = ?
86
- WHERE id = ? AND state IN ('admitted', 'reserved')`)
98
+ .prepare(`UPDATE requests SET state = 'voided', child = NULL, service_json = NULL, diagnostic = NULL, evidence = ?
99
+ WHERE id = ? AND state IN ('admitted', 'reserved', 'begun')`)
100
+ .run(evidence, id);
101
+ }
102
+ export function updateRequestUnproven(database, id, evidence) {
103
+ database
104
+ .prepare(`UPDATE requests SET state = 'unproven', child = NULL, service_json = NULL, diagnostic = NULL, evidence = ?
105
+ WHERE id = ? AND state = 'begun'`)
87
106
  .run(evidence, id);
88
107
  }
@@ -25,10 +25,12 @@ export type TurnRow = Readonly<{
25
25
  body_sequence: number;
26
26
  started_at: string;
27
27
  end_sequence: number | null;
28
- outcome: "answered" | "failed" | null;
28
+ outcome: "answered" | "failed" | "invalid-output" | null;
29
29
  history_id: string | null;
30
30
  session_json: string | null;
31
31
  answer: string | null;
32
+ answer_json: string | null;
33
+ schema_json: string | null;
32
34
  diagnostic: string | null;
33
35
  completed_at: string | null;
34
36
  }>;
@@ -104,6 +106,7 @@ export declare function insertTurnStartFact(database: DatabaseSync, input: Reado
104
106
  bodySequence: number;
105
107
  startedAt: string;
106
108
  call?: string;
109
+ schemaJson?: string;
107
110
  }>): TurnStartFact;
108
111
  export declare function insertTurnEndFact(database: DatabaseSync, input: Omit<TurnEndFact, "sequence">): TurnEndFact;
109
112
  export declare function finishBodyFact(database: DatabaseSync, input: Readonly<{
@@ -44,12 +44,34 @@ export function decodeBodyRow(row) {
44
44
  ...(row.ended_at === null ? {} : { endedAt: row.ended_at }),
45
45
  };
46
46
  }
47
+ function decodeTurnOutcome(row) {
48
+ if (row.outcome === "answered") {
49
+ return {
50
+ kind: "answered",
51
+ ...(row.history_id === null ? {} : { historyId: row.history_id }),
52
+ session: resumeCoordinate(parsed(row.session_json)),
53
+ answer: row.answer,
54
+ ...(row.answer_json === null ? {} : { answerJson: row.answer_json }),
55
+ };
56
+ }
57
+ if (row.outcome === "invalid-output") {
58
+ return {
59
+ kind: "invalid-output",
60
+ diagnostic: row.diagnostic,
61
+ answer: row.answer,
62
+ ...(row.history_id === null ? {} : { historyId: row.history_id }),
63
+ ...(row.session_json === null ? {} : { session: resumeCoordinate(parsed(row.session_json)) }),
64
+ };
65
+ }
66
+ return { kind: "failed", diagnostic: row.diagnostic };
67
+ }
47
68
  export function decodeTurnRow(row) {
48
69
  const start = {
49
70
  kind: "turn-start",
50
71
  sequence: row.sequence,
51
72
  bodySequence: row.body_sequence,
52
73
  startedAt: row.started_at,
74
+ ...(row.schema_json === null || row.schema_json === undefined ? {} : { schemaJson: row.schema_json }),
53
75
  };
54
76
  if (row.outcome === null)
55
77
  return start;
@@ -57,14 +79,7 @@ export function decodeTurnRow(row) {
57
79
  kind: "turn-end",
58
80
  sequence: row.end_sequence,
59
81
  turnSequence: row.sequence,
60
- outcome: row.outcome === "answered"
61
- ? {
62
- kind: "answered",
63
- ...(row.history_id === null ? {} : { historyId: row.history_id }),
64
- session: resumeCoordinate(parsed(row.session_json)),
65
- answer: row.answer,
66
- }
67
- : { kind: "failed", diagnostic: row.diagnostic },
82
+ outcome: decodeTurnOutcome(row),
68
83
  completedAt: row.completed_at,
69
84
  };
70
85
  return { ...start, end };
@@ -177,27 +192,39 @@ export function markBodyHung(database, input) {
177
192
  export function insertTurnStartFact(database, input) {
178
193
  const sequence = Number(database.prepare("INSERT INTO timeline(kind) VALUES ('turn-start')").run().lastInsertRowid);
179
194
  database
180
- .prepare("INSERT INTO turns(sequence, body_sequence, started_at) VALUES (?, ?, ?)")
181
- .run(sequence, input.bodySequence, input.startedAt);
195
+ .prepare("INSERT INTO turns(sequence, body_sequence, started_at, schema_json) VALUES (?, ?, ?, ?)")
196
+ .run(sequence, input.bodySequence, input.startedAt, input.schemaJson ?? null);
182
197
  if (input.call !== undefined) {
183
198
  const callSequence = Number(database.prepare("INSERT INTO timeline(kind) VALUES ('call')").run().lastInsertRowid);
184
199
  database
185
200
  .prepare("INSERT INTO calls(sequence, turn_sequence, body, at) VALUES (?, ?, ?, ?)")
186
201
  .run(callSequence, sequence, input.call, input.startedAt);
187
202
  }
188
- return { kind: "turn-start", sequence, bodySequence: input.bodySequence, startedAt: input.startedAt };
203
+ return {
204
+ kind: "turn-start",
205
+ sequence,
206
+ bodySequence: input.bodySequence,
207
+ startedAt: input.startedAt,
208
+ ...(input.schemaJson === undefined ? {} : { schemaJson: input.schemaJson }),
209
+ };
189
210
  }
190
211
  export function insertTurnEndFact(database, input) {
191
212
  const sequence = Number(database.prepare("INSERT INTO timeline(kind) VALUES ('turn-end')").run().lastInsertRowid);
192
213
  const result = input.outcome.kind === "answered"
193
214
  ? database
194
215
  .prepare(`UPDATE turns SET end_sequence = ?, outcome = 'answered', history_id = ?,
195
- session_json = ?, answer = ?, completed_at = ? WHERE sequence = ? AND end_sequence IS NULL`)
196
- .run(sequence, input.outcome.historyId ?? null, encodeResumeCoordinate(input.outcome.session), input.outcome.answer, input.completedAt, input.turnSequence)
197
- : database
198
- .prepare(`UPDATE turns SET end_sequence = ?, outcome = 'failed', diagnostic = ?, completed_at = ?
199
- WHERE sequence = ? AND end_sequence IS NULL`)
200
- .run(sequence, input.outcome.diagnostic, input.completedAt, input.turnSequence);
216
+ session_json = ?, answer = ?, answer_json = ?, completed_at = ? WHERE sequence = ? AND end_sequence IS NULL`)
217
+ .run(sequence, input.outcome.historyId ?? null, encodeResumeCoordinate(input.outcome.session), input.outcome.answer, input.outcome.answerJson ?? null, input.completedAt, input.turnSequence)
218
+ : input.outcome.kind === "invalid-output"
219
+ ? database
220
+ .prepare(`UPDATE turns SET end_sequence = ?, outcome = 'invalid-output', history_id = ?,
221
+ session_json = ?, answer = ?, diagnostic = ?, completed_at = ?
222
+ WHERE sequence = ? AND end_sequence IS NULL`)
223
+ .run(sequence, input.outcome.historyId ?? null, input.outcome.session === undefined ? null : encodeResumeCoordinate(input.outcome.session), input.outcome.answer, input.outcome.diagnostic, input.completedAt, input.turnSequence)
224
+ : database
225
+ .prepare(`UPDATE turns SET end_sequence = ?, outcome = 'failed', diagnostic = ?, completed_at = ?
226
+ WHERE sequence = ? AND end_sequence IS NULL`)
227
+ .run(sequence, input.outcome.diagnostic, input.completedAt, input.turnSequence);
201
228
  if (result.changes !== 1)
202
229
  throw new Error(`Akuma Turn ${input.turnSequence} is not open`);
203
230
  return { ...input, kind: "turn-end", sequence };
@@ -259,14 +286,14 @@ export function killFactForBody(database, bodySequence) {
259
286
  export function lastAnsweredTurnFact(database) {
260
287
  const row = database
261
288
  .prepare(`SELECT sequence, body_sequence, started_at, end_sequence, outcome, history_id,
262
- session_json, answer, diagnostic, completed_at FROM turns WHERE outcome = 'answered' ORDER BY end_sequence DESC LIMIT 1`)
289
+ session_json, answer, answer_json, schema_json, diagnostic, completed_at FROM turns WHERE outcome = 'answered' ORDER BY end_sequence DESC LIMIT 1`)
263
290
  .get();
264
291
  return row === undefined ? null : decodeTurnRow(row);
265
292
  }
266
293
  export function answeredTurnFact(database, turnSequence) {
267
294
  const row = database
268
295
  .prepare(`SELECT sequence, body_sequence, started_at, end_sequence, outcome, history_id,
269
- session_json, answer, diagnostic, completed_at FROM turns WHERE outcome = 'answered' AND sequence = ?`)
296
+ session_json, answer, answer_json, schema_json, diagnostic, completed_at FROM turns WHERE outcome = 'answered' AND sequence = ?`)
270
297
  .get(turnSequence);
271
298
  return row === undefined ? null : decodeTurnRow(row);
272
299
  }
@@ -2,5 +2,5 @@ import type { DatabaseSync } from "node:sqlite";
2
2
  export declare function assertHeartSchemaVersion(database: DatabaseSync): void;
3
3
  export declare function assertLeashSchemaVersion(database: DatabaseSync): void;
4
4
  export declare function heartSchemaIsCurrent(database: DatabaseSync): boolean;
5
- export declare const HEART_SCHEMA = "\n CREATE TABLE IF NOT EXISTS akuma_schema (\n singleton INTEGER PRIMARY KEY CHECK (singleton = 1),\n version INTEGER NOT NULL CHECK (version = 21)\n ) STRICT;\n INSERT OR IGNORE INTO akuma_schema(singleton, version) VALUES (1, 21);\n CREATE TABLE IF NOT EXISTS soul (\n singleton INTEGER PRIMARY KEY CHECK (singleton = 1),\n soul_json TEXT NOT NULL CHECK (json_valid(soul_json))\n ) STRICT;\n CREATE TABLE IF NOT EXISTS bodies (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n leash_taken_at TEXT NOT NULL,\n hung_diagnostic TEXT,\n hung_at TEXT,\n end TEXT CHECK (end IN ('exited', 'broke-off', 'put-down')),\n ended_at TEXT,\n CHECK ((hung_diagnostic IS NULL AND hung_at IS NULL)\n OR (hung_diagnostic IS NOT NULL AND hung_at IS NOT NULL))\n ) STRICT;\n CREATE TABLE IF NOT EXISTS sessions (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n provider TEXT NOT NULL,\n coordinate_json TEXT NOT NULL CHECK (json_valid(coordinate_json)),\n cwd TEXT NOT NULL,\n options_json TEXT NOT NULL CHECK (json_valid(options_json)),\n admitted_at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS turns (\n sequence INTEGER PRIMARY KEY REFERENCES timeline(sequence) ON DELETE CASCADE,\n body_sequence INTEGER NOT NULL REFERENCES bodies(sequence),\n started_at TEXT NOT NULL,\n end_sequence INTEGER UNIQUE REFERENCES timeline(sequence) ON DELETE SET NULL,\n outcome TEXT CHECK (outcome IN ('answered', 'failed')),\n history_id TEXT UNIQUE,\n session_json TEXT CHECK (session_json IS NULL OR json_valid(session_json)),\n answer TEXT,\n diagnostic TEXT,\n completed_at TEXT,\n CHECK (\n (outcome = 'answered' AND session_json IS NOT NULL AND answer IS NOT NULL AND diagnostic IS NULL)\n OR (outcome = 'failed' AND history_id IS NULL AND session_json IS NULL AND answer IS NULL AND diagnostic IS NOT NULL)\n OR (outcome IS NULL AND end_sequence IS NULL AND history_id IS NULL AND session_json IS NULL AND answer IS NULL AND diagnostic IS NULL AND completed_at IS NULL)\n )\n ) STRICT;\n CREATE TABLE IF NOT EXISTS timeline (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n kind TEXT NOT NULL CHECK (kind IN ('turn-start', 'call', 'activity', 'tell', 'turn-end'))\n ) STRICT;\n CREATE TABLE IF NOT EXISTS calls (\n sequence INTEGER PRIMARY KEY REFERENCES timeline(sequence) ON DELETE CASCADE,\n turn_sequence INTEGER NOT NULL REFERENCES turns(sequence) ON DELETE CASCADE,\n body TEXT NOT NULL,\n at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS activity (\n sequence INTEGER PRIMARY KEY REFERENCES timeline(sequence) ON DELETE CASCADE,\n turn_sequence INTEGER NOT NULL REFERENCES turns(sequence) ON DELETE CASCADE,\n event_json TEXT NOT NULL CHECK (json_valid(event_json)),\n at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS tells (\n id TEXT PRIMARY KEY,\n sequence INTEGER NOT NULL UNIQUE REFERENCES timeline(sequence) ON DELETE CASCADE,\n body TEXT NOT NULL,\n recorded_at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS tell_deliveries (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n tell_id TEXT NOT NULL REFERENCES tells(id) ON DELETE CASCADE,\n route TEXT NOT NULL CHECK (route IN ('launch', 'live')),\n turn_sequence INTEGER NOT NULL REFERENCES turns(sequence) ON DELETE CASCADE,\n fence TEXT NOT NULL,\n receipt TEXT CHECK (receipt IN ('unavailable', 'required')),\n delivered_at TEXT NOT NULL,\n CHECK ((route = 'launch' AND receipt IS NULL) OR (route = 'live' AND receipt IS NOT NULL)),\n UNIQUE (tell_id, turn_sequence, fence)\n ) STRICT;\n CREATE TABLE IF NOT EXISTS tell_receipts (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n evidence TEXT NOT NULL CHECK (evidence IN ('exact', 'fence')),\n tell_id TEXT REFERENCES tells(id) ON DELETE CASCADE,\n turn_sequence INTEGER REFERENCES turns(sequence) ON DELETE CASCADE,\n fence TEXT,\n kind TEXT NOT NULL,\n received_at TEXT NOT NULL,\n CHECK (\n (evidence = 'exact' AND tell_id IS NOT NULL AND turn_sequence IS NULL AND fence IS NULL)\n OR (evidence = 'fence' AND tell_id IS NULL AND turn_sequence IS NOT NULL AND fence IS NOT NULL)\n )\n ) STRICT;\n CREATE UNIQUE INDEX IF NOT EXISTS tell_receipts_exact\n ON tell_receipts(tell_id, kind) WHERE evidence = 'exact';\n CREATE UNIQUE INDEX IF NOT EXISTS tell_receipts_fence\n ON tell_receipts(turn_sequence, fence, kind) WHERE evidence = 'fence';\n CREATE TABLE IF NOT EXISTS requests (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n id TEXT NOT NULL UNIQUE,\n requester TEXT NOT NULL,\n action TEXT NOT NULL,\n payload_json TEXT NOT NULL CHECK (json_valid(payload_json)),\n admitted_at TEXT NOT NULL,\n state TEXT NOT NULL CHECK (state IN ('admitted', 'reserved', 'served', 'refused', 'voided')),\n child TEXT,\n service_json TEXT CHECK (service_json IS NULL OR json_valid(service_json)),\n diagnostic TEXT,\n evidence TEXT,\n CHECK (\n (state = 'admitted' AND child IS NULL AND service_json IS NULL AND diagnostic IS NULL AND evidence IS NULL)\n OR (state = 'reserved' AND child IS NOT NULL AND service_json IS NULL\n AND diagnostic IS NULL AND evidence IS NULL)\n OR (state = 'served'\n AND ((child IS NOT NULL AND service_json IS NULL)\n OR (child IS NULL AND service_json IS NOT NULL))\n AND diagnostic IS NULL AND evidence IS NULL)\n OR (state = 'refused' AND child IS NULL AND service_json IS NULL AND diagnostic IS NOT NULL AND evidence IS NULL)\n OR (state = 'voided' AND child IS NULL AND service_json IS NULL AND diagnostic IS NULL AND evidence IS NOT NULL)\n )\n ) STRICT;\n CREATE TABLE IF NOT EXISTS control (\n kind TEXT PRIMARY KEY CHECK (kind IN ('stop', 'pause')),\n value_json TEXT NOT NULL CHECK (json_valid(value_json)),\n at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS kills (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n body_sequence INTEGER NOT NULL UNIQUE REFERENCES bodies(sequence),\n evidence TEXT NOT NULL CHECK (evidence = 'killed'),\n at TEXT NOT NULL\n ) STRICT;\n";
5
+ export declare const HEART_SCHEMA = "\n CREATE TABLE IF NOT EXISTS akuma_schema (\n singleton INTEGER PRIMARY KEY CHECK (singleton = 1),\n version INTEGER NOT NULL CHECK (version = 24)\n ) STRICT;\n INSERT OR IGNORE INTO akuma_schema(singleton, version) VALUES (1, 24);\n CREATE TABLE IF NOT EXISTS soul (\n singleton INTEGER PRIMARY KEY CHECK (singleton = 1),\n soul_json TEXT NOT NULL CHECK (json_valid(soul_json))\n ) STRICT;\n CREATE TABLE IF NOT EXISTS bodies (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n leash_taken_at TEXT NOT NULL,\n hung_diagnostic TEXT,\n hung_at TEXT,\n end TEXT CHECK (end IN ('exited', 'broke-off', 'put-down')),\n ended_at TEXT,\n CHECK ((hung_diagnostic IS NULL AND hung_at IS NULL)\n OR (hung_diagnostic IS NOT NULL AND hung_at IS NOT NULL))\n ) STRICT;\n CREATE TABLE IF NOT EXISTS sessions (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n provider TEXT NOT NULL,\n coordinate_json TEXT NOT NULL CHECK (json_valid(coordinate_json)),\n cwd TEXT NOT NULL,\n options_json TEXT NOT NULL CHECK (json_valid(options_json)),\n admitted_at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS turns (\n sequence INTEGER PRIMARY KEY REFERENCES timeline(sequence) ON DELETE CASCADE,\n body_sequence INTEGER NOT NULL REFERENCES bodies(sequence),\n started_at TEXT NOT NULL,\n end_sequence INTEGER UNIQUE REFERENCES timeline(sequence) ON DELETE SET NULL,\n outcome TEXT CHECK (outcome IN ('answered', 'failed', 'invalid-output')),\n history_id TEXT UNIQUE,\n session_json TEXT CHECK (session_json IS NULL OR json_valid(session_json)),\n answer TEXT,\n answer_json TEXT CHECK (answer_json IS NULL OR json_valid(answer_json)),\n schema_json TEXT CHECK (schema_json IS NULL OR json_valid(schema_json)),\n diagnostic TEXT,\n completed_at TEXT,\n CHECK (\n (outcome = 'answered' AND session_json IS NOT NULL AND answer IS NOT NULL AND diagnostic IS NULL\n AND ((schema_json IS NULL AND answer_json IS NULL) OR (schema_json IS NOT NULL AND answer_json IS NOT NULL)))\n OR (outcome = 'failed' AND history_id IS NULL AND session_json IS NULL AND answer IS NULL\n AND answer_json IS NULL AND diagnostic IS NOT NULL)\n OR (outcome = 'invalid-output' AND answer IS NOT NULL AND answer_json IS NULL\n AND diagnostic IS NOT NULL AND schema_json IS NOT NULL)\n OR (outcome IS NULL AND end_sequence IS NULL AND history_id IS NULL AND session_json IS NULL\n AND answer IS NULL AND answer_json IS NULL AND diagnostic IS NULL AND completed_at IS NULL)\n )\n ) STRICT;\n CREATE TABLE IF NOT EXISTS timeline (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n kind TEXT NOT NULL CHECK (kind IN ('turn-start', 'call', 'activity', 'tell', 'turn-end'))\n ) STRICT;\n CREATE TABLE IF NOT EXISTS calls (\n sequence INTEGER PRIMARY KEY REFERENCES timeline(sequence) ON DELETE CASCADE,\n turn_sequence INTEGER NOT NULL REFERENCES turns(sequence) ON DELETE CASCADE,\n body TEXT NOT NULL,\n at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS activity (\n sequence INTEGER PRIMARY KEY REFERENCES timeline(sequence) ON DELETE CASCADE,\n turn_sequence INTEGER NOT NULL REFERENCES turns(sequence) ON DELETE CASCADE,\n event_json TEXT NOT NULL CHECK (json_valid(event_json)),\n at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS tells (\n id TEXT PRIMARY KEY,\n sequence INTEGER NOT NULL UNIQUE REFERENCES timeline(sequence) ON DELETE CASCADE,\n body TEXT NOT NULL,\n schema_json TEXT CHECK (schema_json IS NULL OR json_valid(schema_json)),\n recorded_at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS tell_bindings (\n tell_id TEXT PRIMARY KEY REFERENCES tells(id) ON DELETE CASCADE,\n turn_sequence INTEGER NOT NULL REFERENCES turns(sequence) ON DELETE CASCADE,\n bound_at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS tell_deliveries (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n tell_id TEXT NOT NULL REFERENCES tells(id) ON DELETE CASCADE,\n route TEXT NOT NULL CHECK (route IN ('launch', 'live')),\n turn_sequence INTEGER NOT NULL REFERENCES turns(sequence) ON DELETE CASCADE,\n fence TEXT NOT NULL,\n receipt TEXT CHECK (receipt IN ('unavailable', 'required')),\n delivered_at TEXT NOT NULL,\n CHECK ((route = 'launch' AND receipt IS NULL) OR (route = 'live' AND receipt IS NOT NULL)),\n UNIQUE (tell_id, turn_sequence, fence)\n ) STRICT;\n CREATE TABLE IF NOT EXISTS tell_receipts (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n evidence TEXT NOT NULL CHECK (evidence IN ('exact', 'fence')),\n tell_id TEXT REFERENCES tells(id) ON DELETE CASCADE,\n turn_sequence INTEGER REFERENCES turns(sequence) ON DELETE CASCADE,\n fence TEXT,\n kind TEXT NOT NULL,\n received_at TEXT NOT NULL,\n CHECK (\n (evidence = 'exact' AND tell_id IS NOT NULL AND turn_sequence IS NULL AND fence IS NULL)\n OR (evidence = 'fence' AND tell_id IS NULL AND turn_sequence IS NOT NULL AND fence IS NOT NULL)\n )\n ) STRICT;\n CREATE UNIQUE INDEX IF NOT EXISTS tell_receipts_exact\n ON tell_receipts(tell_id, kind) WHERE evidence = 'exact';\n CREATE UNIQUE INDEX IF NOT EXISTS tell_receipts_fence\n ON tell_receipts(turn_sequence, fence, kind) WHERE evidence = 'fence';\n CREATE TABLE IF NOT EXISTS tell_dispositions (\n body_sequence INTEGER NOT NULL REFERENCES bodies(sequence),\n tell_id TEXT NOT NULL REFERENCES tells(id),\n decided_at TEXT NOT NULL,\n resolved_at TEXT,\n PRIMARY KEY (body_sequence, tell_id)\n ) STRICT;\n CREATE TABLE IF NOT EXISTS requests (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n id TEXT NOT NULL UNIQUE,\n requester TEXT NOT NULL,\n action TEXT NOT NULL,\n payload_json TEXT NOT NULL CHECK (json_valid(payload_json)),\n admitted_at TEXT NOT NULL,\n state TEXT NOT NULL CHECK (state IN ('admitted', 'reserved', 'begun', 'served', 'refused', 'voided', 'unproven')),\n child TEXT,\n service_json TEXT CHECK (service_json IS NULL OR json_valid(service_json)),\n diagnostic TEXT,\n evidence TEXT,\n CHECK (\n (state = 'admitted' AND child IS NULL AND service_json IS NULL AND diagnostic IS NULL AND evidence IS NULL)\n OR (state = 'reserved' AND child IS NOT NULL AND service_json IS NULL\n AND diagnostic IS NULL AND evidence IS NULL)\n OR (state = 'begun' AND child IS NULL AND service_json IS NULL AND diagnostic IS NULL AND evidence IS NULL)\n OR (state = 'served'\n AND ((child IS NOT NULL AND service_json IS NULL)\n OR (child IS NULL AND service_json IS NOT NULL))\n AND diagnostic IS NULL AND evidence IS NULL)\n OR (state = 'refused' AND child IS NULL AND service_json IS NULL AND diagnostic IS NOT NULL AND evidence IS NULL)\n OR (state = 'voided' AND child IS NULL AND service_json IS NULL AND diagnostic IS NULL AND evidence IS NOT NULL)\n OR (state = 'unproven' AND child IS NULL AND service_json IS NULL AND diagnostic IS NULL AND evidence IS NOT NULL)\n )\n ) STRICT;\n CREATE TABLE IF NOT EXISTS control (\n kind TEXT PRIMARY KEY CHECK (kind IN ('stop', 'pause')),\n value_json TEXT NOT NULL CHECK (json_valid(value_json)),\n at TEXT NOT NULL\n ) STRICT;\n CREATE TABLE IF NOT EXISTS kills (\n sequence INTEGER PRIMARY KEY AUTOINCREMENT,\n body_sequence INTEGER NOT NULL UNIQUE REFERENCES bodies(sequence),\n evidence TEXT NOT NULL CHECK (evidence = 'killed'),\n at TEXT NOT NULL\n ) STRICT;\n";
6
6
  export declare const LEASH_SCHEMA = "\n PRAGMA journal_mode=DELETE;\n CREATE TABLE IF NOT EXISTS leash_schema (\n singleton INTEGER PRIMARY KEY CHECK (singleton = 1),\n version INTEGER NOT NULL CHECK (version = 4)\n ) STRICT;\n INSERT OR IGNORE INTO leash_schema(singleton, version) VALUES (1, 4);\n CREATE TABLE IF NOT EXISTS seal (\n singleton INTEGER PRIMARY KEY CHECK (singleton = 1),\n evidence TEXT NOT NULL,\n at TEXT NOT NULL\n ) STRICT;\n";