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