@astrosheep/keiyaku 4.6.27 → 4.6.28

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 (285) hide show
  1. package/README.md +76 -35
  2. package/build/plugins/claude/.claude-plugin/plugin.json +1 -1
  3. package/build/plugins/claude/skills/keiyaku-akuma/SKILL.md +39 -4
  4. package/build/plugins/claude/skills/keiyaku-akuma/references/automation.md +4 -7
  5. package/build/plugins/claude/skills/keiyaku-babysit/SKILL.md +1 -1
  6. package/build/plugins/claude/skills/keiyaku-bind/SKILL.md +39 -15
  7. package/build/plugins/claude/skills/keiyaku-workflow/SKILL.md +42 -22
  8. package/build/plugins/codex/.codex-plugin/plugin.json +1 -1
  9. package/build/plugins/codex/skills/keiyaku-akuma/SKILL.md +39 -4
  10. package/build/plugins/codex/skills/keiyaku-akuma/references/automation.md +4 -7
  11. package/build/plugins/codex/skills/keiyaku-babysit/SKILL.md +1 -1
  12. package/build/plugins/codex/skills/keiyaku-bind/SKILL.md +39 -15
  13. package/build/plugins/codex/skills/keiyaku-workflow/SKILL.md +42 -22
  14. package/build/plugins/opencode/package.json +1 -1
  15. package/build/plugins/opencode/skills/keiyaku-akuma/SKILL.md +39 -4
  16. package/build/plugins/opencode/skills/keiyaku-akuma/references/automation.md +4 -7
  17. package/build/plugins/opencode/skills/keiyaku-babysit/SKILL.md +1 -1
  18. package/build/plugins/opencode/skills/keiyaku-bind/SKILL.md +39 -15
  19. package/build/plugins/opencode/skills/keiyaku-workflow/SKILL.md +42 -22
  20. package/build/plugins/pi/README.md +7 -12
  21. package/build/plugins/pi/akuma-tools.ts +3 -18
  22. package/build/skills/keiyaku-akuma/SKILL.md +39 -4
  23. package/build/skills/keiyaku-akuma/references/automation.md +4 -7
  24. package/build/skills/keiyaku-babysit/SKILL.md +1 -1
  25. package/build/skills/keiyaku-bind/SKILL.md +39 -15
  26. package/build/skills/keiyaku-workflow/SKILL.md +42 -22
  27. package/build/src/akuma/akuma-call.d.ts +15 -18
  28. package/build/src/akuma/akuma-call.js +55 -112
  29. package/build/src/akuma/akuma-errors.d.ts +0 -5
  30. package/build/src/akuma/akuma-errors.js +0 -9
  31. package/build/src/akuma/akuma-instance.d.ts +0 -2
  32. package/build/src/akuma/akuma-instance.js +18 -14
  33. package/build/src/akuma/akuma-observe.js +0 -1
  34. package/build/src/akuma/akuma-owner.js +0 -1
  35. package/build/src/akuma/akuma.d.ts +10 -26
  36. package/build/src/akuma/akuma.js +0 -2
  37. package/build/src/akuma/archetype.d.ts +0 -2
  38. package/build/src/akuma/archetype.js +3 -9
  39. package/build/src/akuma/body-launch.d.ts +12 -4
  40. package/build/src/akuma/body.d.ts +2 -5
  41. package/build/src/akuma/body.js +5 -49
  42. package/build/src/akuma/call-initial-tell.d.ts +39 -147
  43. package/build/src/akuma/call-initial-tell.js +0 -48
  44. package/build/src/akuma/caller.d.ts +0 -4
  45. package/build/src/akuma/custody.js +1 -57
  46. package/build/src/akuma/heart/facts.d.ts +0 -39
  47. package/build/src/akuma/heart/facts.js +0 -1
  48. package/build/src/akuma/heart/index.d.ts +1 -2
  49. package/build/src/akuma/heart/index.js +0 -1
  50. package/build/src/akuma/heart/schema.js +0 -26
  51. package/build/src/akuma/heart/soul.d.ts +2 -2
  52. package/build/src/akuma/heart/soul.js +2 -4
  53. package/build/src/akuma/identity.d.ts +0 -1
  54. package/build/src/akuma/identity.js +0 -2
  55. package/build/src/akuma/in-process-body.d.ts +1 -1
  56. package/build/src/akuma/index.d.ts +0 -2
  57. package/build/src/akuma/index.js +0 -1
  58. package/build/src/akuma/projection.d.ts +23 -23
  59. package/build/src/akuma/provider.d.ts +0 -5
  60. package/build/src/akuma/provider.js +0 -1
  61. package/build/src/akuma/providers/acp/core.js +1 -1
  62. package/build/src/akuma/providers/claude/index.js +1 -2
  63. package/build/src/akuma/providers/codex-app-server/index.js +1 -1
  64. package/build/src/akuma/providers/execution-environment.d.ts +1 -3
  65. package/build/src/akuma/providers/execution-environment.js +1 -7
  66. package/build/src/akuma/providers/opencode-sdk/index.js +0 -1
  67. package/build/src/akuma/providers/opencode-sdk/session.d.ts +0 -1
  68. package/build/src/akuma/providers/opencode-sdk/session.js +4 -4
  69. package/build/src/akuma/providers/paseo/index.js +2 -9
  70. package/build/src/akuma/providers/pi/index.js +8 -14
  71. package/build/src/akuma/publication.d.ts +24 -59
  72. package/build/src/akuma/publication.js +29 -64
  73. package/build/src/akuma/selection-observation.d.ts +47 -2036
  74. package/build/src/akuma/selection-observation.js +1 -59
  75. package/build/src/akuma/turn-drive.d.ts +0 -8
  76. package/build/src/akuma/turn-drive.js +19 -103
  77. package/build/src/akuma-body.js +3 -6
  78. package/build/src/body-host.d.ts +8 -2
  79. package/build/src/body-host.js +30 -4
  80. package/build/src/cli/commands/akuma-grammar.d.ts +0 -2
  81. package/build/src/cli/commands/akuma-grammar.js +10 -33
  82. package/build/src/cli/commands/akuma.js +31 -49
  83. package/build/src/cli/commands/contract-grammar.d.ts +7 -1
  84. package/build/src/cli/commands/contract-grammar.js +15 -0
  85. package/build/src/cli/commands/contract-help.d.ts +13 -1
  86. package/build/src/cli/commands/contract-help.js +47 -16
  87. package/build/src/cli/commands/contract.js +27 -26
  88. package/build/src/cli/commands/debt.js +3 -3
  89. package/build/src/cli/commands/whoami.d.ts +0 -2
  90. package/build/src/cli/commands/whoami.js +0 -5
  91. package/build/src/cli/coordinates.js +15 -0
  92. package/build/src/cli/delivery-identity.d.ts +3 -0
  93. package/build/src/cli/delivery-identity.js +23 -0
  94. package/build/src/cli/parse.js +1 -0
  95. package/build/src/cli/render/akuma-activity.d.ts +0 -1
  96. package/build/src/cli/render/akuma-activity.js +1 -4
  97. package/build/src/cli/render/catalog.js +7 -1
  98. package/build/src/cli/render/contract-history.js +16 -38
  99. package/build/src/cli/render/contract-observation.d.ts +23 -10
  100. package/build/src/cli/render/contract-observation.js +9 -6
  101. package/build/src/cli/render/contract-wait.d.ts +2 -0
  102. package/build/src/cli/render/contract-wait.js +18 -0
  103. package/build/src/cli/render/contract.d.ts +2 -2
  104. package/build/src/cli/render/contract.js +57 -39
  105. package/build/src/cli/render/delivery-preview.js +13 -26
  106. package/build/src/cli/render/execution-progress.js +9 -9
  107. package/build/src/cli/render/kanshi.js +4 -11
  108. package/build/src/cli/render/receipt.d.ts +18 -0
  109. package/build/src/cli/render/receipt.js +34 -10
  110. package/build/src/cli/render/reconcile.js +3 -0
  111. package/build/src/cli/render/refusal.js +28 -6
  112. package/build/src/cli/render/whoami.js +1 -4
  113. package/build/src/cli/runtime.d.ts +0 -2
  114. package/build/src/cli/runtime.js +3 -7
  115. package/build/src/cli/selectors.d.ts +1 -2
  116. package/build/src/cli/selectors.js +2 -5
  117. package/build/src/contract-guidance.js +4 -2
  118. package/build/src/contract-worktree.d.ts +3 -3
  119. package/build/src/core/facts/codec.js +38 -5
  120. package/build/src/core/facts/fold.js +1 -0
  121. package/build/src/core/facts/gate.d.ts +10 -4
  122. package/build/src/core/facts/gate.js +56 -39
  123. package/build/src/core/facts/observation.d.ts +7 -1
  124. package/build/src/core/facts/observation.js +15 -0
  125. package/build/src/core/facts/offer.d.ts +2 -2
  126. package/build/src/core/facts/types.d.ts +17 -1
  127. package/build/src/core/facts/types.js +6 -0
  128. package/build/src/core/verbs/amend.js +8 -0
  129. package/build/src/core/verbs/bind.d.ts +3 -1
  130. package/build/src/core/verbs/bind.js +9 -1
  131. package/build/src/core/verbs/deliver.d.ts +2 -1
  132. package/build/src/core/verbs/deliver.js +4 -1
  133. package/build/src/core/verbs/placement.d.ts +2 -1
  134. package/build/src/core/verbs/placement.js +7 -4
  135. package/build/src/core/verbs/reintegrate.d.ts +2 -1
  136. package/build/src/core/verbs/reintegrate.js +4 -1
  137. package/build/src/debt/board.d.ts +10 -10
  138. package/build/src/debt/created-observation.d.ts +3 -3
  139. package/build/src/debt/document.d.ts +1 -1
  140. package/build/src/debt/index.d.ts +2 -24
  141. package/build/src/debt/index.js +11 -71
  142. package/build/src/debt/operation-types.d.ts +44 -3
  143. package/build/src/git/admission.js +18 -6
  144. package/build/src/git/integration.d.ts +4 -4
  145. package/build/src/git/integration.js +7 -1
  146. package/build/src/git/observe.d.ts +6 -4
  147. package/build/src/git/observe.js +18 -17
  148. package/build/src/git/reconcile.d.ts +20 -8
  149. package/build/src/git/reconcile.js +94 -45
  150. package/build/src/git/repository.d.ts +3 -1
  151. package/build/src/git/repository.js +26 -4
  152. package/build/src/git/scratch.d.ts +1 -1
  153. package/build/src/git/scratch.js +19 -18
  154. package/build/src/git/target-placement.d.ts +33 -7
  155. package/build/src/git/target-placement.js +67 -18
  156. package/build/src/git/target.d.ts +32 -0
  157. package/build/src/git/target.js +84 -0
  158. package/build/src/git/terminal-reconcile.d.ts +1 -1
  159. package/build/src/git/terminal-reconcile.js +34 -6
  160. package/build/src/index.d.ts +9 -7
  161. package/build/src/index.js +1 -1
  162. package/build/src/library/address.d.ts +1 -20
  163. package/build/src/library/address.js +3 -24
  164. package/build/src/library/akuma-creation.d.ts +2 -5
  165. package/build/src/library/akuma-creation.js +60 -44
  166. package/build/src/library/akumas.d.ts +3 -9
  167. package/build/src/library/akumas.js +12 -25
  168. package/build/src/library/bind.d.ts +21 -9
  169. package/build/src/library/bind.js +5 -1
  170. package/build/src/library/continuation.d.ts +130 -155
  171. package/build/src/library/continuation.js +1 -4
  172. package/build/src/library/contract-composition.d.ts +4 -9
  173. package/build/src/library/contract-composition.js +8 -12
  174. package/build/src/library/contract-creation.d.ts +1 -3
  175. package/build/src/library/contract-creation.js +2 -3
  176. package/build/src/library/contract-execution.d.ts +3 -5
  177. package/build/src/library/contract-execution.js +55 -96
  178. package/build/src/library/contract-outcomes.d.ts +2 -1
  179. package/build/src/library/contract-types.d.ts +5 -0
  180. package/build/src/library/contract-wait.d.ts +35 -0
  181. package/build/src/library/contract-wait.js +141 -0
  182. package/build/src/library/delivery.d.ts +13 -525
  183. package/build/src/library/delivery.js +1 -12
  184. package/build/src/library/input.js +2 -1
  185. package/build/src/library/invocation.d.ts +5 -1
  186. package/build/src/library/invocation.js +9 -2
  187. package/build/src/library/keiyaku.d.ts +7 -6
  188. package/build/src/library/keiyaku.js +12 -7
  189. package/build/src/library/outcome.d.ts +461 -12586
  190. package/build/src/library/outcome.js +15 -234
  191. package/build/src/library/reconcile.d.ts +12 -0
  192. package/build/src/library/reconcile.js +1 -0
  193. package/build/src/library/refusal.d.ts +251 -34
  194. package/build/src/library/refusal.js +10 -5
  195. package/build/src/library/selection.d.ts +9 -35
  196. package/build/src/library/selection.js +59 -129
  197. package/build/src/plugin/akuma-signals.d.ts +2 -2
  198. package/build/src/plugin/akuma-signals.js +2 -2
  199. package/build/src/plugin/public.d.ts +0 -7
  200. package/build/src/plugin/runtime.d.ts +2 -2
  201. package/build/src/plugin/runtime.js +4 -3
  202. package/build/src/protocol/attempt.js +7 -3
  203. package/build/src/protocol/bind.d.ts +18 -4
  204. package/build/src/protocol/bind.js +102 -35
  205. package/build/src/protocol/completion.d.ts +18 -331
  206. package/build/src/protocol/completion.js +17 -130
  207. package/build/src/protocol/deliver.d.ts +3 -3
  208. package/build/src/protocol/deliver.js +64 -74
  209. package/build/src/protocol/delivery-preview.d.ts +85 -28
  210. package/build/src/protocol/delivery-preview.js +34 -22
  211. package/build/src/protocol/execution-observation.d.ts +12 -9
  212. package/build/src/protocol/execution-observation.js +0 -1
  213. package/build/src/protocol/intent.d.ts +20 -50
  214. package/build/src/protocol/intent.js +37 -44
  215. package/build/src/protocol/operations.d.ts +299 -19
  216. package/build/src/protocol/operations.js +68 -27
  217. package/build/src/protocol/outcome.d.ts +1 -1
  218. package/build/src/protocol/placement.d.ts +2 -2
  219. package/build/src/protocol/placement.js +4 -3
  220. package/build/src/protocol/progress.d.ts +35 -2
  221. package/build/src/protocol/progress.js +12 -1
  222. package/build/src/protocol/read/observation.js +15 -4
  223. package/build/src/protocol/read/status.d.ts +9 -14
  224. package/build/src/protocol/read/status.js +15 -22
  225. package/build/src/protocol/reintegrate.d.ts +4 -3
  226. package/build/src/protocol/reintegrate.js +21 -5
  227. package/build/src/protocol/review.d.ts +9 -15
  228. package/build/src/protocol/review.js +2 -6
  229. package/build/src/protocol/run.d.ts +1 -1
  230. package/build/src/protocol/verify.d.ts +51 -0
  231. package/build/src/protocol/verify.js +128 -0
  232. package/build/src/verification/declaration.d.ts +5 -1
  233. package/build/src/verification/declaration.js +19 -2
  234. package/build/src/verification/execution.d.ts +5 -2
  235. package/build/src/verification/execution.js +48 -53
  236. package/build/src/verification/observation.d.ts +10 -6
  237. package/build/src/verification/observation.js +5 -2
  238. package/package.json +3 -3
  239. package/plugins/paseo/plugin/index.server.ts +3 -0
  240. package/plugins/paseo/plugin/server/attach.ts +42 -0
  241. package/plugins/paseo/plugin/server/mirror-identity.ts +51 -0
  242. package/plugins/paseo/plugin/server/model.ts +26 -0
  243. package/plugins/paseo/plugin/server/provider.ts +4 -6
  244. package/plugins/paseo/plugin/server/reopen.ts +4 -39
  245. package/plugins/pi/README.md +7 -12
  246. package/plugins/pi/akuma-tools.ts +3 -18
  247. package/skills/keiyaku-akuma/SKILL.md +39 -4
  248. package/skills/keiyaku-akuma/references/automation.md +4 -7
  249. package/skills/keiyaku-babysit/SKILL.md +1 -1
  250. package/skills/keiyaku-bind/SKILL.md +39 -15
  251. package/skills/keiyaku-workflow/SKILL.md +42 -22
  252. package/build/src/akuma/allowed.d.ts +0 -23
  253. package/build/src/akuma/allowed.js +0 -63
  254. package/build/src/akuma/call-request.d.ts +0 -308
  255. package/build/src/akuma/call-request.js +0 -175
  256. package/build/src/akuma/heart/request-authority.d.ts +0 -16
  257. package/build/src/akuma/heart/request-authority.js +0 -140
  258. package/build/src/akuma/heart/request-rows.d.ts +0 -18
  259. package/build/src/akuma/heart/request-rows.js +0 -107
  260. package/build/src/akuma/request-lifecycle.d.ts +0 -42
  261. package/build/src/akuma/request-lifecycle.js +0 -197
  262. package/build/src/akuma/request-observation.d.ts +0 -45
  263. package/build/src/akuma/request-observation.js +0 -162
  264. package/build/src/akuma/request-rendezvous.d.ts +0 -32
  265. package/build/src/akuma/request-rendezvous.js +0 -189
  266. package/build/src/akuma/request-serve.d.ts +0 -16
  267. package/build/src/akuma/request-serve.js +0 -188
  268. package/build/src/akuma/request-wire.d.ts +0 -143
  269. package/build/src/akuma/request-wire.js +0 -146
  270. package/build/src/akuma/requests.d.ts +0 -20
  271. package/build/src/akuma/requests.js +0 -72
  272. package/build/src/akuma/selection-owner-port.d.ts +0 -14
  273. package/build/src/akuma/selection-owner-port.js +0 -54
  274. package/build/src/akuma/selection-request.d.ts +0 -305
  275. package/build/src/akuma/selection-request.js +0 -307
  276. package/build/src/body-requests.d.ts +0 -13
  277. package/build/src/body-requests.js +0 -131
  278. package/build/src/cli/square-identity.d.ts +0 -10
  279. package/build/src/cli/square-identity.js +0 -130
  280. package/build/src/debt/mutation-result.d.ts +0 -981
  281. package/build/src/debt/mutation-result.js +0 -109
  282. package/build/src/debt/mutation.d.ts +0 -424
  283. package/build/src/debt/mutation.js +0 -223
  284. package/build/src/library/contract-operations.d.ts +0 -382
  285. package/build/src/library/contract-operations.js +0 -300
@@ -1,189 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { access, readFile } from "node:fs/promises";
3
- import { abortableDelay } from "./abort.js";
4
- import { publishRequestCancellation, readRequestProgress, } from "./request-observation.js";
5
- import { atomicJson, decodeReceiptEnvelope, receiptPath, requestPath } from "./request-wire.js";
6
- const POLL_MS = 100;
7
- export class AkumaBodyRequestError extends Error {
8
- action;
9
- outcome;
10
- diagnostic;
11
- requestId;
12
- kind = "akuma-body-request";
13
- constructor(action, outcome, diagnostic, requestId, options) {
14
- super(`${action} ${outcome === "refused" ? "refused" : outcome}: ${diagnostic}`, options);
15
- this.action = action;
16
- this.outcome = outcome;
17
- this.diagnostic = diagnostic;
18
- this.requestId = requestId;
19
- this.name = "AkumaBodyRequestError";
20
- }
21
- }
22
- function decodeOwnerFailure(failure, decodeFailure) {
23
- if (failure === undefined || decodeFailure === undefined)
24
- return null;
25
- try {
26
- return decodeFailure(failure);
27
- }
28
- catch {
29
- return null;
30
- }
31
- }
32
- function withRequestMetadata(response, id, action) {
33
- Object.defineProperties(response, {
34
- requestId: { value: id, enumerable: false },
35
- action: { value: action, enumerable: false },
36
- });
37
- return response;
38
- }
39
- async function readRequestReceipt(input, path, id) {
40
- let bytes;
41
- try {
42
- bytes = await readFile(path, "utf8");
43
- }
44
- catch (error) {
45
- if (error.code === "ENOENT")
46
- return undefined;
47
- throw error;
48
- }
49
- return decodeRequestReceipt(input.command, bytes, id);
50
- }
51
- function decodeRequestReceipt(command, bytes, id) {
52
- const receipt = decodeReceiptEnvelope(bytes, id, command.action);
53
- if (receipt === null)
54
- throw new Error(`Akuma body request ${id} has an invalid receipt`);
55
- if (receipt.state === "refused") {
56
- throw new AkumaBodyRequestError(receipt.action, "refused", receipt.diagnostic, receipt.id);
57
- }
58
- if (receipt.state === "unproven" || (receipt.state === "voided" && receipt.outcome === undefined)) {
59
- const failure = decodeOwnerFailure(receipt.failure, command.decodeFailure);
60
- if (receipt.state === "voided" && failure !== null)
61
- throw withRequestMetadata(failure, id, command.action);
62
- throw new AkumaBodyRequestError(command.action, receipt.state, receipt.evidence, id, failure === null ? undefined : { cause: failure });
63
- }
64
- if (receipt.state === "served" && "outcome" in receipt && receipt.outcome.kind === "failed") {
65
- const failure = receipt.outcome.failure;
66
- throw new Error(failure.kind === "failed" ? failure.diagnostic : `Akuma ${failure.id} was not born`);
67
- }
68
- const label = "reference" in receipt ? "durable reference" : receipt.state === "voided" ? "voided live result" : "live result";
69
- try {
70
- if ("reference" in receipt) {
71
- return withRequestMetadata({ kind: "reference", reference: command.decodeReference(receipt.reference) }, id, command.action);
72
- }
73
- const value = receipt.state === "voided"
74
- ? receipt.outcome
75
- : receipt.outcome.kind === "returned"
76
- ? receipt.outcome.result
77
- : undefined;
78
- return withRequestMetadata({ kind: "returned", result: command.decodeResult(value) }, id, command.action);
79
- }
80
- catch (error) {
81
- throw new Error(`transport integrity: request ${id} action ${command.action} returned an invalid ${label}: ${error instanceof Error ? error.message : String(error)}`);
82
- }
83
- }
84
- /** Generic rendezvous only: operation owners supply their own request and result codecs. */
85
- export async function requestBodyCommand(input) {
86
- const id = input.id ?? randomUUID();
87
- input.signal?.throwIfAborted();
88
- const transportId = randomUUID();
89
- const payload = input.command.encodeRequest(input.value);
90
- try {
91
- await atomicJson(requestPath(input.directory, transportId), {
92
- id,
93
- action: input.command.action,
94
- payload,
95
- }, input.signal);
96
- }
97
- catch (error) {
98
- if (error.code !== "ENOENT")
99
- throw error;
100
- if (input.signal?.aborted)
101
- input.signal.throwIfAborted();
102
- throw new AkumaBodyRequestError(input.command.action, "unknown", "parent request channel closed before request publication", id);
103
- }
104
- const path = receiptPath(input.directory, transportId);
105
- return await observePublishedRequest(input, id, transportId, path);
106
- }
107
- async function observePublishedRequest(input, id, transportId, path) {
108
- let observedSequence = 0;
109
- const consumeProgress = async () => {
110
- if (input.onProgress === undefined && input.onProgressGap === undefined)
111
- return;
112
- const snapshot = await readRequestProgress({
113
- directory: input.directory,
114
- transportId,
115
- id,
116
- action: input.command.action,
117
- });
118
- observedSequence = consumeSnapshot(snapshot, observedSequence, input.onProgress, input.onProgressGap);
119
- };
120
- const publishCancellation = () => {
121
- void publishRequestCancellation({ directory: input.directory, transportId, id, action: input.command.action });
122
- };
123
- const cancellable = input.command.supportsCancellation === true;
124
- if (cancellable && input.signal !== undefined) {
125
- input.signal.addEventListener("abort", publishCancellation, { once: true });
126
- if (input.signal.aborted)
127
- publishCancellation();
128
- }
129
- try {
130
- for (;;) {
131
- let response;
132
- try {
133
- response = await readRequestReceipt(input, path, id);
134
- }
135
- finally {
136
- // A visible receipt guarantees its final progress was already published, even on failure.
137
- await consumeProgress();
138
- }
139
- if (response !== undefined)
140
- return response;
141
- if (!(await access(input.directory).then(() => true, () => false))) {
142
- throw new AkumaBodyRequestError(input.command.action, "unknown", "parent request channel closed before a receipt", id);
143
- }
144
- try {
145
- await abortableDelay(POLL_MS, cancellable ? undefined : input.signal);
146
- }
147
- catch (error) {
148
- if (input.signal?.aborted) {
149
- throw new AkumaBodyRequestError(input.command.action, "unknown", "request was cancelled after publication and before a receipt", id);
150
- }
151
- throw error;
152
- }
153
- }
154
- }
155
- finally {
156
- if (cancellable && input.signal !== undefined)
157
- input.signal.removeEventListener("abort", publishCancellation);
158
- }
159
- }
160
- function consumeSnapshot(snapshot, observedSequence, onProgress, onProgressGap) {
161
- if (snapshot === undefined)
162
- return observedSequence;
163
- const events = snapshot.events.filter((event) => event.sequence > observedSequence);
164
- const latestSequence = snapshot.nextSequence - 1;
165
- const firstAvailable = events[0]?.sequence ?? latestSequence + 1;
166
- const gap = firstAvailable - observedSequence - 1;
167
- if (gap > 0) {
168
- notifyObserver(onProgressGap, gap);
169
- }
170
- if (events.length === 0)
171
- return Math.max(observedSequence, latestSequence);
172
- for (const event of events) {
173
- observedSequence = event.sequence;
174
- notifyObserver(onProgress, event.value);
175
- }
176
- return observedSequence;
177
- }
178
- function notifyObserver(observer, value) {
179
- if (observer === undefined)
180
- return;
181
- try {
182
- void Promise.resolve(observer(value)).catch(() => {
183
- // A caller observation failure cannot affect its in-flight request.
184
- });
185
- }
186
- catch {
187
- // A caller observation failure cannot affect its in-flight request.
188
- }
189
- }
@@ -1,16 +0,0 @@
1
- import { type ErasedRequestCommand, type RequestEnvelope } from "./request-wire.js";
2
- import { BodyRequestPump as LifecycleBodyRequestPump, type PumpInput as LifecyclePumpInput } from "./request-lifecycle.js";
3
- export { clearBodyRequestTransport, settleBodyRequests } from "./request-lifecycle.js";
4
- type PumpInput = LifecyclePumpInput & Readonly<{
5
- commands: Readonly<Record<string, ErasedRequestCommand>>;
6
- }>;
7
- export type ServeInput = Omit<PumpInput, "bodySequence"> & Readonly<{
8
- directory: string;
9
- transportId: string;
10
- claim: RequestEnvelope;
11
- admissionOpen(): boolean;
12
- }>;
13
- export declare function serveRequest(input: ServeInput): Promise<void>;
14
- export declare class BodyRequestPump extends LifecycleBodyRequestPump {
15
- static open(input: PumpInput): Promise<BodyRequestPump>;
16
- }
@@ -1,188 +0,0 @@
1
- import { admitRequest, beginRequest, isRequestInputConflict, readHeart, readRequest, serveRequest as serveChildRequest, serveUpstreamRequest, voidRequest, unproveRequest, } from "./heart/index.js";
2
- import { atomicJson, receiptPath, } from "./request-wire.js";
3
- import { observeRequestCancellation, publishRequestProgress } from "./request-observation.js";
4
- import { BodyRequestPump as LifecycleBodyRequestPump, settleReserved, } from "./request-lifecycle.js";
5
- export { clearBodyRequestTransport, settleBodyRequests } from "./request-lifecycle.js";
6
- function replayReference(fact, command) {
7
- if (fact.state !== "served") {
8
- throw new Error(`request ${fact.id} completion corruption: cannot replay ${fact.state} request`);
9
- }
10
- switch (command.completion) {
11
- case "child":
12
- if (!("child" in fact)) {
13
- throw new Error(`request ${fact.id} completion corruption: child command has a service settlement`);
14
- }
15
- return command.projectChild(fact.child);
16
- case "service":
17
- if (!("serviceJson" in fact)) {
18
- throw new Error(`request ${fact.id} completion corruption: service command has a child settlement`);
19
- }
20
- return command.projectServiceJson(fact.serviceJson);
21
- }
22
- }
23
- async function projectCommandReceipt(input, fact, command, outcome, failure) {
24
- const identity = { id: fact.id, action: fact.action };
25
- const receipt = fact.state === "refused"
26
- ? { ...identity, state: fact.state, diagnostic: fact.diagnostic }
27
- : fact.state === "voided" || fact.state === "unproven"
28
- ? {
29
- ...identity,
30
- state: fact.state,
31
- evidence: fact.evidence,
32
- ...(failure === undefined ? {} : { failure }),
33
- ...(fact.state === "voided" && outcome !== undefined ? { outcome: outcome.result } : {}),
34
- }
35
- : {
36
- ...identity,
37
- state: "served",
38
- ...(outcome === undefined ? { reference: replayReference(fact, command) } : { outcome }),
39
- };
40
- try {
41
- await atomicJson(receiptPath(input.directory, input.transportId), receipt);
42
- }
43
- catch (error) {
44
- if (error.code !== "ENOENT")
45
- throw error;
46
- }
47
- }
48
- async function serveResolvedCommand(input, command, request, execute) {
49
- let fact = await admitRequest(input.paths, {
50
- id: input.claim.id,
51
- action: input.claim.action,
52
- payloadJson: request.payloadJson,
53
- admittedAt: input.now(),
54
- permitted: request.isPermitted(input.allowed),
55
- });
56
- if (fact.state === "admitted" && !input.admissionOpen()) {
57
- fact = await voidRequest(input.paths, fact.id, "body closed request admission");
58
- }
59
- if (fact.state !== "admitted") {
60
- await projectCommandReceipt(input, fact, command);
61
- return;
62
- }
63
- const observation = {
64
- directory: input.directory,
65
- transportId: input.transportId,
66
- id: fact.id,
67
- action: fact.action,
68
- };
69
- const progress = publishRequestProgress(observation);
70
- const cancellation = command.completion === "service" && request.supportsCancellation
71
- ? observeRequestCancellation(observation)
72
- : undefined;
73
- let outcome;
74
- let failure;
75
- try {
76
- const served = await execute(fact, {
77
- id: fact.id,
78
- admittedAt: fact.admittedAt,
79
- requester: fact.requester,
80
- signal: cancellation === undefined ? input.signal : AbortSignal.any([input.signal, cancellation.signal]),
81
- admissionOpen: input.admissionOpen,
82
- progress: progress.progress,
83
- });
84
- fact = served.fact;
85
- outcome = served.outcome;
86
- }
87
- catch (error) {
88
- failure = encodedFailure(request, error);
89
- const current = await readRequest(input.paths, fact.id);
90
- if (current === null)
91
- throw error;
92
- if (current.state === "reserved") {
93
- const parent = (await readHeart(input.paths)).soul;
94
- if (parent === null)
95
- throw new Error("reserved request settlement requires a born parent Soul");
96
- if (!(await settleReserved(input.paths, parent, current, input.now, input.signal)))
97
- return;
98
- fact = (await readRequest(input.paths, fact.id)) ?? current;
99
- }
100
- else {
101
- fact =
102
- current.state === "admitted"
103
- ? await voidRequest(input.paths, fact.id, diagnostic(error))
104
- : current.state === "begun"
105
- ? provesNoProductEffect(failure)
106
- ? await voidRequest(input.paths, fact.id, diagnostic(error))
107
- : await unproveRequest(input.paths, fact.id, diagnostic(error))
108
- : current;
109
- }
110
- }
111
- finally {
112
- try {
113
- await progress.close();
114
- }
115
- finally {
116
- await cancellation?.close();
117
- }
118
- }
119
- // Transport failure cannot re-enter execution settlement.
120
- await projectCommandReceipt(input, fact, command, outcome, failure);
121
- }
122
- async function serveCommand(input, command) {
123
- const { completion, request } = command.completion === "child"
124
- ? { completion: "child", request: command.resolve(input.claim.payload) }
125
- : { completion: "service", request: command.resolve(input.claim.payload) };
126
- if (request === null)
127
- throw new Error(`registered request action ${input.claim.action} rejected its payload`);
128
- return await serveResolvedCommand(input, command, request, async (fact, facts) => {
129
- if (completion === "child") {
130
- const served = await request.execute(facts);
131
- return {
132
- fact: await serveChildRequest(input.paths, fact.id, served.child),
133
- outcome: { kind: "returned", result: served.result },
134
- };
135
- }
136
- await beginRequest(input.paths, fact.id);
137
- const served = await request.execute(facts);
138
- return served.kind === "voided"
139
- ? {
140
- fact: await voidRequest(input.paths, fact.id, "returned owner outcome was voided"),
141
- outcome: { kind: "returned", result: served.outcome },
142
- }
143
- : {
144
- fact: await serveUpstreamRequest(input.paths, fact.id, served.serviceJson),
145
- outcome: { kind: "returned", result: served.result },
146
- };
147
- });
148
- }
149
- function diagnostic(error) {
150
- return error instanceof Error ? error.message : String(error);
151
- }
152
- function encodedFailure(request, error) {
153
- try {
154
- const failure = request.encodeFailure(error);
155
- return failure === null ? undefined : failure;
156
- }
157
- catch {
158
- return undefined;
159
- }
160
- }
161
- function provesNoProductEffect(failure) {
162
- return failure !== null && typeof failure === "object" && failure.kind === "refused";
163
- }
164
- export async function serveRequest(input) {
165
- const command = input.commands[input.claim.action];
166
- let diagnostic = `request action ${input.claim.action} is not registered`;
167
- if (command !== undefined) {
168
- try {
169
- return await serveCommand(input, command);
170
- }
171
- catch (error) {
172
- if (!isRequestInputConflict(error))
173
- throw error;
174
- diagnostic = error.message;
175
- }
176
- }
177
- await atomicJson(receiptPath(input.directory, input.transportId), {
178
- id: input.claim.id,
179
- action: input.claim.action,
180
- state: "refused",
181
- diagnostic,
182
- });
183
- }
184
- export class BodyRequestPump extends LifecycleBodyRequestPump {
185
- static async open(input) {
186
- return (await LifecycleBodyRequestPump.openWithService(input, async (claim) => await serveRequest({ ...claim, commands: input.commands })));
187
- }
188
- }
@@ -1,143 +0,0 @@
1
- import { z } from "zod";
2
- declare const requestEnvelopeSchema: z.ZodObject<{
3
- id: z.ZodString;
4
- action: z.ZodString;
5
- payload: z.ZodUnknown;
6
- }, z.core.$strict>;
7
- declare const receiptEnvelopeSchema: z.ZodUnion<readonly [z.ZodObject<{
8
- id: z.ZodString;
9
- action: z.ZodString;
10
- state: z.ZodLiteral<"served">;
11
- outcome: z.ZodUnion<readonly [z.ZodObject<{
12
- kind: z.ZodLiteral<"returned">;
13
- result: z.ZodUnknown;
14
- }, z.core.$strict>, z.ZodObject<{
15
- kind: z.ZodLiteral<"failed">;
16
- failure: z.ZodUnion<readonly [z.ZodObject<{
17
- kind: z.ZodLiteral<"akuma-not-born">;
18
- id: z.ZodString;
19
- }, z.core.$strict>, z.ZodObject<{
20
- kind: z.ZodLiteral<"failed">;
21
- diagnostic: z.ZodString;
22
- }, z.core.$strict>]>;
23
- }, z.core.$strict>]>;
24
- }, z.core.$strict>, z.ZodObject<{
25
- id: z.ZodString;
26
- action: z.ZodString;
27
- state: z.ZodLiteral<"served">;
28
- reference: z.ZodUnknown;
29
- }, z.core.$strict>, z.ZodObject<{
30
- id: z.ZodString;
31
- action: z.ZodString;
32
- state: z.ZodLiteral<"refused">;
33
- diagnostic: z.ZodString;
34
- }, z.core.$strict>, z.ZodObject<{
35
- id: z.ZodString;
36
- action: z.ZodString;
37
- state: z.ZodLiteral<"voided">;
38
- evidence: z.ZodString;
39
- failure: z.ZodOptional<z.ZodUnknown>;
40
- outcome: z.ZodOptional<z.ZodUnknown>;
41
- }, z.core.$strict>, z.ZodObject<{
42
- id: z.ZodString;
43
- action: z.ZodString;
44
- state: z.ZodLiteral<"unproven">;
45
- evidence: z.ZodString;
46
- failure: z.ZodOptional<z.ZodUnknown>;
47
- }, z.core.$strict>]>;
48
- export type RequestEnvelope = z.infer<typeof requestEnvelopeSchema>;
49
- /**
50
- * A Body Request operation is owned by the verb that defines its public
51
- * values. Transport only carries the opaque values produced here.
52
- */
53
- export type RequestProtocol<Input, Output, Reference> = Readonly<{
54
- action: string;
55
- /** An opted-in service request accepts cancellation after publication. */
56
- supportsCancellation?: boolean;
57
- encodeRequest(input: Input): unknown;
58
- decodeRequest(payload: unknown): Input | null;
59
- encodeResult(result: Output): unknown;
60
- decodeResult(result: unknown): Output;
61
- encodeFailure?(error: unknown): unknown | null;
62
- decodeFailure?(failure: unknown): Error | null;
63
- decodeReference(reference: unknown): Reference;
64
- isPermitted(allowed: readonly string[]): boolean;
65
- }>;
66
- export type ExecutionFacts = Readonly<{
67
- id: string;
68
- admittedAt: string;
69
- requester: string;
70
- signal: AbortSignal;
71
- admissionOpen(): boolean;
72
- /** Best-effort, ephemeral progress for this one live request. */
73
- progress?(value: unknown): void;
74
- }>;
75
- export type ChildRequestCommand<Input, Output, Reference> = Readonly<{
76
- completion: "child";
77
- protocol: RequestProtocol<Input, Output, Reference>;
78
- projectChild(child: string): Reference;
79
- execute(input: Input, facts: ExecutionFacts): Promise<Readonly<{
80
- result: Output;
81
- child: string;
82
- }>>;
83
- }>;
84
- export type ServiceCompletion<Output, Service> = Readonly<{
85
- kind: "served";
86
- result: Output;
87
- service: Service;
88
- }> | Readonly<{
89
- kind: "voided";
90
- outcome: Output;
91
- }>;
92
- export type ServiceRequestCommand<Input, Output, Service, Reference> = Readonly<{
93
- completion: "service";
94
- protocol: RequestProtocol<Input, Output, Reference>;
95
- encodeService(service: Service): unknown;
96
- decodeService(service: unknown): Service;
97
- projectService(service: Service): Reference;
98
- execute(input: Input, facts: ExecutionFacts): Promise<ServiceCompletion<Output, Service>>;
99
- }>;
100
- type ErasedRequest = Readonly<{
101
- payloadJson: string;
102
- supportsCancellation: boolean;
103
- isPermitted(allowed: readonly string[]): boolean;
104
- encodeFailure(error: unknown): unknown | null;
105
- }>;
106
- type ErasedChildRequest = ErasedRequest & Readonly<{
107
- execute(facts: ExecutionFacts): Promise<Readonly<{
108
- result: unknown;
109
- child: string;
110
- }>>;
111
- }>;
112
- type ErasedServiceRequest = ErasedRequest & Readonly<{
113
- execute(facts: ExecutionFacts): Promise<Readonly<{
114
- kind: "served";
115
- result: unknown;
116
- serviceJson: string;
117
- }> | Readonly<{
118
- kind: "voided";
119
- outcome: unknown;
120
- }>>;
121
- }>;
122
- export type ErasedRequestCommand = Readonly<{
123
- action: string;
124
- completion: "child";
125
- projectChild(child: string): unknown;
126
- resolve(payload: unknown): ErasedChildRequest | null;
127
- }> | Readonly<{
128
- action: string;
129
- completion: "service";
130
- projectServiceJson(serviceJson: string): unknown;
131
- resolve(payload: unknown): ErasedServiceRequest | null;
132
- }>;
133
- /** Erases only static TypeScript parameters; each operation codec remains captured by its owner descriptor. */
134
- export declare function eraseRequestCommand<Input, Output, Reference>(command: ChildRequestCommand<Input, Output, Reference>): ErasedRequestCommand;
135
- export declare function eraseRequestCommand<Input, Output, Service, Reference>(command: ServiceRequestCommand<Input, Output, Service, Reference>): ErasedRequestCommand;
136
- export declare function composeRequestCommands(...maps: readonly Readonly<Record<string, ErasedRequestCommand>>[]): Readonly<Record<string, ErasedRequestCommand>>;
137
- export type ReceiptEnvelope = z.infer<typeof receiptEnvelopeSchema>;
138
- export declare function decodeEnvelope(bytes: string, fileId: string): RequestEnvelope | null;
139
- export declare function decodeReceiptEnvelope(bytes: string, id: string, action: string): ReceiptEnvelope | null;
140
- export declare function requestPath(directory: string, id: string): string;
141
- export declare function receiptPath(directory: string, id: string): string;
142
- export declare function atomicJson(path: string, value: unknown, signal?: AbortSignal, beforeRename?: () => void | Promise<void>): Promise<void>;
143
- export {};
@@ -1,146 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { rename, rm, writeFile } from "node:fs/promises";
3
- import { join } from "node:path";
4
- import { z } from "zod";
5
- const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u;
6
- const requestIdSchema = z.string().regex(UUID);
7
- const actionSchema = z.string().refine((value) => value.trim() !== "");
8
- const requestEnvelopeSchema = z.object({ id: requestIdSchema, action: actionSchema, payload: z.unknown() }).strict();
9
- const upstreamRequestFailureSchema = z.union([
10
- z.object({ kind: z.literal("akuma-not-born"), id: z.string() }).strict(),
11
- z.object({ kind: z.literal("failed"), diagnostic: z.string() }).strict(),
12
- ]);
13
- const returnedEnvelopeSchema = z.union([
14
- z.object({ kind: z.literal("returned"), result: z.unknown() }).strict(),
15
- z.object({ kind: z.literal("failed"), failure: upstreamRequestFailureSchema }).strict(),
16
- ]);
17
- const receiptEnvelopeSchema = z.union([
18
- z
19
- .object({ id: requestIdSchema, action: actionSchema, state: z.literal("served"), outcome: returnedEnvelopeSchema })
20
- .strict(),
21
- z.object({ id: requestIdSchema, action: actionSchema, state: z.literal("served"), reference: z.unknown() }).strict(),
22
- z.object({ id: requestIdSchema, action: actionSchema, state: z.literal("refused"), diagnostic: z.string() }).strict(),
23
- z
24
- .object({
25
- id: requestIdSchema,
26
- action: actionSchema,
27
- state: z.literal("voided"),
28
- evidence: z.string(),
29
- failure: z.unknown().optional(),
30
- outcome: z.unknown().optional(),
31
- })
32
- .strict(),
33
- z
34
- .object({
35
- id: requestIdSchema,
36
- action: actionSchema,
37
- state: z.literal("unproven"),
38
- evidence: z.string(),
39
- failure: z.unknown().optional(),
40
- })
41
- .strict(),
42
- ]);
43
- export function eraseRequestCommand(command) {
44
- const { protocol } = command;
45
- switch (command.completion) {
46
- case "child":
47
- return {
48
- action: protocol.action,
49
- completion: "child",
50
- projectChild: command.projectChild,
51
- resolve: (payload) => {
52
- const input = protocol.decodeRequest(payload);
53
- if (input === null)
54
- return null;
55
- return {
56
- payloadJson: JSON.stringify(protocol.encodeRequest(input)),
57
- supportsCancellation: protocol.supportsCancellation === true,
58
- isPermitted: (allowed) => protocol.isPermitted(allowed),
59
- execute: async (facts) => {
60
- const served = await command.execute(input, facts);
61
- return { result: protocol.encodeResult(served.result), child: served.child };
62
- },
63
- encodeFailure: (error) => protocol.encodeFailure?.(error) ?? null,
64
- };
65
- },
66
- };
67
- case "service":
68
- return {
69
- action: protocol.action,
70
- completion: "service",
71
- projectServiceJson: (serviceJson) => command.projectService(command.decodeService(JSON.parse(serviceJson))),
72
- resolve: (payload) => {
73
- const input = protocol.decodeRequest(payload);
74
- if (input === null)
75
- return null;
76
- return {
77
- payloadJson: JSON.stringify(protocol.encodeRequest(input)),
78
- supportsCancellation: protocol.supportsCancellation === true,
79
- isPermitted: (allowed) => protocol.isPermitted(allowed),
80
- execute: async (facts) => {
81
- const served = await command.execute(input, facts);
82
- if (served.kind === "voided")
83
- return { kind: "voided", outcome: protocol.encodeResult(served.outcome) };
84
- return {
85
- kind: "served",
86
- result: protocol.encodeResult(served.result),
87
- serviceJson: JSON.stringify(command.encodeService(served.service)),
88
- };
89
- },
90
- encodeFailure: (error) => protocol.encodeFailure?.(error) ?? null,
91
- };
92
- },
93
- };
94
- }
95
- }
96
- export function composeRequestCommands(...maps) {
97
- const commands = Object.create(null);
98
- for (const map of maps) {
99
- for (const [action, command] of Object.entries(map)) {
100
- if (Object.hasOwn(commands, action))
101
- throw new Error(`duplicate request command action: ${action}`);
102
- commands[action] = command;
103
- }
104
- }
105
- return commands;
106
- }
107
- export function decodeEnvelope(bytes, fileId) {
108
- let decoded;
109
- try {
110
- decoded = JSON.parse(bytes);
111
- }
112
- catch {
113
- return null;
114
- }
115
- const parsed = requestEnvelopeSchema.safeParse(decoded);
116
- return UUID.test(fileId) && parsed.success ? parsed.data : null;
117
- }
118
- export function decodeReceiptEnvelope(bytes, id, action) {
119
- let decoded;
120
- try {
121
- decoded = JSON.parse(bytes);
122
- }
123
- catch {
124
- return null;
125
- }
126
- const parsed = receiptEnvelopeSchema.safeParse(decoded);
127
- return parsed.success && parsed.data.id === id && parsed.data.action === action ? parsed.data : null;
128
- }
129
- export function requestPath(directory, id) {
130
- return join(directory, `${id}.request.json`);
131
- }
132
- export function receiptPath(directory, id) {
133
- return join(directory, `${id}.receipt.json`);
134
- }
135
- export async function atomicJson(path, value, signal, beforeRename) {
136
- const temporary = `${path}.${randomUUID()}.tmp`;
137
- try {
138
- await writeFile(temporary, `${JSON.stringify(value)}\n`, { flag: "wx" });
139
- await beforeRename?.();
140
- signal?.throwIfAborted();
141
- await rename(temporary, path);
142
- }
143
- finally {
144
- await rm(temporary, { force: true });
145
- }
146
- }