async-bulkhead-llm 3.17.0 → 3.18.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.
- package/CHANGELOG.md +181 -0
- package/README.md +46 -0
- package/dist/cjs/bulkhead.cjs +140 -1817
- package/dist/cjs/bulkhead.cjs.map +1 -1
- package/dist/cjs/engine/acquire.cjs +144 -0
- package/dist/cjs/engine/acquire.cjs.map +1 -0
- package/dist/cjs/engine/admission-counts.cjs +111 -0
- package/dist/cjs/engine/admission-counts.cjs.map +1 -0
- package/dist/cjs/engine/admission-request.cjs +28 -0
- package/dist/cjs/engine/admission-request.cjs.map +1 -0
- package/dist/cjs/engine/admitted-run.cjs +143 -0
- package/dist/cjs/engine/admitted-run.cjs.map +1 -0
- package/dist/cjs/engine/admitted-token.cjs +283 -0
- package/dist/cjs/engine/admitted-token.cjs.map +1 -0
- package/dist/cjs/engine/bypass.cjs +162 -0
- package/dist/cjs/engine/bypass.cjs.map +1 -0
- package/dist/cjs/engine/capacity.cjs +89 -0
- package/dist/cjs/engine/capacity.cjs.map +1 -0
- package/dist/cjs/engine/class-limits.cjs +101 -0
- package/dist/cjs/engine/class-limits.cjs.map +1 -0
- package/dist/cjs/engine/class-slots.cjs +49 -0
- package/dist/cjs/engine/class-slots.cjs.map +1 -0
- package/dist/cjs/engine/dedup-flow.cjs +207 -0
- package/dist/cjs/engine/dedup-flow.cjs.map +1 -0
- package/dist/cjs/engine/events.cjs +36 -0
- package/dist/cjs/engine/events.cjs.map +1 -0
- package/dist/cjs/engine/limit-fit.cjs +108 -0
- package/dist/cjs/engine/limit-fit.cjs.map +1 -0
- package/dist/cjs/engine/limits.cjs +334 -0
- package/dist/cjs/engine/limits.cjs.map +1 -0
- package/dist/cjs/engine/observe-stats.cjs +43 -0
- package/dist/cjs/engine/observe-stats.cjs.map +1 -0
- package/dist/cjs/engine/options.cjs +79 -0
- package/dist/cjs/engine/options.cjs.map +1 -0
- package/dist/cjs/engine/reject-detail.cjs +103 -0
- package/dist/cjs/engine/reject-detail.cjs.map +1 -0
- package/dist/cjs/engine/reservation.cjs +55 -0
- package/dist/cjs/engine/reservation.cjs.map +1 -0
- package/dist/cjs/engine/run-options.cjs +59 -0
- package/dist/cjs/engine/run-options.cjs.map +1 -0
- package/dist/cjs/engine/run.cjs +76 -0
- package/dist/cjs/engine/run.cjs.map +1 -0
- package/dist/cjs/engine/running-calls.cjs +79 -0
- package/dist/cjs/engine/running-calls.cjs.map +1 -0
- package/dist/cjs/engine/shared-usage.cjs +70 -0
- package/dist/cjs/engine/shared-usage.cjs.map +1 -0
- package/dist/cjs/engine/shared.cjs +23 -0
- package/dist/cjs/engine/shared.cjs.map +1 -0
- package/dist/cjs/engine/shutdown.cjs +76 -0
- package/dist/cjs/engine/shutdown.cjs.map +1 -0
- package/dist/cjs/engine/stats.cjs +94 -0
- package/dist/cjs/engine/stats.cjs.map +1 -0
- package/dist/cjs/engine/stop-policy.cjs +40 -0
- package/dist/cjs/engine/stop-policy.cjs.map +1 -0
- package/dist/cjs/engine/stop.cjs +91 -0
- package/dist/cjs/engine/stop.cjs.map +1 -0
- package/dist/cjs/engine/token-ledger.cjs +117 -0
- package/dist/cjs/engine/token-ledger.cjs.map +1 -0
- package/dist/cjs/engine/usage-report.cjs +96 -0
- package/dist/cjs/engine/usage-report.cjs.map +1 -0
- package/dist/cjs/engine/would-admit.cjs +37 -0
- package/dist/cjs/engine/would-admit.cjs.map +1 -0
- package/dist/cjs/errors.cjs +25 -1
- package/dist/cjs/errors.cjs.map +1 -1
- package/dist/cjs/index.cjs +14 -3
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/profiles 2.cjs +16 -0
- package/dist/cjs/types 2.cjs +3 -0
- package/dist/cjs/types.cjs 2.map +1 -0
- package/dist/cjs/validation.cjs 2.map +1 -0
- package/dist/esm/bulkhead.js +140 -1817
- package/dist/esm/bulkhead.js.map +1 -1
- package/dist/esm/engine/acquire.js +141 -0
- package/dist/esm/engine/acquire.js.map +1 -0
- package/dist/esm/engine/admission-counts.js +108 -0
- package/dist/esm/engine/admission-counts.js.map +1 -0
- package/dist/esm/engine/admission-request.js +25 -0
- package/dist/esm/engine/admission-request.js.map +1 -0
- package/dist/esm/engine/admitted-run.js +140 -0
- package/dist/esm/engine/admitted-run.js.map +1 -0
- package/dist/esm/engine/admitted-token.js +280 -0
- package/dist/esm/engine/admitted-token.js.map +1 -0
- package/dist/esm/engine/bypass.js +159 -0
- package/dist/esm/engine/bypass.js.map +1 -0
- package/dist/esm/engine/capacity.js +86 -0
- package/dist/esm/engine/capacity.js.map +1 -0
- package/dist/esm/engine/class-limits.js +95 -0
- package/dist/esm/engine/class-limits.js.map +1 -0
- package/dist/esm/engine/class-slots.js +46 -0
- package/dist/esm/engine/class-slots.js.map +1 -0
- package/dist/esm/engine/dedup-flow.js +204 -0
- package/dist/esm/engine/dedup-flow.js.map +1 -0
- package/dist/esm/engine/events.js +33 -0
- package/dist/esm/engine/events.js.map +1 -0
- package/dist/esm/engine/limit-fit.js +105 -0
- package/dist/esm/engine/limit-fit.js.map +1 -0
- package/dist/esm/engine/limits.js +331 -0
- package/dist/esm/engine/limits.js.map +1 -0
- package/dist/esm/engine/observe-stats.js +40 -0
- package/dist/esm/engine/observe-stats.js.map +1 -0
- package/dist/esm/engine/options.js +76 -0
- package/dist/esm/engine/options.js.map +1 -0
- package/dist/esm/engine/reject-detail.js +100 -0
- package/dist/esm/engine/reject-detail.js.map +1 -0
- package/dist/esm/engine/reservation.js +52 -0
- package/dist/esm/engine/reservation.js.map +1 -0
- package/dist/esm/engine/run-options.js +55 -0
- package/dist/esm/engine/run-options.js.map +1 -0
- package/dist/esm/engine/run.js +73 -0
- package/dist/esm/engine/run.js.map +1 -0
- package/dist/esm/engine/running-calls.js +76 -0
- package/dist/esm/engine/running-calls.js.map +1 -0
- package/dist/esm/engine/shared-usage.js +67 -0
- package/dist/esm/engine/shared-usage.js.map +1 -0
- package/dist/esm/engine/shared.js +20 -0
- package/dist/esm/engine/shared.js.map +1 -0
- package/dist/esm/engine/shutdown.js +73 -0
- package/dist/esm/engine/shutdown.js.map +1 -0
- package/dist/esm/engine/stats.js +91 -0
- package/dist/esm/engine/stats.js.map +1 -0
- package/dist/esm/engine/stop-policy.js +37 -0
- package/dist/esm/engine/stop-policy.js.map +1 -0
- package/dist/esm/engine/stop.js +88 -0
- package/dist/esm/engine/stop.js.map +1 -0
- package/dist/esm/engine/token-ledger.js +114 -0
- package/dist/esm/engine/token-ledger.js.map +1 -0
- package/dist/esm/engine/usage-report.js +87 -0
- package/dist/esm/engine/usage-report.js.map +1 -0
- package/dist/esm/engine/would-admit.js +34 -0
- package/dist/esm/engine/would-admit.js.map +1 -0
- package/dist/esm/errors.js +23 -0
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.js +13 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/types/bulkhead.d.ts +38 -6
- package/dist/types/bulkhead.d.ts.map +1 -1
- package/dist/types/engine/acquire.d.ts +25 -0
- package/dist/types/engine/acquire.d.ts.map +1 -0
- package/dist/types/engine/admission-counts.d.ts +34 -0
- package/dist/types/engine/admission-counts.d.ts.map +1 -0
- package/dist/types/engine/admission-request.d.ts +12 -0
- package/dist/types/engine/admission-request.d.ts.map +1 -0
- package/dist/types/engine/admitted-run.d.ts +25 -0
- package/dist/types/engine/admitted-run.d.ts.map +1 -0
- package/dist/types/engine/admitted-token.d.ts +49 -0
- package/dist/types/engine/admitted-token.d.ts.map +1 -0
- package/dist/types/engine/bypass.d.ts +17 -0
- package/dist/types/engine/bypass.d.ts.map +1 -0
- package/dist/types/engine/capacity.d.ts +36 -0
- package/dist/types/engine/capacity.d.ts.map +1 -0
- package/dist/types/engine/class-limits.d.ts +20 -0
- package/dist/types/engine/class-limits.d.ts.map +1 -0
- package/dist/types/engine/class-slots.d.ts +20 -0
- package/dist/types/engine/class-slots.d.ts.map +1 -0
- package/dist/types/engine/dedup-flow.d.ts +41 -0
- package/dist/types/engine/dedup-flow.d.ts.map +1 -0
- package/dist/types/engine/events.d.ts +13 -0
- package/dist/types/engine/events.d.ts.map +1 -0
- package/dist/types/engine/limit-fit.d.ts +30 -0
- package/dist/types/engine/limit-fit.d.ts.map +1 -0
- package/dist/types/engine/limits.d.ts +47 -0
- package/dist/types/engine/limits.d.ts.map +1 -0
- package/dist/types/engine/observe-stats.d.ts +14 -0
- package/dist/types/engine/observe-stats.d.ts.map +1 -0
- package/dist/types/engine/options.d.ts +21 -0
- package/dist/types/engine/options.d.ts.map +1 -0
- package/dist/types/engine/reject-detail.d.ts +25 -0
- package/dist/types/engine/reject-detail.d.ts.map +1 -0
- package/dist/types/engine/reservation.d.ts +13 -0
- package/dist/types/engine/reservation.d.ts.map +1 -0
- package/dist/types/engine/run-options.d.ts +25 -0
- package/dist/types/engine/run-options.d.ts.map +1 -0
- package/dist/types/engine/run.d.ts +28 -0
- package/dist/types/engine/run.d.ts.map +1 -0
- package/dist/types/engine/running-calls.d.ts +55 -0
- package/dist/types/engine/running-calls.d.ts.map +1 -0
- package/dist/types/engine/shared-usage.d.ts +28 -0
- package/dist/types/engine/shared-usage.d.ts.map +1 -0
- package/dist/types/engine/shared.d.ts +76 -0
- package/dist/types/engine/shared.d.ts.map +1 -0
- package/dist/types/engine/shutdown.d.ts +23 -0
- package/dist/types/engine/shutdown.d.ts.map +1 -0
- package/dist/types/engine/stats.d.ts +28 -0
- package/dist/types/engine/stats.d.ts.map +1 -0
- package/dist/types/engine/stop-policy.d.ts +15 -0
- package/dist/types/engine/stop-policy.d.ts.map +1 -0
- package/dist/types/engine/stop.d.ts +44 -0
- package/dist/types/engine/stop.d.ts.map +1 -0
- package/dist/types/engine/token-ledger.d.ts +40 -0
- package/dist/types/engine/token-ledger.d.ts.map +1 -0
- package/dist/types/engine/usage-report.d.ts +65 -0
- package/dist/types/engine/usage-report.d.ts.map +1 -0
- package/dist/types/engine/would-admit.d.ts +16 -0
- package/dist/types/engine/would-admit.d.ts.map +1 -0
- package/dist/types/errors.d.ts +21 -2
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/index.d.ts +14 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +103 -2
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
export function createTokenLedger(budgetEnabled, classIds) {
|
|
2
|
+
let inFlightTokens = 0;
|
|
3
|
+
let totalReserved = 0;
|
|
4
|
+
let totalConsumed = 0;
|
|
5
|
+
let totalRefunded = 0;
|
|
6
|
+
let totalOverrun = 0;
|
|
7
|
+
const perClass = new Map();
|
|
8
|
+
for (const id of classIds) {
|
|
9
|
+
perClass.set(id, {
|
|
10
|
+
inFlightTokens: 0,
|
|
11
|
+
totalReserved: 0,
|
|
12
|
+
totalConsumed: 0,
|
|
13
|
+
totalRefunded: 0,
|
|
14
|
+
totalOverrun: 0,
|
|
15
|
+
totalBorrowedTokensReserved: 0,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function countersOf(classId) {
|
|
19
|
+
return classId === undefined ? undefined : perClass.get(classId);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Admission: hold `reserved` tokens for a call. `borrowedTokens` is the
|
|
23
|
+
* part of the reservation that sits above the class's protected floor.
|
|
24
|
+
*/
|
|
25
|
+
function reserve(classId, reserved, borrowedTokens) {
|
|
26
|
+
const cls = countersOf(classId);
|
|
27
|
+
if (budgetEnabled) {
|
|
28
|
+
inFlightTokens += reserved;
|
|
29
|
+
totalReserved += reserved;
|
|
30
|
+
}
|
|
31
|
+
if (cls !== undefined) {
|
|
32
|
+
cls.totalBorrowedTokensReserved += borrowedTokens;
|
|
33
|
+
if (budgetEnabled) {
|
|
34
|
+
cls.inFlightTokens += reserved;
|
|
35
|
+
cls.totalReserved += reserved;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** A usage report pushed a call's hold above what it held: overrun. */
|
|
40
|
+
function grow(classId, delta) {
|
|
41
|
+
const cls = countersOf(classId);
|
|
42
|
+
inFlightTokens += delta;
|
|
43
|
+
totalOverrun += delta;
|
|
44
|
+
if (cls !== undefined) {
|
|
45
|
+
cls.inFlightTokens += delta;
|
|
46
|
+
cls.totalOverrun += delta;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** A usage report lowered a call's hold: an early refund. */
|
|
50
|
+
function shrink(classId, refund) {
|
|
51
|
+
const cls = countersOf(classId);
|
|
52
|
+
inFlightTokens = Math.max(0, inFlightTokens - refund);
|
|
53
|
+
totalRefunded += refund;
|
|
54
|
+
if (cls !== undefined) {
|
|
55
|
+
cls.inFlightTokens = Math.max(0, cls.inFlightTokens - refund);
|
|
56
|
+
cls.totalRefunded += refund;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Final release: return a call's whole hold, and record what it actually
|
|
61
|
+
* consumed when usage is known. Returns the tokens refunded.
|
|
62
|
+
*/
|
|
63
|
+
function settle(classId, held, usage) {
|
|
64
|
+
const cls = countersOf(classId);
|
|
65
|
+
let refunded = 0;
|
|
66
|
+
if (usage && held > 0) {
|
|
67
|
+
const actual = usage.input + usage.output;
|
|
68
|
+
totalConsumed += actual;
|
|
69
|
+
if (cls !== undefined) {
|
|
70
|
+
cls.totalConsumed += actual;
|
|
71
|
+
}
|
|
72
|
+
if (actual < held) {
|
|
73
|
+
refunded = held - actual;
|
|
74
|
+
totalRefunded += refunded;
|
|
75
|
+
if (cls !== undefined) {
|
|
76
|
+
cls.totalRefunded += refunded;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
inFlightTokens = Math.max(0, inFlightTokens - held);
|
|
81
|
+
if (cls !== undefined) {
|
|
82
|
+
cls.inFlightTokens = Math.max(0, cls.inFlightTokens - held);
|
|
83
|
+
}
|
|
84
|
+
return refunded;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
reserve,
|
|
88
|
+
grow,
|
|
89
|
+
shrink,
|
|
90
|
+
settle,
|
|
91
|
+
/** Tokens currently held across the whole pool. */
|
|
92
|
+
inFlight: () => inFlightTokens,
|
|
93
|
+
/** Tokens currently held by one class. */
|
|
94
|
+
classInFlight: (classId) => perClass.get(classId).inFlightTokens,
|
|
95
|
+
/** A live read of one class's held tokens, without a lookup per read. */
|
|
96
|
+
classReader: (classId) => {
|
|
97
|
+
const cls = perClass.get(classId);
|
|
98
|
+
return () => cls.inFlightTokens;
|
|
99
|
+
},
|
|
100
|
+
/** Detached copy of the pool counters. */
|
|
101
|
+
totals: () => ({
|
|
102
|
+
inFlightTokens,
|
|
103
|
+
totalReserved,
|
|
104
|
+
totalConsumed,
|
|
105
|
+
totalRefunded,
|
|
106
|
+
totalOverrun,
|
|
107
|
+
}),
|
|
108
|
+
/** Detached copy of one class's counters. */
|
|
109
|
+
classTotals: (classId) => ({
|
|
110
|
+
...perClass.get(classId),
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=token-ledger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-ledger.js","sourceRoot":"","sources":["../../../src/engine/token-ledger.ts"],"names":[],"mappings":"AAwBA,MAAM,UAAU,iBAAiB,CAC/B,aAAsB,EACtB,QAA0B;IAE1B,IAAI,cAAc,GAAG,CAAC,CAAC;IACvB,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,MAAM,QAAQ,GAAG,IAAI,GAAG,EAA8B,CAAC;IACvD,KAAK,MAAM,EAAE,IAAI,QAAQ,EAAE,CAAC;QAC1B,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE;YACf,cAAc,EAAE,CAAC;YACjB,aAAa,EAAE,CAAC;YAChB,aAAa,EAAE,CAAC;YAChB,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AAI;QACJ,MAAM;QACN,MAAM;QACN,mDAAmD;QACnD,QAAQ,EAAE,GAAW,EAAE,CAAC,cAAc;QACtC,0CAA0C;QAC1C,aAAa,EAAE,CAAC,OAAe,EAAU,EAAE,CACzC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAE,CAAC,cAAc;QACvC,yEAAyE;QACzE,WAAW,EAAE,CAAC,OAAe,EAAkB,EAAE;YAC/C,MAAM,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAE,CAAC;YACnC,OAAO,GAAG,EAAE,CAAC,GAAG,CAAC,cAAc,CAAC;QAClC,CAAC;QACD,0CAA0C;QAC1C,MAAM,EAAE,GAAkB,EAAE,CAAC,CAAC;YAC5B,cAAc;YACd,aAAa;YACb,aAAa;YACb,aAAa;YACb,YAAY;SACb,CAAC;QACF,6CAA6C;QAC7C,WAAW,EAAE,CAAC,OAAe,EAAsB,EAAE,CAAC,CAAC;YACrD,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAE;SAC1B,CAAC;KACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { assertNonNegativeInteger, assertOptionalNonNegativeInteger, } from "../validation.js";
|
|
2
|
+
export function assertReconciliation(reconciliation, parts) {
|
|
3
|
+
if (reconciliation === undefined)
|
|
4
|
+
return;
|
|
5
|
+
assertNonNegativeInteger(reconciliation.remainingOutputTokens, "progressive reconciliation remainingOutputTokens");
|
|
6
|
+
assertOptionalNonNegativeInteger(reconciliation.safetyMarginTokens, "progressive reconciliation safetyMarginTokens");
|
|
7
|
+
if (parts !== null &&
|
|
8
|
+
reconciliation.remainingOutputTokens > parts.maxOutput) {
|
|
9
|
+
throw new RangeError("progressive reconciliation remainingOutputTokens must not exceed the reserved output cap");
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Clamp to monotonic non-decreasing per field — cumulative stream usage
|
|
14
|
+
* never shrinks; a lower report is stale.
|
|
15
|
+
*/
|
|
16
|
+
export function mergeUsage(previous, valid) {
|
|
17
|
+
return previous
|
|
18
|
+
? {
|
|
19
|
+
input: Math.max(previous.input, valid.input),
|
|
20
|
+
output: Math.max(previous.output, valid.output),
|
|
21
|
+
}
|
|
22
|
+
: valid;
|
|
23
|
+
}
|
|
24
|
+
/** Did the report just merged into the cell move either field? */
|
|
25
|
+
export function usageChanged(cell, previous) {
|
|
26
|
+
return (previous === undefined ||
|
|
27
|
+
cell.reported.input !== previous.input ||
|
|
28
|
+
cell.reported.output !== previous.output);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Tokens a call should hold after a usage report.
|
|
32
|
+
*
|
|
33
|
+
* Legacy mode holds known input plus the full output reservation.
|
|
34
|
+
* Progressive mode is opt-in and may be used only after prefill is
|
|
35
|
+
* known complete: it holds the caller-declared future output plus an
|
|
36
|
+
* explicit safety margin. Output beyond the configured cap is added
|
|
37
|
+
* back as overrun protection.
|
|
38
|
+
*/
|
|
39
|
+
export function holdAfterReport(parts, cell, reconciliation) {
|
|
40
|
+
const outputOverrun = Math.max(0, cell.reported.output - parts.maxOutput);
|
|
41
|
+
return reconciliation === undefined
|
|
42
|
+
? cell.reported.input + Math.max(parts.maxOutput, cell.reported.output)
|
|
43
|
+
: reconciliation.remainingOutputTokens +
|
|
44
|
+
(reconciliation.safetyMarginTokens ?? 0) +
|
|
45
|
+
outputOverrun;
|
|
46
|
+
}
|
|
47
|
+
/** Event fields that mark a report as a progressive reconciliation. */
|
|
48
|
+
export function progressiveFields(reconciliation) {
|
|
49
|
+
return reconciliation === undefined
|
|
50
|
+
? {}
|
|
51
|
+
: {
|
|
52
|
+
progressive: true,
|
|
53
|
+
remainingOutputTokens: reconciliation.remainingOutputTokens,
|
|
54
|
+
safetyMarginTokens: reconciliation.safetyMarginTokens ?? 0,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** Cumulative tokens a call has reported so far. */
|
|
58
|
+
export function consumedTokens(cell) {
|
|
59
|
+
return cell.reported ? cell.reported.input + cell.reported.output : 0;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Build the `UsageReport` for one call.
|
|
63
|
+
*
|
|
64
|
+
* The caller works out `consumed` (with `consumedTokens`) and `outputCap`
|
|
65
|
+
* first, and only then reads the call's `sequence` and `held`.
|
|
66
|
+
*/
|
|
67
|
+
export function usageSnapshot(call) {
|
|
68
|
+
const { parts, cell, consumed, outputCap } = call;
|
|
69
|
+
return {
|
|
70
|
+
admissionId: call.admissionId,
|
|
71
|
+
limitRevision: call.limitRevision,
|
|
72
|
+
...(call.admissionClass !== undefined
|
|
73
|
+
? { admissionClass: call.admissionClass }
|
|
74
|
+
: {}),
|
|
75
|
+
sequence: call.sequence,
|
|
76
|
+
reserved: parts?.reserved ?? 0,
|
|
77
|
+
held: call.held,
|
|
78
|
+
consumed,
|
|
79
|
+
outputCap,
|
|
80
|
+
outputRemaining: outputCap === null
|
|
81
|
+
? null
|
|
82
|
+
: Math.max(0, outputCap - (cell.reported?.output ?? 0)),
|
|
83
|
+
overReservation: parts ? consumed > parts.reserved : false,
|
|
84
|
+
...(call.stopped === true ? { stopped: true } : {}),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=usage-report.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage-report.js","sourceRoot":"","sources":["../../../src/engine/usage-report.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,wBAAwB,EACxB,gCAAgC,GACjC,MAAM,kBAAkB,CAAC;AAa1B,MAAM,UAAU,oBAAoB,CAClC,cAA4D,EAC5D,KAA8B;IAE9B,IAAI,cAAc,KAAK,SAAS;QAAE,OAAO;IACzC,wBAAwB,CACtB,cAAc,CAAC,qBAAqB,EACpC,kDAAkD,CACnD,CAAC;IACF,gCAAgC,CAC9B,cAAc,CAAC,kBAAkB,EACjC,+CAA+C,CAChD,CAAC;IACF,IACE,KAAK,KAAK,IA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,CAAC,CAAC,KAAK;QAC1D,GAAG,CAAC,IAAI,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC7D,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function createWouldAdmit(deps) {
|
|
2
|
+
const { concurrency, capacity, buildRejectDetail } = deps;
|
|
3
|
+
return function wouldAdmitResolved(admission, includeDetail) {
|
|
4
|
+
const { priority, admissionClass, reserved } = admission;
|
|
5
|
+
const withDetail = (result, constraint) => {
|
|
6
|
+
if (includeDetail) {
|
|
7
|
+
result.detail = buildRejectDetail(reserved, priority, admissionClass, constraint);
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
const base = concurrency.stats();
|
|
12
|
+
if (base.closed) {
|
|
13
|
+
return withDetail({ admit: false, reason: "shutdown" });
|
|
14
|
+
}
|
|
15
|
+
const capacityFailure = capacity.check(reserved, priority, admissionClass);
|
|
16
|
+
if (capacityFailure !== undefined) {
|
|
17
|
+
return withDetail({ admit: false, reason: capacityFailure.reason }, capacityFailure.constraint);
|
|
18
|
+
}
|
|
19
|
+
if (base.inFlight < base.maxConcurrent) {
|
|
20
|
+
return withDetail({ admit: true });
|
|
21
|
+
}
|
|
22
|
+
if (base.maxConcurrent === 0) {
|
|
23
|
+
return withDetail({ admit: false, reason: "concurrency_limit" }, "global");
|
|
24
|
+
}
|
|
25
|
+
if (base.pending < base.maxQueue) {
|
|
26
|
+
return withDetail({ admit: true });
|
|
27
|
+
}
|
|
28
|
+
return withDetail({
|
|
29
|
+
admit: false,
|
|
30
|
+
reason: base.maxQueue > 0 ? "queue_limit" : "concurrency_limit",
|
|
31
|
+
}, "global");
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=would-admit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"would-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U,CACf,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,mBAAmB,EAAE,EAC7C,QAAQ,CACT,CAAC;QACJ,CAAC;QACD,IAAI,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;YACjC,OAAO,UAAU,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACrC,CAAC;QACD,OAAO,UAAU,CACf;YACE,KAAK,EAAE,KAAK;YACZ,MAAM,EAAE,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,mBAAmB;SAChE,EACD,QAAQ,CACT,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/esm/errors.js
CHANGED
|
@@ -29,4 +29,27 @@ export class LLMBorrowedConcurrencyDeadlineError extends Error {
|
|
|
29
29
|
this.name = "LLMBorrowedConcurrencyDeadlineError";
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Returned by `run()` when the engine stops a call it had admitted (v3.18).
|
|
34
|
+
* It is also the abort reason of the callback's signal.
|
|
35
|
+
*
|
|
36
|
+
* `cause` names the trigger: `"overrun"`, `"drain_deadline"` or `"limits"`.
|
|
37
|
+
* The local concurrency slot has been returned, but token accounting
|
|
38
|
+
* remains held until the underlying callback actually settles.
|
|
39
|
+
*/
|
|
40
|
+
export class LLMAdmissionStoppedError extends Error {
|
|
41
|
+
cause;
|
|
42
|
+
admissionId;
|
|
43
|
+
resources;
|
|
44
|
+
admissionClass;
|
|
45
|
+
code = "LLM_ADMISSION_STOPPED";
|
|
46
|
+
constructor(cause, admissionId, resources, admissionClass) {
|
|
47
|
+
super(`LLM admission stopped: ${cause}`);
|
|
48
|
+
this.cause = cause;
|
|
49
|
+
this.admissionId = admissionId;
|
|
50
|
+
this.resources = resources;
|
|
51
|
+
this.admissionClass = admissionClass;
|
|
52
|
+
this.name = "LLMAdmissionStoppedError";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
32
55
|
//# sourceMappingURL=errors.js.map
|
package/dist/esm/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAWA,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IAMtC;IALF,IAAI,GAAG,uBAAgC,CAAC;IACxC,MAAM,CAA8B;IAG7C,YACW,MAAuB,EAChC,MAAwB;QAExB,KAAK,CAAC,0BAA0B,MAAM,EAAE,CAAC,CAAC;QAHjC,WAAM,GAAN,MAAM,CAAiB;QAIhC,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;QACvC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,OAAO,mCAAoC,SAAQ,KAAK;IAIjD;IACA;IACA;IACA;IANF,IAAI,GAAG,mCAA4C,CAAC;IAE7D,YACW,UAAkB,EAClB,WAAmB,EACnB,SAAgC,EAChC,cAAuB;QAEhC,KAAK,CAAC,mDAAmD,UAAU,IAAI,CAAC,CAAC;QALhE,eAAU,GAAV,UAAU,CAAQ;QAClB,gBAAW,GAAX,WAAW,CAAQ;QACnB,cAAS,GAAT,SAAS,CAAuB;QAChC,mBAAc,GAAd,cAAc,CAAS;QAGhC,IAAI,CAAC,IAAI,GAAG,qCAAqC,CAAC;IACpD,CAAC;CACF;AAED;;;;;;;GAOG;AACH,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IAI7B;IACT;IACA;IACA;IANF,IAAI,GAAG,uBAAgC,CAAC;IAEjD,YACoB,KAA4B,EACrC,WAAmB,EACnB,SAAgC,EAChC,cAAuB;QAEhC,KAAK,CAAC,0BAA0B,KAAK,EAAE,CAAC,CAAC;QALvB,UAAK,GAAL,KAAK,CAAuB;QACrC,gBAAW,GAAX,WAAW,CAAQ;QACnB,cAAS,GAAT,SAAS,CAAuB;QAChC,mBAAc,GAAd,cAAc,CAAS;QAGhC,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF"}
|
package/dist/esm/index.js
CHANGED
|
@@ -5,18 +5,28 @@
|
|
|
5
5
|
* implementation lives in focused modules:
|
|
6
6
|
*
|
|
7
7
|
* - `types.ts` — request/result/options/stats/event types
|
|
8
|
-
* - `errors.ts` — `LLMBulkheadRejectedError`
|
|
8
|
+
* - `errors.ts` — `LLMBulkheadRejectedError` and the two errors for
|
|
9
|
+
* admitted work that ended early
|
|
9
10
|
* - `profiles.ts` — `PROFILES` presets
|
|
10
11
|
* - `estimators.ts` — naive + model-aware estimators, `extractTextLength`
|
|
11
12
|
* - `adaptive.ts` — `createAdaptiveTokenEstimator` (v3.8)
|
|
12
13
|
* - `dedup.ts` — deduplication internals (keying, share safety)
|
|
13
14
|
* - `validation.ts` — internal numeric/estimate/usage guards
|
|
14
|
-
* - `bulkhead.ts` — `createLLMBulkhead
|
|
15
|
+
* - `bulkhead.ts` — `createLLMBulkhead`: validates options and wires the
|
|
16
|
+
* engine parts together
|
|
17
|
+
* - `reconfigurable-bulkhead.ts` — internal concurrency gate (global slot
|
|
18
|
+
* count and queue)
|
|
19
|
+
* - `engine/` — the parts of the admission engine, one concern per
|
|
20
|
+
* file (limits, token ledger, class slots, admission
|
|
21
|
+
* counts, capacity checks, the admitted token, `run()`,
|
|
22
|
+
* observe mode, deduplication, stats, shutdown, the
|
|
23
|
+
* running calls and the stop triggers); the full list
|
|
24
|
+
* is in the header of `bulkhead.ts`
|
|
15
25
|
*
|
|
16
26
|
* Deep-importing the internal modules is not supported; the package
|
|
17
27
|
* `exports` map exposes only this entry point.
|
|
18
28
|
*/
|
|
19
|
-
export { LLMBorrowedConcurrencyDeadlineError, LLMBulkheadRejectedError, } from "./errors.js";
|
|
29
|
+
export { LLMAdmissionStoppedError, LLMBorrowedConcurrencyDeadlineError, LLMBulkheadRejectedError, } from "./errors.js";
|
|
20
30
|
export { PROFILES } from "./profiles.js";
|
|
21
31
|
export { createModelAwareTokenEstimator, extractTextLength, naiveTokenEstimator, } from "./estimators.js";
|
|
22
32
|
export { createAdaptiveTokenEstimator } from "./adaptive.js";
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAkDH,OAAO,EACL,wBAAwB,EACxB,mCAAmC,EACnC,wBAAwB,GACzB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGzC,OAAO,EACL,8BAA8B,EAC9B,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AAMzB,OAAO,EAAE,4BAA4B,EAAE,MAAM,eAAe,CAAC;AAO7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/types/bulkhead.d.ts
CHANGED
|
@@ -1,11 +1,43 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `createLLMBulkhead`:
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* `createLLMBulkhead`: the entry point of the admission engine.
|
|
3
|
+
*
|
|
4
|
+
* This file validates the options, builds the engine's parts, wires them
|
|
5
|
+
* together and returns the public API. The parts live in `src/engine`,
|
|
6
|
+
* one concern per file.
|
|
7
|
+
*
|
|
8
|
+
* Owners. Each limit or counter is written in exactly one of these files,
|
|
9
|
+
* and every other part reads or changes it through that owner. An
|
|
10
|
+
* admission class is always passed around as its id:
|
|
11
|
+
*
|
|
12
|
+
* - `limits.ts` — revision, token budget, high-priority reserve,
|
|
13
|
+
* the class limits and the stop policy
|
|
14
|
+
* - `token-ledger.ts` — token counters, for the pool and per class
|
|
15
|
+
* - `class-slots.ts` — slot counts per class (the global slot count
|
|
16
|
+
* stays in `reconfigurable-bulkhead.ts`)
|
|
17
|
+
* - `admission-counts.ts` — admitted, released, rejected, abandoned and
|
|
18
|
+
* stopped counts, global and per class
|
|
19
|
+
* - `running-calls.ts` — the admitted calls still running, and the
|
|
20
|
+
* order in which they would be stopped
|
|
21
|
+
* - `observe-stats.ts` — observe-mode counters
|
|
22
|
+
* - `dedup-flow.ts` — the in-flight deduplication map and hit count
|
|
23
|
+
*
|
|
24
|
+
* Decisions and flows:
|
|
25
|
+
*
|
|
26
|
+
* - `options.ts`, `class-limits.ts` — option validation
|
|
27
|
+
* - `admission-request.ts`, `reservation.ts` — what one call asks for
|
|
28
|
+
* - `shared-usage.ts`, `capacity.ts` — does it fit; reserve it
|
|
29
|
+
* - `reject-detail.ts`, `would-admit.ts` — capacity snapshots and the
|
|
30
|
+
* advisory dry run
|
|
31
|
+
* - `acquire.ts` — the admission sequence
|
|
32
|
+
* - `admitted-token.ts`, `usage-report.ts` — the admitted token and its
|
|
33
|
+
* usage reports
|
|
34
|
+
* - `run.ts`, `run-options.ts`, `admitted-run.ts`, `bypass.ts` — `run()`
|
|
35
|
+
* - `stop.ts`, `limit-fit.ts`, `stop-policy.ts` — when admitted work is
|
|
36
|
+
* stopped, and which calls
|
|
37
|
+
* - `stats.ts`, `events.ts`, `shutdown.ts`
|
|
5
38
|
*/
|
|
6
|
-
import type { AcquireOptions } from "async-bulkhead-ts";
|
|
7
39
|
import type { Listener, LLMAdmissionLimits, LLMApplyLimitsResult, LLMAcquireOptions, LLMAcquireResult, LLMBulkheadOptions, LLMDrainResult, LLMEventType, LLMPriority, LLMRequest, LLMReservationEstimate, LLMReservationOverride, LLMRunContext, LLMRunOptions, LLMStats, LLMWouldAdmitResult } from "./types.js";
|
|
8
|
-
type BulkheadSignal
|
|
40
|
+
import type { BulkheadSignal } from "./engine/shared.js";
|
|
9
41
|
export declare function createLLMBulkhead(opts: LLMBulkheadOptions): {
|
|
10
42
|
estimate: (request: LLMRequest) => LLMReservationEstimate | null;
|
|
11
43
|
acquire: (request: LLMRequest, ao?: LLMAcquireOptions) => Promise<LLMAcquireResult>;
|
|
@@ -25,10 +57,10 @@ export declare function createLLMBulkhead(opts: LLMBulkheadOptions): {
|
|
|
25
57
|
(): Promise<void>;
|
|
26
58
|
(opts: {
|
|
27
59
|
timeoutMs: number;
|
|
60
|
+
stopOnTimeout?: boolean;
|
|
28
61
|
}): Promise<LLMDrainResult>;
|
|
29
62
|
};
|
|
30
63
|
on: <K extends LLMEventType>(event: K, listener: Listener<K>) => () => void;
|
|
31
64
|
};
|
|
32
65
|
export type LLMBulkhead = ReturnType<typeof createLLMBulkhead>;
|
|
33
|
-
export {};
|
|
34
66
|
//# sourceMappingURL=bulkhead.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bulkhead.d.ts","sourceRoot":"","sources":["../../src/bulkhead.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"bulkhead.d.ts","sourceRoot":"","sources":["../../src/bulkhead.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,KAAK,EACV,QAAQ,EACR,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,YAAY,EACZ,WAAW,EACX,UAAU,EACV,sBAAsB,EACtB,sBAAsB,EACtB,aAAa,EACb,aAAa,EACb,QAAQ,EACR,mBAAmB,EACpB,MAAM,YAAY,CAAC;AAkBpB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAYzD,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,kBAAkB;wBA4F7B,UAAU,KAAG,sBAAsB,GAAG,IAAI;uBAW1D,UAAU,OACf,iBAAiB,KACpB,OAAO,CAAC,gBAAgB,CAAC;UAoBhB,CAAC,WACF,UAAU,MACf,CAAC,MAAM,CAAC,EAAE,cAAc,EAAE,GAAG,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,OAC3D,aAAa,CAAC,CAAC,CAAC,KAClB,OAAO,CAAC,CAAC,CAAC;0BAmCJ,UAAU,SACb;QACJ,QAAQ,CAAC,EAAE,WAAW,CAAC;QACvB,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,WAAW,CAAC,EAAE,sBAAsB,CAAC;QACrC,MAAM,CAAC,EAAE,OAAO,CAAC;KAClB,KACA,mBAAmB;iBAKJ,QAAQ;kBAKP,kBAAkB;wBAoBV,kBAAkB,KAAG,oBAAoB;wBAczC,MAAM,KAAG,IAAI;iBAUtB,IAAI;;YAuCJ,OAAO,CAAC,IAAI,CAAC;eACV;YACnB,SAAS,EAAE,MAAM,CAAC;YAClB,aAAa,CAAC,EAAE,OAAO,CAAC;SACzB,GAAG,OAAO,CAAC,cAAc,CAAC;;SAef,CAAC,SAAS,YAAY,SACzB,CAAC,YACE,QAAQ,CAAC,CAAC,CAAC,KACpB,MAAM,IAAI;EAiBd;AAGD,MAAM,MAAM,WAAW,GAAG,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { LLMAcquireOptions, LLMRequest, LLMShadowableRejectReason } from "../types.js";
|
|
2
|
+
import type { AdmissionCounts } from "./admission-counts.js";
|
|
3
|
+
import type { ResolveAdmission } from "./admission-request.js";
|
|
4
|
+
import type { WrapToken } from "./admitted-token.js";
|
|
5
|
+
import type { Capacity } from "./capacity.js";
|
|
6
|
+
import type { EventHub } from "./events.js";
|
|
7
|
+
import type { Limits } from "./limits.js";
|
|
8
|
+
import type { BuildRejectDetail } from "./reject-detail.js";
|
|
9
|
+
import type { ConcurrencyGate, InternalAcquireResult, ResolvedAdmission } from "./shared.js";
|
|
10
|
+
type Deps = {
|
|
11
|
+
concurrency: ConcurrencyGate;
|
|
12
|
+
limits: Limits;
|
|
13
|
+
capacity: Capacity;
|
|
14
|
+
counts: AdmissionCounts;
|
|
15
|
+
events: EventHub;
|
|
16
|
+
buildRejectDetail: BuildRejectDetail;
|
|
17
|
+
resolveAdmission: ResolveAdmission;
|
|
18
|
+
wrapToken: WrapToken;
|
|
19
|
+
/** Bulkhead-level queue-wait timeout, used when a call passes none. */
|
|
20
|
+
timeoutMs: number | undefined;
|
|
21
|
+
};
|
|
22
|
+
export declare function createAcquire(deps: Deps): (request: LLMRequest, ao: LLMAcquireOptions, resolved?: ResolvedAdmission, untimedRejectReasons?: ReadonlySet<LLMShadowableRejectReason>) => Promise<InternalAcquireResult>;
|
|
23
|
+
export type Acquire = ReturnType<typeof createAcquire>;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=acquire.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"acquire.d.ts","sourceRoot":"","sources":["../../../src/engine/acquire.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EACV,iBAAiB,EAKjB,UAAU,EACV,yBAAyB,EAC1B,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,KAAK,EACV,eAAe,EACf,qBAAqB,EACrB,iBAAiB,EAClB,MAAM,aAAa,CAAC;AAErB,KAAK,IAAI,GAAG;IACV,WAAW,EAAE,eAAe,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,QAAQ,CAAC;IACnB,MAAM,EAAE,eAAe,CAAC;IACxB,MAAM,EAAE,QAAQ,CAAC;IACjB,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,SAAS,EAAE,SAAS,CAAC;IACrB,uEAAuE;IACvE,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/B,CAAC;AAiDF,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,IASpC,SAAS,UAAU,EACnB,IAAI,iBAAiB,EACrB,WAAU,iBAAsD,EAChE,uBAAuB,WAAW,CAAC,yBAAyB,CAAC,KAC5D,OAAO,CAAC,qBAAqB,CAAC,CAiHlC;AAED,MAAM,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Owner of the admission outcome counts: admitted, released, rejected,
|
|
3
|
+
* borrowed-concurrency abandoned, and stopped.
|
|
4
|
+
*
|
|
5
|
+
* Each count exists once for the whole bulkhead (the `llm`-prefixed
|
|
6
|
+
* variables) and once per admission class. Each function below moves the
|
|
7
|
+
* global count and the count of the class the call belongs to together.
|
|
8
|
+
* Nothing outside this file writes them; a class is always named by its
|
|
9
|
+
* id.
|
|
10
|
+
*/
|
|
11
|
+
import type { LLMAdmissionStopCause, LLMBorrowedConcurrencyAbandonCause, LLMRejectReason, LLMRequestStats } from "../types.js";
|
|
12
|
+
type ClassCounts = {
|
|
13
|
+
admitted: number;
|
|
14
|
+
released: number;
|
|
15
|
+
borrowedConcurrencyAbandoned: number;
|
|
16
|
+
rejected: number;
|
|
17
|
+
rejectedByReason: Partial<Record<LLMRejectReason, number>>;
|
|
18
|
+
stopped: number;
|
|
19
|
+
};
|
|
20
|
+
export declare function createAdmissionCounts(classIds: Iterable<string>): {
|
|
21
|
+
noteAdmit: (classId: string | undefined) => void;
|
|
22
|
+
noteRelease: (classId: string | undefined) => void;
|
|
23
|
+
noteReject: (classId: string | undefined, reason: LLMRejectReason) => void;
|
|
24
|
+
noteAbandon: (classId: string | undefined, cause: LLMBorrowedConcurrencyAbandonCause) => void;
|
|
25
|
+
noteStop: (classId: string | undefined, cause: LLMAdmissionStopCause) => void;
|
|
26
|
+
/** Has any call been stopped yet? */
|
|
27
|
+
anyStopped: () => boolean;
|
|
28
|
+
unsettled: () => number;
|
|
29
|
+
totals: () => LLMRequestStats;
|
|
30
|
+
classTotals: (classId: string) => ClassCounts;
|
|
31
|
+
};
|
|
32
|
+
export type AdmissionCounts = ReturnType<typeof createAdmissionCounts>;
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=admission-counts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"admission-counts.d.ts","sourceRoot":"","sources":["../../../src/engine/admission-counts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EACV,qBAAqB,EACrB,kCAAkC,EAClC,eAAe,EACf,eAAe,EAChB,MAAM,aAAa,CAAC;AAErB,KAAK,WAAW,GAAG;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,4BAA4B,EAAE,MAAM,CAAC;IACrC,QAAQ,EAAE,MAAM,CAAC;IACjB,gBAAgB,EAAE,OAAO,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC,CAAC;IAC3D,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC;yBA6BlC,MAAM,GAAG,SAAS,KAAG,IAAI;2BAOvB,MAAM,GAAG,SAAS,KAAG,IAAI;0BAQ5C,MAAM,GAAG,SAAS,UACnB,eAAe,KACtB,IAAI;2BAcI,MAAM,GAAG,SAAS,SACpB,kCAAkC,KACxC,IAAI;wBAUI,MAAM,GAAG,SAAS,SACpB,qBAAqB,KAC3B,IAAI;IA4CL,qCAAqC;sBACrB,OAAO;qBArCH,MAAM;kBAKT,eAAe;2BAoBJ,MAAM,KAAG,WAAW;EAiBnD;AAED,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolving what one call asks for: its priority, its admission class (as
|
|
3
|
+
* an id) and its token reservation. Invalid input throws here, before any
|
|
4
|
+
* capacity is looked at.
|
|
5
|
+
*/
|
|
6
|
+
import type { LLMAcquireOptions, LLMRequest } from "../types.js";
|
|
7
|
+
import type { Limits } from "./limits.js";
|
|
8
|
+
import type { Reservations } from "./reservation.js";
|
|
9
|
+
import type { ResolvedAdmission } from "./shared.js";
|
|
10
|
+
export declare function createAdmissionResolver(limits: Limits, reservations: Reservations): (request: LLMRequest, ao: Pick<LLMAcquireOptions, "priority" | "reservation" | "admissionClass">, resolvedClass?: string | undefined) => ResolvedAdmission;
|
|
11
|
+
export type ResolveAdmission = ReturnType<typeof createAdmissionResolver>;
|
|
12
|
+
//# sourceMappingURL=admission-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"admission-request.d.ts","sourceRoot":"","sources":["../../../src/engine/admission-request.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,iBAAiB,EAAe,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAUrD,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,YAAY,IAOxB,SAAS,UAAU,EACnB,IAAI,IAAI,CACN,iBAAiB,EACjB,UAAU,GAAG,aAAa,GAAG,gBAAgB,CAC9C,EACD,gBAAe,MAAM,GAAG,SAAkD,KACzE,iBAAiB,CAUrB;AAED,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,OAAO,uBAAuB,CAAC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Running a callback that was admitted: build its context, call it, and
|
|
3
|
+
* release its token with whatever usage is known. Also the two ways the
|
|
4
|
+
* caller can get its answer before the callback settles: the wall-clock
|
|
5
|
+
* deadline that takes back a borrowed concurrency slot, and a stop by the
|
|
6
|
+
* engine.
|
|
7
|
+
*/
|
|
8
|
+
import type { LLMRunContext, LLMRunOptions, TokenUsage } from "../types.js";
|
|
9
|
+
import { type BulkheadSignal, type InternalAcquireResult } from "./shared.js";
|
|
10
|
+
type Admitted = Extract<InternalAcquireResult, {
|
|
11
|
+
ok: true;
|
|
12
|
+
}>;
|
|
13
|
+
/**
|
|
14
|
+
* Start the callback of an admitted call and return the promise the
|
|
15
|
+
* caller should see. Not `async` on purpose: the callback starts
|
|
16
|
+
* synchronously, exactly where `run()` asks for it.
|
|
17
|
+
*
|
|
18
|
+
* `stoppable` is true when a stop policy was active at admission. Only
|
|
19
|
+
* then, or under a borrowed-slot deadline, does the callback get a signal
|
|
20
|
+
* the engine can abort. Otherwise it gets exactly what the caller passed,
|
|
21
|
+
* the same object or nothing, and a stop can only reject the caller.
|
|
22
|
+
*/
|
|
23
|
+
export declare function runAdmitted<T>(r: Admitted, fn: (signal?: BulkheadSignal, ctx?: LLMRunContext) => Promise<T>, ao: Pick<LLMRunOptions<T>, "signal">, getUsage: ((result: T) => TokenUsage | undefined) | undefined, borrowedConcurrencyDeadlineMs: number | undefined, stoppable: boolean): Promise<T>;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=admitted-run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"admitted-run.d.ts","sourceRoot":"","sources":["../../../src/engine/admitted-run.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAG5E,OAAO,EAGL,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC3B,MAAM,aAAa,CAAC;AAErB,KAAK,QAAQ,GAAG,OAAO,CAAC,qBAAqB,EAAE;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,CAAC,CAAC;AA6C7D;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC3B,CAAC,EAAE,QAAQ,EACX,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,cAAc,EAAE,GAAG,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,EAChE,EAAE,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,EACpC,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,UAAU,GAAG,SAAS,CAAC,GAAG,SAAS,EAC7D,6BAA6B,EAAE,MAAM,GAAG,SAAS,EACjD,SAAS,EAAE,OAAO,GACjB,OAAO,CAAC,CAAC,CAAC,CA0GZ"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The admitted token: what a caller holds between admission and final
|
|
3
|
+
* release. It reports streaming usage (which can grow or shrink the token
|
|
4
|
+
* hold), can hand a borrowed concurrency slot back early, can be stopped
|
|
5
|
+
* by the engine, and settles everything exactly once at release.
|
|
6
|
+
*
|
|
7
|
+
* This module keeps no pool or class counters. A token carries only its
|
|
8
|
+
* own call's progress; every shared number it moves goes through the
|
|
9
|
+
* owner of that number.
|
|
10
|
+
*/
|
|
11
|
+
import type { Token } from "async-bulkhead-ts";
|
|
12
|
+
import type { LLMAdmissionResources, LLMAdmissionStopCause, LLMPriority, LLMRequest } from "../types.js";
|
|
13
|
+
import type { AdmissionCounts } from "./admission-counts.js";
|
|
14
|
+
import type { ClassSlots } from "./class-slots.js";
|
|
15
|
+
import type { EventHub } from "./events.js";
|
|
16
|
+
import type { RunningCall, RunningCalls } from "./running-calls.js";
|
|
17
|
+
import { type ReservationParts, type WrappedLLMToken } from "./shared.js";
|
|
18
|
+
import type { TokenLedger } from "./token-ledger.js";
|
|
19
|
+
type Deps = {
|
|
20
|
+
budgetEnabled: boolean;
|
|
21
|
+
ledger: TokenLedger;
|
|
22
|
+
slots: ClassSlots;
|
|
23
|
+
counts: AdmissionCounts;
|
|
24
|
+
events: EventHub;
|
|
25
|
+
running: RunningCalls;
|
|
26
|
+
/** A usage report raised a running call's hold by `delta` tokens. */
|
|
27
|
+
holdGrew: (call: RunningCall, delta: number) => void;
|
|
28
|
+
/** Wake `drain()` callers once the last admitted call has settled. */
|
|
29
|
+
settleDrainWaiters: () => void;
|
|
30
|
+
};
|
|
31
|
+
/** Everything fixed at the moment a call was admitted. */
|
|
32
|
+
export type AdmittedCall = {
|
|
33
|
+
/** The concurrency gate's own token for the slot this call holds. */
|
|
34
|
+
readonly base: Token;
|
|
35
|
+
readonly parts: ReservationParts | null;
|
|
36
|
+
readonly request: LLMRequest;
|
|
37
|
+
readonly admissionId: string;
|
|
38
|
+
readonly limitRevision: number;
|
|
39
|
+
readonly priority: LLMPriority;
|
|
40
|
+
readonly admissionClass: string | undefined;
|
|
41
|
+
readonly resources: LLMAdmissionResources;
|
|
42
|
+
};
|
|
43
|
+
export declare function createTokenWrapper(deps: Deps): {
|
|
44
|
+
wrap: (call: AdmittedCall) => WrappedLLMToken;
|
|
45
|
+
stop: (running: RunningCall, cause: LLMAdmissionStopCause) => boolean;
|
|
46
|
+
};
|
|
47
|
+
export type WrapToken = ReturnType<typeof createTokenWrapper>["wrap"];
|
|
48
|
+
export {};
|
|
49
|
+
//# sourceMappingURL=admitted-token.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adm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AQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC;IAC/B,QAAQ,CAAC,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5C,QAAQ,CAAC,SAAS,EAAE,qBAAqB,CAAC;CAC3C,CAAC;AA0RF,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,IAAI;iBAMvB,YAAY,KAAG,eAAe;oBAgE3B,WAAW,SAAS,qBAAqB,KAAG,OAAO;EAM3E;AAED,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { LLMRejectDetail, LLMRequest, LLMRunContext, LLMRunOptions, LLMShadowableRejectReason } from "../types.js";
|
|
2
|
+
import type { AdmissionCounts } from "./admission-counts.js";
|
|
3
|
+
import type { EventHub } from "./events.js";
|
|
4
|
+
import type { ObserveStats } from "./observe-stats.js";
|
|
5
|
+
import type { BuildRejectDetail } from "./reject-detail.js";
|
|
6
|
+
import { type BulkheadSignal, type ConcurrencyGate, type ResolvedAdmission } from "./shared.js";
|
|
7
|
+
type Deps = {
|
|
8
|
+
concurrency: ConcurrencyGate;
|
|
9
|
+
counts: AdmissionCounts;
|
|
10
|
+
events: EventHub;
|
|
11
|
+
observeStats: ObserveStats;
|
|
12
|
+
buildRejectDetail: BuildRejectDetail;
|
|
13
|
+
};
|
|
14
|
+
export declare function createBypass(deps: Deps): <T>(request: LLMRequest, fn: (signal?: BulkheadSignal, ctx?: LLMRunContext) => Promise<T>, opts: Pick<LLMRunOptions<T>, "signal" | "getUsage">, admission: ResolvedAdmission, reason: LLMShadowableRejectReason, detail: LLMRejectDetail | undefined, limitRevision: number, raced: boolean) => Promise<T>;
|
|
15
|
+
export type RunBypassed = ReturnType<typeof createBypass>;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=bypass.d.ts.map
|
|
@@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**
|
|
2
|
+
* The capacity decision: would this call fit, and if so, reserve it.
|
|
3
|
+
*
|
|
4
|
+
* `check` reads every non-queue capacity layer in a fixed order and names
|
|
5
|
+
* the first limit that is broken. That order is what decides the `reason`
|
|
6
|
+
* and `constraint` a caller sees when a request breaks several limits at
|
|
7
|
+
* once, so it must not be rearranged:
|
|
8
|
+
*
|
|
9
|
+
* 1. global token budget
|
|
10
|
+
* 2. class token ceiling
|
|
11
|
+
* 3. class concurrency ceiling
|
|
12
|
+
* 4. shared concurrency left after every class floor
|
|
13
|
+
* 5. shared tokens left after every class floor
|
|
14
|
+
*
|
|
15
|
+
* `tryReserve` is all or nothing: it runs the same checks and only then
|
|
16
|
+
* asks the owners to move their counters.
|
|
17
|
+
*/
|
|
18
|
+
import type { LLMPriority } from "../types.js";
|
|
19
|
+
import type { ClassSlots } from "./class-slots.js";
|
|
20
|
+
import type { Limits } from "./limits.js";
|
|
21
|
+
import type { CapacityFailure, CapacityReservationResult, ConcurrencyGate } from "./shared.js";
|
|
22
|
+
import type { SharedUsage } from "./shared-usage.js";
|
|
23
|
+
import type { TokenLedger } from "./token-ledger.js";
|
|
24
|
+
export declare function createCapacity(deps: {
|
|
25
|
+
budgetEnabled: boolean;
|
|
26
|
+
concurrency: ConcurrencyGate;
|
|
27
|
+
limits: Limits;
|
|
28
|
+
ledger: TokenLedger;
|
|
29
|
+
slots: ClassSlots;
|
|
30
|
+
usage: SharedUsage;
|
|
31
|
+
}): {
|
|
32
|
+
check: (reserved: number, priority: LLMPriority, classId: string | undefined, globalSlotHeld?: boolean) => CapacityFailure | undefined;
|
|
33
|
+
tryReserve: (reserved: number, priority: LLMPriority, classId: string | undefined) => CapacityReservationResult;
|
|
34
|
+
};
|
|
35
|
+
export type Capacity = ReturnType<typeof createCapacity>;
|
|
36
|
+
//# sourceMappingURL=capacity.d.ts.map
|