async-bulkhead-llm 3.16.0 → 3.17.1

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 (173) hide show
  1. package/CHANGELOG.md +127 -4
  2. package/README.md +28 -2
  3. package/dist/cjs/bulkhead.cjs +119 -1666
  4. package/dist/cjs/bulkhead.cjs.map +1 -1
  5. package/dist/cjs/engine/acquire.cjs +144 -0
  6. package/dist/cjs/engine/acquire.cjs.map +1 -0
  7. package/dist/cjs/engine/admission-counts.cjs +92 -0
  8. package/dist/cjs/engine/admission-counts.cjs.map +1 -0
  9. package/dist/cjs/engine/admission-request.cjs +28 -0
  10. package/dist/cjs/engine/admission-request.cjs.map +1 -0
  11. package/dist/cjs/engine/admitted-run.cjs +100 -0
  12. package/dist/cjs/engine/admitted-run.cjs.map +1 -0
  13. package/dist/cjs/engine/admitted-token.cjs +192 -0
  14. package/dist/cjs/engine/admitted-token.cjs.map +1 -0
  15. package/dist/cjs/engine/bypass.cjs +162 -0
  16. package/dist/cjs/engine/bypass.cjs.map +1 -0
  17. package/dist/cjs/engine/capacity.cjs +89 -0
  18. package/dist/cjs/engine/capacity.cjs.map +1 -0
  19. package/dist/cjs/engine/class-limits.cjs +101 -0
  20. package/dist/cjs/engine/class-limits.cjs.map +1 -0
  21. package/dist/cjs/engine/class-slots.cjs +49 -0
  22. package/dist/cjs/engine/class-slots.cjs.map +1 -0
  23. package/dist/cjs/engine/dedup-flow.cjs +207 -0
  24. package/dist/cjs/engine/dedup-flow.cjs.map +1 -0
  25. package/dist/cjs/engine/events.cjs +35 -0
  26. package/dist/cjs/engine/events.cjs.map +1 -0
  27. package/dist/cjs/engine/limits.cjs +313 -0
  28. package/dist/cjs/engine/limits.cjs.map +1 -0
  29. package/dist/cjs/engine/observe-stats.cjs +43 -0
  30. package/dist/cjs/engine/observe-stats.cjs.map +1 -0
  31. package/dist/cjs/engine/options.cjs +79 -0
  32. package/dist/cjs/engine/options.cjs.map +1 -0
  33. package/dist/cjs/engine/reject-detail.cjs +103 -0
  34. package/dist/cjs/engine/reject-detail.cjs.map +1 -0
  35. package/dist/cjs/engine/reservation.cjs +55 -0
  36. package/dist/cjs/engine/reservation.cjs.map +1 -0
  37. package/dist/cjs/engine/run-options.cjs +59 -0
  38. package/dist/cjs/engine/run-options.cjs.map +1 -0
  39. package/dist/cjs/engine/run.cjs +76 -0
  40. package/dist/cjs/engine/run.cjs.map +1 -0
  41. package/dist/cjs/engine/shared-usage.cjs +70 -0
  42. package/dist/cjs/engine/shared-usage.cjs.map +1 -0
  43. package/dist/cjs/engine/shared.cjs +17 -0
  44. package/dist/cjs/engine/shared.cjs.map +1 -0
  45. package/dist/cjs/engine/shutdown.cjs +58 -0
  46. package/dist/cjs/engine/shutdown.cjs.map +1 -0
  47. package/dist/cjs/engine/stats.cjs +92 -0
  48. package/dist/cjs/engine/stats.cjs.map +1 -0
  49. package/dist/cjs/engine/token-ledger.cjs +117 -0
  50. package/dist/cjs/engine/token-ledger.cjs.map +1 -0
  51. package/dist/cjs/engine/usage-report.cjs +95 -0
  52. package/dist/cjs/engine/usage-report.cjs.map +1 -0
  53. package/dist/cjs/engine/would-admit.cjs +37 -0
  54. package/dist/cjs/engine/would-admit.cjs.map +1 -0
  55. package/dist/cjs/errors.cjs +22 -1
  56. package/dist/cjs/errors.cjs.map +1 -1
  57. package/dist/cjs/index.cjs +11 -2
  58. package/dist/cjs/index.cjs.map +1 -1
  59. package/dist/esm/bulkhead.js +119 -1666
  60. package/dist/esm/bulkhead.js.map +1 -1
  61. package/dist/esm/engine/acquire.js +141 -0
  62. package/dist/esm/engine/acquire.js.map +1 -0
  63. package/dist/esm/engine/admission-counts.js +89 -0
  64. package/dist/esm/engine/admission-counts.js.map +1 -0
  65. package/dist/esm/engine/admission-request.js +25 -0
  66. package/dist/esm/engine/admission-request.js.map +1 -0
  67. package/dist/esm/engine/admitted-run.js +97 -0
  68. package/dist/esm/engine/admitted-run.js.map +1 -0
  69. package/dist/esm/engine/admitted-token.js +189 -0
  70. package/dist/esm/engine/admitted-token.js.map +1 -0
  71. package/dist/esm/engine/bypass.js +159 -0
  72. package/dist/esm/engine/bypass.js.map +1 -0
  73. package/dist/esm/engine/capacity.js +86 -0
  74. package/dist/esm/engine/capacity.js.map +1 -0
  75. package/dist/esm/engine/class-limits.js +95 -0
  76. package/dist/esm/engine/class-limits.js.map +1 -0
  77. package/dist/esm/engine/class-slots.js +46 -0
  78. package/dist/esm/engine/class-slots.js.map +1 -0
  79. package/dist/esm/engine/dedup-flow.js +204 -0
  80. package/dist/esm/engine/dedup-flow.js.map +1 -0
  81. package/dist/esm/engine/events.js +32 -0
  82. package/dist/esm/engine/events.js.map +1 -0
  83. package/dist/esm/engine/limits.js +310 -0
  84. package/dist/esm/engine/limits.js.map +1 -0
  85. package/dist/esm/engine/observe-stats.js +40 -0
  86. package/dist/esm/engine/observe-stats.js.map +1 -0
  87. package/dist/esm/engine/options.js +76 -0
  88. package/dist/esm/engine/options.js.map +1 -0
  89. package/dist/esm/engine/reject-detail.js +100 -0
  90. package/dist/esm/engine/reject-detail.js.map +1 -0
  91. package/dist/esm/engine/reservation.js +52 -0
  92. package/dist/esm/engine/reservation.js.map +1 -0
  93. package/dist/esm/engine/run-options.js +55 -0
  94. package/dist/esm/engine/run-options.js.map +1 -0
  95. package/dist/esm/engine/run.js +73 -0
  96. package/dist/esm/engine/run.js.map +1 -0
  97. package/dist/esm/engine/shared-usage.js +67 -0
  98. package/dist/esm/engine/shared-usage.js.map +1 -0
  99. package/dist/esm/engine/shared.js +14 -0
  100. package/dist/esm/engine/shared.js.map +1 -0
  101. package/dist/esm/engine/shutdown.js +55 -0
  102. package/dist/esm/engine/shutdown.js.map +1 -0
  103. package/dist/esm/engine/stats.js +89 -0
  104. package/dist/esm/engine/stats.js.map +1 -0
  105. package/dist/esm/engine/token-ledger.js +114 -0
  106. package/dist/esm/engine/token-ledger.js.map +1 -0
  107. package/dist/esm/engine/usage-report.js +86 -0
  108. package/dist/esm/engine/usage-report.js.map +1 -0
  109. package/dist/esm/engine/would-admit.js +34 -0
  110. package/dist/esm/engine/would-admit.js.map +1 -0
  111. package/dist/esm/errors.js +20 -0
  112. package/dist/esm/errors.js.map +1 -1
  113. package/dist/esm/index.js +10 -2
  114. package/dist/esm/index.js.map +1 -1
  115. package/dist/types/bulkhead.d.ts +33 -6
  116. package/dist/types/bulkhead.d.ts.map +1 -1
  117. package/dist/types/engine/acquire.d.ts +25 -0
  118. package/dist/types/engine/acquire.d.ts.map +1 -0
  119. package/dist/types/engine/admission-counts.d.ts +30 -0
  120. package/dist/types/engine/admission-counts.d.ts.map +1 -0
  121. package/dist/types/engine/admission-request.d.ts +12 -0
  122. package/dist/types/engine/admission-request.d.ts.map +1 -0
  123. package/dist/types/engine/admitted-run.d.ts +19 -0
  124. package/dist/types/engine/admitted-run.d.ts.map +1 -0
  125. package/dist/types/engine/admitted-token.d.ts +42 -0
  126. package/dist/types/engine/admitted-token.d.ts.map +1 -0
  127. package/dist/types/engine/bypass.d.ts +17 -0
  128. package/dist/types/engine/bypass.d.ts.map +1 -0
  129. package/dist/types/engine/capacity.d.ts +36 -0
  130. package/dist/types/engine/capacity.d.ts.map +1 -0
  131. package/dist/types/engine/class-limits.d.ts +20 -0
  132. package/dist/types/engine/class-limits.d.ts.map +1 -0
  133. package/dist/types/engine/class-slots.d.ts +20 -0
  134. package/dist/types/engine/class-slots.d.ts.map +1 -0
  135. package/dist/types/engine/dedup-flow.d.ts +41 -0
  136. package/dist/types/engine/dedup-flow.d.ts.map +1 -0
  137. package/dist/types/engine/events.d.ts +13 -0
  138. package/dist/types/engine/events.d.ts.map +1 -0
  139. package/dist/types/engine/limits.d.ts +45 -0
  140. package/dist/types/engine/limits.d.ts.map +1 -0
  141. package/dist/types/engine/observe-stats.d.ts +14 -0
  142. package/dist/types/engine/observe-stats.d.ts.map +1 -0
  143. package/dist/types/engine/options.d.ts +21 -0
  144. package/dist/types/engine/options.d.ts.map +1 -0
  145. package/dist/types/engine/reject-detail.d.ts +25 -0
  146. package/dist/types/engine/reject-detail.d.ts.map +1 -0
  147. package/dist/types/engine/reservation.d.ts +13 -0
  148. package/dist/types/engine/reservation.d.ts.map +1 -0
  149. package/dist/types/engine/run-options.d.ts +25 -0
  150. package/dist/types/engine/run-options.d.ts.map +1 -0
  151. package/dist/types/engine/run.d.ts +28 -0
  152. package/dist/types/engine/run.d.ts.map +1 -0
  153. package/dist/types/engine/shared-usage.d.ts +28 -0
  154. package/dist/types/engine/shared-usage.d.ts.map +1 -0
  155. package/dist/types/engine/shared.d.ts +63 -0
  156. package/dist/types/engine/shared.d.ts.map +1 -0
  157. package/dist/types/engine/shutdown.d.ts +20 -0
  158. package/dist/types/engine/shutdown.d.ts.map +1 -0
  159. package/dist/types/engine/stats.d.ts +28 -0
  160. package/dist/types/engine/stats.d.ts.map +1 -0
  161. package/dist/types/engine/token-ledger.d.ts +40 -0
  162. package/dist/types/engine/token-ledger.d.ts.map +1 -0
  163. package/dist/types/engine/usage-report.d.ts +63 -0
  164. package/dist/types/engine/usage-report.d.ts.map +1 -0
  165. package/dist/types/engine/would-admit.d.ts +16 -0
  166. package/dist/types/engine/would-admit.d.ts.map +1 -0
  167. package/dist/types/errors.d.ts +15 -2
  168. package/dist/types/errors.d.ts.map +1 -1
  169. package/dist/types/index.d.ts +11 -3
  170. package/dist/types/index.d.ts.map +1 -1
  171. package/dist/types/types.d.ts +74 -1
  172. package/dist/types/types.d.ts.map +1 -1
  173. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -7,6 +7,115 @@ and adheres to [Semantic Versioning](https://semver.org/).
7
7
 
8
8
  ---
9
9
 
10
+ ## [3.17.1] - 2026-11-22
11
+
12
+ Internal restructuring release. No public API, runtime behavior, or
13
+ dependency changed; no upgrade action is required.
14
+
15
+ ### Internal
16
+
17
+ * **`createLLMBulkhead` split into focused engine modules.** The former
18
+ 2,690-line `src/bulkhead.ts` is now a short entry point that validates
19
+ options and wires together 25 modules under `src/engine/`, one concern
20
+ each: limits, token ledger, class slots, admission counts, capacity checks,
21
+ the admitted token, `run()`, observe mode, deduplication, stats, events,
22
+ and shutdown. No file under `src/engine/` is longer than 500 lines and no
23
+ function is longer than 150.
24
+
25
+ * **One owner per counter.** Every limit and counter is now written in
26
+ exactly one module, and a global counter and its per-class twin are always
27
+ moved by the same function. Token counters live in `token-ledger.ts`;
28
+ admitted, released, rejected, and abandoned counts in
29
+ `admission-counts.ts`; per-class slot counts in `class-slots.ts`; and the
30
+ revision, budget, high-priority reserve, and class limits in `limits.ts`.
31
+ The per-class state object that functions used to pass around is gone: a
32
+ class travels as its id. The global slot count stays inside
33
+ `reconfigurable-bulkhead.ts`, which is unchanged.
34
+
35
+ * **`rename-cjs.mjs` handles nested output folders.** The CommonJS rename
36
+ step now walks `dist/cjs` recursively, so the `.cjs` renames, `require()`
37
+ rewrites, `sourceMappingURL` trailers, and source-map `file` fields are
38
+ also correct for `dist/cjs/engine/*`.
39
+
40
+ ### Added
41
+
42
+ * **Class-versus-global counter tests.**
43
+ `test/class_counter_invariants.test.ts` takes admissions through a
44
+ reserve, growing and shrinking usage reports, abandonment, limit updates,
45
+ and release, and checks after every step that each per-class counter adds
46
+ up to its global counterpart. The tests read `stats()` only. Before this,
47
+ nothing in the suite could see a class counter drift from its global twin.
48
+
49
+ ### Compatibility
50
+
51
+ * The package entry point exports the identical surface, and the declared
52
+ type of `createLLMBulkhead` and of each of its methods is unchanged.
53
+ * Results, errors, event payloads, rejection reasons and constraints,
54
+ `stats()` snapshots, and the order of counter updates relative to events
55
+ match 3.17.0. This was checked by driving the 3.17.0 build and this build
56
+ with the same 30,000 seeded random scenarios, including listeners that call
57
+ back into the bulkhead, and comparing everything they returned and emitted.
58
+ * `dist/` gains an `engine/` folder for ESM, CJS, and declarations. Deep
59
+ imports of internal modules remain unsupported; the `exports` map still
60
+ exposes only the entry point.
61
+ * The runtime dependency remains `async-bulkhead-ts@^1.0.1`.
62
+
63
+ ## [3.17.0] - 2026-09-03
64
+
65
+ ### Added
66
+
67
+ * **Resource attribution for admitted work.** Successful acquire results,
68
+ tokens, run contexts, and admission/release events now expose immutable
69
+ `resources` with exact borrowed-concurrency and borrowed-token attribution.
70
+
71
+ * **Borrowed local-slot abandonment.** `LLMToken` and admitted run contexts add
72
+ idempotent `abandonBorrowedConcurrency()`. It returns only a concurrency slot
73
+ that came from shared class capacity; token holds remain charged until final
74
+ release. New global and per-class counters plus the
75
+ `borrowedConcurrencyAbandon` event expose the split lifecycle. The event
76
+ carries a `cause` of `manual` or `deadline`, and
77
+ `llm.borrowedConcurrencyAbandonedByCause` counts each separately, so
78
+ deliberate shedding is distinguishable from a lease configured too tight.
79
+
80
+ * **Wall-clock borrowed-slot deadline.** `run()` accepts
81
+ `borrowedConcurrencyDeadlineMs`. The post-admission timer applies only to a
82
+ borrowed local slot. Expiry returns that slot, aborts the callback signal,
83
+ and rejects the caller with `LLMBorrowedConcurrencyDeadlineError` while the
84
+ underlying callback retains responsibility for token settlement.
85
+
86
+ ### Fixed
87
+
88
+ * `drain()` now waits for final LLM settlement even after local concurrency was
89
+ abandoned. Timed drain results report outstanding token-holding admissions
90
+ instead of incorrectly declaring the bulkhead drained when only the local
91
+ slot has returned.
92
+
93
+ ### Contract
94
+
95
+ * A client-side deadline is an enforceable restoration bound for the local
96
+ admission slot only. It does not prove that a provider account, model queue,
97
+ or accelerator stopped work. Consumers needing a hard upstream guarantee
98
+ must keep an unlent floor or use authoritative provider-side reclamation.
99
+
100
+ ### Compatibility
101
+
102
+ * Existing methods and event names remain available. Exact object-equality
103
+ consumers should account for new resource fields and counters. Callers that
104
+ do not use borrowed-slot abandonment retain their previous lifecycle.
105
+
106
+ * After `abandonBorrowedConcurrency()`, `drain()` without `timeoutMs` waits for
107
+ the matching token settlement even though the local slot is already free.
108
+ Manual `acquire()` callers must eventually call `release()` or an unbounded
109
+ drain remains pending; bounded drain reports the admission as outstanding.
110
+ This split lifecycle is new in 3.17 and prevents shutdown from reporting
111
+ false completion.
112
+
113
+ * `Listener` and `LLMBorrowedConcurrencyAbandonCause` are new entry-point type
114
+ exports. `Listener` was previously reachable only through `on()` without
115
+ being nameable by consumers.
116
+
117
+ * The runtime dependency remains `async-bulkhead-ts@^1.0.1`.
118
+
10
119
  ## [3.16.0] - 2026-08-25
11
120
 
12
121
  ### Added
@@ -77,8 +186,9 @@ dependency behavior changed.
77
186
 
78
187
  ## [3.15.2] - 2026-08-17
79
188
 
80
- Documentation-only release. No runtime code changed and the public API is
81
- identical to 3.15.1; no upgrade action is required.
189
+ Documentation release, with one development-only dependency update. No runtime
190
+ code changed and the public API is identical to 3.15.1; no upgrade action is
191
+ required.
82
192
 
83
193
  ### Changed
84
194
 
@@ -136,9 +246,22 @@ identical to 3.15.1; no upgrade action is required.
136
246
  use absolute repository URLs so they work from GitHub, npm, and the
137
247
  documentation site.
138
248
 
249
+ ### Security
250
+
251
+ * **Resolved GHSA-2v37-7h3g-55p8 in `nanoid` (development-only).** `npm audit`
252
+ reported this high-severity advisory -- custom generators can loop
253
+ indefinitely when `size` is zero -- against every `nanoid` before 3.3.18. The
254
+ lockfile now resolves 3.3.18, which carries the fix. `nanoid` reaches this
255
+ repository only as a development-time transitive dependency (`vitest` ->
256
+ `vite` -> `postcss` -> `nanoid`); it has never been a runtime dependency and
257
+ is not part of the published tarball, so consumers of `async-bulkhead-llm`
258
+ were never exposed. `npm audit` now reports no known vulnerabilities at any
259
+ severity.
260
+
139
261
  ### Compatibility
140
262
 
141
- * No API, behavior, or dependency changes for consumers. The published tarball
263
+ * No API, behavior, or runtime dependency changes for consumers; the `nanoid`
264
+ bump is confined to development-only tooling. The published tarball
142
265
  contents are unchanged apart from the rewritten `README.md`. `guides/` and
143
266
  the documentation tooling are development-only and are not published.
144
267
 
@@ -1032,4 +1155,4 @@ Changes required only if you:
1032
1155
  * `tryAcquire()` from the base library is not exposed — synchronous non-blocking admission is not meaningful for LLM workloads where token budget requires the full request object
1033
1156
  * Zero dependencies beyond `async-bulkhead-ts`
1034
1157
 
1035
- ---
1158
+ ---
package/README.md CHANGED
@@ -161,6 +161,24 @@ bulkhead.on('reject', ({ reason, detail }) => metrics.increment(reason));
161
161
  underlying concurrency wait, so local admission cost and queue contention stay
162
162
  separate.
163
163
 
164
+ Admissions also expose exact resource attribution through `resources`.
165
+ For class policies that lend shared concurrency, `run()` can enforce a
166
+ wall-clock lease on the borrowed local slot:
167
+
168
+ ```ts
169
+ await bulkhead.run(request, callProvider, {
170
+ admissionClass: 'background',
171
+ borrowedConcurrencyDeadlineMs: 30_000,
172
+ });
173
+ ```
174
+
175
+ Expiry returns that local concurrency slot, aborts the callback signal, and
176
+ rejects with `LLMBorrowedConcurrencyDeadlineError`. It deliberately retains
177
+ the token hold until the callback settles. This is a local restoration bound,
178
+ not proof that a provider account, accelerator queue, or remote generation was
179
+ reclaimed. Keep an unlent floor or a real provider-side partition for upstream
180
+ capacity that client cancellation cannot authoritatively release.
181
+
164
182
  → [Stats and events](guides/telemetry.md)
165
183
 
166
184
  ## Shutting down cleanly
@@ -171,8 +189,16 @@ bulkhead.close(); // stop admitting
171
189
  await bulkhead.drain({ timeoutMs: 10_000 }); // wait, but not forever
172
190
  ```
173
191
 
174
- `drain()` with a deadline always resolves and tells you what it abandoned, so
175
- one stuck upstream stream can't hold your deploy hostage.
192
+ `drain()` with a deadline always resolves and reports what remains outstanding;
193
+ it does not abandon or release that work. One stuck upstream stream therefore
194
+ cannot hold the shutdown decision open indefinitely.
195
+
196
+ Without `timeoutMs`, `drain()` waits for final token settlement as well as base
197
+ concurrency. In particular, returning a borrowed slot with
198
+ `abandonBorrowedConcurrency()` does not complete the admission: a manual
199
+ `acquire()` caller must still call `release()`, or the unbounded drain remains
200
+ pending. This preserves the distinction between restoring a local slot and
201
+ settling the resource accounting for the work behind it.
176
202
 
177
203
  ---
178
204