@sema-ai/registry-core 0.10.3

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 (103) hide show
  1. package/CHANGELOG.md +131 -0
  2. package/README.md +84 -0
  3. package/dist/api/auth-bridge.d.ts +332 -0
  4. package/dist/api/auth-bridge.d.ts.map +1 -0
  5. package/dist/api/auth-bridge.js +211 -0
  6. package/dist/api/auth-bridge.js.map +1 -0
  7. package/dist/api/auth.d.ts +258 -0
  8. package/dist/api/auth.d.ts.map +1 -0
  9. package/dist/api/auth.js +204 -0
  10. package/dist/api/auth.js.map +1 -0
  11. package/dist/api/scopes.d.ts +353 -0
  12. package/dist/api/scopes.d.ts.map +1 -0
  13. package/dist/api/scopes.js +229 -0
  14. package/dist/api/scopes.js.map +1 -0
  15. package/dist/bundle.d.ts +14 -0
  16. package/dist/bundle.d.ts.map +1 -0
  17. package/dist/bundle.js +68 -0
  18. package/dist/bundle.js.map +1 -0
  19. package/dist/config-fns.d.ts +236 -0
  20. package/dist/config-fns.d.ts.map +1 -0
  21. package/dist/config-fns.js +272 -0
  22. package/dist/config-fns.js.map +1 -0
  23. package/dist/cross-domain.d.ts +35 -0
  24. package/dist/cross-domain.d.ts.map +1 -0
  25. package/dist/cross-domain.js +119 -0
  26. package/dist/cross-domain.js.map +1 -0
  27. package/dist/file-edit.d.ts +37 -0
  28. package/dist/file-edit.d.ts.map +1 -0
  29. package/dist/file-edit.js +126 -0
  30. package/dist/file-edit.js.map +1 -0
  31. package/dist/file-store.d.ts +39 -0
  32. package/dist/file-store.d.ts.map +1 -0
  33. package/dist/file-store.js +142 -0
  34. package/dist/file-store.js.map +1 -0
  35. package/dist/fleet.d.ts +479 -0
  36. package/dist/fleet.d.ts.map +1 -0
  37. package/dist/fleet.js +303 -0
  38. package/dist/fleet.js.map +1 -0
  39. package/dist/hash.d.ts +33 -0
  40. package/dist/hash.d.ts.map +1 -0
  41. package/dist/hash.js +60 -0
  42. package/dist/hash.js.map +1 -0
  43. package/dist/hooks.d.ts +5478 -0
  44. package/dist/hooks.d.ts.map +1 -0
  45. package/dist/hooks.js +628 -0
  46. package/dist/hooks.js.map +1 -0
  47. package/dist/index.d.ts +23 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +27 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/local-load.d.ts +33 -0
  52. package/dist/local-load.d.ts.map +1 -0
  53. package/dist/local-load.js +164 -0
  54. package/dist/local-load.js.map +1 -0
  55. package/dist/migrate.d.ts +245 -0
  56. package/dist/migrate.d.ts.map +1 -0
  57. package/dist/migrate.js +543 -0
  58. package/dist/migrate.js.map +1 -0
  59. package/dist/node.d.ts +12 -0
  60. package/dist/node.d.ts.map +1 -0
  61. package/dist/node.js +12 -0
  62. package/dist/node.js.map +1 -0
  63. package/dist/reader.d.ts +19 -0
  64. package/dist/reader.d.ts.map +1 -0
  65. package/dist/reader.js +2 -0
  66. package/dist/reader.js.map +1 -0
  67. package/dist/remote-exec.d.ts +275 -0
  68. package/dist/remote-exec.d.ts.map +1 -0
  69. package/dist/remote-exec.js +183 -0
  70. package/dist/remote-exec.js.map +1 -0
  71. package/dist/resolve-roster.d.ts +29 -0
  72. package/dist/resolve-roster.d.ts.map +1 -0
  73. package/dist/resolve-roster.js +105 -0
  74. package/dist/resolve-roster.js.map +1 -0
  75. package/dist/safety-merge-spec.d.ts +328 -0
  76. package/dist/safety-merge-spec.d.ts.map +1 -0
  77. package/dist/safety-merge-spec.js +65 -0
  78. package/dist/safety-merge-spec.js.map +1 -0
  79. package/dist/scheduler-store-node.d.ts +15 -0
  80. package/dist/scheduler-store-node.d.ts.map +1 -0
  81. package/dist/scheduler-store-node.js +43 -0
  82. package/dist/scheduler-store-node.js.map +1 -0
  83. package/dist/scheduler-store.d.ts +65 -0
  84. package/dist/scheduler-store.d.ts.map +1 -0
  85. package/dist/scheduler-store.js +59 -0
  86. package/dist/scheduler-store.js.map +1 -0
  87. package/dist/secret-refs.d.ts +34 -0
  88. package/dist/secret-refs.d.ts.map +1 -0
  89. package/dist/secret-refs.js +49 -0
  90. package/dist/secret-refs.js.map +1 -0
  91. package/dist/sha256.d.ts +17 -0
  92. package/dist/sha256.d.ts.map +1 -0
  93. package/dist/sha256.js +115 -0
  94. package/dist/sha256.js.map +1 -0
  95. package/dist/skills-manifest.d.ts +13 -0
  96. package/dist/skills-manifest.d.ts.map +1 -0
  97. package/dist/skills-manifest.js +55 -0
  98. package/dist/skills-manifest.js.map +1 -0
  99. package/dist/types.d.ts +9911 -0
  100. package/dist/types.d.ts.map +1 -0
  101. package/dist/types.js +1727 -0
  102. package/dist/types.js.map +1 -0
  103. package/package.json +130 -0
@@ -0,0 +1,479 @@
1
+ /**
2
+ * @sema-ai/registry-core/fleet — the PURE scheduling/fleet contract for the sema-center control plane
3
+ * (design: cc-parity CONTROL-PLANE-SCHEDULING-DESIGN.md, clay 拍 2026-07-04 — "one contract, three orchestrators").
4
+ *
5
+ * Seven domains (rolling update / discovery / traffic / failover / LB / quota / token governance) are ONE
6
+ * data-plane lifecycle contract reused by three deployment forms — whoever HOLDS the engine handle orchestrates,
7
+ * but all three read THESE shapes:
8
+ * - TOC shell (engineLifecycleManager): health readiness + upgrade classification + local blue-green demo
9
+ * - TOB (user k8s / fixed baseurl): preStop→drain semantics + readiness probe = !draining
10
+ * - center (orchestrator reconciler): instance discovery table + /api/dispatch gateway + rolling primitive
11
+ *
12
+ * 🔴 Contract discipline (same rules as scheduler-store):
13
+ * - ABSTRACT: types + zod wire shapes + PURE decision functions. ZERO IO, ZERO node:* — browser-safe barrel.
14
+ * - FINAL-STATE: shapes codify what is SHIPPED on the wire (server 1.96.0 drain: /health `version`/`draining`/
15
+ * `drainingSince`, billable submit 503+Retry-After) — no transitional aliases, breaking changes over shims
16
+ * (clay 拍: no temporary compatibility design; the target is long-term stability).
17
+ * - EXTENSIBLE: enums + optional fields externalize decisions (add an LB strategy = add an enum value,
18
+ * the skeleton does not move).
19
+ */
20
+ import { z } from "zod";
21
+ /** Lifecycle of a draining instance. `running`→`draining` on the stop signal; `drained` once in-flight hits
22
+ * zero (clean) or the grace deadline forces the issue. Exit always follows `drained`. */
23
+ export declare const DRAIN_PHASES: readonly ["running", "draining", "drained"];
24
+ export type DrainPhase = (typeof DRAIN_PHASES)[number];
25
+ /** Shipped default (server 1.96.0): generous by design — the only unrecoverable loss on restart is the
26
+ * interactive turn currently streaming, so we wait long rather than cut short. Deployments override via
27
+ * DRAIN_GRACE_MS; center overrides per-worker through the worker spec. */
28
+ export declare const DEFAULT_DRAIN_GRACE_MS = 600000;
29
+ /** Hard floor — a grace below this is treated as this (protects against a fat-fingered 0). */
30
+ export declare const DRAIN_GRACE_FLOOR_MS = 10000;
31
+ /** `Retry-After` (seconds) on the 503 a draining instance returns for NEW billable submissions. Read paths,
32
+ * steer/cancel/respond stay open — only new work is refused. */
33
+ export declare const DRAIN_RETRY_AFTER_SECONDS = 15;
34
+ /** What a draining decision needs to know — all times are epoch ms supplied by the caller (pure fn, no clock). */
35
+ export interface DrainSnapshot {
36
+ /** when the stop signal flipped `draining` on */
37
+ startedMs: number;
38
+ nowMs: number;
39
+ /** configured grace (DRAIN_GRACE_MS / worker-spec override); floored at DRAIN_GRACE_FLOOR_MS */
40
+ graceMs: number;
41
+ /** in-flight billable legs (inflight runs ∪ steerable runs) */
42
+ inflight: number;
43
+ }
44
+ export type DrainVerdict = "wait" | "exit_clean" | "exit_forced";
45
+ /** The drain state machine's single decision point: keep waiting, exit clean (drained), or force the exit
46
+ * (grace exhausted with legs still in flight — durable legs resume cross-instance via resumeFromRunId). */
47
+ export declare function drainVerdict(s: DrainSnapshot): DrainVerdict;
48
+ /** The `/health` self-description every orchestrator reads (shipped shape, server 1.96.0). Liveness and
49
+ * readiness are deliberately SPLIT: `status` stays ok while draining (the process is alive); routability is
50
+ * `isReadyForTraffic`. Unknown fields pass through — health is a self-description, not a closed record. */
51
+ export declare const InstanceHealth: z.ZodObject<{
52
+ instanceId: z.ZodString;
53
+ /** build self-description — "did the restart actually land new code" (upgrade classification) */
54
+ version: z.ZodString;
55
+ /** absent = not draining */
56
+ draining: z.ZodOptional<z.ZodBoolean>;
57
+ /** epoch ms, present iff draining */
58
+ drainingSince: z.ZodOptional<z.ZodNumber>;
59
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
60
+ instanceId: z.ZodString;
61
+ /** build self-description — "did the restart actually land new code" (upgrade classification) */
62
+ version: z.ZodString;
63
+ /** absent = not draining */
64
+ draining: z.ZodOptional<z.ZodBoolean>;
65
+ /** epoch ms, present iff draining */
66
+ drainingSince: z.ZodOptional<z.ZodNumber>;
67
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
68
+ instanceId: z.ZodString;
69
+ /** build self-description — "did the restart actually land new code" (upgrade classification) */
70
+ version: z.ZodString;
71
+ /** absent = not draining */
72
+ draining: z.ZodOptional<z.ZodBoolean>;
73
+ /** epoch ms, present iff draining */
74
+ drainingSince: z.ZodOptional<z.ZodNumber>;
75
+ }, z.ZodTypeAny, "passthrough">>;
76
+ export type InstanceHealth = z.infer<typeof InstanceHealth>;
77
+ /** Readiness = the routability predicate (k8s readiness probe, gateway health filter, shell seam switch). */
78
+ export declare function isReadyForTraffic(h: Pick<InstanceHealth, "draining">): boolean;
79
+ /** Heartbeat cadence the announcement declares (center judges staleness against the DECLARED cadence, so a
80
+ * slow-heartbeat worker is not falsely reaped). */
81
+ export declare const DEFAULT_HEARTBEAT_MS = 30000;
82
+ /** An instance is stale once silent for this many missed heartbeats (aligned with the reconciler's own
83
+ * stale window: 2× cadence, not a fixed wall-clock). */
84
+ export declare const STALE_HEARTBEAT_MULTIPLIER = 2;
85
+ /** Register/heartbeat upsert (worker → center, D2: worker-active). ONE shape for both — registration is the
86
+ * first heartbeat; every heartbeat re-announces the full row (idempotent upsert, no drift between a
87
+ * "register" and a "renew" schema). `draining:true` is the instance SELF-removing from the routable set the
88
+ * moment the stop signal lands — faster than any observer. */
89
+ export declare const InstanceAnnouncement: z.ZodObject<{
90
+ instanceId: z.ZodString;
91
+ /** worker (deployment) this instance belongs to — the dispatch key */
92
+ worker: z.ZodString;
93
+ /** reachable base URL for the deployment form: k8s pod IP:port / docker net alias / 127.0.0.1:<engine.port> */
94
+ address: z.ZodString;
95
+ version: z.ZodString;
96
+ /** self-declared: bootDone && !draining */
97
+ ready: z.ZodBoolean;
98
+ draining: z.ZodDefault<z.ZodBoolean>;
99
+ heartbeatMs: z.ZodDefault<z.ZodNumber>;
100
+ }, "strip", z.ZodTypeAny, {
101
+ draining: boolean;
102
+ instanceId: string;
103
+ version: string;
104
+ worker: string;
105
+ address: string;
106
+ ready: boolean;
107
+ heartbeatMs: number;
108
+ }, {
109
+ instanceId: string;
110
+ version: string;
111
+ worker: string;
112
+ address: string;
113
+ ready: boolean;
114
+ draining?: boolean | undefined;
115
+ heartbeatMs?: number | undefined;
116
+ }>;
117
+ export type InstanceAnnouncement = z.infer<typeof InstanceAnnouncement>;
118
+ /** Explicit goodbye (stop signal handler) — removes the row immediately instead of waiting out staleness. */
119
+ export declare const InstanceDeregistration: z.ZodObject<{
120
+ instanceId: z.ZodString;
121
+ worker: z.ZodString;
122
+ reason: z.ZodOptional<z.ZodEnum<["draining", "shutdown", "superseded"]>>;
123
+ }, "strip", z.ZodTypeAny, {
124
+ instanceId: string;
125
+ worker: string;
126
+ reason?: "draining" | "shutdown" | "superseded" | undefined;
127
+ }, {
128
+ instanceId: string;
129
+ worker: string;
130
+ reason?: "draining" | "shutdown" | "superseded" | undefined;
131
+ }>;
132
+ export type InstanceDeregistration = z.infer<typeof InstanceDeregistration>;
133
+ /** The stored discovery-table row = last announcement + center-side bookkeeping. `observedReady` is the
134
+ * reconciler's observation (phase Running) — the SECOND source; absent where no reconciler exists (TOC/TOB). */
135
+ export declare const InstanceRegistration: z.ZodObject<{
136
+ instanceId: z.ZodString;
137
+ /** worker (deployment) this instance belongs to — the dispatch key */
138
+ worker: z.ZodString;
139
+ /** reachable base URL for the deployment form: k8s pod IP:port / docker net alias / 127.0.0.1:<engine.port> */
140
+ address: z.ZodString;
141
+ version: z.ZodString;
142
+ /** self-declared: bootDone && !draining */
143
+ ready: z.ZodBoolean;
144
+ draining: z.ZodDefault<z.ZodBoolean>;
145
+ heartbeatMs: z.ZodDefault<z.ZodNumber>;
146
+ } & {
147
+ registeredMs: z.ZodNumber;
148
+ lastSeenMs: z.ZodNumber;
149
+ observedReady: z.ZodOptional<z.ZodBoolean>;
150
+ }, "strip", z.ZodTypeAny, {
151
+ draining: boolean;
152
+ instanceId: string;
153
+ version: string;
154
+ worker: string;
155
+ address: string;
156
+ ready: boolean;
157
+ heartbeatMs: number;
158
+ registeredMs: number;
159
+ lastSeenMs: number;
160
+ observedReady?: boolean | undefined;
161
+ }, {
162
+ instanceId: string;
163
+ version: string;
164
+ worker: string;
165
+ address: string;
166
+ ready: boolean;
167
+ registeredMs: number;
168
+ lastSeenMs: number;
169
+ draining?: boolean | undefined;
170
+ heartbeatMs?: number | undefined;
171
+ observedReady?: boolean | undefined;
172
+ }>;
173
+ export type InstanceRegistration = z.infer<typeof InstanceRegistration>;
174
+ /** Silent past the declared cadence × multiplier ⇒ stale (auto-removed from the routable set). */
175
+ export declare function isInstanceStale(r: Pick<InstanceRegistration, "lastSeenMs" | "heartbeatMs">, nowMs: number): boolean;
176
+ /** Health fusion: routable ⇔ self-declared ready ∧ not draining ∧ not stale ∧ observer does not veto.
177
+ * Two sources merge as an AND — EITHER source's "not ready" removes the instance (fail-closed). */
178
+ export declare function isRoutable(r: InstanceRegistration, nowMs: number): boolean;
179
+ export declare function routableInstances(rs: InstanceRegistration[], nowMs: number): InstanceRegistration[];
180
+ export declare const DISPATCH_STRATEGIES: readonly ["round-robin", "least-inflight", "consistent-hash"];
181
+ export type DispatchStrategy = (typeof DISPATCH_STRATEGIES)[number];
182
+ /** Per-worker dispatch policy (center-configured; the gateway is OPTIONAL — D3: TOC/self-maintained TOB go
183
+ * direct, sema-managed fleets route through /api/dispatch). Strategy is an enum on purpose: a new LB
184
+ * algorithm is a new value, not a new skeleton. */
185
+ export declare const DispatchPolicy: z.ZodObject<{
186
+ strategy: z.ZodDefault<z.ZodEnum<["round-robin", "least-inflight", "consistent-hash"]>>;
187
+ /** live-steer legs must return to their instance (cross-replica live-steer is 409 by design) — when true,
188
+ * a request carrying an affinity key pins via consistent hash regardless of `strategy`. If the pinned
189
+ * instance dies, the run resumes elsewhere via resumeFromRunId (affinity avoids the gap; resume bounds it). */
190
+ sessionAffinity: z.ZodDefault<z.ZodBoolean>;
191
+ /** gateway-level concurrency gate (per worker) */
192
+ maxInflight: z.ZodOptional<z.ZodNumber>;
193
+ /** per-actor fairness gate */
194
+ perActorMaxInflight: z.ZodOptional<z.ZodNumber>;
195
+ /** cost clamp applied to submissions passing the gateway */
196
+ maxCostUsd: z.ZodOptional<z.ZodNumber>;
197
+ }, "strip", z.ZodTypeAny, {
198
+ strategy: "round-robin" | "least-inflight" | "consistent-hash";
199
+ sessionAffinity: boolean;
200
+ maxInflight?: number | undefined;
201
+ perActorMaxInflight?: number | undefined;
202
+ maxCostUsd?: number | undefined;
203
+ }, {
204
+ strategy?: "round-robin" | "least-inflight" | "consistent-hash" | undefined;
205
+ sessionAffinity?: boolean | undefined;
206
+ maxInflight?: number | undefined;
207
+ perActorMaxInflight?: number | undefined;
208
+ maxCostUsd?: number | undefined;
209
+ }>;
210
+ export type DispatchPolicy = z.infer<typeof DispatchPolicy>;
211
+ /** 🔴 Submit idempotency — the precondition that makes failover re-routing SAFE: a new-run submission MUST
212
+ * carry the client-minted durable taskId, and the server dedupes on it. Without this, "landed but the
213
+ * response was lost" re-routes into a duplicate run. Re-route freely: reads (always) and submissions
214
+ * (iff keyed). In-flight interactive turns are NEVER auto-retried (tool side effects are not idempotent). */
215
+ export declare const SUBMIT_IDEMPOTENCY_KEY = "taskId";
216
+ export interface DispatchCandidate {
217
+ instanceId: string;
218
+ /** current in-flight count, for least-inflight (absent = 0) */
219
+ inflight?: number;
220
+ }
221
+ /**
222
+ * The single instance-selection decision, PURE and deterministic:
223
+ * - candidates are sorted by instanceId first ⇒ the pick is stable under input reordering;
224
+ * - `affinityKey` (+ sessionAffinity or consistent-hash strategy) ⇒ stable hash pick — same key, same
225
+ * instance, for as long as the candidate set holds (live-steer affinity);
226
+ * - round-robin consumes `cursor` (caller-held monotonic counter);
227
+ * - least-inflight breaks ties by lowest instanceId.
228
+ * Returns null on an empty candidate set (caller surfaces "no routable instance").
229
+ */
230
+ export declare function pickInstance(policy: DispatchPolicy, candidates: DispatchCandidate[], opts?: {
231
+ cursor?: number;
232
+ affinityKey?: string;
233
+ }): DispatchCandidate | null;
234
+ /** A pre-authorized slice of a principal's global budget, granted by center to ONE worker: the worker deducts
235
+ * locally (no per-call round-trip), renews when running low, and center reconciles reported consumption
236
+ * against the grant after the fact — the roaming/CDN pattern.
237
+ *
238
+ * 0.6.1 双轴 slice(EXPERT-REDESIGN §4-2/§4-6,黑板 [542] service 回执"发布即接单"):slice = **{weightedTokens,
239
+ * costMicroUsd}** 双轴,带 TTL 不变。token 轴单位=**加权 tokens**(配额轴,ModelEntry.quotaWeight 的
240
+ * weight-at-burn 折算——与 UsageEntry.weightedTokens 同一时点同口径,扣减与聚合两路径不歪)。判定语义在
241
+ * 类型层定死:**任一轴耗尽 = 拒绝**({@link leaseExhausted} deny-wins)、**任一轴达 80% = 提前续租**
242
+ * ({@link shouldRenewLease})。$ 轴名 `costMicroUsd` 沿用 0.5 既定名不改(service $ 单轴已 SHIPPED)。
243
+ * 0.7.1(BREAKING,黑板 [551] 回执收账):0.5 轴名 `tokens` 的 preprocess shim 已撤——service 1.150.0
244
+ * 已切 typed 双轴,legacy 别名成死分支;老形状租约 parse 后 tokens 键被 strip(= 该轴不受此租约治理)。 */
245
+ export declare const QuotaLease: z.ZodObject<{
246
+ leaseId: z.ZodString;
247
+ principal: z.ZodString;
248
+ worker: z.ZodString;
249
+ /** granted dimensions — either or both; a dimension absent = not governed by this lease.
250
+ * `weightedTokens` = 加权 tokens(配额轴)。 */
251
+ grant: z.ZodObject<{
252
+ weightedTokens: z.ZodOptional<z.ZodNumber>;
253
+ costMicroUsd: z.ZodOptional<z.ZodNumber>;
254
+ }, "strip", z.ZodTypeAny, {
255
+ weightedTokens?: number | undefined;
256
+ costMicroUsd?: number | undefined;
257
+ }, {
258
+ weightedTokens?: number | undefined;
259
+ costMicroUsd?: number | undefined;
260
+ }>;
261
+ consumed: z.ZodDefault<z.ZodObject<{
262
+ weightedTokens: z.ZodDefault<z.ZodNumber>;
263
+ costMicroUsd: z.ZodDefault<z.ZodNumber>;
264
+ }, "strip", z.ZodTypeAny, {
265
+ weightedTokens: number;
266
+ costMicroUsd: number;
267
+ }, {
268
+ weightedTokens?: number | undefined;
269
+ costMicroUsd?: number | undefined;
270
+ }>>;
271
+ issuedMs: z.ZodNumber;
272
+ expiresMs: z.ZodNumber;
273
+ }, "strip", z.ZodTypeAny, {
274
+ worker: string;
275
+ leaseId: string;
276
+ principal: string;
277
+ grant: {
278
+ weightedTokens?: number | undefined;
279
+ costMicroUsd?: number | undefined;
280
+ };
281
+ consumed: {
282
+ weightedTokens: number;
283
+ costMicroUsd: number;
284
+ };
285
+ issuedMs: number;
286
+ expiresMs: number;
287
+ }, {
288
+ worker: string;
289
+ leaseId: string;
290
+ principal: string;
291
+ grant: {
292
+ weightedTokens?: number | undefined;
293
+ costMicroUsd?: number | undefined;
294
+ };
295
+ issuedMs: number;
296
+ expiresMs: number;
297
+ consumed?: {
298
+ weightedTokens?: number | undefined;
299
+ costMicroUsd?: number | undefined;
300
+ } | undefined;
301
+ }>;
302
+ export type QuotaLease = z.infer<typeof QuotaLease>;
303
+ /** Remaining per granted dimension; null = the dimension is not governed by this lease. Never negative. */
304
+ export declare function leaseRemaining(l: QuotaLease): {
305
+ weightedTokens: number | null;
306
+ costMicroUsd: number | null;
307
+ };
308
+ /** Exhausted ⇔ ANY governed dimension is used up (deny-wins across the two axes — 任一轴耗尽即拒), or the
309
+ * lease expired. */
310
+ export declare function leaseExhausted(l: QuotaLease, nowMs: number): boolean;
311
+ /** Renew EARLY — at `renewAtFraction` of ANY governed dimension (default 80% — 任一轴达 80% 即提前续租) or
312
+ * of the lease lifetime — so the refresh round-trip overlaps remaining headroom instead of blocking a call
313
+ * at zero. */
314
+ export declare function shouldRenewLease(l: QuotaLease, nowMs: number, opts?: {
315
+ renewAtFraction?: number;
316
+ }): boolean;
317
+ /** Reconciliation (center side): consumption is MONOTONE — the reconciled row takes the max of what center
318
+ * believed and what the worker reported (a lost report can only under-count, never roll back).
319
+ * 0.7.1:legacy `tokens` report alias removed with the QuotaLease shim(同一收账点,[551] 回执). */
320
+ export declare function reconcileLease(l: QuotaLease, reported: {
321
+ weightedTokens?: number;
322
+ costMicroUsd?: number;
323
+ }): QuotaLease;
324
+ /** One principal×model line in a report window (mirrors the worker's modelUsageTracker accumulation). */
325
+ export declare const UsageEntry: z.ZodObject<{
326
+ principal: z.ZodString;
327
+ model: z.ZodString;
328
+ inputTokens: z.ZodNumber;
329
+ outputTokens: z.ZodNumber;
330
+ costMicroUsd: z.ZodNumber;
331
+ runs: z.ZodOptional<z.ZodNumber>;
332
+ /** 加权 tokens(配额轴计账值,EXPERT-REDESIGN §4-1):=(input+output)×记账时的 quotaWeight,
333
+ * **weight-at-burn 持久化**——倍率 publish 只影响此后记账,不回算窗口内历史(余量/恢复时间不跳变)。
334
+ * 0.6.0 additive optional:旧 worker 不报 → 聚合按 quotaWeight=1 兜底折算。 */
335
+ weightedTokens: z.ZodOptional<z.ZodNumber>;
336
+ /** 记账时固化的该模型 quotaWeight(审计口径,与 weightedTokens 同时点)——lease 扣减与 registry 聚合两
337
+ * 路径按同一时点折算(同口径,§11-1)。 */
338
+ quotaWeightAtUse: z.ZodOptional<z.ZodNumber>;
339
+ }, "strip", z.ZodTypeAny, {
340
+ principal: string;
341
+ costMicroUsd: number;
342
+ model: string;
343
+ inputTokens: number;
344
+ outputTokens: number;
345
+ weightedTokens?: number | undefined;
346
+ runs?: number | undefined;
347
+ quotaWeightAtUse?: number | undefined;
348
+ }, {
349
+ principal: string;
350
+ costMicroUsd: number;
351
+ model: string;
352
+ inputTokens: number;
353
+ outputTokens: number;
354
+ weightedTokens?: number | undefined;
355
+ runs?: number | undefined;
356
+ quotaWeightAtUse?: number | undefined;
357
+ }>;
358
+ export type UsageEntry = z.infer<typeof UsageEntry>;
359
+ /** Periodic batch (worker → center POST). Windows are half-open [start, end) so adjacent batches never
360
+ * double-count a boundary instant. */
361
+ export declare const UsageReport: z.ZodObject<{
362
+ worker: z.ZodString;
363
+ instanceId: z.ZodString;
364
+ windowStartMs: z.ZodNumber;
365
+ windowEndMs: z.ZodNumber;
366
+ entries: z.ZodArray<z.ZodObject<{
367
+ principal: z.ZodString;
368
+ model: z.ZodString;
369
+ inputTokens: z.ZodNumber;
370
+ outputTokens: z.ZodNumber;
371
+ costMicroUsd: z.ZodNumber;
372
+ runs: z.ZodOptional<z.ZodNumber>;
373
+ /** 加权 tokens(配额轴计账值,EXPERT-REDESIGN §4-1):=(input+output)×记账时的 quotaWeight,
374
+ * **weight-at-burn 持久化**——倍率 publish 只影响此后记账,不回算窗口内历史(余量/恢复时间不跳变)。
375
+ * 0.6.0 additive optional:旧 worker 不报 → 聚合按 quotaWeight=1 兜底折算。 */
376
+ weightedTokens: z.ZodOptional<z.ZodNumber>;
377
+ /** 记账时固化的该模型 quotaWeight(审计口径,与 weightedTokens 同时点)——lease 扣减与 registry 聚合两
378
+ * 路径按同一时点折算(同口径,§11-1)。 */
379
+ quotaWeightAtUse: z.ZodOptional<z.ZodNumber>;
380
+ }, "strip", z.ZodTypeAny, {
381
+ principal: string;
382
+ costMicroUsd: number;
383
+ model: string;
384
+ inputTokens: number;
385
+ outputTokens: number;
386
+ weightedTokens?: number | undefined;
387
+ runs?: number | undefined;
388
+ quotaWeightAtUse?: number | undefined;
389
+ }, {
390
+ principal: string;
391
+ costMicroUsd: number;
392
+ model: string;
393
+ inputTokens: number;
394
+ outputTokens: number;
395
+ weightedTokens?: number | undefined;
396
+ runs?: number | undefined;
397
+ quotaWeightAtUse?: number | undefined;
398
+ }>, "many">;
399
+ }, "strip", z.ZodTypeAny, {
400
+ instanceId: string;
401
+ entries: {
402
+ principal: string;
403
+ costMicroUsd: number;
404
+ model: string;
405
+ inputTokens: number;
406
+ outputTokens: number;
407
+ weightedTokens?: number | undefined;
408
+ runs?: number | undefined;
409
+ quotaWeightAtUse?: number | undefined;
410
+ }[];
411
+ worker: string;
412
+ windowStartMs: number;
413
+ windowEndMs: number;
414
+ }, {
415
+ instanceId: string;
416
+ entries: {
417
+ principal: string;
418
+ costMicroUsd: number;
419
+ model: string;
420
+ inputTokens: number;
421
+ outputTokens: number;
422
+ weightedTokens?: number | undefined;
423
+ runs?: number | undefined;
424
+ quotaWeightAtUse?: number | undefined;
425
+ }[];
426
+ worker: string;
427
+ windowStartMs: number;
428
+ windowEndMs: number;
429
+ }>;
430
+ export type UsageReport = z.infer<typeof UsageReport>;
431
+ /** 额度清零事件(EXPERT-REDESIGN §4-4,契约随 E1 冻结、E4 消费):申请刷新获批即写一条;5h/周双窗聚合
432
+ * **只计最后一次 reset 之后**的 usage 报文(usage_report 是计费审计事实,全量留存,绝不删)。拒绝逻辑/
433
+ * 恢复时间/用量卡读数全部感知 reset;批准生效延迟 ≤ 一次租约 TTL(旧租约续租时放行)。 */
434
+ export declare const QuotaResetEvent: z.ZodObject<{
435
+ id: z.ZodString;
436
+ principal: z.ZodString;
437
+ /** 清零生效时刻(ISO)——双窗聚合的下边界。 */
438
+ reset_at: z.ZodString;
439
+ /** 批准人(console username)。 */
440
+ decided_by: z.ZodString;
441
+ /** 关联的 quota_refresh_request id;admin 直接清零(无申请)时可缺。 */
442
+ request_id: z.ZodOptional<z.ZodString>;
443
+ }, "strip", z.ZodTypeAny, {
444
+ principal: string;
445
+ id: string;
446
+ reset_at: string;
447
+ decided_by: string;
448
+ request_id?: string | undefined;
449
+ }, {
450
+ principal: string;
451
+ id: string;
452
+ reset_at: string;
453
+ decided_by: string;
454
+ request_id?: string | undefined;
455
+ }>;
456
+ export type QuotaResetEvent = z.infer<typeof QuotaResetEvent>;
457
+ export interface AggregatedPrincipalUsage {
458
+ inputTokens: number;
459
+ outputTokens: number;
460
+ costMicroUsd: number;
461
+ runs: number;
462
+ /** 加权 tokens 总量(配额轴,§4-1/§11-1):兑现 UsageEntry doc 的聚合承诺——entry 带 weightedTokens 用
463
+ * 原值;旧 worker 不报 → 按 quotaWeightAtUse(缺省 1)兜底折算 (input+output)×weight。与 lease 扣减同
464
+ * 口径,E4 双窗读数直接复用(E1 评审 opus MINOR:doc/impl 自相矛盾修复)。quotaWeightAtUse 是审计
465
+ * 时点值,不聚合。 */
466
+ weightedTokens: number;
467
+ byModel: Record<string, {
468
+ inputTokens: number;
469
+ outputTokens: number;
470
+ costMicroUsd: number;
471
+ runs: number;
472
+ weightedTokens: number;
473
+ }>;
474
+ }
475
+ /** Center-side fold: per-principal totals ACROSS workers/instances (the "one user, many workers, one bill"
476
+ * view). Pure — storage/dedup of replayed batches is the caller's concern (key batches by
477
+ * worker+instanceId+windowStartMs). */
478
+ export declare function aggregateUsage(reports: UsageReport[]): Record<string, AggregatedPrincipalUsage>;
479
+ //# sourceMappingURL=fleet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fleet.d.ts","sourceRoot":"","sources":["../src/fleet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB;0FAC0F;AAC1F,eAAO,MAAM,YAAY,6CAA8C,CAAC;AACxE,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAEvD;;2EAE2E;AAC3E,eAAO,MAAM,sBAAsB,SAAU,CAAC;AAC9C,8FAA8F;AAC9F,eAAO,MAAM,oBAAoB,QAAS,CAAC;AAC3C;iEACiE;AACjE,eAAO,MAAM,yBA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