@lushly-dev/afd-server 2.4.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/README.md +239 -56
  2. package/dist/admission.d.ts +137 -0
  3. package/dist/admission.d.ts.map +1 -0
  4. package/dist/admission.js +144 -0
  5. package/dist/admission.js.map +1 -0
  6. package/dist/app-call-registry.d.ts +31 -0
  7. package/dist/app-call-registry.d.ts.map +1 -0
  8. package/dist/app-call-registry.js +64 -0
  9. package/dist/app-call-registry.js.map +1 -0
  10. package/dist/approval-admission.d.ts +139 -0
  11. package/dist/approval-admission.d.ts.map +1 -0
  12. package/dist/approval-admission.js +252 -0
  13. package/dist/approval-admission.js.map +1 -0
  14. package/dist/approval-crypto.d.ts +37 -17
  15. package/dist/approval-crypto.d.ts.map +1 -1
  16. package/dist/approval-crypto.js +84 -75
  17. package/dist/approval-crypto.js.map +1 -1
  18. package/dist/approval-process-state.d.ts +21 -0
  19. package/dist/approval-process-state.d.ts.map +1 -0
  20. package/dist/approval-process-state.js +29 -0
  21. package/dist/approval-process-state.js.map +1 -0
  22. package/dist/approval-relay.d.ts +192 -9
  23. package/dist/approval-relay.d.ts.map +1 -1
  24. package/dist/approval-relay.js +45 -9
  25. package/dist/approval-relay.js.map +1 -1
  26. package/dist/approval-up-front.d.ts +40 -0
  27. package/dist/approval-up-front.d.ts.map +1 -0
  28. package/dist/approval-up-front.js +409 -0
  29. package/dist/approval-up-front.js.map +1 -0
  30. package/dist/approval.d.ts +65 -12
  31. package/dist/approval.d.ts.map +1 -1
  32. package/dist/approval.js +311 -47
  33. package/dist/approval.js.map +1 -1
  34. package/dist/approver.d.ts +84 -0
  35. package/dist/approver.d.ts.map +1 -0
  36. package/dist/approver.js +107 -0
  37. package/dist/approver.js.map +1 -0
  38. package/dist/auth-middleware-check.d.ts +23 -0
  39. package/dist/auth-middleware-check.d.ts.map +1 -0
  40. package/dist/auth-middleware-check.js +48 -0
  41. package/dist/auth-middleware-check.js.map +1 -0
  42. package/dist/bootstrap/afd-context.d.ts.map +1 -1
  43. package/dist/bootstrap/afd-context.js +27 -3
  44. package/dist/bootstrap/afd-context.js.map +1 -1
  45. package/dist/bootstrap/afd-docs.d.ts.map +1 -1
  46. package/dist/bootstrap/afd-docs.js +4 -0
  47. package/dist/bootstrap/afd-docs.js.map +1 -1
  48. package/dist/bootstrap/afd-help.d.ts +4 -1
  49. package/dist/bootstrap/afd-help.d.ts.map +1 -1
  50. package/dist/bootstrap/afd-help.js +19 -4
  51. package/dist/bootstrap/afd-help.js.map +1 -1
  52. package/dist/bootstrap/described-command.d.ts +3 -1
  53. package/dist/bootstrap/described-command.d.ts.map +1 -1
  54. package/dist/bootstrap/described-command.js.map +1 -1
  55. package/dist/command-allow.d.ts +4 -4
  56. package/dist/command-allow.d.ts.map +1 -1
  57. package/dist/command-allow.js +10 -15
  58. package/dist/command-allow.js.map +1 -1
  59. package/dist/command-routing.d.ts +56 -8
  60. package/dist/command-routing.d.ts.map +1 -1
  61. package/dist/command-routing.js +82 -16
  62. package/dist/command-routing.js.map +1 -1
  63. package/dist/command-timeout.d.ts +17 -0
  64. package/dist/command-timeout.d.ts.map +1 -0
  65. package/dist/command-timeout.js +99 -0
  66. package/dist/command-timeout.js.map +1 -0
  67. package/dist/confirmation-gate.d.ts +7 -7
  68. package/dist/confirmation-gate.d.ts.map +1 -1
  69. package/dist/confirmation-gate.js +26 -12
  70. package/dist/confirmation-gate.js.map +1 -1
  71. package/dist/confirmations.d.ts +28 -3
  72. package/dist/confirmations.d.ts.map +1 -1
  73. package/dist/confirmations.js +6 -7
  74. package/dist/confirmations.js.map +1 -1
  75. package/dist/context-fields.d.ts +16 -1
  76. package/dist/context-fields.d.ts.map +1 -1
  77. package/dist/context-fields.js +30 -13
  78. package/dist/context-fields.js.map +1 -1
  79. package/dist/context-scope.d.ts +3 -3
  80. package/dist/context-scope.d.ts.map +1 -1
  81. package/dist/context-scope.js +7 -8
  82. package/dist/context-scope.js.map +1 -1
  83. package/dist/define-core.d.ts +63 -1
  84. package/dist/define-core.d.ts.map +1 -1
  85. package/dist/define-core.js +27 -4
  86. package/dist/define-core.js.map +1 -1
  87. package/dist/define.d.ts +4 -3
  88. package/dist/define.d.ts.map +1 -1
  89. package/dist/define.js +1 -1
  90. package/dist/define.js.map +1 -1
  91. package/dist/direct-registry.d.ts +54 -8
  92. package/dist/direct-registry.d.ts.map +1 -1
  93. package/dist/direct-registry.js +64 -8
  94. package/dist/direct-registry.js.map +1 -1
  95. package/dist/direct-tool-surface.d.ts +1 -1
  96. package/dist/direct-tool-surface.d.ts.map +1 -1
  97. package/dist/direct-tool-surface.js +28 -6
  98. package/dist/direct-tool-surface.js.map +1 -1
  99. package/dist/direct.d.ts +6 -2
  100. package/dist/direct.d.ts.map +1 -1
  101. package/dist/direct.js +4 -1
  102. package/dist/direct.js.map +1 -1
  103. package/dist/elicitation-state.d.ts +5 -1
  104. package/dist/elicitation-state.d.ts.map +1 -1
  105. package/dist/elicitation-state.js +4 -1
  106. package/dist/elicitation-state.js.map +1 -1
  107. package/dist/execution.d.ts +52 -6
  108. package/dist/execution.d.ts.map +1 -1
  109. package/dist/execution.js +183 -19
  110. package/dist/execution.js.map +1 -1
  111. package/dist/host-state.d.ts +81 -0
  112. package/dist/host-state.d.ts.map +1 -0
  113. package/dist/host-state.js +62 -0
  114. package/dist/host-state.js.map +1 -0
  115. package/dist/http-handler.d.ts +48 -11
  116. package/dist/http-handler.d.ts.map +1 -1
  117. package/dist/http-handler.js +207 -58
  118. package/dist/http-handler.js.map +1 -1
  119. package/dist/http-security.d.ts +14 -1
  120. package/dist/http-security.d.ts.map +1 -1
  121. package/dist/http-security.js +79 -17
  122. package/dist/http-security.js.map +1 -1
  123. package/dist/http-sessions.d.ts +45 -5
  124. package/dist/http-sessions.d.ts.map +1 -1
  125. package/dist/http-sessions.js +52 -10
  126. package/dist/http-sessions.js.map +1 -1
  127. package/dist/http-sse.d.ts +9 -0
  128. package/dist/http-sse.d.ts.map +1 -1
  129. package/dist/http-sse.js +12 -3
  130. package/dist/http-sse.js.map +1 -1
  131. package/dist/human-relay.d.ts +195 -0
  132. package/dist/human-relay.d.ts.map +1 -0
  133. package/dist/human-relay.js +670 -0
  134. package/dist/human-relay.js.map +1 -0
  135. package/dist/idempotency-step.d.ts +231 -0
  136. package/dist/idempotency-step.d.ts.map +1 -0
  137. package/dist/idempotency-step.js +389 -0
  138. package/dist/idempotency-step.js.map +1 -0
  139. package/dist/idempotency-store.d.ts +107 -0
  140. package/dist/idempotency-store.d.ts.map +1 -0
  141. package/dist/idempotency-store.js +100 -0
  142. package/dist/idempotency-store.js.map +1 -0
  143. package/dist/index.d.ts +18 -8
  144. package/dist/index.d.ts.map +1 -1
  145. package/dist/index.js +15 -4
  146. package/dist/index.js.map +1 -1
  147. package/dist/instance-state.d.ts +53 -0
  148. package/dist/instance-state.d.ts.map +1 -0
  149. package/dist/instance-state.js +94 -0
  150. package/dist/instance-state.js.map +1 -0
  151. package/dist/journal-off-tape.d.ts +52 -0
  152. package/dist/journal-off-tape.d.ts.map +1 -0
  153. package/dist/journal-off-tape.js +150 -0
  154. package/dist/journal-off-tape.js.map +1 -0
  155. package/dist/journal-replay.d.ts.map +1 -1
  156. package/dist/journal-replay.js +48 -3
  157. package/dist/journal-replay.js.map +1 -1
  158. package/dist/journal-view.d.ts +50 -0
  159. package/dist/journal-view.d.ts.map +1 -0
  160. package/dist/journal-view.js +214 -0
  161. package/dist/journal-view.js.map +1 -0
  162. package/dist/journal.d.ts +34 -6
  163. package/dist/journal.d.ts.map +1 -1
  164. package/dist/journal.js +183 -47
  165. package/dist/journal.js.map +1 -1
  166. package/dist/json-rpc.d.ts +3 -2
  167. package/dist/json-rpc.d.ts.map +1 -1
  168. package/dist/json-rpc.js +5 -3
  169. package/dist/json-rpc.js.map +1 -1
  170. package/dist/lazy-tools.d.ts +27 -2
  171. package/dist/lazy-tools.d.ts.map +1 -1
  172. package/dist/lazy-tools.js +22 -4
  173. package/dist/lazy-tools.js.map +1 -1
  174. package/dist/mcp-auth-bearer.d.ts +2 -0
  175. package/dist/mcp-auth-bearer.d.ts.map +1 -1
  176. package/dist/mcp-auth-bearer.js +1 -1
  177. package/dist/mcp-auth-bearer.js.map +1 -1
  178. package/dist/mcp-auth-dispatch.d.ts +38 -0
  179. package/dist/mcp-auth-dispatch.d.ts.map +1 -0
  180. package/dist/mcp-auth-dispatch.js +113 -0
  181. package/dist/mcp-auth-dispatch.js.map +1 -0
  182. package/dist/mcp-auth-jwt.d.ts +3 -2
  183. package/dist/mcp-auth-jwt.d.ts.map +1 -1
  184. package/dist/mcp-auth-jwt.js.map +1 -1
  185. package/dist/mcp-auth-resource.d.ts +6 -2
  186. package/dist/mcp-auth-resource.d.ts.map +1 -1
  187. package/dist/mcp-auth-resource.js +6 -5
  188. package/dist/mcp-auth-resource.js.map +1 -1
  189. package/dist/mcp-auth-scopes.d.ts +84 -0
  190. package/dist/mcp-auth-scopes.d.ts.map +1 -0
  191. package/dist/mcp-auth-scopes.js +125 -0
  192. package/dist/mcp-auth-scopes.js.map +1 -0
  193. package/dist/mcp-auth-view.d.ts +50 -0
  194. package/dist/mcp-auth-view.d.ts.map +1 -0
  195. package/dist/mcp-auth-view.js +179 -0
  196. package/dist/mcp-auth-view.js.map +1 -0
  197. package/dist/mcp-auth.d.ts +94 -3
  198. package/dist/mcp-auth.d.ts.map +1 -1
  199. package/dist/mcp-auth.js +241 -23
  200. package/dist/mcp-auth.js.map +1 -1
  201. package/dist/mcp-modern.d.ts +80 -6
  202. package/dist/mcp-modern.d.ts.map +1 -1
  203. package/dist/mcp-modern.js +378 -91
  204. package/dist/mcp-modern.js.map +1 -1
  205. package/dist/meta-argument.d.ts +9 -8
  206. package/dist/meta-argument.d.ts.map +1 -1
  207. package/dist/meta-argument.js +28 -10
  208. package/dist/meta-argument.js.map +1 -1
  209. package/dist/meta-tools.js +2 -2
  210. package/dist/meta-tools.js.map +1 -1
  211. package/dist/middleware.d.ts +16 -1
  212. package/dist/middleware.d.ts.map +1 -1
  213. package/dist/middleware.js +57 -10
  214. package/dist/middleware.js.map +1 -1
  215. package/dist/output-budget.d.ts +77 -0
  216. package/dist/output-budget.d.ts.map +1 -0
  217. package/dist/output-budget.js +202 -0
  218. package/dist/output-budget.js.map +1 -0
  219. package/dist/principal-binding.js +1 -1
  220. package/dist/principal-binding.js.map +1 -1
  221. package/dist/relay-evidence.d.ts +74 -0
  222. package/dist/relay-evidence.d.ts.map +1 -0
  223. package/dist/relay-evidence.js +143 -0
  224. package/dist/relay-evidence.js.map +1 -0
  225. package/dist/resources.d.ts +35 -10
  226. package/dist/resources.d.ts.map +1 -1
  227. package/dist/resources.js +59 -18
  228. package/dist/resources.js.map +1 -1
  229. package/dist/schema.d.ts +4 -3
  230. package/dist/schema.d.ts.map +1 -1
  231. package/dist/schema.js +3 -3
  232. package/dist/schema.js.map +1 -1
  233. package/dist/scope-view.d.ts +65 -0
  234. package/dist/scope-view.d.ts.map +1 -0
  235. package/dist/scope-view.js +45 -0
  236. package/dist/scope-view.js.map +1 -0
  237. package/dist/server-types.d.ts +182 -27
  238. package/dist/server-types.d.ts.map +1 -1
  239. package/dist/server-types.js.map +1 -1
  240. package/dist/server.d.ts +26 -0
  241. package/dist/server.d.ts.map +1 -1
  242. package/dist/server.js +403 -61
  243. package/dist/server.js.map +1 -1
  244. package/dist/skills.d.ts.map +1 -1
  245. package/dist/skills.js +99 -3
  246. package/dist/skills.js.map +1 -1
  247. package/dist/task-dispatch.d.ts +35 -0
  248. package/dist/task-dispatch.d.ts.map +1 -0
  249. package/dist/task-dispatch.js +25 -0
  250. package/dist/task-dispatch.js.map +1 -0
  251. package/dist/tasks-runtime.d.ts +115 -0
  252. package/dist/tasks-runtime.d.ts.map +1 -0
  253. package/dist/tasks-runtime.js +760 -0
  254. package/dist/tasks-runtime.js.map +1 -0
  255. package/dist/tasks.d.ts +237 -0
  256. package/dist/tasks.d.ts.map +1 -0
  257. package/dist/tasks.js +321 -0
  258. package/dist/tasks.js.map +1 -0
  259. package/dist/tool-router.d.ts +68 -3
  260. package/dist/tool-router.d.ts.map +1 -1
  261. package/dist/tool-router.js +293 -70
  262. package/dist/tool-router.js.map +1 -1
  263. package/dist/tools.d.ts +32 -0
  264. package/dist/tools.d.ts.map +1 -1
  265. package/dist/tools.js +162 -55
  266. package/dist/tools.js.map +1 -1
  267. package/dist/up-front-steps.d.ts +35 -0
  268. package/dist/up-front-steps.d.ts.map +1 -0
  269. package/dist/up-front-steps.js +159 -0
  270. package/dist/up-front-steps.js.map +1 -0
  271. package/dist/validation.d.ts +38 -3
  272. package/dist/validation.d.ts.map +1 -1
  273. package/dist/validation.js +118 -23
  274. package/dist/validation.js.map +1 -1
  275. package/dist/verified-credentials.d.ts +13 -5
  276. package/dist/verified-credentials.d.ts.map +1 -1
  277. package/dist/verified-credentials.js +43 -10
  278. package/dist/verified-credentials.js.map +1 -1
  279. package/package.json +15 -11
@@ -0,0 +1,137 @@
1
+ /**
2
+ * @fileoverview One admission pipeline for every lane of the MCP server (#1743).
3
+ *
4
+ * Each lane (the MCP 2026-07-28 `/mcp` endpoint and stdio, the 2025-era `/message` lane, and the
5
+ * HTTP `/rpc`, `/batch` and `/stream` routes) only decodes its request into a {@link DecodedCall}
6
+ * and encodes the outcome. Admission runs the same named stages for all of them, in this order:
7
+ *
8
+ * 1. `evidence`: approval evidence in the arguments is refused, from any caller (rule 5 of
9
+ * "Approval evidence from an approver client", `spec/approvals.md`).
10
+ * 2. `meta`: a meta-tool's `meta` argument counts as `_meta` from here on.
11
+ * 3. `principal`: the call's context state, `createContext`'s values, the declared context fields
12
+ * and the output-budget ask (`createContext` wins over a declared field), and the actor and
13
+ * credentials the call starts with.
14
+ * 4. `approver`: whether the caller is an approver, and whether it may carry its evidence here
15
+ * (only on one command from an MCP 2026-07-28 client).
16
+ * 5. `bind`: the call's `CommandContext`: the values a handler sees, with the session, scope view,
17
+ * approver and the lane's channels in its host state (`host-state.ts`).
18
+ *
19
+ * Then the tool router (`tool-router.ts`) runs the rest, the same on every lane:
20
+ *
21
+ * 6. `scope`: MCP authorization (M6), a batch or pipeline as a whole;
22
+ * 7. `resolve`: the tool to its commands (one for `afd-call`, a grouped action or a command's own
23
+ * tool; several for `afd-batch` and `afd-pipe`; none for `afd-discover` and `afd-detail`);
24
+ * 8. `allow`, 9. `lookup` (D4: unexposed and hidden commands are unknown), 10. `context`
25
+ * (`COMMAND_NOT_IN_CONTEXT`), 11. `host-verified`, 12. `confirm`, 13. `task`;
26
+ * 14. `execute`: the engine (`spec/dispatch.md`, from input validation on).
27
+ *
28
+ * A stage that refuses ends the call with its error. A refusal of stages 1 to 5 is a `CommandResult`
29
+ * over MCP, whatever the tool (`spec/vectors/approver-client.json`), a `BatchResult` on `/batch`
30
+ * and an error chunk on `/stream`; the router shapes its own refusals as the tool's result. The
31
+ * HTTP routes decode as the meta-tools they stand for: `/rpc` and `/stream` as `afd-call`, and
32
+ * `/batch` as `afd-batch`, so one call gets the same answer on every lane.
33
+ *
34
+ * Unexpected errors (a throwing `createContext` or `approver` predicate, credentials that cannot
35
+ * be bound) are thrown to the lane, which answers them as an internal error.
36
+ */
37
+ import type { Actor, CommandContext, CommandError } from '@lushly-dev/afd-core';
38
+ import { type ApproverCall, type ApproverPredicate } from './approver.js';
39
+ import type { ContextState } from './bootstrap/afd-context.js';
40
+ import { type ContextField } from './context-fields.js';
41
+ import { type ScopeView } from './scope-view.js';
42
+ import type { RouteOutcome, RunAdmitted, ToolRouter } from './tool-router.js';
43
+ /** Which lane received a call: what it can carry differs, the stages do not. */
44
+ export type AdmissionLane =
45
+ /** MCP 2026-07-28: `POST /mcp` and a modern stdio connection. */
46
+ 'mcp'
47
+ /** A 2025-era MCP request: the `/message` lane, a legacy `/mcp` or stdio connection. */
48
+ | 'mcp-legacy'
49
+ /** The HTTP `/rpc`, `/batch` and `/stream` routes. */
50
+ | 'rest';
51
+ /** The stages admission runs before the router, by name. */
52
+ export declare const ADMISSION_STAGES: readonly ['evidence', 'meta', 'principal', 'approver', 'bind'];
53
+ /** Who a call runs as, as it starts. */
54
+ export interface CallPrincipal {
55
+ /** The actor the commands run with: `createContext`'s, or the server's default. */
56
+ readonly actor: Actor | undefined;
57
+ /** `createContext`'s (or the verified request's) credentials. */
58
+ readonly credentials: unknown;
59
+ /** The approver's call, when the caller is an approver. */
60
+ readonly approver: ApproverCall | undefined;
61
+ }
62
+ /** One call, as a lane decoded it. */
63
+ export interface DecodedCall {
64
+ readonly lane: AdmissionLane;
65
+ /** The tool called. `/rpc` and `/stream` decode as `afd-call`, `/batch` as `afd-batch`. */
66
+ readonly tool: string;
67
+ /** The tool's arguments, without `_meta`. */
68
+ readonly args: unknown;
69
+ /** The request's own `_meta`. */
70
+ readonly meta: unknown;
71
+ /**
72
+ * The caller's context state, given the call's `_meta` values (the `meta` stage's): a
73
+ * session's, a request's, `null` for a caller without one, or `undefined` for the server's own.
74
+ * May throw the lane's protocol error (an unknown context, an unknown session).
75
+ */
76
+ state(meta: unknown): ContextState | null | undefined;
77
+ /** `createContext`'s values for the request (none on stdio). */
78
+ values(): Promise<Record<string, unknown>> | Record<string, unknown>;
79
+ /** The request's scope view under MCP authorization (M6). */
80
+ readonly view?: ScopeView;
81
+ /** Aborts the call (the client went away). */
82
+ readonly signal?: AbortSignal;
83
+ /**
84
+ * How the lane tells callers apart, for the idempotency step (`spec/idempotency.md`, S3):
85
+ * `http` for an HTTP request, or the connection a stdio call arrived on. Absent: in process.
86
+ */
87
+ readonly transport?: {
88
+ readonly kind: 'http';
89
+ } | {
90
+ readonly kind: 'connection';
91
+ readonly connection: object;
92
+ };
93
+ /**
94
+ * Bind what this request can carry (confirmation and relay channels, the approval relay, task
95
+ * dispatch) to the call's context, once its principal is known.
96
+ */
97
+ channels?(context: CommandContext, principal: CallPrincipal): CommandContext;
98
+ }
99
+ /** What stages 1 to 5 decided. */
100
+ export type Admission = {
101
+ kind: 'refused';
102
+ stage: (typeof ADMISSION_STAGES)[number];
103
+ error: CommandError;
104
+ } | {
105
+ kind: 'admitted';
106
+ tool: string;
107
+ args: unknown;
108
+ context: CommandContext;
109
+ };
110
+ export interface AdmissionDeps {
111
+ /** Runs stages 6 to 14. */
112
+ router: ToolRouter;
113
+ /** Declared context fields, read from each call's `_meta`. */
114
+ contextFields?: readonly ContextField[];
115
+ /** Which callers are approvers (the server's `approver` option). */
116
+ approver?: ApproverPredicate;
117
+ /** The actor of a call whose context has none (the server's `actor` option). */
118
+ actor?: Actor;
119
+ /**
120
+ * Whether the server can tell apart the HTTP caller with `credentials` (S3): it has `auth` and
121
+ * the request carries credentials, or `createContext` is declared to identify every caller.
122
+ * Default: it cannot.
123
+ */
124
+ identifiesHttpCaller?: (credentials: unknown) => boolean;
125
+ }
126
+ export interface AdmissionPipeline {
127
+ /** Run stages 1 to 5. */
128
+ admit(call: DecodedCall): Promise<Admission>;
129
+ /**
130
+ * Admit `call` and route it; `run` replaces the engine for the one command a single-command
131
+ * call admits (the `/stream` lane streams it).
132
+ */
133
+ run(call: DecodedCall, run?: RunAdmitted): Promise<RouteOutcome>;
134
+ }
135
+ /** Create the admission pipeline every lane of one server shares. */
136
+ export declare function createAdmission(deps: AdmissionDeps): AdmissionPipeline;
137
+ //# sourceMappingURL=admission.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"admission.d.ts","sourceRoot":"","sources":["../src/admission.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEhF,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,iBAAiB,EAItB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EAAE,KAAK,YAAY,EAAsB,MAAM,qBAAqB,CAAC;AAM5E,OAAO,EAAiB,KAA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E,aAAa,GAAG,iBAAiB,CA4FtE"}
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @fileoverview One admission pipeline for every lane of the MCP server (#1743).
3
+ *
4
+ * Each lane (the MCP 2026-07-28 `/mcp` endpoint and stdio, the 2025-era `/message` lane, and the
5
+ * HTTP `/rpc`, `/batch` and `/stream` routes) only decodes its request into a {@link DecodedCall}
6
+ * and encodes the outcome. Admission runs the same named stages for all of them, in this order:
7
+ *
8
+ * 1. `evidence`: approval evidence in the arguments is refused, from any caller (rule 5 of
9
+ * "Approval evidence from an approver client", `spec/approvals.md`).
10
+ * 2. `meta`: a meta-tool's `meta` argument counts as `_meta` from here on.
11
+ * 3. `principal`: the call's context state, `createContext`'s values, the declared context fields
12
+ * and the output-budget ask (`createContext` wins over a declared field), and the actor and
13
+ * credentials the call starts with.
14
+ * 4. `approver`: whether the caller is an approver, and whether it may carry its evidence here
15
+ * (only on one command from an MCP 2026-07-28 client).
16
+ * 5. `bind`: the call's `CommandContext`: the values a handler sees, with the session, scope view,
17
+ * approver and the lane's channels in its host state (`host-state.ts`).
18
+ *
19
+ * Then the tool router (`tool-router.ts`) runs the rest, the same on every lane:
20
+ *
21
+ * 6. `scope`: MCP authorization (M6), a batch or pipeline as a whole;
22
+ * 7. `resolve`: the tool to its commands (one for `afd-call`, a grouped action or a command's own
23
+ * tool; several for `afd-batch` and `afd-pipe`; none for `afd-discover` and `afd-detail`);
24
+ * 8. `allow`, 9. `lookup` (D4: unexposed and hidden commands are unknown), 10. `context`
25
+ * (`COMMAND_NOT_IN_CONTEXT`), 11. `host-verified`, 12. `confirm`, 13. `task`;
26
+ * 14. `execute`: the engine (`spec/dispatch.md`, from input validation on).
27
+ *
28
+ * A stage that refuses ends the call with its error. A refusal of stages 1 to 5 is a `CommandResult`
29
+ * over MCP, whatever the tool (`spec/vectors/approver-client.json`), a `BatchResult` on `/batch`
30
+ * and an error chunk on `/stream`; the router shapes its own refusals as the tool's result. The
31
+ * HTTP routes decode as the meta-tools they stand for: `/rpc` and `/stream` as `afd-call`, and
32
+ * `/batch` as `afd-batch`, so one call gets the same answer on every lane.
33
+ *
34
+ * Unexpected errors (a throwing `createContext` or `approver` predicate, credentials that cannot
35
+ * be bound) are thrown to the lane, which answers them as an internal error.
36
+ */
37
+ import { AFD_IDEMPOTENCY_KEY_META_KEY } from '@lushly-dev/afd-core';
38
+ import { approvalKeyInInput, bindApprover, decideApprover, } from './approver.js';
39
+ import { contextFieldValues } from './context-fields.js';
40
+ import { bindContextState } from './context-scope.js';
41
+ import { withHostState } from './host-state.js';
42
+ import { liftMetaArgument, metaArgumentKeys } from './meta-argument.js';
43
+ import { withMetaOutputBudget } from './output-budget.js';
44
+ import { bindScopeView } from './scope-view.js';
45
+ /** The stages admission runs before the router, by name. */
46
+ export const ADMISSION_STAGES = ['evidence', 'meta', 'principal', 'approver', 'bind'];
47
+ function isRecord(value) {
48
+ return !!value && typeof value === 'object' && !Array.isArray(value);
49
+ }
50
+ /** `_meta["dev.afd/idempotencyKey"]` as `idempotencyKey`, or nothing for an absent or null key. */
51
+ function idempotencyKeyOf(meta) {
52
+ if (!isRecord(meta))
53
+ return {};
54
+ const key = meta[AFD_IDEMPOTENCY_KEY_META_KEY];
55
+ return key === undefined || key === null ? {} : { idempotencyKey: key };
56
+ }
57
+ /** A shallow copy of a plain object; anything else as it is. */
58
+ function shallowCopy(value) {
59
+ return isRecord(value) ? { ...value } : value;
60
+ }
61
+ /** Create the admission pipeline every lane of one server shares. */
62
+ export function createAdmission(deps) {
63
+ const metaKeys = metaArgumentKeys(deps.contextFields);
64
+ async function admit(call) {
65
+ // 1. evidence: approval evidence is never input, from any caller.
66
+ const keyed = approvalKeyInInput(call.args);
67
+ if (keyed)
68
+ return { kind: 'refused', stage: 'evidence', error: keyed };
69
+ // 2. meta: a meta-tool's `meta` argument counts as `_meta` from here on.
70
+ const lifted = liftMetaArgument(call.tool, call.args, call.meta, metaKeys);
71
+ if (!lifted.ok)
72
+ return { kind: 'refused', stage: 'meta', error: lifted.error };
73
+ // 3. principal. Declared fields from `_meta` first, so a value `createContext` sets wins.
74
+ // Neither `actor` nor `credentials` can be a declared field: only `createContext` sets them.
75
+ const state = call.state(lifted.meta);
76
+ const values = await call.values();
77
+ const callValues = withMetaOutputBudget({
78
+ ...contextFieldValues(deps.contextFields, lifted.meta),
79
+ // The idempotency key, as sent: the engine checks its form between the context check and
80
+ // validation (spec/idempotency.md, K2). A value `createContext` sets wins (K1).
81
+ ...idempotencyKeyOf(lifted.meta),
82
+ ...values,
83
+ }, lifted.meta, values);
84
+ // Copied, so that code changing them in place later cannot change what a channel binds.
85
+ const actor = shallowCopy(callValues.actor ?? deps.actor);
86
+ const credentials = shallowCopy(callValues.credentials);
87
+ // 4. approver: an approver's call carries its evidence in the request's own `_meta`.
88
+ const carrier = decideApprover({
89
+ approver: deps.approver,
90
+ context: { ...callValues, actor, interface: 'mcp' },
91
+ actor,
92
+ credentials,
93
+ meta: call.meta,
94
+ tool: call.tool,
95
+ modern: call.lane === 'mcp',
96
+ });
97
+ if (carrier.kind === 'refuse')
98
+ return { kind: 'refused', stage: 'approver', error: carrier.error };
99
+ // 5. bind: the values a handler sees, and the host state it cannot.
100
+ const values5 = {
101
+ ...callValues,
102
+ ...(call.signal && { signal: call.signal }),
103
+ interface: 'mcp',
104
+ };
105
+ let context = bindScopeView(state === undefined ? values5 : bindContextState(values5, state), call.view);
106
+ const caller = idempotencyCaller(call, credentials);
107
+ if (caller)
108
+ context = withHostState(context, { idempotencyCaller: caller });
109
+ if (carrier.approver)
110
+ context = bindApprover(context, carrier.approver);
111
+ if (call.channels) {
112
+ context = call.channels(context, { actor, credentials, approver: carrier.approver });
113
+ }
114
+ return { kind: 'admitted', tool: call.tool, args: lifted.args ?? {}, context };
115
+ }
116
+ /**
117
+ * Who the lane says the caller is (S3): an HTTP caller, identified or not; or, on a connection
118
+ * lane, the connection. Whether a connection's caller has an identity of its own is the step's
119
+ * to decide, after middleware, which may set the actor.
120
+ */
121
+ function idempotencyCaller(call, credentials) {
122
+ const transport = call.transport;
123
+ if (!transport)
124
+ return undefined;
125
+ if (transport.kind === 'http') {
126
+ return { kind: 'http', identified: deps.identifiesHttpCaller?.(credentials) === true };
127
+ }
128
+ return {
129
+ kind: 'connection',
130
+ connection: transport.connection,
131
+ ...(deps.actor !== undefined && { defaultActor: deps.actor }),
132
+ };
133
+ }
134
+ return {
135
+ admit,
136
+ async run(call, run) {
137
+ const admission = await admit(call);
138
+ if (admission.kind === 'refused')
139
+ return { kind: 'refused', error: admission.error };
140
+ return deps.router.route(admission.tool, admission.args, admission.context, run);
141
+ },
142
+ };
143
+ }
144
+ //# sourceMappingURL=admission.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"admission.js","sourceRoot":"","sources":["../src/admission.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAGH,OAAO,EAAE,4BAA4B,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAGN,kBAAkB,EAClB,YAAY,EACZ,cAAc,GACd,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAqB,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC5E,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAkB,MAAM,iBAAiB,CAAC;AAYhE,4DAA4D;AAC5D,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,CAAU,CAAC;AA+E/F,SAAS,QAAQ,CAAC,KAAc;IAC/B,OAAO,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QA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C,IAAI,CAAC,KAAK,KAAK,SAAS,IAAI,EAAE,YAAY,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;SAC7D,CAAC;IACH,CAAC;IAED,OAAO;QACN,KAAK;QACL,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG;YAClB,MAAM,SAAS,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,CAAC;YACpC,IAAI,SAAS,CAAC,IAAI,KAAK,SAAS;gBAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,CAAC;YACrF,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAClF,CAAC;KACD,CAAC;AACH,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { type CallGrant } from '@lushly-dev/afd-core/internal/app-calls';
2
+ import { type DirectCommandRegistry, type DirectRegistryOptions } from './direct-registry.js';
3
+ import type { AnyZodCommandDefinition } from './schema.js';
4
+ /** Options for {@link createAppCallRegistry}: `createDirectRegistry`'s, less `interface` and `tools`. */
5
+ export interface AppCallRegistryOptions extends Omit<DirectRegistryOptions, 'interface' | 'tools'> {
6
+ /**
7
+ * The host's grant check: whether the caller in `context` holds a grant naming `command`. AFD
8
+ * does not define a grant or who the caller is; read the host's own declared context field, such
9
+ * as Loam's `caller`. It runs before the context fields are validated, so check the type of what
10
+ * it reads. Only an explicit `true` grants; anything else, or a throw, refuses.
11
+ */
12
+ callGrant: CallGrant;
13
+ }
14
+ /**
15
+ * Create the registry a host runs an app's calls through, with the `apps` interface.
16
+ *
17
+ * A call runs only when the command sets `expose.apps` and `callGrant` returns `true`; it then runs
18
+ * as in `createDirectRegistry`, with `apps` as its interface, so middleware and the journal see it.
19
+ * Anything else (a missing command, one not exposed to apps, one the caller is not granted, any of
20
+ * them with invalid input) returns one fixed `CALL_NOT_GRANTED` before lookup, the `allow` option,
21
+ * the active context, validation and middleware, so an app learns nothing about commands it may not
22
+ * call. It serves no tool surface (an app calls commands one by one), and `hasCommand` answers
23
+ * `true` for every name, since `execute` answers every name. `DirectClient` and `DirectTransport`
24
+ * refuse to wrap it: their `allow`, middleware and tool listing would answer before the check. Its
25
+ * listings hold the commands exposed to apps, whatever any caller is granted: build what an app
26
+ * may see from your grants, never from them.
27
+ *
28
+ * @throws TypeError without `callGrant`; otherwise as `createDirectRegistry`
29
+ */
30
+ export declare function createAppCallRegistry(commands: AnyZodCommandDefinition[], options: AppCallRegistryOptions): DirectCommandRegistry;
31
+ //# sourceMappingURL=app-call-registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-call-registry.d.ts","sourceRoot":"","sources":["../src/app-call-registry.ts"],"names":[],"mappings":"AAOA,OAAO,EAEN,KAAK,SAAS,EAEd,MAAM,yCAAyC,CAAC;AAMjD,OAAO,EAGN,KAAK,qBAAqB,EAC1B,KAAK,qBAAqB,EAC1B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE3D,yGAAyG;AACzG,MAAM,WAAW,sBAAuB,SAAQ,IAAI,CAAC,qBAAqB,EAAE,WAAW,GAAG,OAAO,CAAC;IACjG;;;;;OAKG;IACH,SAAS,EAAE,SAAS,CAAC;CACrB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CACpC,QAAQ,EAAE,uBAAuB,EAAE,EACnC,OAAO,EAAE,sBAAsB,GAC7B,qBAAqB,CAsCvB"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The in-process registry for app-to-app calls (`spec/app-calls.md`).
3
+ *
4
+ * Its own module, so a host that never serves app calls bundles none of it: `createDirectRegistry`
5
+ * only refuses the `apps` interface.
6
+ */
7
+ import { failure } from '@lushly-dev/afd-core';
8
+ import { appCallGranted, callNotGranted, } from '@lushly-dev/afd-core/internal/app-calls';
9
+ import { createDispatchId, presetDispatchId, setDispatchId, } from '@lushly-dev/afd-core/internal/dispatch-id';
10
+ import { APP_CALLS, createDirectRegistry, } from './direct-registry.js';
11
+ /**
12
+ * Create the registry a host runs an app's calls through, with the `apps` interface.
13
+ *
14
+ * A call runs only when the command sets `expose.apps` and `callGrant` returns `true`; it then runs
15
+ * as in `createDirectRegistry`, with `apps` as its interface, so middleware and the journal see it.
16
+ * Anything else (a missing command, one not exposed to apps, one the caller is not granted, any of
17
+ * them with invalid input) returns one fixed `CALL_NOT_GRANTED` before lookup, the `allow` option,
18
+ * the active context, validation and middleware, so an app learns nothing about commands it may not
19
+ * call. It serves no tool surface (an app calls commands one by one), and `hasCommand` answers
20
+ * `true` for every name, since `execute` answers every name. `DirectClient` and `DirectTransport`
21
+ * refuse to wrap it: their `allow`, middleware and tool listing would answer before the check. Its
22
+ * listings hold the commands exposed to apps, whatever any caller is granted: build what an app
23
+ * may see from your grants, never from them.
24
+ *
25
+ * @throws TypeError without `callGrant`; otherwise as `createDirectRegistry`
26
+ */
27
+ export function createAppCallRegistry(commands, options) {
28
+ const { callGrant, ...rest } = options;
29
+ if (typeof callGrant !== 'function') {
30
+ throw new TypeError("createAppCallRegistry needs callGrant, the host's check of the caller's grants");
31
+ }
32
+ // Neither `interface` nor `tools` reaches it, even from a caller outside the type checker.
33
+ const { interface: _interface, tools: _tools, ...direct } = rest;
34
+ const registry = createDirectRegistry(commands, {
35
+ ...direct,
36
+ interface: 'apps',
37
+ [APP_CALLS]: true,
38
+ });
39
+ const byName = new Map(commands.map((command) => [command.name, command]));
40
+ return {
41
+ interface: registry.interface,
42
+ contextFields: registry.contextFields,
43
+ async execute(name, input, context = {}, callOptions) {
44
+ // The grant check sees the interface the call runs with, as in the core registry, and
45
+ // the call's dispatch id, made now so the grant reads the id the handler will
46
+ // (spec/dispatch.md, "The dispatch id"). The grant gets its own copy: the preset that
47
+ // carries the id to the engine is never handed to host code.
48
+ const dispatchId = createDispatchId();
49
+ const grantContext = { ...context, interface: 'apps' };
50
+ setDispatchId(grantContext, dispatchId);
51
+ if (!appCallGranted(byName.get(name), name, grantContext, callGrant)) {
52
+ return failure(callNotGranted());
53
+ }
54
+ const appContext = { ...context, interface: 'apps' };
55
+ presetDispatchId(appContext, dispatchId);
56
+ return registry.execute(name, input, appContext, callOptions);
57
+ },
58
+ listTools: registry.listTools,
59
+ listCommandNames: registry.listCommandNames,
60
+ listCommands: registry.listCommands,
61
+ hasCommand: () => true,
62
+ };
63
+ }
64
+ //# sourceMappingURL=app-call-registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-call-registry.js","sourceRoot":"","sources":["../src/app-call-registry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAuB,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EACN,cAAc,EAEd,cAAc,GACd,MAAM,yCAAyC,CAAC;AACjD,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,GACb,MAAM,2CAA2C,CAAC;AACnD,OAAO,EACN,SAAS,EACT,oBAAoB,GAGpB,MAAM,sBAAsB,CAAC;AAc9B;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,qBAAqB,CACpC,QAAmC,EACnC,OAA+B;IAE/B,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IACvC,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE,CAAC;QACrC,MAAM,IAAI,SAAS,CAClB,gFAAgF,CAChF,CAAC;IACH,CAAC;IACD,2FAA2F;IAC3F,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,IAA6B,CAAC;IAC1F,MAAM,QAAQ,GAAG,oBAAoB,CAAC,QAAQ,EAAE;QAC/C,GAAG,MAAM;QACT,SAAS,EAAE,MAAiB;QAC5B,CAAC,SAAS,CAAC,EAAE,IAAI;KACQ,CAAC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exported by the package).
3
+ *
4
+ * The execution engine records each call's declared approval policy on the call's own context
5
+ * object before any middleware runs. The approval middleware decides from that record (the
6
+ * command's own policy) and, when it lets the call through, records what it decided on: the
7
+ * command name, the canonical JSON of the input it decided on, and whether the policy its
8
+ * `commands` option lists for the command gates it. The engine dispatches a call whose own or
9
+ * listed policy is not `'not-required'` only when every such admission matches the command and
10
+ * validated input it is about to run, and it takes them as it dispatches, so one admission runs
11
+ * one handler. An input with no canonical form, which only a policy that let the call through
12
+ * admits (the middleware refuses to hold one), is compared by identity, so a change inside it is
13
+ * not seen.
14
+ *
15
+ * Every call gets a fresh context object (`withContextDefaults` copies), so a batch item, a
16
+ * pipeline step or a command run from inside another command's handler has its own record and
17
+ * never inherits an outer call's admission.
18
+ *
19
+ * The records live in one WeakMap on `globalThis`, under a registered symbol, so that two
20
+ * installed copies of this package (an engine from one, the middleware from the other) share it.
21
+ * The first copy installs it as a non-writable, non-configurable property; a later copy uses it
22
+ * only when it is that: a plain `WeakMap` installed that way. Anything else there is ignored and
23
+ * this copy keeps its records to itself, so an admission from elsewhere is not honoured. This is
24
+ * not a boundary against code running in the same process or page.
25
+ */
26
+ import type { ApprovalPolicy, CommandContext, IdempotencySupport } from '@lushly-dev/afd-core';
27
+ /** The command a call runs, as its admission record keeps it. */
28
+ export interface AdmissionCommand {
29
+ name: string;
30
+ approval?: ApprovalPolicy<never>;
31
+ confirmPrompt?: string;
32
+ /** The command's `secret` declaration, which the journal middleware reads. */
33
+ secret?: boolean;
34
+ /** The command's `mutation` declaration, which the retry middleware reads. */
35
+ mutation?: boolean;
36
+ /** The command's `idempotency` declaration, which the retry middleware reads. */
37
+ idempotency?: IdempotencySupport;
38
+ }
39
+ /** The canonical JSON of a value, or undefined when it has none. */
40
+ export type Canonicalize = (value: unknown) => string | undefined;
41
+ /**
42
+ * The shared map on `holder` (`globalThis`) when another copy installed it as this module does,
43
+ * else a fresh one, installed on `holder` when the key is free. Exported for its tests.
44
+ */
45
+ export declare function installAdmissionRecords(holder?: object): WeakMap<object, unknown>;
46
+ /** Record, before any middleware runs, the command this call's `context` runs, not yet admitted. */
47
+ export declare function beginAdmission(context: CommandContext, command: AdmissionCommand): void;
48
+ /** What an approval step decided on when it let a call through. */
49
+ export interface Admit {
50
+ /** The approval step (one middleware instance) admitting it. */
51
+ step: object;
52
+ commandName: string;
53
+ /**
54
+ * The canonical JSON of the input it decided on (verified a token against, or found not to
55
+ * need approval), computed before any await, or undefined when it has none.
56
+ */
57
+ canonical: string | undefined;
58
+ /** The input it decided on, compared by identity when it has no canonical form. */
59
+ input: unknown;
60
+ /** Whether the policy its `commands` option lists for the command gates it. */
61
+ listedGate: boolean;
62
+ /** Its canonical JSON, for the engine to compare the input it dispatches. */
63
+ canonicalize: Canonicalize;
64
+ }
65
+ /** Record that an approval step admitted the call of `context`. A no-op outside the engine. */
66
+ export declare function admitApproval(context: CommandContext, admit: Admit): void;
67
+ /**
68
+ * Record that the policy an approval step's `commands` option lists for the command the call of
69
+ * `context` runs gates it, whatever name the step was called with, so a call renamed on its way
70
+ * to the step is still checked at dispatch. A no-op outside the engine.
71
+ */
72
+ export declare function markListedGate(context: CommandContext): void;
73
+ /**
74
+ * Whether the engine must take and check an admission before it dispatches the call of
75
+ * `context`: its command's own policy gates it, or an approval step recorded that its listed
76
+ * policy does.
77
+ */
78
+ export declare function needsAdmission(context: CommandContext): boolean;
79
+ /** Whether a declared policy needs an approval step: anything but unset or `'not-required'`. */
80
+ export declare function gates(policy: ApprovalPolicy<never> | undefined): boolean;
81
+ /**
82
+ * Take the admissions of the call of `context`: `admitted` when every approval step that ever
83
+ * admitted it admitted it again since the last dispatch, each on `commandName` and `input`: the
84
+ * same name, and the same canonical input (the same value, by `Object.is`, when the input has no
85
+ * canonical form; a change inside such a value is not seen), `missing` with none, `mismatch`
86
+ * otherwise. Taken whatever the answer, so a second dispatch needs a second admission from each.
87
+ */
88
+ export declare function takeAdmission(context: CommandContext, commandName: string, input: unknown): 'admitted' | 'missing' | 'mismatch';
89
+ /**
90
+ * The declared policy of the command the call of `context` runs. `found` is false when the call
91
+ * did not come through the engine (the middleware was called directly).
92
+ */
93
+ export declare function livePolicy(context: CommandContext): {
94
+ found: boolean;
95
+ policy: ApprovalPolicy<never> | undefined;
96
+ };
97
+ /** The command the call of `context` runs, as recorded by the engine; undefined outside it. */
98
+ export declare function liveCommand(context: CommandContext): AdmissionCommand | undefined;
99
+ /**
100
+ * Mark `inner` as the engine's when `outer` is: `composeMiddleware` builds continuations of its own
101
+ * for the parts it composes, and a part the engine reached through it is still the engine's.
102
+ */
103
+ export declare function inheritEngineNext(context: CommandContext, outer: object, inner: object): void;
104
+ /** Mark `next` as a continuation the engine built for the call of `context`. */
105
+ export declare function markEngineNext(context: CommandContext, next: object): void;
106
+ /**
107
+ * What the engine records of the call of `context`, for the journal: for a middleware the engine
108
+ * invoked (its `next` is one the engine built), and `undefined` for any other recorder: outside
109
+ * the engine, or one a handler runs itself on the same context, at any time. Its `secret` is
110
+ * whether the command declares `secret`, and its `token` is the call's identity, by which the
111
+ * journal tells a recorder of the same call from one of a call it runs inside.
112
+ */
113
+ export declare function liveDispatch(context: CommandContext, next: object): {
114
+ token: object;
115
+ secret: boolean;
116
+ } | undefined;
117
+ /**
118
+ * Whether the command the engine runs for `context` declares `mutation`, for the retry
119
+ * middleware: `true` or `false` for a middleware the engine invoked, and `undefined` for any other
120
+ * caller, as {@link liveDispatch} decides.
121
+ */
122
+ export declare function liveMutation(context: CommandContext, next: object): boolean | undefined;
123
+ /**
124
+ * The command's declaration and the call's key, for the retry middleware (`spec/idempotency.md`,
125
+ * R1): for a middleware the engine invoked, and `undefined` for any other caller, as
126
+ * {@link liveDispatch} decides.
127
+ */
128
+ export declare function liveRepeatDeclaration(context: CommandContext, next: object): {
129
+ mutation: boolean;
130
+ idempotency?: IdempotencySupport;
131
+ carriesKey: boolean;
132
+ } | undefined;
133
+ /** Record whether the call of `context` carries a key the retry middleware may count. */
134
+ export declare function recordCallKey(context: CommandContext, carriesKey: boolean): void;
135
+ /** Record that the idempotency step reserved the key of the call of `context` (R3). */
136
+ export declare function markIdempotencyStepActed(context: CommandContext): void;
137
+ /** Whether the idempotency step reserved the key of the call of `context` (R3). */
138
+ export declare function idempotencyStepActed(context: CommandContext): boolean;
139
+ //# sourceMappingURL=approval-admission.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"approval-admission.d.ts","sourceRoot":"","sources":["../src/approval-admission.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE/F,iEAAiE;AACjE,MAAM,WAAW,gBAAgB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC;IACjC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,8EAA8E;IAC9E,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,8EAA8E;IAC9E,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,iFAAiF;IACjF,WAAW,CAAC,EAAE,kBAAkB,CAAC;CACjC;AAUD,oEAAoE;AACpE,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,CAAC;AA2DlE;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,GAAE,MAAmB,GAAG,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CA4B7F;AAQD,oGAAoG;AACpG,wBAAgB,cAAc,CAAC,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAkBvF;AAED,mEAAmE;AACnE,MAAM,WAAW,KAAK;IACrB,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;I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