@proofoftech/flowsafe 0.1.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 (204) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +202 -0
  3. package/README.md +273 -0
  4. package/dist/approval-api/contract.d.ts +45 -0
  5. package/dist/approval-api/contract.d.ts.map +1 -0
  6. package/dist/approval-api/contract.js +37 -0
  7. package/dist/approval-api/contract.js.map +1 -0
  8. package/dist/approval-api/d1-store.d.ts +52 -0
  9. package/dist/approval-api/d1-store.d.ts.map +1 -0
  10. package/dist/approval-api/d1-store.js +468 -0
  11. package/dist/approval-api/d1-store.js.map +1 -0
  12. package/dist/approval-api/grants.d.ts +52 -0
  13. package/dist/approval-api/grants.d.ts.map +1 -0
  14. package/dist/approval-api/grants.js +200 -0
  15. package/dist/approval-api/grants.js.map +1 -0
  16. package/dist/approval-api/index.d.ts +21 -0
  17. package/dist/approval-api/index.d.ts.map +1 -0
  18. package/dist/approval-api/index.js +18 -0
  19. package/dist/approval-api/index.js.map +1 -0
  20. package/dist/approval-api/retention.d.ts +32 -0
  21. package/dist/approval-api/retention.d.ts.map +1 -0
  22. package/dist/approval-api/retention.js +81 -0
  23. package/dist/approval-api/retention.js.map +1 -0
  24. package/dist/approval-api/router.d.ts +51 -0
  25. package/dist/approval-api/router.d.ts.map +1 -0
  26. package/dist/approval-api/router.js +245 -0
  27. package/dist/approval-api/router.js.map +1 -0
  28. package/dist/approval-api/service.d.ts +134 -0
  29. package/dist/approval-api/service.d.ts.map +1 -0
  30. package/dist/approval-api/service.js +537 -0
  31. package/dist/approval-api/service.js.map +1 -0
  32. package/dist/approval-api/store.d.ts +126 -0
  33. package/dist/approval-api/store.d.ts.map +1 -0
  34. package/dist/approval-api/store.js +189 -0
  35. package/dist/approval-api/store.js.map +1 -0
  36. package/dist/approval-api/tenant-brand.d.ts +35 -0
  37. package/dist/approval-api/tenant-brand.d.ts.map +1 -0
  38. package/dist/approval-api/tenant-brand.js +18 -0
  39. package/dist/approval-api/tenant-brand.js.map +1 -0
  40. package/dist/approval-api/tenant-context.d.ts +44 -0
  41. package/dist/approval-api/tenant-context.d.ts.map +1 -0
  42. package/dist/approval-api/tenant-context.js +49 -0
  43. package/dist/approval-api/tenant-context.js.map +1 -0
  44. package/dist/approval-api/tenant-store.d.ts +36 -0
  45. package/dist/approval-api/tenant-store.d.ts.map +1 -0
  46. package/dist/approval-api/tenant-store.js +122 -0
  47. package/dist/approval-api/tenant-store.js.map +1 -0
  48. package/dist/approval-api/types.d.ts +247 -0
  49. package/dist/approval-api/types.d.ts.map +1 -0
  50. package/dist/approval-api/types.js +157 -0
  51. package/dist/approval-api/types.js.map +1 -0
  52. package/dist/approval-ui/App.d.ts +11 -0
  53. package/dist/approval-ui/App.d.ts.map +1 -0
  54. package/dist/approval-ui/App.js +15 -0
  55. package/dist/approval-ui/App.js.map +1 -0
  56. package/dist/approval-ui/DetailView.d.ts +13 -0
  57. package/dist/approval-ui/DetailView.d.ts.map +1 -0
  58. package/dist/approval-ui/DetailView.js +20 -0
  59. package/dist/approval-ui/DetailView.js.map +1 -0
  60. package/dist/approval-ui/MetricsView.d.ts +7 -0
  61. package/dist/approval-ui/MetricsView.d.ts.map +1 -0
  62. package/dist/approval-ui/MetricsView.js +22 -0
  63. package/dist/approval-ui/MetricsView.js.map +1 -0
  64. package/dist/approval-ui/QueueView.d.ts +11 -0
  65. package/dist/approval-ui/QueueView.d.ts.map +1 -0
  66. package/dist/approval-ui/QueueView.js +59 -0
  67. package/dist/approval-ui/QueueView.js.map +1 -0
  68. package/dist/approval-ui/client.d.ts +34 -0
  69. package/dist/approval-ui/client.d.ts.map +1 -0
  70. package/dist/approval-ui/client.js +103 -0
  71. package/dist/approval-ui/client.js.map +1 -0
  72. package/dist/approval-ui/components.d.ts +125 -0
  73. package/dist/approval-ui/components.d.ts.map +1 -0
  74. package/dist/approval-ui/components.js +54 -0
  75. package/dist/approval-ui/components.js.map +1 -0
  76. package/dist/approval-ui/index.d.ts +21 -0
  77. package/dist/approval-ui/index.d.ts.map +1 -0
  78. package/dist/approval-ui/index.js +21 -0
  79. package/dist/approval-ui/index.js.map +1 -0
  80. package/dist/approval-ui/mount.d.ts +23 -0
  81. package/dist/approval-ui/mount.d.ts.map +1 -0
  82. package/dist/approval-ui/mount.js +17 -0
  83. package/dist/approval-ui/mount.js.map +1 -0
  84. package/dist/approval-ui/tips.d.ts +32 -0
  85. package/dist/approval-ui/tips.d.ts.map +1 -0
  86. package/dist/approval-ui/tips.js +32 -0
  87. package/dist/approval-ui/tips.js.map +1 -0
  88. package/dist/approval-ui/use-approval-dashboard.d.ts +70 -0
  89. package/dist/approval-ui/use-approval-dashboard.d.ts.map +1 -0
  90. package/dist/approval-ui/use-approval-dashboard.js +148 -0
  91. package/dist/approval-ui/use-approval-dashboard.js.map +1 -0
  92. package/dist/approval-ui/view-model.d.ts +24 -0
  93. package/dist/approval-ui/view-model.d.ts.map +1 -0
  94. package/dist/approval-ui/view-model.js +62 -0
  95. package/dist/approval-ui/view-model.js.map +1 -0
  96. package/dist/artifacts/index.d.ts +123 -0
  97. package/dist/artifacts/index.d.ts.map +1 -0
  98. package/dist/artifacts/index.js +223 -0
  99. package/dist/artifacts/index.js.map +1 -0
  100. package/dist/audit-export/index.d.ts +71 -0
  101. package/dist/audit-export/index.d.ts.map +1 -0
  102. package/dist/audit-export/index.js +124 -0
  103. package/dist/audit-export/index.js.map +1 -0
  104. package/dist/do-runner/breakwater-keys.d.ts +23 -0
  105. package/dist/do-runner/breakwater-keys.d.ts.map +1 -0
  106. package/dist/do-runner/breakwater-keys.js +38 -0
  107. package/dist/do-runner/breakwater-keys.js.map +1 -0
  108. package/dist/do-runner/cf-types.d.ts +26 -0
  109. package/dist/do-runner/cf-types.d.ts.map +1 -0
  110. package/dist/do-runner/cf-types.js +13 -0
  111. package/dist/do-runner/cf-types.js.map +1 -0
  112. package/dist/do-runner/d1-storage.d.ts +128 -0
  113. package/dist/do-runner/d1-storage.d.ts.map +1 -0
  114. package/dist/do-runner/d1-storage.js +287 -0
  115. package/dist/do-runner/d1-storage.js.map +1 -0
  116. package/dist/do-runner/durable-object.d.ts +28 -0
  117. package/dist/do-runner/durable-object.d.ts.map +1 -0
  118. package/dist/do-runner/durable-object.js +164 -0
  119. package/dist/do-runner/durable-object.js.map +1 -0
  120. package/dist/do-runner/index.d.ts +12 -0
  121. package/dist/do-runner/index.d.ts.map +1 -0
  122. package/dist/do-runner/index.js +11 -0
  123. package/dist/do-runner/index.js.map +1 -0
  124. package/dist/do-runner/init.d.ts +38 -0
  125. package/dist/do-runner/init.d.ts.map +1 -0
  126. package/dist/do-runner/init.js +45 -0
  127. package/dist/do-runner/init.js.map +1 -0
  128. package/dist/do-runner/path-safe-id.d.ts +19 -0
  129. package/dist/do-runner/path-safe-id.d.ts.map +1 -0
  130. package/dist/do-runner/path-safe-id.js +70 -0
  131. package/dist/do-runner/path-safe-id.js.map +1 -0
  132. package/dist/do-runner/resume-ledger.d.ts +48 -0
  133. package/dist/do-runner/resume-ledger.d.ts.map +1 -0
  134. package/dist/do-runner/resume-ledger.js +59 -0
  135. package/dist/do-runner/resume-ledger.js.map +1 -0
  136. package/dist/do-runner/runtime.d.ts +161 -0
  137. package/dist/do-runner/runtime.d.ts.map +1 -0
  138. package/dist/do-runner/runtime.js +509 -0
  139. package/dist/do-runner/runtime.js.map +1 -0
  140. package/dist/host-kit/approval-bridge.d.ts +109 -0
  141. package/dist/host-kit/approval-bridge.d.ts.map +1 -0
  142. package/dist/host-kit/approval-bridge.js +262 -0
  143. package/dist/host-kit/approval-bridge.js.map +1 -0
  144. package/dist/host-kit/bearer-auth.d.ts +19 -0
  145. package/dist/host-kit/bearer-auth.d.ts.map +1 -0
  146. package/dist/host-kit/bearer-auth.js +67 -0
  147. package/dist/host-kit/bearer-auth.js.map +1 -0
  148. package/dist/host-kit/do-response.d.ts +13 -0
  149. package/dist/host-kit/do-response.d.ts.map +1 -0
  150. package/dist/host-kit/do-response.js +24 -0
  151. package/dist/host-kit/do-response.js.map +1 -0
  152. package/dist/host-kit/do-run-topology.d.ts +32 -0
  153. package/dist/host-kit/do-run-topology.d.ts.map +1 -0
  154. package/dist/host-kit/do-run-topology.js +39 -0
  155. package/dist/host-kit/do-run-topology.js.map +1 -0
  156. package/dist/host-kit/env-vars.d.ts +26 -0
  157. package/dist/host-kit/env-vars.d.ts.map +1 -0
  158. package/dist/host-kit/env-vars.js +48 -0
  159. package/dist/host-kit/env-vars.js.map +1 -0
  160. package/dist/host-kit/host-approval-service.d.ts +125 -0
  161. package/dist/host-kit/host-approval-service.d.ts.map +1 -0
  162. package/dist/host-kit/host-approval-service.js +187 -0
  163. package/dist/host-kit/host-approval-service.js.map +1 -0
  164. package/dist/host-kit/index.d.ts +18 -0
  165. package/dist/host-kit/index.d.ts.map +1 -0
  166. package/dist/host-kit/index.js +34 -0
  167. package/dist/host-kit/index.js.map +1 -0
  168. package/dist/host-kit/module.d.ts +34 -0
  169. package/dist/host-kit/module.d.ts.map +1 -0
  170. package/dist/host-kit/module.js +17 -0
  171. package/dist/host-kit/module.js.map +1 -0
  172. package/dist/host-kit/registration.d.ts +11 -0
  173. package/dist/host-kit/registration.d.ts.map +1 -0
  174. package/dist/host-kit/registration.js +21 -0
  175. package/dist/host-kit/registration.js.map +1 -0
  176. package/dist/host-kit/run-route-error.d.ts +11 -0
  177. package/dist/host-kit/run-route-error.d.ts.map +1 -0
  178. package/dist/host-kit/run-route-error.js +22 -0
  179. package/dist/host-kit/run-route-error.js.map +1 -0
  180. package/dist/host-kit/run-router.d.ts +60 -0
  181. package/dist/host-kit/run-router.d.ts.map +1 -0
  182. package/dist/host-kit/run-router.js +212 -0
  183. package/dist/host-kit/run-router.js.map +1 -0
  184. package/dist/host-kit/subdomain-check.d.ts +23 -0
  185. package/dist/host-kit/subdomain-check.d.ts.map +1 -0
  186. package/dist/host-kit/subdomain-check.js +60 -0
  187. package/dist/host-kit/subdomain-check.js.map +1 -0
  188. package/dist/host-kit/tenant-registry.d.ts +51 -0
  189. package/dist/host-kit/tenant-registry.d.ts.map +1 -0
  190. package/dist/host-kit/tenant-registry.js +87 -0
  191. package/dist/host-kit/tenant-registry.js.map +1 -0
  192. package/dist/host-kit/verifier.d.ts +63 -0
  193. package/dist/host-kit/verifier.d.ts.map +1 -0
  194. package/dist/host-kit/verifier.js +191 -0
  195. package/dist/host-kit/verifier.js.map +1 -0
  196. package/dist/host-kit/workflow-meta.d.ts +25 -0
  197. package/dist/host-kit/workflow-meta.d.ts.map +1 -0
  198. package/dist/host-kit/workflow-meta.js +12 -0
  199. package/dist/host-kit/workflow-meta.js.map +1 -0
  200. package/dist/index.d.ts +9 -0
  201. package/dist/index.d.ts.map +1 -0
  202. package/dist/index.js +25 -0
  203. package/dist/index.js.map +1 -0
  204. package/package.json +95 -0
@@ -0,0 +1,200 @@
1
+ // Grant derivation — how a decision becomes an in-run capability.
2
+ //
3
+ // Grants NEVER travel in HTTP bodies. The runner's public resume route stays
4
+ // grant-free (a proxying Worker cannot forward what does not exist), and the
5
+ // requestContext the resumed leg executes under is minted server-side by
6
+ // approvalGrantProvider on EVERY start/resume.
7
+ //
8
+ // Grants are SUSPENSION-SCOPED: a step-keyed approval unlocks its connectors
9
+ // only for the leg that resumes THAT step, and only when the decision binds
10
+ // to the step's CURRENT suspension. The preferred binding is clock-free and
11
+ // exact on the (suspendedAt, resumeCount) fingerprint: the creating bridge
12
+ // captures both into the record (CreateApprovalInput), and minting requires
13
+ // record.suspendedAt === leg.suspendedAt AND
14
+ // record.resumeCount === leg.resumeCount, so a decision bound to any other
15
+ // suspension of the step never mints. resumeCount is the categorical
16
+ // tie-breaker: it is the runtime-owned monotonic per-(run,step) resume
17
+ // ordinal — undefined on a step's FIRST suspension, 1,2,… on successive
18
+ // RE-suspensions — that the runtime increments on EVERY resume, so even two
19
+ // same-step suspensions whose suspendedAt collide within a millisecond
20
+ // (possible only on the synchronous in-process path — production round-trips
21
+ // make them seconds apart) stay distinguishable, and unlike Mastra's
22
+ // payload-conditional resumedAt no no-payload resume can erase it. That closes
23
+ // both leak shapes: approving connector X at one approval point never unlocks
24
+ // X at another point of the run, and when the SAME step suspends again later,
25
+ // the earlier approval is spent — the new suspension needs its own decision,
26
+ // and a rejected re-request never falls back to the old approval. Because the
27
+ // ordinal strictly increments, a chain of three+ re-suspensions compares two
28
+ // distinct counts and never collides — closing the depth-3+ residual the
29
+ // prior (suspendedAt, resumedAt) binding deferred. The ledger is in-memory,
30
+ // but a reset across a DO restart can never leak: a same-ms suspendedAt
31
+ // collision (the only case the ordinal guards) requires a synchronous
32
+ // in-memory store with no I/O between the stamps, so same-ms-collision and
33
+ // surviving-a-restart are mutually exclusive — a durable (D1) deployment gives
34
+ // the two suspensions distinct suspendedAt, and the suspendedAt half alone
35
+ // distinguishes them. A reset ledger only yields leg.resumeCount=undefined
36
+ // against a re-suspension record's defined count → deny (fail-closed re-deny),
37
+ // then a fresh decision mints.
38
+ // Records created WITHOUT observing the suspension (pre-capture bridges)
39
+ // fall back to chronology — service-clock decidedAt strictly after the
40
+ // core-clock suspendedAt — correct only where the two clocks are one
41
+ // (single-Worker/Cloudflare deployments; see the threat model, boundary 6).
42
+ //
43
+ // Run-scoped standing grants are EXPLICIT: a step-less record mints on every
44
+ // leg only when it also carries runScoped === true. A step-less record without
45
+ // the flag mints nothing. The opt-out must be named, never inferred from a
46
+ // missing field — "absent stepPath implies run-wide privilege" was an inverted
47
+ // default that let any record whose stepPath was merely omitted (including one
48
+ // authored over the create route) become a standing capability. Neither field
49
+ // is settable over HTTP (router.ts, TCB_ONLY_CREATE_FIELDS);
50
+ // suspend-observation bridges always set stepPath and never runScoped.
51
+ //
52
+ // The provider always returns the grant key — an empty list when nothing
53
+ // applies — because Mastra merges provided context over the persisted
54
+ // snapshot: overwriting each leg retires the previous leg's grants instead
55
+ // of letting them leak forward. A resume that bypasses decide() (or targets
56
+ // a suspension nothing was decided for) therefore finds no grant — fail
57
+ // closed.
58
+ import { tenantOfRunId } from '../do-runner/path-safe-id.js';
59
+ import { BREAKWATER_APPROVED_CONNECTORS_KEY } from './contract.js';
60
+ import { stepKeyOf } from './store.js';
61
+ // Whether a step-keyed approval is bound to the resumed step's CURRENT
62
+ // suspension (exact (suspendedAt, resumeCount) pair match, or the legacy
63
+ // decidedAt-after fallback) — the predicate that decides whether a decision
64
+ // mints.
65
+ function boundToCurrentSuspension(record, suspendedAt, resumeCount) {
66
+ // Unknown suspension time (snapshot without step timestamps, or an
67
+ // unresolvable resume target) — fail closed for step-keyed grants.
68
+ if (suspendedAt === undefined)
69
+ return false;
70
+ // Preferred, clock-free binding on the (suspendedAt, resumeCount)
71
+ // fingerprint. suspendedAt comes from the core clock; resumeCount is the
72
+ // runtime's monotonic per-(run,step) resume ordinal (undefined on a first
73
+ // suspension, 1,2,… on re-suspensions), captured by the bridge at create
74
+ // time. Both must match EXACTLY. resumeCount — NOT the payload-conditional
75
+ // resumedAt (informational only) — is the tie-breaker: because the runtime
76
+ // increments it on every resume regardless of payload, a first-suspension
77
+ // approval (resumeCount undefined) can never mint into a re-suspension leg
78
+ // (resumeCount defined) even when their suspendedAt collide within one ms,
79
+ // and successive re-suspensions carry strictly increasing counts that never
80
+ // collide. A pre-`resume_count` record (suspendedAt defined, resumeCount
81
+ // undefined from the upgrade window) still mints on a first-suspension leg
82
+ // (undefined === undefined) and fails closed on a re-suspension leg — the
83
+ // documented re-deny, resolved by a fresh decision.
84
+ if (record.suspendedAt !== undefined) {
85
+ return (record.suspendedAt === suspendedAt && record.resumeCount === resumeCount);
86
+ }
87
+ // Legacy fallback for records created without observing the suspension
88
+ // (pre-capture bridges): service-clock decidedAt strictly after the core-
89
+ // clock suspendedAt — correct on same-clock (single-Worker/Cloudflare)
90
+ // topologies only; see security-threat-model.md boundary 6.
91
+ //
92
+ // TRANSITIONAL and self-draining — it fires only while pre-capture rows
93
+ // remain undecided, and no client can force it (`suspendedAt` is in
94
+ // TCB_ONLY_CREATE_FIELDS). INVARIANT for writers: every NEW step-keyed
95
+ // record MUST capture `suspendedAt` (host-kit's bridge does); creating one
96
+ // without it would re-open the clock-dependence this exact-match binding
97
+ // exists to close.
98
+ return (record.decidedAt !== undefined && Date.parse(record.decidedAt) > suspendedAt);
99
+ }
100
+ /**
101
+ * Union of connector ids across APPROVED records that apply to this leg:
102
+ * step-keyed records matching the resumed step AND bound to the step's
103
+ * CURRENT suspension (exact `(suspendedAt, resumeCount)` pair match, or the
104
+ * legacy decidedAt-after fallback for records created without the capture),
105
+ * plus step-less records that explicitly opted in with `runScoped: true`.
106
+ * Start legs and unresolvable resume targets mint run-scoped records only. A
107
+ * step-less record without `runScoped` is inert on every leg — fail closed.
108
+ */
109
+ export async function approvedConnectorsForLeg(store, workflowId, runId, leg) {
110
+ // The `runId` predicate is LOAD-BEARING and must never be "optimized away"
111
+ // in favor of the store's tenant binding: under INV-1 a salted runId
112
+ // belongs to exactly one tenant, so this predicate is what keeps the mint
113
+ // tenant-safe even from an unbound or mis-bound store (the binding is
114
+ // defense in depth, not the fix). grants.test.ts pins the exact filter
115
+ // with a spy store.
116
+ const approved = await store.list({ workflowId, runId, status: 'approved' });
117
+ const targetKey = leg.kind === 'resume' && leg.step !== undefined
118
+ ? stepKeyOf(leg.step)
119
+ : undefined;
120
+ const suspendedAt = leg.kind === 'resume' ? leg.suspendedAt : undefined;
121
+ const resumeCount = leg.kind === 'resume' ? leg.resumeCount : undefined;
122
+ const connectors = new Set();
123
+ for (const record of approved) {
124
+ const recordKey = stepKeyOf(record.stepPath);
125
+ const applies = recordKey === ''
126
+ ? record.runScoped === true
127
+ : targetKey !== undefined &&
128
+ recordKey === targetKey &&
129
+ boundToCurrentSuspension(record, suspendedAt, resumeCount);
130
+ if (!applies)
131
+ continue;
132
+ for (const connector of record.connectors) {
133
+ connectors.add(connector);
134
+ }
135
+ }
136
+ return [...connectors];
137
+ }
138
+ /**
139
+ * RequestContextProvider wiring the approval store into the DO runner:
140
+ * pass as init()'s / RunnerRuntimeOptions' requestContextForRun. Part of the
141
+ * trusted computing base — it writes the breakwater grant key.
142
+ *
143
+ * Takes a TENANT-BOUND store (the brand): a DO builds it as
144
+ * `factory.forTenant(this.tenantId)` from its own identity. A
145
+ * SystemApprovalStore is not assignable here — the type-level assertion in
146
+ * grants.test.ts pins that.
147
+ */
148
+ export function approvalGrantProvider(store) {
149
+ return async (workflowId, runId, leg) => {
150
+ const connectors = await approvedConnectorsForLeg(store, workflowId, runId, leg);
151
+ // Always return the key (even empty) — see the header: overwrite, don't
152
+ // inherit, so stale grants from earlier legs cannot survive the merge.
153
+ return { [BREAKWATER_APPROVED_CONNECTORS_KEY]: connectors };
154
+ };
155
+ }
156
+ /**
157
+ * Factory-backed provider for hosts whose ONE runtime serves every tenant
158
+ * in-process (the dev plugin; DO hosts bind per-instance instead). The
159
+ * tenant is recovered from the leg's runId prefix — the same INV-1 carrier
160
+ * the DO name join uses — and the store is bound per leg. A runId without a
161
+ * valid tenant prefix mints an EMPTY grant list (fail closed), never a
162
+ * cross-tenant read.
163
+ */
164
+ export function approvalGrantProviderFromFactory(factory) {
165
+ return async (workflowId, runId, leg) => {
166
+ const tenantId = tenantOfRunId(runId);
167
+ if (tenantId === undefined) {
168
+ return { [BREAKWATER_APPROVED_CONNECTORS_KEY]: [] };
169
+ }
170
+ const connectors = await approvedConnectorsForLeg(factory.forTenant(tenantId), workflowId, runId, leg);
171
+ return { [BREAKWATER_APPROVED_CONNECTORS_KEY]: connectors };
172
+ };
173
+ }
174
+ /**
175
+ * The default resumeData contract a decided approval resumes with:
176
+ * `{ approved: boolean, comment?, decidedBy? }`. Approval steps either accept
177
+ * this shape in their resumeSchema or the deployment overrides the builder.
178
+ */
179
+ export function defaultResumeData(record, decision) {
180
+ const data = { approved: decision === 'approve' };
181
+ if (record.comment !== undefined)
182
+ data.comment = record.comment;
183
+ if (record.decidedBy !== undefined)
184
+ data.decidedBy = record.decidedBy;
185
+ return data;
186
+ }
187
+ /**
188
+ * ApprovalServiceOptions.resumeRun for same-process deployments: resumes the
189
+ * decided run on a RunnerRuntime (rejects are resumed too — the workflow
190
+ * learns the outcome from resumeData.approved). Cross-Worker deployments
191
+ * implement resumeRun against the run's DO stub instead.
192
+ */
193
+ export function resumeViaRuntime(runtime, options = {}) {
194
+ const buildResumeData = options.resumeData ?? defaultResumeData;
195
+ return (record, decision) => runtime.resume(record.workflowId, record.runId, {
196
+ step: record.stepPath,
197
+ resumeData: buildResumeData(record, decision),
198
+ });
199
+ }
200
+ //# sourceMappingURL=grants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grants.js","sourceRoot":"","sources":["../../src/approval-api/grants.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,EAAE;AACF,6EAA6E;AAC7E,6EAA6E;AAC7E,yEAAyE;AACzE,+CAA+C;AAC/C,EAAE;AACF,6EAA6E;AAC7E,4EAA4E;AAC5E,4EAA4E;AAC5E,2EAA2E;AAC3E,4EAA4E;AAC5E,6CAA6C;AAC7C,2EAA2E;AAC3E,qEAAqE;AACrE,uEAAuE;AACvE,wEAAwE;AACxE,4EAA4E;AAC5E,uEAAuE;AACvE,6EAA6E;AAC7E,qEAAqE;AACrE,+EAA+E;AAC/E,8EAA8E;AAC9E,8EAA8E;AAC9E,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,yEAAyE;AACzE,4EAA4E;AAC5E,wEAAwE;AACxE,sEAAsE;AACtE,2EAA2E;AAC3E,+EAA+E;AAC/E,2EAA2E;AAC3E,2EAA2E;AAC3E,+EAA+E;AAC/E,+BAA+B;AAC/B,yEAAyE;AACzE,uEAAuE;AACvE,qEAAqE;AACrE,4EAA4E;AAC5E,EAAE;AACF,6EAA6E;AAC7E,+EAA+E;AAC/E,2EAA2E;AAC3E,+EAA+E;AAC/E,+EAA+E;AAC/E,8EAA8E;AAC9E,6DAA6D;AAC7D,uEAAuE;AACvE,EAAE;AACF,yEAAyE;AACzE,sEAAsE;AACtE,2EAA2E;AAC3E,4EA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AKI,EAAE;IAEN,MAAM,eAAe,GAAG,OAAO,CAAC,UAAU,IAAI,iBAAiB,CAAC;IAChE,OAAO,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,CAC1B,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,KAAK,EAAE;QAC9C,IAAI,EAAE,MAAM,CAAC,QAAQ;QACrB,UAAU,EAAE,eAAe,CAAC,MAAM,EAAE,QAAQ,CAAC;KAC9C,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,21 @@
1
+ export type { ApprovalActor, ApprovalAuditEvent, ApprovalAuditSink, ApprovalRole, } from './contract.js';
2
+ export { APPROVAL_ROLES, BREAKWATER_ACTOR_KEY, BREAKWATER_APPROVED_CONNECTORS_KEY, BREAKWATER_WORKFLOW_SCOPE_KEY, RUN_START_ROLES, } from './contract.js';
3
+ export type { ApprovalDatabase, ApprovalPreparedStatement, } from './d1-store.js';
4
+ export { approvalGrantProvider, approvalGrantProviderFromFactory, approvedConnectorsForLeg, defaultResumeData, resumeViaRuntime, } from './grants.js';
5
+ export type { PurgeExpiredApprovalsOptions } from './retention.js';
6
+ export { purgeExpiredApprovals } from './retention.js';
7
+ export type { ApprovalRouter, ApprovalRouterOptions } from './router.js';
8
+ export { CLIENT_CREATE_FIELDS, createApprovalRouter, TCB_ONLY_CREATE_FIELDS, } from './router.js';
9
+ export type { ApprovalServiceOptions, SweepSLAOptions } from './service.js';
10
+ export { ApprovalAuthzError, ApprovalConflictError, ApprovalService, InvalidApprovalInputError, sweepSLA, UnknownApprovalError, } from './service.js';
11
+ export type { ApprovalPatch, ApprovalStore, CreateResult } from './store.js';
12
+ export { InMemoryApprovalStore, stepKeyOf } from './store.js';
13
+ export type { SystemApprovalStore, TenantBoundApprovalStore, } from './tenant-brand.js';
14
+ export { TENANT_BOUND } from './tenant-brand.js';
15
+ export type { CreateTenantResolverOptions, TenantContext, TenantResolver, } from './tenant-context.js';
16
+ export { createTenantResolver, TenantResolutionError, } from './tenant-context.js';
17
+ export type { ApprovalStoreFactory } from './tenant-store.js';
18
+ export { D1ApprovalStoreFactory, InMemoryApprovalStoreFactory, } from './tenant-store.js';
19
+ export type { ApprovalCursor, ApprovalDecision, ApprovalListFilter, ApprovalListOrder, ApprovalMetrics, ApprovalPriority, ApprovalRecord, ApprovalStatus, CreateApprovalInput, DecideResult, ResumeOutcome, } from './types.js';
20
+ export { APPROVAL_LIST_ORDERS, APPROVAL_PRIORITIES, APPROVAL_STATUSES, approvalCursor, approvalListOrder, byReviewerOrder, MAX_APPROVAL_LIST_LIMIT, OPEN_STATUSES, parseApprovalCursor, TERMINAL_APPROVAL_STATUSES, } from './types.js';
21
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/approval-api/index.ts"],"names":[],"mappings":"AAQA,YAAY,EACV,aAAa,EACb,kBAAkB,EAClB,iBAAiB,EACjB,YAAY,GACb,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,kCAAkC,EAClC,6BAA6B,EAC7B,eAAe,GAChB,MAAM,eAAe,CAAC;AAIvB,YAAY,EACV,gBAAgB,EAChB,yBAAyB,GAC1B,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,qBAAqB,EACrB,gCAAgC,EAChC,wBAAwB,EACxB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACvD,YAAY,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC5E,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,eAAe,EACf,yBAAyB,EACzB,QAAQ,EACR,oBAAoB,GACrB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC9D,YAAY,EACV,mBAAmB,EACnB,wBAAwB,GACzB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EACV,2BAA2B,EAC3B,aAAa,EACb,cAAc,GACf,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,oBAAoB,EACpB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EACL,sBAAsB,EACtB,4BAA4B,GAC7B,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EAClB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,aAAa,GACd,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,eAAe,EACf,uBAAuB,EACvB,aAAa,EACb,mBAAmB,EACnB,0BAA0B,GAC3B,MAAM,YAAY,CAAC"}
@@ -0,0 +1,18 @@
1
+ // Approval API — the Phase 3 enterprise-controls surface: a CAS-backed
2
+ // approval queue (claim / decide / delegate), SLA tracking with escalation,
3
+ // role-authorized REST routing, audit emission, and the grant-minting seam
4
+ // that turns an approved record into a breakwater connector grant at
5
+ // start/resume (grants.ts). Plugs into the DO runner via
6
+ // requestContextForRun + resumeViaRuntime; persistence is D1 (or the
7
+ // in-memory store for tests/dev).
8
+ export { APPROVAL_ROLES, BREAKWATER_ACTOR_KEY, BREAKWATER_APPROVED_CONNECTORS_KEY, BREAKWATER_WORKFLOW_SCOPE_KEY, RUN_START_ROLES, } from './contract.js';
9
+ export { approvalGrantProvider, approvalGrantProviderFromFactory, approvedConnectorsForLeg, defaultResumeData, resumeViaRuntime, } from './grants.js';
10
+ export { purgeExpiredApprovals } from './retention.js';
11
+ export { CLIENT_CREATE_FIELDS, createApprovalRouter, TCB_ONLY_CREATE_FIELDS, } from './router.js';
12
+ export { ApprovalAuthzError, ApprovalConflictError, ApprovalService, InvalidApprovalInputError, sweepSLA, UnknownApprovalError, } from './service.js';
13
+ export { InMemoryApprovalStore, stepKeyOf } from './store.js';
14
+ export { TENANT_BOUND } from './tenant-brand.js';
15
+ export { createTenantResolver, TenantResolutionError, } from './tenant-context.js';
16
+ export { D1ApprovalStoreFactory, InMemoryApprovalStoreFactory, } from './tenant-store.js';
17
+ export { APPROVAL_LIST_ORDERS, APPROVAL_PRIORITIES, APPROVAL_STATUSES, approvalCursor, approvalListOrder, byReviewerOrder, MAX_APPROVAL_LIST_LIMIT, OPEN_STATUSES, parseApprovalCursor, TERMINAL_APPROVAL_STATUSES, } from './types.js';
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/approval-api/index.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,4EAA4E;AAC5E,2EAA2E;AAC3E,qEAAqE;AACrE,yDAAyD;AACzD,qEAAqE;AACrE,kCAAkC;AAQlC,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,kCAAkC,EAClC,6BAA6B,EAC7B,eAAe,GAChB,MAAM,eAAe,CAAC;AAQvB,OAAO,EACL,qBAAqB,EACrB,gCAAgC,EAChC,wBAAwB,EACxB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,eAAe,EACf,yBAAyB,EACzB,QAAQ,EACR,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAK9D,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAMjD,OAAO,EACL,oBAAoB,EACpB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,sBAAsB,EACtB,4BAA4B,GAC7B,MAAM,mBAAmB,CAAC;AAc3B,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,eAAe,EACf,uBAAuB,EACvB,aAAa,EACb,mBAAmB,EACnB,0BAA0B,GAC3B,MAAM,YAAY,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { SystemApprovalStore } from './tenant-brand.js';
2
+ export interface PurgeExpiredApprovalsOptions {
3
+ /**
4
+ * Decided (approved/rejected) records whose terminal timestamp — decidedAt,
5
+ * or updatedAt when a decided record was persisted without one — is older
6
+ * than this are eligible. Open requests (pending/claimed/escalated) are
7
+ * NEVER purged at any age: an approval still awaiting a decision is not
8
+ * garbage (mirrors purgeExpiredWorkflowRuns' "live runs are never
9
+ * purged"). An abandoned tenant's still-open approvals are reclaimed only
10
+ * by purgeTenant() at offboarding.
11
+ */
12
+ ttlMs: number;
13
+ /** Injectable clock (tests, deterministic TTL math). Default: Date.now. */
14
+ now?: () => number;
15
+ /**
16
+ * Records deleted per call — one LIMIT-batched DELETE per firing; the
17
+ * shrinking eligible set is the cursor across firings (same convention as
18
+ * purgeExpiredWorkflowRuns' row-only path). Default 1000.
19
+ */
20
+ limit?: number;
21
+ }
22
+ /**
23
+ * Deletes decided approval records past their retention TTL, ACROSS
24
+ * TENANTS. Cron-owned TCB code — deliberately NOT a service method and NOT
25
+ * reachable over HTTP: an unfiltered cross-tenant DELETE behind a role check
26
+ * would be the same IDOR-shaped hole sweepSLA's doc comment describes for an
27
+ * HTTP-reachable sweep. The distinct SystemApprovalStore parameter type
28
+ * makes "cross-tenant deletes happen only inside the TCB" a compile-time
29
+ * property, not a convention. Returns the number of deleted records.
30
+ */
31
+ export declare function purgeExpiredApprovals(store: SystemApprovalStore, options: PurgeExpiredApprovalsOptions): Promise<number>;
32
+ //# sourceMappingURL=retention.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retention.d.ts","sourceRoot":"","sources":["../../src/approval-api/retention.ts"],"names":[],"mappings":"AAoDA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAE7D,MAAM,WAAW,4BAA4B;IAC3C;;;;;;;;OAQG;IACH,KAAK,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;GAQG;AACH,wBAAsB,qBAAqB,CACzC,KAAK,EAAE,mBAAmB,EAC1B,OAAO,EAAE,4BAA4B,GACpC,OAAO,CAAC,MAAM,CAAC,CAyBjB"}
@@ -0,0 +1,81 @@
1
+ // Cron-owned approval-queue retention purge (2026-07-11 audit, D3) — the
2
+ // approvals analog of do-runner's purgeExpiredWorkflowRuns
3
+ // (do-runner/d1-storage.ts), structured like sweepSLA (service.ts): a free
4
+ // function over the cross-tenant SystemApprovalStore, unreachable from
5
+ // request scope by type, never a service method, never an HTTP route.
6
+ //
7
+ // TERMINAL-TIMESTAMP CHOICE: a decided (approved/rejected) record's terminal
8
+ // instant is decidedAt — service.ts's decide() always stamps decidedAt and
9
+ // updatedAt together in the same patch, so for every record created through
10
+ // the normal path the two are identical at decision time. A record can only
11
+ // lack decidedAt while carrying a terminal status by being written straight
12
+ // into the store (a TCB bulk-import, a hand-built fixture) — for that edge
13
+ // case updatedAt is the fallback: it is never absent, and for a status that
14
+ // itself never transitions again it is the best available proxy for "when
15
+ // did this row last change".
16
+ //
17
+ // ACCEPTED, BY DESIGN: this purge has NO liveness check against the
18
+ // run/suspension a decided record's grant belongs to — unlike
19
+ // purgeExpiredWorkflowRuns, which is gated on the RUN's own terminal status.
20
+ // A liveness check would need either a join against do-runner's snapshot
21
+ // table (outside this module's structural ApprovalDatabase seam) or a
22
+ // host-supplied predicate, for a failure mode that is now self-healing on its
23
+ // own (see RECOVERY below) — not worth the coupling.
24
+ //
25
+ // RECOVERY (D4, 2026-07-11 audit): service.ts's decide() derives the grant
26
+ // and resumes the run synchronously in the common case; its #resume() catch
27
+ // documents the fallback path for a failed resume — "the run stays
28
+ // suspended; a later resume derives the same grants from the store". If this
29
+ // purge deletes the decided record before that retry lands, the retry fails
30
+ // CLOSED (no leak — see grants.ts) and the run is left suspended with no
31
+ // approval record: the same wedge shape host-kit/approval-bridge.ts's
32
+ // reconcileApprovalsForSummary exists to close for a live re-queue failure.
33
+ // The next status() read of that run re-files a FRESH approval bound to the
34
+ // CURRENT suspension fingerprint, requiring a NEW decision — deliberately:
35
+ // an approval that aged past retention should not silently re-arm the grant
36
+ // it already spent. One separation-of-duties relaxation applies only in this
37
+ // recovery path: the re-filed record's requestedBy is the system actor
38
+ // rather than a human, so the reviewer who decided the purged record MAY
39
+ // legally decide its replacement — there is no human "who advanced the run
40
+ // to this suspension" left to attribute it to. Operators should still set
41
+ // APPROVAL_RETENTION_DAYS well beyond any expected resume-retry window: a
42
+ // re-decision is real reviewer work, not a free retry.
43
+ //
44
+ // reconcileApprovalsForSummary's healing is not limited to this purge-then-
45
+ // retry trigger (2026-07-11 audit, follow-up): it also SUPERSEDES a
46
+ // stale-but-still-OPEN record -- a re-suspension of the same step while an
47
+ // earlier request for it still sits open, independent of any purge or TTL --
48
+ // before filing fresh. See its own doc comment (approval-bridge.ts) for the
49
+ // supersede mechanics; the terminal status it uses is 'rejected', so a
50
+ // superseded record is excluded from grant derivation by its STATUS alone,
51
+ // the same guarantee this purge relies on for a decided-then-purged record.
52
+ /**
53
+ * Deletes decided approval records past their retention TTL, ACROSS
54
+ * TENANTS. Cron-owned TCB code — deliberately NOT a service method and NOT
55
+ * reachable over HTTP: an unfiltered cross-tenant DELETE behind a role check
56
+ * would be the same IDOR-shaped hole sweepSLA's doc comment describes for an
57
+ * HTTP-reachable sweep. The distinct SystemApprovalStore parameter type
58
+ * makes "cross-tenant deletes happen only inside the TCB" a compile-time
59
+ * property, not a convention. Returns the number of deleted records.
60
+ */
61
+ export async function purgeExpiredApprovals(store, options) {
62
+ // Validated here so BOTH backends inherit it: a negative ttlMs turns the
63
+ // cutoff into a FUTURE timestamp, purging just-decided records instead of
64
+ // old ones, and a negative limit diverges by backend — the in-memory
65
+ // purge's `purged >= limit` guard never fires (no-op), while D1's `LIMIT
66
+ // -1` is unbounded (SQLite treats a negative LIMIT as "no limit"), turning
67
+ // a batch cap into a cross-tenant unbounded DELETE. limit: 0 (no-op) and
68
+ // ttlMs: 0 (purge decided approvals now) stay valid — both are real
69
+ // operator intents (see numberVar's allowZero convention).
70
+ if (!Number.isFinite(options.ttlMs) || options.ttlMs < 0) {
71
+ throw new TypeError(`purgeExpiredApprovals: ttlMs must be a non-negative finite number (got ${options.ttlMs})`);
72
+ }
73
+ if (options.limit !== undefined &&
74
+ (!Number.isInteger(options.limit) || options.limit < 0)) {
75
+ throw new TypeError(`purgeExpiredApprovals: limit must be a non-negative integer (got ${options.limit})`);
76
+ }
77
+ const now = options.now ?? Date.now;
78
+ const cutoffIso = new Date(now() - options.ttlMs).toISOString();
79
+ return store.purgeExpired(cutoffIso, options.limit ?? 1000);
80
+ }
81
+ //# sourceMappingURL=retention.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retention.js","sourceRoot":"","sources":["../../src/approval-api/retention.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,2DAA2D;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AC,KAAK,CAAC,IAAI,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,EACvD,CAAC;QACD,MAAM,IAAI,SAAS,CACjB,oEAAoE,OAAO,CAAC,KAAK,GAAG,CACrF,CAAC;IACJ,CAAC;IACD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC;IACpC,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAChE,OAAO,KAAK,CAAC,YAAY,CAAC,SAAS,EAAE,OAAO,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;AAC9D,CAAC"}
@@ -0,0 +1,51 @@
1
+ import { type TenantResolver } from './tenant-context.js';
2
+ /**
3
+ * Fields on CreateApprovalInput that select CAPABILITY or ATTRIBUTION, and so
4
+ * belong to the trusted computing base alone (security-threat-model.md, trust
5
+ * boundary 6). A request body that names any of them is rejected outright:
6
+ *
7
+ * - `connectors` IS the minted grant — an approved record's connectors become
8
+ * the requestContext grant the write gate checks.
9
+ * - `runScoped` turns a step-less record into a standing grant on every leg.
10
+ * - `stepPath`, `suspendedAt`, `resumedAt`, `resumeCount` select WHICH leg a
11
+ * grant mints on. A step-keyed body with no `suspendedAt` would fall into
12
+ * grants.ts's legacy decidedAt-after fallback and mint, so rejecting
13
+ * `connectors` alone is insufficient — reject the whole set.
14
+ * - `requestedBy` is the field decide()'s separation-of-duties check compares
15
+ * against; spoofing it lets one principal approve their own request.
16
+ */
17
+ export declare const TCB_ONLY_CREATE_FIELDS: readonly ["connectors", "stepPath", "suspendedAt", "resumedAt", "resumeCount", "runScoped", "requestedBy"];
18
+ /**
19
+ * The complement: what a request body MAY set. The create route copies ONLY
20
+ * these fields off the body — an allowlist, so a field added to
21
+ * CreateApprovalInput later is INERT over HTTP until it is deliberately
22
+ * classified here (fail closed by construction; a body spread would instead
23
+ * hand every future field to service.create unless someone remembered to
24
+ * extend the denylist above). TCB_ONLY_CREATE_FIELDS stays as the
25
+ * 400-with-a-reason layer; router.test.ts pins at the type level that the two
26
+ * lists exactly cover CreateApprovalInput.
27
+ */
28
+ export declare const CLIENT_CREATE_FIELDS: readonly ["workflowId", "runId", "title", "summary", "payload", "priority", "slaSeconds"];
29
+ export interface ApprovalRouterOptions {
30
+ /**
31
+ * Authenticates the request and binds the tenant-scoped service (INV-2).
32
+ * The router's first line is `resolve(request)`; the store the service
33
+ * wraps is constructed AFTER authentication, bound to the actor's tenant —
34
+ * there is no pre-auth service to leak through. undefined yields 401.
35
+ */
36
+ resolve: TenantResolver;
37
+ /** Route prefix. Default: '/api/approvals'. */
38
+ basePath?: string;
39
+ /**
40
+ * Mount `POST <basePath>` (create). Default false — every first-party host
41
+ * creates records in-process from an observed suspension (host-kit's
42
+ * approval bridge), so the HTTP route is an inert "file a request"
43
+ * affordance at best. When enabled it force-sets `requestedBy` to the
44
+ * authenticated actor and 400s on any TCB_ONLY_CREATE_FIELDS member, so it
45
+ * can never author capability.
46
+ */
47
+ allowCreate?: boolean;
48
+ }
49
+ export type ApprovalRouter = (request: Request) => Promise<Response | null>;
50
+ export declare function createApprovalRouter(options: ApprovalRouterOptions): ApprovalRouter;
51
+ //# sourceMappingURL=router.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/approval-api/router.ts"],"names":[],"mappings":"AAqBA,OAAO,EAEL,KAAK,cAAc,EACpB,MAAM,qBAAqB,CAAC;AAc7B;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,sBAAsB,4GAQzB,CAAC;AAEX;;;;;;;;;GASG;AACH,eAAO,MAAM,oBAAoB,2FAW9B,CAAC;AAEJ,MAAM,WAAW,qBAAqB;IACpC;;;;;OAKG;IACH,OAAO,EAAE,cAAc,CAAC;IACxB,+CAA+C;IAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;AAyH5E,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,qBAAqB,GAC7B,cAAc,CA6FhB"}