@selvajs/solve 2.0.0-beta.9 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.d.cts DELETED
@@ -1,416 +0,0 @@
1
- import { t as SolveInput } from "./solve-input-CUjVGYPC.cjs";
2
- import { DataTree, GrasshopperClient, GrasshopperComputeResponse, SelvaEventTarget, SolveScheduler } from "@selvajs/compute/grasshopper";
3
- import { DefinitionRef, DefinitionRef as DefinitionRef$1, SolveDefinition, SolveDefinition as SolveDefinition$1, isDefinitionRef } from "@selvajs/compute/core";
4
- import { ILogger } from "@selvajs/platform";
5
- /**
6
- * Opaque identity of a compute server — construct only via `serverIdentity()`.
7
- * Kept opaque so "a server is now a pool of URLs behind one id" stays additive.
8
- */
9
- type ServerIdentity = string & {
10
- readonly __brand: 'ServerIdentity';
11
- };
12
- /** Minimal resolved-server shape the cache needs (a subset of the app's `ComputeServerConfig`). */
13
- interface ResolvedServer {
14
- id: string;
15
- serverUrl: string;
16
- /** Sent as the `RhinoComputeKey` header. */
17
- apiKey?: string;
18
- }
19
- export declare function serverIdentity(server: Pick<ResolvedServer, 'id'>): ServerIdentity;
20
- interface CachedClient {
21
- client: GrasshopperClient;
22
- scheduler: SolveScheduler;
23
- }
24
- /**
25
- * Debug verbosity: `false` is silent, `true` gives concise cache/timing logs
26
- * (`onDebugLog`), `'verbose'` also enables full lib-level request/response
27
- * dumps (incl. geometry) — a superset of `true`, not an independent setting.
28
- */
29
- type ClientCacheDebug = boolean | 'verbose';
30
- /** Config injected by the consuming app, so this module stays env-agnostic and testable. */
31
- interface ClientCacheConfig {
32
- /** Per-solve timeout forwarded to the scheduler (`ComputeLimits.solveDeadlineMs`). */
33
- solveDeadlineMs: number;
34
- cachesolve: boolean;
35
- /** Reference large definitions by server cache key instead of re-uploading. */
36
- reuseServerDefinitionCache: boolean;
37
- /**
38
- * Max solves that may wait in the FIFO queue, excluding in-flight solves
39
- * (capped at `maxConcurrent`, itself driven by the server's probed child
40
- * count). `0` = unbounded (`ComputeLimits.computeMaxQueueDepth`); a full
41
- * queue rejects new solves with `QUEUE_FULL`.
42
- */
43
- maxQueueDepth: number;
44
- /**
45
- * Max ms a solve may sit queued before executing; `0` = no deadline
46
- * (`ComputeLimits.computeQueueWaitMs`). Too long a wait rejects with
47
- * `QUEUE_TIMEOUT`.
48
- */
49
- queueWaitMs: number;
50
- /**
51
- * Byte budget for this client's in-process solve cache. Applies per warm
52
- * client — worst-case heap is this × `maxWarmComputeServers`. `0` disables
53
- * the cache (`ComputeLimits.computeSolveCacheBytes`, env `COMPUTE_SOLVE_CACHE_MB`).
54
- */
55
- responseCacheMaxBytes: number;
56
- /**
57
- * Largest compute response body read; past it the download is cancelled (`too_large`).
58
- * Default: no limit.
59
- */
60
- maxResponseBytes?: number;
61
- debug: ClientCacheDebug;
62
- /** Max distinct warm compute servers before the LRU evicts the oldest. Default 16. */
63
- maxWarmComputeServers?: number;
64
- /** Sink for the debug lines. `onDebugLog` only fires when `debug` is not `false`. */
65
- onDebugLog?: (message: string) => void;
66
- }
67
- interface ClientCache {
68
- /**
69
- * Get (or create) the warm client + scheduler for a resolved compute server,
70
- * keyed by its `id`. `definitionGuid`, when present, is stamped as the
71
- * `X-Selva-Definition` header on this client's outbound solve/IO requests —
72
- * inert routing/telemetry metadata until a pool router exists.
73
- */
74
- getClient(server: ResolvedServer, opts?: {
75
- definitionGuid?: string;
76
- }): Promise<CachedClient>;
77
- /** Dispose and drop the warm client for `id`, so the next request rebuilds against fresh connection details. */
78
- evict(id: string | ServerIdentity): void;
79
- /** Solve-cache counters summed across every warm client. Counters die with the client that owns them, so totals can fall over time. */
80
- solveCacheStats(): SolveCacheStats;
81
- /**
82
- * Drop every retained solve result, keeping the warm clients (and their
83
- * connections and server-side definition pointers) intact. Nothing expires
84
- * these on its own — the byte budget is the only other pressure — so this is
85
- * the operator's release valve when a definition's inputs no longer describe
86
- * its output, e.g. it reads an external source that has since changed.
87
- */
88
- clearSolveCaches(): void;
89
- /** Dispose every warm client. Test seam / shutdown hook. */
90
- disposeAll(): void;
91
- }
92
- interface SolveCacheStats {
93
- warmClients: number;
94
- entries: number;
95
- bytes: number;
96
- hits: number;
97
- misses: number;
98
- /** Entries dropped under size/byte pressure (not replacement or manual clears). */
99
- evictions: number;
100
- }
101
- export declare function createClientCache(config: ClientCacheConfig): ClientCache;
102
- /**
103
- * In-process cache of `.gh` definition bytes, keyed by immutable **version id**.
104
- *
105
- * Turns an eager `storage.get(fileKey)` on every solve request into a lazy
106
- * `DefinitionRef.load()`: the scheduler calls `load()` only when an upload is
107
- * unavoidable (a pointer-known re-solve never touches storage or this cache).
108
- *
109
- * The key MUST be the version id, never a `fileKey` — a delete-latest-then-
110
- * reupload can reuse a `fileKey` for different content, which would silently
111
- * serve one version's bytes for another with no diagnostic. Callers pass
112
- * `version.id`, and the returned `DefinitionRef.key` is that same id, so the
113
- * scheduler's own result/pointer cache keys on the same immutable identity.
114
- *
115
- * Eviction is LRU by total byte budget, not entry count — definitions range
116
- * from tens of KB to hundreds of MB, so a count cap would either pin
117
- * gigabytes or evict uselessly. `COMPUTE_DEFINITION_CACHE_MB` sets the
118
- * budget (see `createDefinitionByteCache`). No TTL: version ids are
119
- * immutable, so a cached entry can never go stale. An entry larger than the
120
- * whole budget is served but never retained.
121
- */
122
- /**
123
- * Per-`getOrLoad` outcome, observable after the scheduler has (or hasn't)
124
- * called `load()` — lets the solve pipeline emit a `def_bytes` Server-Timing
125
- * verdict (`skipped` / `hit` / `miss`).
126
- */
127
- interface ByteRefOutcome {
128
- loaded: boolean;
129
- fromCache: boolean;
130
- }
131
- /** A definition reference shaped for `@selvajs/compute`'s `DefinitionRef`. */
132
- interface ByteCacheRef {
133
- key: string;
134
- load: () => Promise<Uint8Array>;
135
- outcome: ByteRefOutcome;
136
- }
137
- interface ByteCacheStats {
138
- hits: number;
139
- misses: number;
140
- evictions: number;
141
- entries: number;
142
- bytes: number;
143
- }
144
- interface DefinitionByteCache {
145
- getOrLoad(versionId: string, load: () => Promise<Uint8Array>): ByteCacheRef;
146
- stats(): ByteCacheStats;
147
- clear(): void;
148
- }
149
- /**
150
- * @param maxBytes total retained-byte budget; `0` (or negative) disables caching
151
- * entirely — every `load()` calls the loader and nothing is retained.
152
- */
153
- export declare function createDefinitionByteCache(maxBytes: number): DefinitionByteCache;
154
- /** Compute-response contract version. Bump (and document the change) whenever the envelope's shape changes in a way a consumer could observe. */
155
- export declare const COMPUTE_CONTRACT_VERSION: 1;
156
- export declare const COMPUTE_VERSION_HEADER = "X-Selva-Compute-Version";
157
- type PipelineInput = SolveInput;
158
- interface SolvePipelineArgs {
159
- /**
160
- * Raw `.gh` bytes, or a byte-cache `DefinitionRef` whose bytes the scheduler
161
- * materializes only when an upload is unavoidable — a pointer-known solve
162
- * moves zero bytes.
163
- */
164
- definitionSource: SolveDefinition$1;
165
- /** Mutable load outcome for a `DefinitionRef` source, so the `def_bytes` Server-Timing verdict can be emitted. Omit for raw-bytes solves. */
166
- byteRefOutcome?: ByteRefOutcome;
167
- /** Persisted input params; only those with a `paramType` are sent to the solve. */
168
- inputs: PipelineInput[];
169
- /**
170
- * A tree the caller already built with {@link buildSolveInputTree}; skips the
171
- * pipeline's own build and solves this object directly.
172
- *
173
- * A caller coalescing concurrent solves needs this: a single-flight key
174
- * derived from raw `{inputs, values}` would split two requests that transform
175
- * to the same tree, which is the identity the scheduler actually caches on.
176
- */
177
- inputTree?: DataTree[];
178
- /** User-chosen values keyed by input id; missing keys fall back to the schema default. */
179
- values: Record<string, unknown>;
180
- client: CachedClient;
181
- responseMaxBytes: number;
182
- /** Only phrases the timeout message — the scheduler enforces the deadline itself. */
183
- solveDeadlineMs: number;
184
- /** Client's `Accept-Encoding`; gzip is applied only when it advertises `gzip`. */
185
- acceptEncoding: string;
186
- /** Per-solve live-event callback the compute server hands to the definition; absent when the caller opened no stream. */
187
- selvaEvents?: SelvaEventTarget;
188
- /**
189
- * Forwarded to the scheduler so a client disconnect cancels the upstream
190
- * compute call. Its `aborted` flag also distinguishes a client disconnect
191
- * from the scheduler's own deadline firing.
192
- */
193
- signal: AbortSignal;
194
- /** Wall-clock origin (`performance.now()` at the top of the request) so `load`/`total` phase timings line up with the caller's pre-solve prep. */
195
- loadStartMs: number;
196
- /** Pre-solve "load" phase duration the caller already measured (auth + DB + fetch). */
197
- defLoadMs: number;
198
- /** Pre-solve prep sub-phase marks (`[label, ms]`), surfaced verbatim as `p_*` Server-Timing entries. */
199
- prepMarks?: [string, number][];
200
- }
201
- /** Phase timings the pipeline measured; the caller uses them for debug logging. */
202
- interface SolvePhaseMetrics {
203
- treeBuildMs: number;
204
- solveMs: number;
205
- serializeMs: number;
206
- gzipMs: number;
207
- serverTotalMs: number;
208
- serializedBytes: number;
209
- /** Null when compression was skipped. */
210
- compressedBytes: number | null;
211
- }
212
- /** A ready-to-send response: body + headers + the solve result + phase metrics. */
213
- interface SolveEnvelope {
214
- /** A gzip `Uint8Array` when `encoding === 'gzip'`, else the JSON string. */
215
- body: string | Uint8Array;
216
- encoding?: 'gzip';
217
- headers: Record<string, string>;
218
- result: GrasshopperComputeResponse;
219
- metrics: SolvePhaseMetrics;
220
- }
221
- /**
222
- * `ok` carries the envelope; every other variant names an expected failure the
223
- * calling route maps to a status code (timeout→504, client_abort→499,
224
- * too_large→413, shed→503+Retry-After, compute_error→generic 500/503).
225
- * `durationMs` on error variants is the solve wall time up to the failure, for
226
- * the metric record.
227
- */
228
- type SolveOutcome = {
229
- kind: 'ok';
230
- envelope: SolveEnvelope;
231
- solveMs: number;
232
- errorCount: number;
233
- warningCount: number;
234
- } | {
235
- kind: 'timeout';
236
- durationMs: number;
237
- message: string;
238
- } | {
239
- kind: 'client_abort';
240
- durationMs: number;
241
- } | {
242
- kind: 'too_large';
243
- } |
244
- /** Scheduler backpressure rejected the solve before it ran: queue was full (`queue_full`) or the wait exceeded the deadline (`queue_timeout`). Retryable — `retryAfterSeconds` is a backoff hint. */
245
- {
246
- kind: 'shed';
247
- durationMs: number;
248
- reason: 'queue_full' | 'queue_timeout';
249
- retryAfterSeconds: number;
250
- message: string;
251
- } | {
252
- kind: 'compute_error';
253
- durationMs: number;
254
- error: unknown;
255
- };
256
- export declare function runSolvePipeline(args: SolvePipelineArgs): Promise<SolveOutcome>;
257
- /**
258
- * Re-key a coalesced envelope to a single waiter's `Accept-Encoding`.
259
- *
260
- * The single-flight coalescer runs ONE pipeline execution for N identical
261
- * concurrent solves and hands every waiter the same {@link SolveEnvelope},
262
- * baked from the FIRST caller's `Accept-Encoding`. A later waiter with a
263
- * different `Accept-Encoding` would otherwise get a body labelled with the
264
- * wrong encoding — `Vary` can't help here, since it's one shared object, not a
265
- * cache lookup. Encoding is deliberately left out of the coalesce key so mixed
266
- * clients still coalesce; this adapts the shared result to each waiter instead.
267
- *
268
- * Only the correctness-critical direction is adapted: a gzip envelope served to
269
- * a non-gzip waiter is gunzipped back to JSON. The reverse (plain JSON to a
270
- * gzip-capable waiter) is left uncompressed — correct, just not maximally small.
271
- */
272
- export declare function adaptEnvelopeToEncoding(envelope: SolveEnvelope, acceptEncoding: string): {
273
- body: string | Uint8Array;
274
- headers: Record<string, string>;
275
- };
276
- /** The input tree a solve sends: each input's value, or its schema default when it has none. */
277
- export declare function buildSolveInputTree(inputs: SolveInput[], values: Record<string, unknown>): DataTree[];
278
- /**
279
- * In-process single-flight — dogpile protection for every solve.
280
- *
281
- * Always on, not gated behind a result cache being configured: the dogpile is worst
282
- * when nothing else is caching, since N concurrent identical solves each pay a full
283
- * Rhino round trip (the scheduler doesn't coalesce in-flight requests itself, and
284
- * Rhino's `cachesolve` still costs a round trip per repeat). An earlier version
285
- * gated this on cache config and so disabled it in exactly the deployments most
286
- * exposed to a cold-key stampede — hot public definition plus a deploy.
287
- *
288
- * Per app instance, above `ISolveResultCache`. No cross-instance lease (Redis
289
- * `SET NX`) yet.
290
- *
291
- * The shared promise resolves to one value for every waiter, so `work` must return
292
- * something safe to share by reference — the solve pipeline's envelope qualifies
293
- * because the app serializes it per response.
294
- */
295
- interface SolveCacheSingleFlight {
296
- /**
297
- * Coalesces concurrent calls under the same key: the first caller runs `work`,
298
- * later callers for that key await the same promise, and the key frees as soon
299
- * as it settles.
300
- *
301
- * `onWaiterJoined` fires only on the owner's call, only while `work` is still
302
- * running, each time another caller joins. Ownership changes what the owner's
303
- * abort signal should mean: a solo run can cancel on its own client's
304
- * disconnect, but a shared run can't without 499-ing every waiter.
305
- */
306
- run<T>(key: string, work: () => Promise<T>, onWaiterJoined?: () => void): Promise<T>;
307
- inFlight(): number;
308
- }
309
- interface SolveCacheSingleFlightOptions {
310
- onJoin?: (key: string) => void;
311
- }
312
- export declare function createSolveCacheSingleFlight(options?: SolveCacheSingleFlightOptions): SolveCacheSingleFlight;
313
- /**
314
- * Subset of `ComputeLimits` (`@selvajs/server/compute`) the engine needs.
315
- * Pass the resolved object through as-is — extra fields are ignored.
316
- */
317
- interface SolveEngineLimits {
318
- solveDeadlineMs: number;
319
- computeResponseMaxBytes: number;
320
- computeReuseDefinitionCache: boolean;
321
- computeServerCachesolve: boolean;
322
- computeMaxQueueDepth: number;
323
- computeQueueWaitMs: number;
324
- computeDefinitionCacheBytes: number;
325
- computeSolveCacheBytes: number;
326
- }
327
- interface SolveEngineOptions {
328
- limits: SolveEngineLimits;
329
- logger?: ILogger;
330
- /** Max distinct warm compute servers before the LRU evicts the oldest. Default 16. */
331
- maxWarmComputeServers?: number;
332
- /** Concise cache/timing logs when truthy; `'verbose'` also dumps full lib-level requests/responses. Default off. */
333
- debug?: ClientCacheDebug;
334
- onDebugLog?: (message: string) => void;
335
- /** Fired when a caller joins an already-in-flight solve instead of running its own. */
336
- onSolveCoalesced?: (key: string) => void;
337
- }
338
- /**
339
- * Accepts every form `runSolvePipeline` does, plus `{versionId, load}` sugar
340
- * that builds (and caches) a `ByteCacheRef` internally. A `ByteCacheRef`
341
- * obtained ahead of time from `engine.definitionRef()` (e.g. to read bytes for
342
- * schema extraction before solving) is passed through as-is — detected by its
343
- * `outcome` field, which a plain `DefinitionRef` never has.
344
- */
345
- type SolveEngineDefinitionSource = Uint8Array | string | DefinitionRef$1 | ByteCacheRef | {
346
- versionId: string;
347
- load: () => Promise<Uint8Array>;
348
- };
349
- interface SolveEngineSolveArgs {
350
- server: ResolvedServer;
351
- definitionSource: SolveEngineDefinitionSource;
352
- /**
353
- * Coalesce-key identity for a raw `Uint8Array`/`string` source, which has
354
- * no natural identity the way a `DefinitionRef`'s `.key` does. Required in
355
- * that case — `solve()` throws rather than silently hashing the bytes.
356
- * Ignored for the other source forms.
357
- */
358
- definitionKey?: string;
359
- inputs: PipelineInput[];
360
- values: Record<string, unknown>;
361
- signal: AbortSignal;
362
- acceptEncoding?: string;
363
- /** Stamped as `X-Selva-Definition` on this solve's client. */
364
- definitionGuid?: string;
365
- loadStartMs?: number;
366
- defLoadMs?: number;
367
- prepMarks?: [string, number][];
368
- /**
369
- * Per-solve live-event callback. A coalesced waiter shares the first caller's
370
- * compute call, so only that caller's target reaches the definition; the
371
- * others still get the server-emitted start/end events on their own stream.
372
- */
373
- selvaEvents?: SelvaEventTarget;
374
- }
375
- interface FrameworkAgnosticResponse {
376
- status: number;
377
- headers: Record<string, string>;
378
- body: string | Uint8Array;
379
- }
380
- interface SolveEngineStats {
381
- client: SolveCacheStats;
382
- definitionBytes: ByteCacheStats;
383
- coalescing: {
384
- inFlight: number;
385
- };
386
- }
387
- export declare class SolveEngine {
388
- private readonly limits;
389
- private readonly clientCache;
390
- private readonly byteCache;
391
- private readonly singleFlight;
392
- constructor(options: SolveEngineOptions);
393
- getClient(server: ResolvedServer, opts?: {
394
- definitionGuid?: string;
395
- }): Promise<CachedClient>;
396
- definitionRef(versionId: string, load: () => Promise<Uint8Array>): ByteCacheRef;
397
- evictServer(id: string): void;
398
- clearSolveCaches(): void;
399
- /**
400
- * Drop every retained `.gh` byte entry. Safe at any time: entries key on an
401
- * immutable version id, so clearing costs a storage re-read and nothing else.
402
- */
403
- clearDefinitionCache(): void;
404
- stats(): SolveEngineStats;
405
- solve(args: SolveEngineSolveArgs): Promise<SolveOutcome>;
406
- toResponse(outcome: SolveOutcome, opts?: {
407
- onError?: (status: number, body: {
408
- message: string;
409
- retryAfter?: number;
410
- }) => never;
411
- }): FrameworkAgnosticResponse;
412
- toWebResponse(outcome: SolveOutcome): Response;
413
- private resolveDefinitionSource;
414
- }
415
- export { type ByteCacheRef, type ByteCacheStats, type ByteRefOutcome, type CachedClient, type ClientCache, type ClientCacheConfig, type DefinitionByteCache, type DefinitionRef, type FrameworkAgnosticResponse, type PipelineInput, type ResolvedServer, type ServerIdentity, type SolveCacheSingleFlight, type SolveCacheSingleFlightOptions, type SolveCacheStats, type SolveDefinition, type SolveEngineDefinitionSource, type SolveEngineLimits, type SolveEngineOptions, type SolveEngineSolveArgs, type SolveEngineStats, type SolveEnvelope, type SolveInput, type SolveOutcome, type SolvePhaseMetrics, type SolvePipelineArgs, isDefinitionRef };
416
- //# sourceMappingURL=server.d.cts.map
package/dist/shared.cjs DELETED
@@ -1 +0,0 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./outcome-DUHQoZqq.cjs");exports.SOLVE_BLOCKED_MARKER=e.i,exports.decodeOutcome=e.t,exports.finalizeResult=e.n,exports.outcomeFromComputeMessages=e.r;
package/dist/shared.d.cts DELETED
@@ -1,3 +0,0 @@
1
- import { a as SOLVE_BLOCKED_MARKER, c as SolveResult, i as outcomeFromComputeMessages, n as decodeOutcome, o as SolveDiagnostic, r as finalizeResult, s as SolveFn, t as SolveOutcome } from "./outcome-DsNmx27P.cjs";
2
- import { t as SolveInput } from "./solve-input-CUjVGYPC.cjs";
3
- export { SOLVE_BLOCKED_MARKER, type SolveDiagnostic, type SolveFn, type SolveInput, type SolveOutcome, type SolveResult, decodeOutcome, finalizeResult, outcomeFromComputeMessages };
@@ -1,11 +0,0 @@
1
- import { SchemaInput } from "@selvajs/schemas";
2
- /**
3
- * A schema input carrying the optional numeric bounds tree building clamps to. Only numeric params
4
- * (sliders) carry them; text or geometry inputs carry none.
5
- */
6
- type SolveInput = SchemaInput & {
7
- minimum?: number;
8
- maximum?: number;
9
- };
10
- export { SolveInput as t };
11
- //# sourceMappingURL=solve-input-CUjVGYPC.d.cts.map