@selvajs/solve 2.1.0-beta.2 → 2.1.0-beta.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.
- package/dist/server.d.ts +8 -7
- package/dist/server.js +56 -39
- package/dist/server.js.map +1 -1
- package/package.json +2 -2
package/dist/server.d.ts
CHANGED
|
@@ -225,16 +225,19 @@ interface SolveEngineSolveArgs {
|
|
|
225
225
|
values: Record<string, unknown>;
|
|
226
226
|
signal: AbortSignal;
|
|
227
227
|
acceptEncoding?: string;
|
|
228
|
-
/**
|
|
229
|
-
* Stamped as `X-Selva-Definition` when this solve builds the server's warm client. The header
|
|
230
|
-
* is fixed per client, so later solves on that server carry the first definition's guid.
|
|
231
|
-
*/
|
|
228
|
+
/** Sent as `X-Selva-Definition`, a key a router can keep one definition's solves together on. */
|
|
232
229
|
definitionGuid?: string;
|
|
233
230
|
loadStartMs?: number;
|
|
234
231
|
defLoadMs?: number;
|
|
235
232
|
prepMarks?: PrepMark[];
|
|
236
233
|
/** Per-solve live-event callback. A solve carrying one runs its own compute call, never a shared one. */
|
|
237
234
|
selvaEvents?: SelvaEventTarget;
|
|
235
|
+
/**
|
|
236
|
+
* Sent as `X-Session-Id` so the compute fork routes the solve to the child holding this
|
|
237
|
+
* session's warm document. Not part of the coalesce key: a joined flight routes on the id of
|
|
238
|
+
* the caller that started it.
|
|
239
|
+
*/
|
|
240
|
+
sessionId?: string;
|
|
238
241
|
}
|
|
239
242
|
interface SolveEngineStats {
|
|
240
243
|
client: SolveCacheStats;
|
|
@@ -251,9 +254,7 @@ export declare class SolveEngine {
|
|
|
251
254
|
private readonly clientGenerations;
|
|
252
255
|
private lastClientGeneration;
|
|
253
256
|
constructor(options: SolveEngineOptions);
|
|
254
|
-
getClient(server: ResolvedServer
|
|
255
|
-
definitionGuid?: string;
|
|
256
|
-
}): Promise<CachedClient>;
|
|
257
|
+
getClient(server: ResolvedServer): Promise<CachedClient>;
|
|
257
258
|
definitionRef(versionId: string, load: (signal: AbortSignal) => Promise<Uint8Array>): ByteCacheRef;
|
|
258
259
|
/**
|
|
259
260
|
* Drops the warm client for `id`; call it when the server's URL or key changes. Solves already
|
package/dist/server.js
CHANGED
|
@@ -76,6 +76,41 @@ const unlimitedFetch = ((input, init) => fetch(input, {
|
|
|
76
76
|
function fetchForDeadline(deadlineMs) {
|
|
77
77
|
return deadlineMs >= UNDICI_DEFAULT_LIMIT_MS ? unlimitedFetch : void 0;
|
|
78
78
|
}
|
|
79
|
+
// src/server/server-timing.ts
|
|
80
|
+
/**
|
|
81
|
+
* False for a header carrying only the proxy's `affinity`, from a child that sends no timing:
|
|
82
|
+
* read as zeros, it would put the whole solve on `compute_link`.
|
|
83
|
+
*/
|
|
84
|
+
function reportsTiming(rhino) {
|
|
85
|
+
return rhino.cacheHit === true || rhino.decode !== void 0 || rhino.solve !== void 0 || rhino.encode !== void 0;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* `total` is the server's headers-to-out wall time, so browser `ttfb − total`
|
|
89
|
+
* ≈ network+send latency. When the compute server reports its own timing
|
|
90
|
+
* (VektorNode fork), `rhino_*` is time on the compute server and `compute_link`
|
|
91
|
+
* is everything between it (network + queue wait). `rhino_cache` is 1 when the
|
|
92
|
+
* compute server answered from its own result cache.
|
|
93
|
+
*/
|
|
94
|
+
function buildServerTiming(parts) {
|
|
95
|
+
let header = `load;dur=${parts.defLoadMs.toFixed(1)}, tree;dur=${parts.treeBuildMs.toFixed(1)}, solve;dur=${parts.solveMs.toFixed(1)}, serialize;dur=${parts.serializeMs.toFixed(1)}, gzip;dur=${parts.gzipMs.toFixed(1)}, total;dur=${parts.serverTotalMs.toFixed(1)}`;
|
|
96
|
+
const rhino = parts.rhino;
|
|
97
|
+
if (rhino && reportsTiming(rhino)) {
|
|
98
|
+
const decode = rhino.decode ?? 0;
|
|
99
|
+
const solve = rhino.solve ?? 0;
|
|
100
|
+
const encode = rhino.encode ?? 0;
|
|
101
|
+
const computeLinkMs = Math.max(0, parts.solveMs - decode - solve - encode);
|
|
102
|
+
header += `, rhino_decode;dur=${decode.toFixed(1)}, rhino_solve;dur=${solve.toFixed(1)}, rhino_encode;dur=${encode.toFixed(1)}, compute_link;dur=${computeLinkMs.toFixed(1)}, rhino_cache;dur=${rhino.cacheHit ? 1 : 0}`;
|
|
103
|
+
if (rhino.gh !== void 0) header += `, rhino_gh;dur=${rhino.gh.toFixed(1)}`;
|
|
104
|
+
if (rhino.recomputed) header += `, rhino_recomputed;desc="${rhino.recomputed.ran}/${rhino.recomputed.total}"`;
|
|
105
|
+
}
|
|
106
|
+
if (rhino?.affinity) header += `, rhino_affinity;desc=${rhino.affinity}`;
|
|
107
|
+
for (const [label, ms] of parts.prepMarks) header += `, p_${label};dur=${ms.toFixed(1)}`;
|
|
108
|
+
const settle = parts.settle;
|
|
109
|
+
header += `, selva_cache;dur=${settle.fromCache ? 1 : 0}`;
|
|
110
|
+
if (settle.definitionReuploaded !== void 0) header += `, def_reupload;dur=${settle.definitionReuploaded ? 1 : 0}`;
|
|
111
|
+
if (parts.defBytes) header += `, def_bytes;desc=${parts.defBytes}`;
|
|
112
|
+
return header;
|
|
113
|
+
}
|
|
79
114
|
// src/server/server-limits.ts
|
|
80
115
|
/** Rhino.Compute's own default `--timeout`, for a server that can't report its own. */
|
|
81
116
|
const DEFAULT_SOLVE_DEADLINE_MS = 1e5;
|
|
@@ -165,7 +200,7 @@ function createClientCache(config) {
|
|
|
165
200
|
draining.delete(entry);
|
|
166
201
|
entry.scheduler.dispose();
|
|
167
202
|
}
|
|
168
|
-
async function build(server
|
|
203
|
+
async function build(server) {
|
|
169
204
|
const debugVerbose = config.debug === "verbose";
|
|
170
205
|
const longFetch = fetchForDeadline(MAX_SOLVE_DEADLINE_MS);
|
|
171
206
|
const clientConfig = {
|
|
@@ -177,8 +212,13 @@ function createClientCache(config) {
|
|
|
177
212
|
...longFetch && { fetch: longFetch }
|
|
178
213
|
};
|
|
179
214
|
if (config.debug) clientConfig.onServerTiming = (t) => {
|
|
215
|
+
const routed = t.affinity ? ` | session ${t.affinity}` : "";
|
|
180
216
|
if (t.cacheHit) {
|
|
181
|
-
debugLog(
|
|
217
|
+
debugLog(`[Compute/rhino-cache] solve-cache (cachesolve): HIT (cached result, solve skipped)${routed}`);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (!reportsTiming(t)) {
|
|
221
|
+
debugLog(`[Compute/rhino-cache] no solve timing${routed}`);
|
|
182
222
|
return;
|
|
183
223
|
}
|
|
184
224
|
const decode = t.decode ?? 0;
|
|
@@ -187,9 +227,8 @@ function createClientCache(config) {
|
|
|
187
227
|
const defCache = decode <= 1 ? "HIT (definition reused, no re-upload/parse)" : "miss (full parse)";
|
|
188
228
|
const solveCache = config.cachesolve ? "miss (solved fresh)" : "off";
|
|
189
229
|
const ran = t.recomputed ? ` | ran ${t.recomputed.ran}/${t.recomputed.total} objects` : "";
|
|
190
|
-
debugLog(`[Compute/rhino-cache] decode=${decode}ms solve=${solve}ms encode=${encode}ms | definition-cache: ${defCache} | solve-cache (cachesolve): ${solveCache}${ran}`);
|
|
230
|
+
debugLog(`[Compute/rhino-cache] decode=${decode}ms solve=${solve}ms encode=${encode}ms | definition-cache: ${defCache} | solve-cache (cachesolve): ${solveCache}${ran}${routed}`);
|
|
191
231
|
};
|
|
192
|
-
if (definitionGuid) clientConfig.headers = { "X-Selva-Definition": definitionGuid };
|
|
193
232
|
const client = await GrasshopperClient.create(clientConfig);
|
|
194
233
|
const probe = createConcurrencyProbe(client, debugLog);
|
|
195
234
|
const readReported = async () => await Promise.resolve(client.serverStats?.getLimits?.()).catch(() => null) ?? null;
|
|
@@ -256,14 +295,14 @@ function createClientCache(config) {
|
|
|
256
295
|
retire(entry);
|
|
257
296
|
}
|
|
258
297
|
const self = {
|
|
259
|
-
async getClient(server
|
|
298
|
+
async getClient(server) {
|
|
260
299
|
const key = server.id;
|
|
261
300
|
const existing = cache.get(key);
|
|
262
301
|
if (existing) {
|
|
263
302
|
cache.delete(key);
|
|
264
303
|
cache.set(key, existing);
|
|
265
304
|
if (!cacheEnabled || await deploymentChecks.get(existing)?.() !== false) return existing;
|
|
266
|
-
return self.getClient(server
|
|
305
|
+
return self.getClient(server);
|
|
267
306
|
}
|
|
268
307
|
const inFlight = pending.get(key);
|
|
269
308
|
if (inFlight) return inFlight.promise;
|
|
@@ -280,7 +319,7 @@ function createClientCache(config) {
|
|
|
280
319
|
const promise = (async () => {
|
|
281
320
|
let entry;
|
|
282
321
|
try {
|
|
283
|
-
entry = await build(server
|
|
322
|
+
entry = await build(server);
|
|
284
323
|
} catch (err) {
|
|
285
324
|
if (!token.stale) failed.set(key, {
|
|
286
325
|
error: err,
|
|
@@ -605,33 +644,6 @@ function createResponseEncoder(response, maxBytes) {
|
|
|
605
644
|
};
|
|
606
645
|
} };
|
|
607
646
|
}
|
|
608
|
-
// src/server/server-timing.ts
|
|
609
|
-
/**
|
|
610
|
-
* `total` is the server's headers-to-out wall time, so browser `ttfb − total`
|
|
611
|
-
* ≈ network+send latency. When the compute server reports its own timing
|
|
612
|
-
* (VektorNode fork), `rhino_*` is time on the compute server and `compute_link`
|
|
613
|
-
* is everything between it (network + queue wait). `rhino_cache` is 1 when the
|
|
614
|
-
* compute server answered from its own result cache.
|
|
615
|
-
*/
|
|
616
|
-
function buildServerTiming(parts) {
|
|
617
|
-
let header = `load;dur=${parts.defLoadMs.toFixed(1)}, tree;dur=${parts.treeBuildMs.toFixed(1)}, solve;dur=${parts.solveMs.toFixed(1)}, serialize;dur=${parts.serializeMs.toFixed(1)}, gzip;dur=${parts.gzipMs.toFixed(1)}, total;dur=${parts.serverTotalMs.toFixed(1)}`;
|
|
618
|
-
const rhino = parts.rhino;
|
|
619
|
-
if (rhino) {
|
|
620
|
-
const decode = rhino.decode ?? 0;
|
|
621
|
-
const solve = rhino.solve ?? 0;
|
|
622
|
-
const encode = rhino.encode ?? 0;
|
|
623
|
-
const computeLinkMs = Math.max(0, parts.solveMs - decode - solve - encode);
|
|
624
|
-
header += `, rhino_decode;dur=${decode.toFixed(1)}, rhino_solve;dur=${solve.toFixed(1)}, rhino_encode;dur=${encode.toFixed(1)}, compute_link;dur=${computeLinkMs.toFixed(1)}, rhino_cache;dur=${rhino.cacheHit ? 1 : 0}`;
|
|
625
|
-
if (rhino.gh !== void 0) header += `, rhino_gh;dur=${rhino.gh.toFixed(1)}`;
|
|
626
|
-
if (rhino.recomputed) header += `, rhino_recomputed;desc="${rhino.recomputed.ran}/${rhino.recomputed.total}"`;
|
|
627
|
-
}
|
|
628
|
-
for (const [label, ms] of parts.prepMarks) header += `, p_${label};dur=${ms.toFixed(1)}`;
|
|
629
|
-
const settle = parts.settle;
|
|
630
|
-
header += `, selva_cache;dur=${settle.fromCache ? 1 : 0}`;
|
|
631
|
-
if (settle.definitionReuploaded !== void 0) header += `, def_reupload;dur=${settle.definitionReuploaded ? 1 : 0}`;
|
|
632
|
-
if (parts.defBytes) header += `, def_bytes;desc=${parts.defBytes}`;
|
|
633
|
-
return header;
|
|
634
|
-
}
|
|
635
647
|
// src/server/solve-cache-single-flight.ts
|
|
636
648
|
function createSolveCacheSingleFlight(options = {}) {
|
|
637
649
|
const inflight = /* @__PURE__ */ new Map();
|
|
@@ -720,8 +732,9 @@ function buildSolveInputTree(inputs, values) {
|
|
|
720
732
|
// src/server/solve-pipeline.ts
|
|
721
733
|
/**
|
|
722
734
|
* Runs one solve against a warm scheduler and classifies how it ended: a solved response, or an
|
|
723
|
-
* expected failure as a value instead of a throw.
|
|
724
|
-
*
|
|
735
|
+
* expected failure as a value instead of a throw. Single-flight shares this part, so its args come
|
|
736
|
+
* from the caller that started the flight. Anything per caller must be in the coalesce key, or,
|
|
737
|
+
* like `sessionId`, be harmless for the joiners to inherit.
|
|
725
738
|
*/
|
|
726
739
|
function shed(durationMs, reason, message) {
|
|
727
740
|
return {
|
|
@@ -782,6 +795,8 @@ async function runSolvePipeline(args) {
|
|
|
782
795
|
const detail = await args.client.scheduler.solveDetailed(args.definitionSource, args.inputTree, {
|
|
783
796
|
signal: args.signal,
|
|
784
797
|
selvaevents: args.selvaEvents,
|
|
798
|
+
sessionId: args.sessionId,
|
|
799
|
+
headers: args.headers,
|
|
785
800
|
onServerTiming: (t) => {
|
|
786
801
|
rhino = t;
|
|
787
802
|
}
|
|
@@ -852,8 +867,8 @@ var SolveEngine = class {
|
|
|
852
867
|
this.byteCache = createDefinitionByteCache(this.limits.computeDefinitionCacheBytes);
|
|
853
868
|
this.singleFlight = createSolveCacheSingleFlight({ onJoin: options.onSolveCoalesced });
|
|
854
869
|
}
|
|
855
|
-
getClient(server
|
|
856
|
-
return this.clientCache.getClient(server
|
|
870
|
+
getClient(server) {
|
|
871
|
+
return this.clientCache.getClient(server);
|
|
857
872
|
}
|
|
858
873
|
definitionRef(versionId, load) {
|
|
859
874
|
return this.byteCache.getOrLoad(versionId, load);
|
|
@@ -892,7 +907,7 @@ var SolveEngine = class {
|
|
|
892
907
|
};
|
|
893
908
|
let client;
|
|
894
909
|
try {
|
|
895
|
-
client = await this.clientCache.getClient(args.server
|
|
910
|
+
client = await this.clientCache.getClient(args.server);
|
|
896
911
|
} catch (error) {
|
|
897
912
|
if (args.signal.aborted) return {
|
|
898
913
|
kind: "client_abort",
|
|
@@ -928,6 +943,8 @@ var SolveEngine = class {
|
|
|
928
943
|
client,
|
|
929
944
|
responseMaxBytes: this.limits.computeResponseMaxBytes,
|
|
930
945
|
selvaEvents: args.selvaEvents,
|
|
946
|
+
sessionId: args.sessionId,
|
|
947
|
+
...args.definitionGuid && { headers: { "X-Selva-Definition": args.definitionGuid } },
|
|
931
948
|
signal
|
|
932
949
|
}), args.signal);
|
|
933
950
|
if (args.signal.aborted) return {
|
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","names":["undiciFetch"],"sources":["../src/server/concurrency-probe.ts","../src/server/long-fetch.ts","../src/server/server-limits.ts","../src/server/client-cache.ts","../src/server/definition-byte-cache.ts","../src/server/response-encoding.ts","../src/server/server-timing.ts","../src/server/solve-cache-single-flight.ts","../src/server/transform-input.ts","../src/server/solve-pipeline.ts","../src/server/solve-engine.ts"],"sourcesContent":["import type { GrasshopperClient } from '@selvajs/compute/grasshopper';\n\n/**\n * Fallback concurrency while the first probe is still in flight, and the value\n * kept whenever a probe fails or reports 0 — never send more than a single\n * solve to a server whose real capacity is unknown.\n */\nconst FALLBACK_CONCURRENCY = 1;\n\n/**\n * How stale the compute server's child count may get before a solve triggers a\n * re-read. Short enough that a resized pool is picked up within a few solves,\n * long enough that a busy server is probed rarely — the probe rides on solve\n * completion, so an idle server is never polled at all.\n */\nconst CONCURRENCY_PROBE_INTERVAL_MS = 5 * 60 * 1000;\n\nexport interface ConcurrencyProbe {\n\treadonly maxConcurrent: number;\n\t/** One blocking read at connect, so the first solves already use the right cap. */\n\trefresh(): Promise<void>;\n\t/** Piggybacks on real traffic: at most once per window, and never on the request path. */\n\trefreshIfDue(scheduler: { setMaxConcurrent(n: number): void }): void;\n}\n\n/**\n * rhino.compute hands out children in strict rotation and never reports \"busy\", so nothing\n * downstream enforces a concurrency cap — send more than it has children and requests silently\n * double up on one. Rhino.Compute is the only source of truth for its own child count, so this\n * always probes rather than trusting an operator-supplied guess.\n *\n * The count is not static: children exit on their own under `-idlespan`, and `/launch-child` +\n * `/recycle-children` resize the pool at runtime. So the probe re-runs after solves rather than\n * only at connect.\n */\nexport function createConcurrencyProbe(\n\tclient: Pick<GrasshopperClient, 'serverStats'>,\n\tdebugLog: (message: string) => void\n): ConcurrencyProbe {\n\tlet maxConcurrent = FALLBACK_CONCURRENCY;\n\tlet lastProbeAt = 0;\n\tlet probing = false;\n\n\t/** `initialize: false` keeps this from spawning children on an idle server, so a cold one reports 0. */\n\tasync function refresh(scheduler?: { setMaxConcurrent(n: number): void }) {\n\t\tif (probing) return;\n\t\tprobing = true;\n\t\ttry {\n\t\t\tconst children = await Promise.resolve(\n\t\t\t\tclient.serverStats?.getActiveChildren({ initialize: false })\n\t\t\t).catch(() => null);\n\t\t\tlastProbeAt = Date.now();\n\t\t\tconst next = !children || children <= 0 ? FALLBACK_CONCURRENCY : children;\n\t\t\tif (next === maxConcurrent) return;\n\t\t\tdebugLog(\n\t\t\t\t`[Compute/client-cache] concurrency ${maxConcurrent} → ${next} (server child count)`\n\t\t\t);\n\t\t\tmaxConcurrent = next;\n\t\t\tscheduler?.setMaxConcurrent(next);\n\t\t} finally {\n\t\t\tprobing = false;\n\t\t}\n\t}\n\n\treturn {\n\t\tget maxConcurrent() {\n\t\t\treturn maxConcurrent;\n\t\t},\n\t\trefresh: () => refresh(),\n\t\trefreshIfDue(scheduler) {\n\t\t\tif (Date.now() - lastProbeAt >= CONCURRENCY_PROBE_INTERVAL_MS) void refresh(scheduler);\n\t\t}\n\t};\n}\n","import { Agent, fetch as undiciFetch } from 'undici';\n\n/** undici's default `headersTimeout` and `bodyTimeout`, which `timeoutMs` can't extend. */\nconst UNDICI_DEFAULT_LIMIT_MS = 300_000;\n\n/** Both off, not raised: the scheduler's `timeoutMs` stays the one deadline on a solve. */\nconst unlimited = new Agent({ headersTimeout: 0, bodyTimeout: 0 });\n\n// npm undici's own fetch, not the global one: a dispatcher has to match the undici that calls it,\n// and Node's bundled copy is a different version.\n//\n// A call with no signal has no deadline of its own (`getIO` is one), so it keeps undici's limit:\n// switching the timeouts off for it would let a hung server hang a page load forever.\nconst unlimitedFetch = ((input: string, init?: { signal?: AbortSignal }) =>\n\tundiciFetch(input, {\n\t\t...init,\n\t\tsignal: init?.signal ?? AbortSignal.timeout(UNDICI_DEFAULT_LIMIT_MS),\n\t\tdispatcher: unlimited\n\t})) as unknown as typeof fetch;\n\n/** The fetch for a solve deadline: `undefined` means the global one reaches it. */\nexport function fetchForDeadline(deadlineMs: number): typeof fetch | undefined {\n\treturn deadlineMs >= UNDICI_DEFAULT_LIMIT_MS ? unlimitedFetch : undefined;\n}\n","import type { ServerLimits } from '@selvajs/compute/grasshopper';\n\n/** Rhino.Compute's own default `--timeout`, for a server that can't report its own. */\nexport const DEFAULT_SOLVE_DEADLINE_MS = 100_000;\n\n/**\n * Ceiling on any solve deadline, whatever the server reports. Bounds how long a request, its\n * live-event token and a delegated session stay open; past an hour a solve belongs in a job.\n */\nexport const MAX_SOLVE_DEADLINE_MS = 60 * 60_000;\n\n/** Selva waits this much past the server's timeout, so Compute's own error arrives first. */\nconst DEADLINE_SLACK_MS = 5_000;\n\n/** What Selva holds a solve to on one compute server. */\nexport interface SolveLimits {\n\tsolveDeadlineMs: number;\n\t/** `null` when the server didn't say: its own 413 is then the only check. */\n\tmaxRequestBytes: number | null;\n\t/**\n\t * Send a pointer to the server's cached definition instead of the `.gh`. Only on a server that\n\t * answers a stale pointer with `definition_not_cached`: one that doesn't returns empty geometry.\n\t */\n\treuseDefinitionCache: boolean;\n\t/** The server's deployment (`ServerLimits.deploymentId`); another one may solve differently. */\n\tdeploymentId: string | null;\n}\n\nexport function solveLimitsFor(reported: ServerLimits | null): SolveLimits {\n\tif (!reported) {\n\t\treturn {\n\t\t\tsolveDeadlineMs: DEFAULT_SOLVE_DEADLINE_MS,\n\t\t\tmaxRequestBytes: null,\n\t\t\treuseDefinitionCache: false,\n\t\t\tdeploymentId: null\n\t\t};\n\t}\n\treturn {\n\t\tsolveDeadlineMs: Math.min(\n\t\t\treported.timeoutSeconds * 1000 + DEADLINE_SLACK_MS,\n\t\t\tMAX_SOLVE_DEADLINE_MS\n\t\t),\n\t\tmaxRequestBytes: reported.maxRequestBytes,\n\t\treuseDefinitionCache: reported.signalsStaleDefinition,\n\t\t// An older `@selvajs/compute` leaves it undefined.\n\t\tdeploymentId: reported.deploymentId ?? null\n\t};\n}\n\nexport function sameSolveLimits(a: SolveLimits, b: SolveLimits): boolean {\n\treturn (\n\t\ta.solveDeadlineMs === b.solveDeadlineMs &&\n\t\ta.maxRequestBytes === b.maxRequestBytes &&\n\t\ta.reuseDefinitionCache === b.reuseDefinitionCache &&\n\t\ta.deploymentId === b.deploymentId\n\t);\n}\n","import {\n\tGrasshopperClient,\n\ttype GrasshopperComputeConfig,\n\ttype SolveScheduler\n} from '@selvajs/compute/grasshopper';\nimport { ComputeError, enableDebugLogging, ErrorCodes } from '@selvajs/compute/core';\n\nimport { createConcurrencyProbe } from './concurrency-probe';\nimport { fetchForDeadline } from './long-fetch';\nimport {\n\tMAX_SOLVE_DEADLINE_MS,\n\tsameSolveLimits,\n\tsolveLimitsFor,\n\ttype SolveLimits\n} from './server-limits';\n\n// ============================================================================\n// Shared compute client cache\n// ============================================================================\n//\n// One warm `GrasshopperClient` (+ its `SolveScheduler`) per distinct compute\n// server, keyed by the server's `id`, never its URL — a rotated URL/apiKey\n// keeps the same key with stale connection details, so the config-write path\n// MUST call `evict(id)` when a server's config changes.\n//\n// Per-server LRU, not a single shared client: definitions can pin different\n// servers, so two definitions on two servers keep two warm clients, with\n// churn from one-off servers bounded by `maxWarmComputeServers`.\n//\n// Both hot paths share this cache — the solve endpoint uses the entry's\n// `scheduler`; the definition-viewer render path uses the entry's `client`\n// for `getIO` — so a definition that was just solved renders from the same\n// warm client instead of re-handshaking Rhino.Compute per page load.\n\n/** Minimal resolved-server shape the cache needs (a subset of the app's `ComputeServerConfig`). */\nexport interface ResolvedServer {\n\tid: string;\n\tserverUrl: string;\n\t/** Sent as the `RhinoComputeKey` header. */\n\tapiKey?: string;\n}\n\nexport interface CachedClient {\n\tclient: GrasshopperClient;\n\tscheduler: SolveScheduler;\n\t/** Read from the server when the client was built; a changed server rebuilds the client. */\n\tlimits: SolveLimits;\n}\n\n/**\n * Debug verbosity: `false` is silent, `true` gives concise cache/timing logs\n * (`onDebugLog`), `'verbose'` also enables full lib-level request/response\n * dumps (incl. geometry) — a superset of `true`, not an independent setting.\n */\nexport type ClientCacheDebug = boolean | 'verbose';\n\n/** Config injected by the consuming app, so this module stays env-agnostic and testable. */\nexport interface ClientCacheConfig {\n\tcachesolve: boolean;\n\t/**\n\t * Max solves that may wait in the FIFO queue, excluding in-flight solves\n\t * (capped at `maxConcurrent`, itself driven by the server's probed child\n\t * count). `0` = unbounded (`ComputeLimits.computeMaxQueueDepth`); a full\n\t * queue rejects new solves with `QUEUE_FULL`.\n\t */\n\tmaxQueueDepth: number;\n\t/**\n\t * Max ms a solve may sit queued before executing; `0` = no deadline\n\t * (`ComputeLimits.computeQueueWaitMs`). Too long a wait rejects with\n\t * `QUEUE_TIMEOUT`.\n\t */\n\tqueueWaitMs: number;\n\t/**\n\t * Byte budget for this client's in-process solve cache. Applies per warm\n\t * client — worst-case heap is this × `maxWarmComputeServers`. `0` disables\n\t * the cache (`ComputeLimits.computeSolveCacheBytes`, env `COMPUTE_SOLVE_CACHE_MB`).\n\t */\n\tresponseCacheMaxBytes: number;\n\t/**\n\t * Largest compute response body read; past it the download is cancelled (`too_large`).\n\t * Default: no limit.\n\t */\n\tmaxResponseBytes?: number;\n\tdebug: ClientCacheDebug;\n\t/** Max distinct warm compute servers before the LRU evicts the oldest. */\n\tmaxWarmComputeServers?: number;\n\t/** Sink for the debug lines. `onDebugLog` only fires when `debug` is not `false`. */\n\tonDebugLog?: (message: string) => void;\n}\n\nexport interface ClientCache {\n\t/**\n\t * Get (or create) the warm client + scheduler for a resolved compute server, keyed by its `id`.\n\t * With the solve cache on, a warm client last checked over 30 s ago first re-reads the server's\n\t * `/limits` (at most 1 s), and one that reports another deployment is rebuilt with an empty cache.\n\t *\n\t * @throws {ComputeError} `ABORTED` when `evict` or `disposeAll` overtakes the build.\n\t */\n\tgetClient(server: ResolvedServer, opts?: { definitionGuid?: string }): Promise<CachedClient>;\n\t/**\n\t * Drops the warm client for `id`, so the next request rebuilds, and disposes it once idle. A\n\t * build in flight for `id` is abandoned.\n\t */\n\tevict(id: string): void;\n\t/** Solve-cache counters summed across every warm client. Counters die with the client that owns them, so totals can fall over time. */\n\tsolveCacheStats(): SolveCacheStats;\n\t/**\n\t * Drop every retained solve result, keeping the warm clients (and their\n\t * connections and server-side definition pointers) intact. Nothing expires\n\t * these on its own — the byte budget is the only other pressure — so this is\n\t * the operator's release valve when a definition's inputs no longer describe\n\t * its output, e.g. it reads an external source that has since changed.\n\t */\n\tclearSolveCaches(): void;\n\t/** Dispose every warm client now, running solves included. Test seam / shutdown hook. */\n\tdisposeAll(): void;\n}\n\nexport interface SolveCacheStats {\n\twarmClients: number;\n\tentries: number;\n\tbytes: number;\n\thits: number;\n\tmisses: number;\n\t/** Entries dropped under size/byte pressure (not replacement or manual clears). */\n\tevictions: number;\n}\n\nconst DEFAULT_MAX_WARM_COMPUTE_SERVERS = 16;\n\n/**\n * How often a warm client re-reads its server's limits, riding on solve completion like the\n * concurrency probe. An operator who changes `--timeout` sees it within this window.\n */\nconst LIMITS_RECHECK_MS = 5 * 60 * 1000;\n\n/**\n * How stale the server's `deploymentId` may be before a warm client whose solve cache could\n * answer is handed out again. A cache hit never reaches the server, so riding on solve completion\n * would let a redeployed plugin's old results be served until the next miss.\n */\nconst DEPLOYMENT_RECHECK_MS = 30 * 1000;\n\n/**\n * Longest a request waits on that check. The proxy answers `/limits` itself, in milliseconds; an\n * unreachable server must not hold a request that the cache could answer. The check finishes\n * behind it and retires the client if the deployment changed.\n */\nconst DEPLOYMENT_CHECK_WAIT_MS = 1000;\n\n/**\n * How long a failed build is remembered before the next `getClient` retries the\n * handshake. Without this, every solve against a down server pays the full probe\n * ladder again, so a user clicking Solve twice waits twice.\n *\n * Short on purpose: this is a convenience for the rapid-retry case, not a\n * circuit breaker. An operator who starts the compute VM should not have to wait\n * out a long penalty window before the app notices.\n */\nconst FAILED_BUILD_TTL_MS = 5000;\n\n/**\n * A build failure worth replaying to the next caller, with the time it happened\n * so {@link FAILED_BUILD_TTL_MS} can expire it.\n */\ninterface FailedBuild {\n\terror: unknown;\n\tat: number;\n}\n\ninterface PendingBuild {\n\tpromise: Promise<CachedClient>;\n\t/** Set when the connection details this build started from stopped being true. */\n\ttoken: { stale: boolean };\n}\n\ninterface SettledSolve {\n\tfromCache?: boolean;\n\tdurationMs: number;\n\tdefinitionReuploaded?: boolean;\n\tresponse?: { errors?: unknown };\n}\n\nfunction describeSettledSolve(result: SettledSolve): string[] {\n\tconst verdict = result.fromCache\n\t\t? 'HIT — served from cache (no compute call)'\n\t\t: 'miss — went to Rhino.Compute';\n\tconst lines = [`[Compute/selva-cache] ${verdict} (${Math.round(result.durationMs)}ms)`];\n\tif (result.fromCache) return lines;\n\n\tlines.push(\n\t\tresult.definitionReuploaded === true\n\t\t\t? '[Compute/def-cache] miss — definition RE-UPLOADED (pointer was cold/stale)'\n\t\t\t: result.definitionReuploaded === false\n\t\t\t\t? '[Compute/def-cache] HIT — definition reused from server cache (no upload)'\n\t\t\t\t: '[Compute/def-cache] n/a — reuse disabled or non-reusable definition'\n\t);\n\tconst errors = result.response?.errors;\n\tif (Array.isArray(errors) && errors.length > 0) {\n\t\t// A count: solver text can quote user input, and logs are out of erasure's reach.\n\t\tlines.push(`[Compute/rhino-cache] ${errors.length} GH error(s); not cached by Rhino.Compute.`);\n\t}\n\treturn lines;\n}\n\nexport function createClientCache(config: ClientCacheConfig): ClientCache {\n\tconst maxWarmComputeServers = config.maxWarmComputeServers ?? DEFAULT_MAX_WARM_COMPUTE_SERVERS;\n\tconst cacheEnabled = config.responseCacheMaxBytes > 0;\n\tconst cache = new Map<string, CachedClient>();\n\t// Builds in flight, keyed like `cache`. Concurrent `getClient` calls for the\n\t// same id must share ONE build: without this, both miss, both handshake, and\n\t// the loser's entry is overwritten in the map without ever being disposed.\n\tconst pending = new Map<string, PendingBuild>();\n\t// Recent build failures, keyed like `cache`. Cleared by `evict` so a config\n\t// fix takes effect immediately rather than waiting out the TTL.\n\tconst failed = new Map<string, FailedBuild>();\n\t// Out of `cache`, waiting for their running solves to finish.\n\tconst draining = new Set<CachedClient>();\n\t// Per warm client: resolves false once the server reports another deployment (see\n\t// DEPLOYMENT_RECHECK_MS), true while it may still serve from its solve cache.\n\tconst deploymentChecks = new WeakMap<CachedClient, () => Promise<boolean>>();\n\n\tif (config.debug === 'verbose') enableDebugLogging();\n\n\tconst debugLog = (message: string) => {\n\t\tif (config.debug) config.onDebugLog?.(message);\n\t};\n\n\tfunction isIdle({ scheduler }: CachedClient) {\n\t\treturn scheduler.inFlightCount === 0 && scheduler.queueDepth === 0;\n\t}\n\n\t/** Disposing a busy scheduler aborts its solves, so a config write would fail them all. */\n\tfunction retire(entry: CachedClient) {\n\t\tif (isIdle(entry)) entry.scheduler.dispose();\n\t\telse draining.add(entry);\n\t}\n\n\tfunction finishDraining(entry: CachedClient) {\n\t\tif (!draining.has(entry) || !isIdle(entry)) return;\n\t\tdraining.delete(entry);\n\t\tentry.scheduler.dispose();\n\t}\n\n\tasync function build(server: ResolvedServer, definitionGuid?: string): Promise<CachedClient> {\n\t\tconst debugVerbose = config.debug === 'verbose';\n\t\t// The deadline comes from the server, read only once the client exists: pick the fetch\n\t\t// for the longest one. Calls without a signal keep undici's limits either way.\n\t\tconst longFetch = fetchForDeadline(MAX_SOLVE_DEADLINE_MS);\n\n\t\tconst clientConfig: GrasshopperComputeConfig = {\n\t\t\tserverUrl: server.serverUrl,\n\t\t\tapiKey: server.apiKey,\n\t\t\t// Lib-level debug is the verbose one; keep it on the verbose flag so the\n\t\t\t// concise cache logs can run without the noise.\n\t\t\tdebug: debugVerbose,\n\t\t\tcachesolve: config.cachesolve,\n\t\t\tmaxResponseBytes: config.maxResponseBytes,\n\t\t\t...(longFetch && { fetch: longFetch })\n\t\t};\n\n\t\tif (config.debug) {\n\t\t\tclientConfig.onServerTiming = (t) => {\n\t\t\t\tif (t.cacheHit) {\n\t\t\t\t\tdebugLog(\n\t\t\t\t\t\t'[Compute/rhino-cache] solve-cache (cachesolve): HIT (cached result, solve skipped)'\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst decode = t.decode ?? 0;\n\t\t\t\tconst solve = t.solve ?? 0;\n\t\t\t\tconst encode = t.encode ?? 0;\n\t\t\t\t// Heuristic threshold: a genuine parse is many ms; a reused definition is\n\t\t\t\t// sub-millisecond. Only meaningful relative to the first (cold) solve.\n\t\t\t\tconst defCache =\n\t\t\t\t\tdecode <= 1 ? 'HIT (definition reused, no re-upload/parse)' : 'miss (full parse)';\n\t\t\t\tconst solveCache = config.cachesolve ? 'miss (solved fresh)' : 'off';\n\t\t\t\tconst ran = t.recomputed ? ` | ran ${t.recomputed.ran}/${t.recomputed.total} objects` : '';\n\t\t\t\tdebugLog(\n\t\t\t\t\t`[Compute/rhino-cache] decode=${decode}ms solve=${solve}ms encode=${encode}ms ` +\n\t\t\t\t\t\t`| definition-cache: ${defCache} | solve-cache (cachesolve): ${solveCache}${ran}`\n\t\t\t\t);\n\t\t\t};\n\t\t}\n\n\t\t// Baked at client-create time because the scheduler has no per-request\n\t\t// header hook.\n\t\tif (definitionGuid) {\n\t\t\tclientConfig.headers = { 'X-Selva-Definition': definitionGuid };\n\t\t}\n\n\t\tconst client = await GrasshopperClient.create(clientConfig);\n\n\t\tconst probe = createConcurrencyProbe(client, debugLog);\n\t\tconst readReported = async () =>\n\t\t\t(await Promise.resolve(client.serverStats?.getLimits?.()).catch(() => null)) ?? null;\n\t\tconst [reported] = await Promise.all([readReported(), probe.refresh()]);\n\t\tconst limits = solveLimitsFor(reported);\n\t\tdebugLog(\n\t\t\t`[Compute/client-cache] server ${server.id}: deadline ${limits.solveDeadlineMs}ms, ` +\n\t\t\t\t`request cap ${limits.maxRequestBytes ?? 'not reported'}, pointer reuse ${limits.reuseDefinitionCache}`\n\t\t);\n\t\tlet limitsCheckedAt = Date.now();\n\t\tlet recheck: Promise<boolean> | null = null;\n\t\t/** Re-reads the limits; true when they changed, which retires this client. */\n\t\tconst recheckLimits = () =>\n\t\t\t(recheck ??= readReported()\n\t\t\t\t.then((next) => {\n\t\t\t\t\tlimitsCheckedAt = Date.now();\n\t\t\t\t\t// Unanswered by a server that did answer: down or restarting, not reconfigured.\n\t\t\t\t\t// Its defaults would switch pointer reuse off until the next recheck.\n\t\t\t\t\tif (next === null && reported !== null) return false;\n\t\t\t\t\tif (sameSolveLimits(solveLimitsFor(next), limits)) return false;\n\t\t\t\t\tdebugLog(`[Compute/client-cache] server ${server.id} limits changed — rebuilding`);\n\t\t\t\t\tif (cache.get(server.id) === entry) evictEntry(server.id);\n\t\t\t\t\treturn true;\n\t\t\t\t})\n\t\t\t\t.finally(() => {\n\t\t\t\t\trecheck = null;\n\t\t\t\t}));\n\t\tconst recheckLimitsIfDue = () => {\n\t\t\tif (Date.now() - limitsCheckedAt >= LIMITS_RECHECK_MS) void recheckLimits();\n\t\t};\n\n\t\tconst scheduler = client.createScheduler({\n\t\t\tmode: 'queue',\n\t\t\t// Queue mode defaults maxConcurrent to 1, serializing every solve on\n\t\t\t// this server through a single slot while the compute VM's other\n\t\t\t// compute.geometry children idle — always pass it explicitly.\n\t\t\tmaxConcurrent: probe.maxConcurrent,\n\t\t\t// The scheduler treats undefined as unbounded/no-deadline, so map our\n\t\t\t// `0`-means-off convention to undefined rather than passing 0 (which\n\t\t\t// would reject EVERY queued solve).\n\t\t\tmaxQueueDepth: config.maxQueueDepth || undefined,\n\t\t\tqueueWaitMs: config.queueWaitMs || undefined,\n\t\t\ttimeoutMs: limits.solveDeadlineMs,\n\t\t\t// The byte budget is the only eviction pressure. A solve is a pure function of\n\t\t\t// (definition, inputs) on one deployment: expiring a result only forces a paid\n\t\t\t// re-solve of the same answer. A redeployed plugin is another deployment, so the\n\t\t\t// client is retired with its cache (`deploymentChecks`).\n\t\t\tcache: cacheEnabled ? { maxBytes: config.responseCacheMaxBytes } : false,\n\t\t\treuseServerDefinitionCache: limits.reuseDefinitionCache,\n\t\t\tonSettle: (_ctx, result) => {\n\t\t\t\t// Deferred: this hook fires before the scheduler drops the solve from its in-flight set.\n\t\t\t\tqueueMicrotask(() => finishDraining(entry));\n\t\t\t\tif (result.status !== 'success') return;\n\t\t\t\t// A cache hit means we never reached the server, so it says nothing about the pool.\n\t\t\t\t// `scheduler` is safe despite being referenced from inside its own initializer:\n\t\t\t\t// onSettle only ever fires during a solve, long after binding.\n\t\t\t\tif (result.fromCache !== true) {\n\t\t\t\t\tprobe.refreshIfDue(scheduler);\n\t\t\t\t\trecheckLimitsIfDue();\n\t\t\t\t}\n\t\t\t\tif (config.debug) describeSettledSolve(result).forEach(debugLog);\n\t\t\t}\n\t\t});\n\t\tconst entry: CachedClient = { client, scheduler, limits };\n\t\tdeploymentChecks.set(entry, async () => {\n\t\t\tif (Date.now() - limitsCheckedAt < DEPLOYMENT_RECHECK_MS) return true;\n\t\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\t\t\tconst waited = new Promise<false>((resolve) => {\n\t\t\t\ttimer = setTimeout(() => resolve(false), DEPLOYMENT_CHECK_WAIT_MS);\n\t\t\t});\n\t\t\ttry {\n\t\t\t\treturn !(await Promise.race([recheckLimits(), waited]));\n\t\t\t} finally {\n\t\t\t\tclearTimeout(timer);\n\t\t\t}\n\t\t});\n\t\treturn entry;\n\t}\n\n\tfunction evictEntry(id: string) {\n\t\tconst entry = cache.get(id);\n\t\tif (!entry) return;\n\t\tcache.delete(id);\n\t\tretire(entry);\n\t}\n\n\tconst self: ClientCache = {\n\t\tasync getClient(server, opts): Promise<CachedClient> {\n\t\t\tconst key = server.id;\n\t\t\tconst existing = cache.get(key);\n\t\t\tif (existing) {\n\t\t\t\t// Refresh LRU position.\n\t\t\t\tcache.delete(key);\n\t\t\t\tcache.set(key, existing);\n\t\t\t\t// Only a client whose cache could answer waits on its server's deployment.\n\t\t\t\tif (!cacheEnabled || (await deploymentChecks.get(existing)?.()) !== false) return existing;\n\t\t\t\t// Retired by the check: whatever now stands for `key` serves, or a fresh build.\n\t\t\t\treturn self.getClient(server, opts);\n\t\t\t}\n\n\t\t\tconst inFlight = pending.get(key);\n\t\t\tif (inFlight) return inFlight.promise;\n\n\t\t\t// Replay a recent failure instead of re-running the probe ladder. The\n\t\t\t// original error is rethrown verbatim so the caller still sees the real\n\t\t\t// cause (refused / unauthorized / timeout), not a generic cache miss.\n\t\t\tconst recentFailure = failed.get(key);\n\t\t\tif (recentFailure) {\n\t\t\t\tif (Date.now() - recentFailure.at < FAILED_BUILD_TTL_MS) {\n\t\t\t\t\tdebugLog(\n\t\t\t\t\t\t`[Compute/client-cache] server ${key} failed to build <${FAILED_BUILD_TTL_MS}ms ago — replaying error`\n\t\t\t\t\t);\n\t\t\t\t\tthrow recentFailure.error;\n\t\t\t\t}\n\t\t\t\tfailed.delete(key);\n\t\t\t}\n\n\t\t\t// A build is a Rhino.Compute handshake — expensive, and repeated builds for\n\t\t\t// the same id mean churn (LRU thrash or config-rotation evictions).\n\t\t\tdebugLog(`[Compute/client-cache] miss — building warm client for server ${key}`);\n\t\t\tconst token = { stale: false };\n\t\t\tconst promise = (async () => {\n\t\t\t\tlet entry: CachedClient;\n\t\t\t\ttry {\n\t\t\t\t\tentry = await build(server, opts?.definitionGuid);\n\t\t\t\t} catch (err) {\n\t\t\t\t\tif (!token.stale) failed.set(key, { error: err, at: Date.now() });\n\t\t\t\t\tthrow err;\n\t\t\t\t}\n\n\t\t\t\t// Installing it would carry the old URL or key past the eviction, with nothing left to\n\t\t\t\t// evict it again. Its caller holds a client built from those same details, so it fails too.\n\t\t\t\tif (token.stale) {\n\t\t\t\t\tentry.scheduler.dispose();\n\t\t\t\t\tthrow new ComputeError(\n\t\t\t\t\t\t`Compute server ${key} changed while connecting.`,\n\t\t\t\t\t\tErrorCodes.ABORTED\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tif (cache.size >= maxWarmComputeServers) {\n\t\t\t\t\tconst oldestKey = cache.keys().next().value;\n\t\t\t\t\tif (oldestKey !== undefined) {\n\t\t\t\t\t\tdebugLog(\n\t\t\t\t\t\t\t`[Compute/client-cache] LRU evicted warm client for server ${oldestKey} (cap ${maxWarmComputeServers})`\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst oldest = cache.get(oldestKey);\n\t\t\t\t\t\tcache.delete(oldestKey);\n\t\t\t\t\t\tif (oldest) retire(oldest);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tcache.set(key, entry);\n\t\t\t\treturn entry;\n\t\t\t})().finally(() => {\n\t\t\t\tif (pending.get(key)?.token === token) pending.delete(key);\n\t\t\t});\n\t\t\tpending.set(key, { promise, token });\n\t\t\treturn promise;\n\t\t},\n\n\t\tevict(id): void {\n\t\t\t// Drop any remembered failure too: `evict` means the connection details\n\t\t\t// changed, so the reason the last build failed no longer applies.\n\t\t\tfailed.delete(id);\n\t\t\tconst build = pending.get(id);\n\t\t\tif (build) {\n\t\t\t\tbuild.token.stale = true;\n\t\t\t\tpending.delete(id);\n\t\t\t}\n\t\t\tif (cache.has(id)) {\n\t\t\t\tdebugLog(`[Compute/client-cache] evicted warm client for server ${id} (config change)`);\n\t\t\t\tevictEntry(id);\n\t\t\t}\n\t\t},\n\n\t\tsolveCacheStats(): SolveCacheStats {\n\t\t\tconst total: SolveCacheStats = {\n\t\t\t\twarmClients: cache.size,\n\t\t\t\tentries: 0,\n\t\t\t\tbytes: 0,\n\t\t\t\thits: 0,\n\t\t\t\tmisses: 0,\n\t\t\t\tevictions: 0\n\t\t\t};\n\t\t\tfor (const entry of cache.values()) {\n\t\t\t\tconst s = entry.scheduler.cacheStats();\n\t\t\t\ttotal.entries += s.entries;\n\t\t\t\ttotal.bytes += s.bytes;\n\t\t\t\ttotal.hits += s.hits;\n\t\t\t\ttotal.misses += s.misses;\n\t\t\t\ttotal.evictions += s.evictions;\n\t\t\t}\n\t\t\treturn total;\n\t\t},\n\n\t\tclearSolveCaches(): void {\n\t\t\tdebugLog(`[Compute/client-cache] cleared solve caches on ${cache.size} warm client(s)`);\n\t\t\tfor (const entry of cache.values()) entry.scheduler.clearCache();\n\t\t},\n\n\t\tdisposeAll(): void {\n\t\t\tdebugLog(`[Compute/client-cache] disposing all ${cache.size} warm client(s)`);\n\t\t\tfor (const entry of [...cache.values(), ...draining]) entry.scheduler.dispose();\n\t\t\tfor (const build of pending.values()) build.token.stale = true;\n\t\t\tpending.clear();\n\t\t\tcache.clear();\n\t\t\tdraining.clear();\n\t\t\tfailed.clear();\n\t\t}\n\t};\n\treturn self;\n}\n","/**\n * In-process cache of `.gh` definition bytes, keyed by immutable **version id**.\n *\n * Turns an eager `storage.get(fileKey)` on every solve request into a lazy\n * `DefinitionRef.load()`: the scheduler calls `load()` only when an upload is\n * unavoidable (a pointer-known re-solve never touches storage or this cache).\n *\n * The key MUST be the version id, never a `fileKey` — a delete-latest-then-\n * reupload can reuse a `fileKey` for different content, which would silently\n * serve one version's bytes for another with no diagnostic. Callers pass\n * `version.id`, and the returned `DefinitionRef.key` is that same id, so the\n * scheduler's own result/pointer cache keys on the same immutable identity.\n *\n * Eviction is LRU by total byte budget, not entry count — definitions range\n * from tens of KB to hundreds of MB, so a count cap would either pin\n * gigabytes or evict uselessly. `COMPUTE_DEFINITION_CACHE_MB` sets the\n * budget (see `createDefinitionByteCache`). No TTL: version ids are\n * immutable, so a cached entry can never go stale. An entry larger than the\n * whole budget is served but never retained.\n */\n\nimport { raceAbort } from '../shared/abort.js';\n\n/**\n * Per-`getOrLoad` outcome, observable after the scheduler has (or hasn't)\n * called `load()` — lets the solve pipeline emit a `def_bytes` Server-Timing\n * verdict (`skipped` / `hit` / `miss`).\n */\nexport interface ByteRefOutcome {\n\tloaded: boolean;\n\tfromCache: boolean;\n}\n\n/** A definition reference shaped for `@selvajs/compute`'s `DefinitionRef`. */\nexport interface ByteCacheRef {\n\tkey: string;\n\tload: (signal?: AbortSignal) => Promise<Uint8Array>;\n\toutcome: ByteRefOutcome;\n}\n\nexport interface ByteCacheStats {\n\thits: number;\n\tmisses: number;\n\tevictions: number;\n\tentries: number;\n\tbytes: number;\n}\n\nexport interface DefinitionByteCache {\n\t/** `load` gets a signal that fires once every caller waiting on this read has gone. */\n\tgetOrLoad(versionId: string, load: (signal: AbortSignal) => Promise<Uint8Array>): ByteCacheRef;\n\tstats(): ByteCacheStats;\n\tclear(): void;\n}\n\n/** A `ByteCacheRef` carries `outcome`, which a plain `DefinitionRef` never has. */\nexport function isByteCacheRef(source: unknown): source is ByteCacheRef {\n\treturn typeof source === 'object' && source !== null && 'outcome' in source;\n}\n\n/** One storage read, shared by every caller that misses while it runs. */\ninterface Loading {\n\tpromise: Promise<Uint8Array>;\n\tcontroller: AbortController;\n\twaiters: number;\n}\n\n/**\n * @param maxBytes total retained-byte budget; `0` (or negative) disables caching\n * entirely — every `load()` calls the loader and nothing is retained.\n */\nexport function createDefinitionByteCache(maxBytes: number): DefinitionByteCache {\n\tconst budget = Math.max(0, Math.floor(maxBytes));\n\t// Map iteration order is insertion order, so it doubles as LRU order:\n\t// re-inserting a key on access moves it to the most-recently-used end.\n\tconst cache = new Map<string, Uint8Array>();\n\tconst loading = new Map<string, Loading>();\n\tlet retainedBytes = 0;\n\tconst stats = { hits: 0, misses: 0, evictions: 0 };\n\n\tfunction evictToFit(): void {\n\t\tfor (const [key, bytes] of cache) {\n\t\t\tif (retainedBytes <= budget) break;\n\t\t\tcache.delete(key);\n\t\t\tretainedBytes -= bytes.byteLength;\n\t\t\tstats.evictions += 1;\n\t\t}\n\t}\n\n\tfunction store(key: string, bytes: Uint8Array): void {\n\t\tif (budget === 0) return;\n\t\t// Caching an entry bigger than the budget would evict everything\n\t\t// (including itself) on the next evictToFit — serve it, cache nothing.\n\t\tif (bytes.byteLength > budget) return;\n\t\tcache.set(key, bytes);\n\t\tretainedBytes += bytes.byteLength;\n\t\tevictToFit();\n\t}\n\n\tfunction startLoad(key: string, load: (signal: AbortSignal) => Promise<Uint8Array>): Loading {\n\t\tconst controller = new AbortController();\n\t\tconst entry: Loading = {\n\t\t\tcontroller,\n\t\t\twaiters: 0,\n\t\t\tpromise: (async () => {\n\t\t\t\tconst bytes = await load(controller.signal);\n\t\t\t\tstore(key, bytes);\n\t\t\t\treturn bytes;\n\t\t\t})().finally(() => {\n\t\t\t\tif (loading.get(key) === entry) loading.delete(key);\n\t\t\t})\n\t\t};\n\t\tloading.set(key, entry);\n\t\treturn entry;\n\t}\n\n\t/**\n\t * A caller gone stops waiting, but the read runs on for whoever else is on it. A caller with no\n\t * signal can't leave, so it stays counted and the read is never aborted under it.\n\t */\n\tasync function wait(entry: Loading, signal?: AbortSignal): Promise<Uint8Array> {\n\t\tentry.waiters += 1;\n\t\tif (!signal) return entry.promise;\n\t\ttry {\n\t\t\treturn await raceAbort(entry.promise, signal);\n\t\t} finally {\n\t\t\tentry.waiters -= 1;\n\t\t\tif (entry.waiters === 0 && signal.aborted) entry.controller.abort();\n\t\t}\n\t}\n\n\treturn {\n\t\tgetOrLoad(versionId, load) {\n\t\t\tconst outcome: ByteRefOutcome = { loaded: false, fromCache: false };\n\t\t\treturn {\n\t\t\t\tkey: versionId,\n\t\t\t\toutcome,\n\t\t\t\tload: async function read(signal): Promise<Uint8Array> {\n\t\t\t\t\toutcome.loaded = true;\n\t\t\t\t\tconst hit = cache.get(versionId);\n\t\t\t\t\tif (hit) {\n\t\t\t\t\t\tcache.delete(versionId);\n\t\t\t\t\t\tcache.set(versionId, hit);\n\t\t\t\t\t\tstats.hits += 1;\n\t\t\t\t\t\toutcome.fromCache = true;\n\t\t\t\t\t\treturn hit;\n\t\t\t\t\t}\n\t\t\t\t\tconst running = loading.get(versionId);\n\t\t\t\t\tif (running?.controller.signal.aborted) {\n\t\t\t\t\t\t// Abandoned, and a store that ignores its signal may never finish. Waiting it out\n\t\t\t\t\t\t// rather than starting a second read keeps a hung store to one read per version.\n\t\t\t\t\t\tconst woundDown = running.promise.then(\n\t\t\t\t\t\t\t() => undefined,\n\t\t\t\t\t\t\t() => undefined\n\t\t\t\t\t\t);\n\t\t\t\t\t\tawait (signal ? raceAbort(woundDown, signal) : woundDown);\n\t\t\t\t\t\treturn read(signal);\n\t\t\t\t\t}\n\t\t\t\t\tif (running) {\n\t\t\t\t\t\tstats.hits += 1;\n\t\t\t\t\t\toutcome.fromCache = true;\n\t\t\t\t\t\treturn wait(running, signal);\n\t\t\t\t\t}\n\t\t\t\t\tstats.misses += 1;\n\t\t\t\t\treturn wait(startLoad(versionId, load), signal);\n\t\t\t\t}\n\t\t\t};\n\t\t},\n\t\tstats() {\n\t\t\treturn {\n\t\t\t\thits: stats.hits,\n\t\t\t\tmisses: stats.misses,\n\t\t\t\tevictions: stats.evictions,\n\t\t\t\tentries: cache.size,\n\t\t\t\tbytes: retainedBytes\n\t\t\t};\n\t\t},\n\t\tclear() {\n\t\t\tcache.clear();\n\t\t\tretainedBytes = 0;\n\t\t}\n\t};\n}\n","/**\n * Turns one solve's response into each caller's wire form. Runs after single-flight, not inside\n * it: coalesced callers can want different encodings, and each reports its own timing.\n */\n\nimport { gzip } from 'node:zlib';\nimport { promisify } from 'node:util';\nimport type { ServerTiming } from '@selvajs/compute/core';\nimport type { GrasshopperComputeResponse } from '@selvajs/compute/grasshopper';\nimport type { CacheVerdict, DefBytesVerdict } from './server-timing.js';\n\n// gzipSync blocks the event loop for multi-MB bodies; promisify(gzip) doesn't.\nconst gzipAsync = promisify(gzip);\n\n/**\n * Display output is base64 of already-deflated bytes, where higher levels find nothing more:\n * level 1 lands within 1% of the default's size, in up to a fifth of the time.\n */\nconst GZIP_LEVEL = 1;\n\n/** Below this gzip isn't worth the CPU for the wire savings. */\nconst GZIP_MIN_BYTES = 1024;\n\n// ============================================================================\n// Wire contract version\n// ============================================================================\n\n/** Compute-response contract version. Bump (and document the change) whenever the envelope's shape changes in a way a consumer could observe. */\nexport const COMPUTE_CONTRACT_VERSION = 1 as const;\nexport const COMPUTE_VERSION_HEADER = 'X-Selva-Compute-Version';\n\n// ============================================================================\n// Envelope\n// ============================================================================\n\n/** What the `Server-Timing` header carries for one caller, as data: read this, don't parse that. */\nexport interface SolvePhaseMetrics {\n\tdefLoadMs: number;\n\ttreeBuildMs: number;\n\tsolveMs: number;\n\tserializeMs: number;\n\tgzipMs: number;\n\tserverTotalMs: number;\n\tserializedBytes: number;\n\t/** Null when compression was skipped. */\n\tcompressedBytes: number | null;\n\t/** The compute server's own timing; null when it reports none. */\n\trhino: ServerTiming | null;\n\tsettle: CacheVerdict;\n\t/** Absent for raw-bytes solves. */\n\tdefBytes?: DefBytesVerdict;\n}\n\n/** A ready-to-send response: body + headers + the solve result + phase metrics. */\nexport interface SolveEnvelope {\n\t/** A gzip `Uint8Array` when `encoding === 'gzip'`, else the JSON string. */\n\tbody: string | Uint8Array;\n\tencoding?: 'gzip';\n\theaders: Record<string, string>;\n\tresult: GrasshopperComputeResponse;\n\tmetrics: SolvePhaseMetrics;\n}\n\nexport interface EncodedBody {\n\tbody: string | Uint8Array;\n\tencoding?: 'gzip';\n\tserializedBytes: number;\n\tserializeMs: number;\n\tgzipMs: number;\n}\n\nexport type TooLarge = 'too_large';\n\nexport function buildEnvelope(\n\tencoded: EncodedBody,\n\tresult: GrasshopperComputeResponse,\n\tserverTiming: string,\n\tmetrics: SolvePhaseMetrics\n): SolveEnvelope {\n\tconst headers: Record<string, string> = {\n\t\t'Content-Type': 'application/json',\n\t\t'Server-Timing': serverTiming,\n\t\t[COMPUTE_VERSION_HEADER]: String(COMPUTE_CONTRACT_VERSION),\n\t\tVary: 'Accept-Encoding'\n\t};\n\tconst { body, encoding } = encoded;\n\tif (encoding === 'gzip') {\n\t\t// Content-Encoding tells a downstream proxy the body is already compressed,\n\t\t// so it won't gzip it again.\n\t\theaders['Content-Encoding'] = 'gzip';\n\t}\n\theaders['Content-Length'] = String(\n\t\ttypeof body === 'string' ? Buffer.byteLength(body) : body.byteLength\n\t);\n\treturn { body, ...(encoding ? { encoding } : {}), headers, result, metrics };\n}\n\n// ============================================================================\n// Encoding\n// ============================================================================\n\n/** Whether an `Accept-Encoding` header takes gzip. `gzip;q=0` is a client refusing it. */\nexport function acceptsGzip(acceptEncoding: string): boolean {\n\treturn acceptEncoding.split(',').some((part) => {\n\t\tconst [coding, ...params] = part.split(';').map((s) => s.trim().toLowerCase());\n\t\tif (coding !== 'gzip') return false;\n\t\tconst q = params.find((p) => p.startsWith('q='));\n\t\treturn q === undefined || Number(q.slice(2)) > 0;\n\t});\n}\n\ninterface GzippedBody {\n\tbody: Uint8Array;\n\tserializedBytes: number;\n}\n\n/**\n * A scheduler cache hit returns the same response object, so a repeat solve reuses its bytes rather\n * than paying to serialize and compress them again. Entries live exactly as long as the response.\n * Only the gzip form is kept: browsers always take it, and the JSON string would double the memory.\n * Holds the promise, so callers arriving during the compression share it.\n */\nconst gzippedBodies = new WeakMap<object, Promise<GzippedBody | TooLarge>>();\n\nfunction gzipOnce(response: object, json: string, serializedBytes: number) {\n\tconst pending = gzipAsync(Buffer.from(json), { level: GZIP_LEVEL }).then((body) => ({\n\t\tbody,\n\t\tserializedBytes\n\t}));\n\tgzippedBodies.set(response, pending);\n\t// A failed compression must not stay memoized while the response is cached.\n\tpending.catch(() => gzippedBodies.delete(response));\n\treturn pending;\n}\n\nexport interface ResponseEncoder {\n\t/** Buffered, not streamed, so Content-Length is known upfront and a connection cut mid-transfer fails hard instead of silently truncating the JSON. */\n\tencode(acceptEncoding: string): Promise<EncodedBody | TooLarge>;\n}\n\n/**\n * One per solve. The JSON is kept here rather than beside the gzip body: it only has to outlive the\n * callers of this flight, who would otherwise each stringify the same response and hold a copy.\n */\nexport function createResponseEncoder(response: object, maxBytes: number): ResponseEncoder {\n\tlet serialized: { json: string; bytes: number } | TooLarge | undefined;\n\n\tfunction serialize() {\n\t\tif (serialized !== undefined) return serialized;\n\t\ttry {\n\t\t\tconst json = JSON.stringify(response);\n\t\t\tconst bytes = Buffer.byteLength(json);\n\t\t\tserialized = bytes > maxBytes ? 'too_large' : { json, bytes };\n\t\t} catch (err) {\n\t\t\t// V8 caps a string at ~512 MB; an oversized `file` output trips a RangeError\n\t\t\t// here. Treat it the same as the byte cap.\n\t\t\tif (!(err instanceof RangeError)) throw err;\n\t\t\tserialized = 'too_large';\n\t\t}\n\t\treturn serialized;\n\t}\n\n\treturn {\n\t\tasync encode(acceptEncoding) {\n\t\t\tconst wantsGzip = acceptsGzip(acceptEncoding);\n\t\t\tlet gzipped = wantsGzip ? gzippedBodies.get(response) : undefined;\n\t\t\tlet serializeMs = 0;\n\n\t\t\tif (!gzipped) {\n\t\t\t\tconst serializeStart = performance.now();\n\t\t\t\tconst plain = serialize();\n\t\t\t\tserializeMs = performance.now() - serializeStart;\n\t\t\t\tif (plain === 'too_large') {\n\t\t\t\t\tif (wantsGzip) gzippedBodies.set(response, Promise.resolve('too_large'));\n\t\t\t\t\treturn 'too_large';\n\t\t\t\t}\n\t\t\t\tif (!wantsGzip || plain.bytes <= GZIP_MIN_BYTES) {\n\t\t\t\t\treturn { body: plain.json, serializedBytes: plain.bytes, serializeMs, gzipMs: 0 };\n\t\t\t\t}\n\t\t\t\tgzipped = gzipOnce(response, plain.json, plain.bytes);\n\t\t\t}\n\n\t\t\tconst gzipStart = performance.now();\n\t\t\tconst verdict = await gzipped;\n\t\t\tif (verdict === 'too_large') return verdict;\n\t\t\treturn {\n\t\t\t\tbody: verdict.body,\n\t\t\t\tencoding: 'gzip',\n\t\t\t\tserializedBytes: verdict.serializedBytes,\n\t\t\t\tserializeMs,\n\t\t\t\tgzipMs: performance.now() - gzipStart\n\t\t\t};\n\t\t}\n\t};\n}\n","import type { ServerTiming } from '@selvajs/compute/core';\n\n/** A pre-solve prep sub-phase: `[label, ms]`, surfaced as a `p_*` Server-Timing entry. */\nexport type PrepMark = [string, number];\n\nexport interface CacheVerdict {\n\tfromCache: boolean;\n\tdefinitionReuploaded?: boolean;\n}\n\n/**\n * skipped = scheduler never materialized bytes (pointer-known solve, the whole point of this\n * path); hit = warm byte cache; miss = fell through to storage.\n */\nexport type DefBytesVerdict = 'skipped' | 'hit' | 'miss';\n\ninterface ServerTimingParts {\n\tdefLoadMs: number;\n\ttreeBuildMs: number;\n\tsolveMs: number;\n\tserializeMs: number;\n\tgzipMs: number;\n\tserverTotalMs: number;\n\trhino: ServerTiming | null;\n\tsettle: CacheVerdict;\n\t/** Absent for raw-bytes solves. */\n\tdefBytes?: DefBytesVerdict;\n\tprepMarks: PrepMark[];\n}\n\n/**\n * `total` is the server's headers-to-out wall time, so browser `ttfb − total`\n * ≈ network+send latency. When the compute server reports its own timing\n * (VektorNode fork), `rhino_*` is time on the compute server and `compute_link`\n * is everything between it (network + queue wait). `rhino_cache` is 1 when the\n * compute server answered from its own result cache.\n */\nexport function buildServerTiming(parts: ServerTimingParts): string {\n\tlet header =\n\t\t`load;dur=${parts.defLoadMs.toFixed(1)}, ` +\n\t\t`tree;dur=${parts.treeBuildMs.toFixed(1)}, ` +\n\t\t`solve;dur=${parts.solveMs.toFixed(1)}, ` +\n\t\t`serialize;dur=${parts.serializeMs.toFixed(1)}, ` +\n\t\t`gzip;dur=${parts.gzipMs.toFixed(1)}, ` +\n\t\t`total;dur=${parts.serverTotalMs.toFixed(1)}`;\n\n\tconst rhino = parts.rhino;\n\tif (rhino) {\n\t\tconst decode = rhino.decode ?? 0;\n\t\tconst solve = rhino.solve ?? 0;\n\t\tconst encode = rhino.encode ?? 0;\n\t\tconst computeLinkMs = Math.max(0, parts.solveMs - decode - solve - encode);\n\t\theader +=\n\t\t\t`, rhino_decode;dur=${decode.toFixed(1)}` +\n\t\t\t`, rhino_solve;dur=${solve.toFixed(1)}` +\n\t\t\t`, rhino_encode;dur=${encode.toFixed(1)}` +\n\t\t\t`, compute_link;dur=${computeLinkMs.toFixed(1)}` +\n\t\t\t`, rhino_cache;dur=${rhino.cacheHit ? 1 : 0}`;\n\t\tif (rhino.gh !== undefined) header += `, rhino_gh;dur=${rhino.gh.toFixed(1)}`;\n\t\tif (rhino.recomputed) {\n\t\t\theader += `, rhino_recomputed;desc=\"${rhino.recomputed.ran}/${rhino.recomputed.total}\"`;\n\t\t}\n\t}\n\n\tfor (const [label, ms] of parts.prepMarks) {\n\t\theader += `, p_${label};dur=${ms.toFixed(1)}`;\n\t}\n\n\tconst settle = parts.settle;\n\theader += `, selva_cache;dur=${settle.fromCache ? 1 : 0}`;\n\tif (settle.definitionReuploaded !== undefined) {\n\t\theader += `, def_reupload;dur=${settle.definitionReuploaded ? 1 : 0}`;\n\t}\n\n\tif (parts.defBytes) header += `, def_bytes;desc=${parts.defBytes}`;\n\n\treturn header;\n}\n","/**\n * In-process single-flight — dogpile protection for every solve.\n *\n * Always on, not gated behind a result cache being configured: the dogpile is worst\n * when nothing else is caching, since N concurrent identical solves each pay a full\n * Rhino round trip (the scheduler doesn't coalesce in-flight requests itself, and\n * Rhino's `cachesolve` still costs a round trip per repeat).\n *\n * Per app instance. No cross-instance lease (Redis `SET NX`) yet.\n *\n * The shared promise resolves to one value for every caller, so `work` must return\n * something safe to share by reference.\n */\n\nexport interface SolveCacheSingleFlight {\n\t/**\n\t * Coalesces concurrent calls under the same key: the first caller runs `work`, later\n\t * callers await the same promise, and the key frees as soon as it settles.\n\t *\n\t * The flight belongs to all its callers. It aborts `work`'s signal when the last one's\n\t * `callerSignal` fires, and frees the key right then, so a caller arriving after that starts\n\t * a fresh flight instead of joining one that is dying. A caller that leaves earlier still\n\t * resolves with the flight: check its own signal before using the result.\n\t */\n\trun<T>(\n\t\tkey: string,\n\t\twork: (signal: AbortSignal) => Promise<T>,\n\t\tcallerSignal: AbortSignal\n\t): Promise<T>;\n\tinFlight(): number;\n}\n\nexport interface SolveCacheSingleFlightOptions {\n\tonJoin?: (key: string) => void;\n}\n\ninterface Flight {\n\tpromise: Promise<unknown>;\n\tcontroller: AbortController;\n\tcallers: number;\n}\n\nexport function createSolveCacheSingleFlight(\n\toptions: SolveCacheSingleFlightOptions = {}\n): SolveCacheSingleFlight {\n\tconst inflight = new Map<string, Flight>();\n\n\tfunction free(key: string, flight: Flight) {\n\t\t// Guard against deleting a key a later caller has already re-taken.\n\t\tif (inflight.get(key) === flight) inflight.delete(key);\n\t}\n\n\tfunction attend<T>(key: string, flight: Flight, signal: AbortSignal): Promise<T> {\n\t\tflight.callers += 1;\n\t\t// `once: true` doesn't detach on a normal completion, and the caller's signal outlives\n\t\t// this call, so without the `finally` every solve leaks a listener onto it.\n\t\tconst leave = () => {\n\t\t\tflight.callers -= 1;\n\t\t\tif (flight.callers > 0) return;\n\t\t\tfree(key, flight);\n\t\t\tflight.controller.abort();\n\t\t};\n\t\tsignal.addEventListener('abort', leave, { once: true });\n\t\treturn (flight.promise as Promise<T>).finally(() => signal.removeEventListener('abort', leave));\n\t}\n\n\treturn {\n\t\trun<T>(key: string, work: (signal: AbortSignal) => Promise<T>, callerSignal: AbortSignal) {\n\t\t\tconst existing = inflight.get(key);\n\t\t\tif (existing) {\n\t\t\t\ttry {\n\t\t\t\t\toptions.onJoin?.(key);\n\t\t\t\t} catch (err) {\n\t\t\t\t\t// Telemetry must not fail a solve that is otherwise fine.\n\t\t\t\t\tconsole.error('[Solve/single-flight] onJoin threw:', err);\n\t\t\t\t}\n\t\t\t\treturn attend<T>(key, existing, callerSignal);\n\t\t\t}\n\n\t\t\tconst controller = new AbortController();\n\t\t\tconst flight: Flight = {\n\t\t\t\tcontroller,\n\t\t\t\tcallers: 0,\n\t\t\t\t// Wrapped so a synchronous throw in `work` still rejects instead of escaping `run`.\n\t\t\t\tpromise: (async () => work(controller.signal))().finally(() => free(key, flight))\n\t\t\t};\n\t\t\tinflight.set(key, flight);\n\t\t\treturn attend<T>(key, flight, callerSignal);\n\t\t},\n\t\tinFlight(): number {\n\t\t\treturn inflight.size;\n\t\t}\n\t};\n}\n","import { TreeBuilder, type DataTree } from '@selvajs/compute/grasshopper';\nimport { getInputItems, type UISchema } from '@selvajs/schemas';\nimport type { SolveInput } from '../shared/solve-input.js';\nimport { serverSourcedInputIds } from '../shared/server-sourced.js';\n\n/**\n * What `SolveEngine.solve` takes as `inputs`, from a schema the host stored. The layout, not\n * `schema.inputs`, carries the number widgets' bounds and each input's source: without this,\n * nothing clamps and a `server` input takes the caller's value.\n */\nexport function solveInputsFromSchema(schema: UISchema): SolveInput[] {\n\tconst bounds = new Map<string, { minimum?: number; maximum?: number }>();\n\tfor (const item of getInputItems(schema)) {\n\t\tif (item.widgetType === 'number' && item.config) bounds.set(item.paramId, item.config);\n\t}\n\tconst serverSourced = serverSourcedInputIds(schema);\n\treturn schema.inputs.map((input) => {\n\t\tconst widget = bounds.get(input.id);\n\t\tif (!widget && !serverSourced.has(input.id)) return input;\n\t\treturn {\n\t\t\t...input,\n\t\t\t...(widget ? { minimum: widget.minimum, maximum: widget.maximum } : {}),\n\t\t\t...(serverSourced.has(input.id) ? { serverSourced: true } : {})\n\t\t};\n\t});\n}\n\n/**\n * Ids in `values` that set a server-sourced input. A host refuses a request carrying any: the\n * input tree would ignore them, and the caller should hear so rather than solve on the default.\n */\nexport function postedServerSourcedIds(\n\tinputs: SolveInput[],\n\tvalues: Record<string, unknown>\n): string[] {\n\treturn inputs\n\t\t.filter((input) => input.serverSourced && Object.hasOwn(values, input.id))\n\t\t.map((input) => input.id);\n}\n\n/**\n * The input tree a solve sends: each input's value, or its schema default when it has none. A\n * server-sourced input always takes its default.\n */\nexport function buildSolveInputTree(\n\tinputs: SolveInput[],\n\tvalues: Record<string, unknown>\n): DataTree[] {\n\treturn TreeBuilder.fromValues(\n\t\tinputs\n\t\t\t// Stored schemas are validated at upload only, so an older one can lack `paramType`.\n\t\t\t.filter((input) => input.paramType)\n\t\t\t.map((input) => ({\n\t\t\t\tparamName: input.nickname,\n\t\t\t\tparamType: input.paramType,\n\t\t\t\t// `hasOwn`: an input id of `constructor` would read the inherited function.\n\t\t\t\tvalue:\n\t\t\t\t\t(!input.serverSourced && Object.hasOwn(values, input.id)\n\t\t\t\t\t\t? values[input.id]\n\t\t\t\t\t\t: undefined) ?? input.default,\n\t\t\t\tminimum: input.minimum,\n\t\t\t\tmaximum: input.maximum\n\t\t\t}))\n\t);\n}\n","/**\n * Runs one solve against a warm scheduler and classifies how it ended: a solved response, or an\n * expected failure as a value instead of a throw. This is the part single-flight shares, so\n * nothing here depends on who is asking.\n */\n\nimport {\n\tErrorCodes,\n\tComputeError,\n\ttype ServerTiming,\n\ttype SolveDefinition\n} from '@selvajs/compute/core';\nimport type {\n\tDataTree,\n\tGrasshopperComputeResponse,\n\tSelvaEventTarget\n} from '@selvajs/compute/grasshopper';\nimport type { CachedClient } from './client-cache.js';\nimport { isByteCacheRef } from './definition-byte-cache.js';\nimport {\n\tcreateResponseEncoder,\n\ttype ResponseEncoder,\n\ttype SolveEnvelope\n} from './response-encoding.js';\nimport type { CacheVerdict, DefBytesVerdict } from './server-timing.js';\n\nexport interface SolvePipelineArgs {\n\t/**\n\t * Raw `.gh` bytes, or a byte-cache `DefinitionRef` whose bytes the scheduler\n\t * materializes only when an upload is unavoidable — a pointer-known solve\n\t * moves zero bytes.\n\t */\n\tdefinitionSource: SolveDefinition;\n\tinputTree: DataTree[];\n\tclient: CachedClient;\n\tresponseMaxBytes: number;\n\t/** Per-solve live-event callback the compute server hands to the definition; absent when the caller opened no stream. */\n\tselvaEvents?: SelvaEventTarget;\n\t/**\n\t * Forwarded to the scheduler so a client disconnect cancels the upstream\n\t * compute call. Its `aborted` flag also tells that apart from an abort nobody asked for.\n\t */\n\tsignal: AbortSignal;\n}\n\n/**\n * `ok` carries the envelope; every other variant names an expected failure:\n * `SolveEngine.toWebResponse` maps each to a status. `durationMs` on error variants is the solve\n * wall time up to the failure, for the metric record.\n */\nexport type SolveEngineOutcome =\n\t| {\n\t\t\tkind: 'ok';\n\t\t\tenvelope: SolveEnvelope;\n\t\t\tsolveMs: number;\n\t\t\terrorCount: number;\n\t\t\twarningCount: number;\n\t }\n\t| SolveFailure;\n\nexport type SolveFailure =\n\t| { kind: 'timeout'; durationMs: number; message: string }\n\t| { kind: 'client_abort'; durationMs: number }\n\t| { kind: 'too_large' }\n\t/** The compute server refused the request body (its `RHINO_COMPUTE_MAX_REQUEST_SIZE`). */\n\t| { kind: 'request_too_large'; durationMs: number; message: string }\n\t/**\n\t * Retryable: no result, through no fault of the request, and `retryAfterSeconds` is a backoff\n\t * hint. Scheduler backpressure rejected the solve before it ran (`queue_full`,\n\t * `queue_timeout`), or its client was shut down under it (`interrupted`).\n\t */\n\t| {\n\t\t\tkind: 'shed';\n\t\t\tdurationMs: number;\n\t\t\treason: 'queue_full' | 'queue_timeout' | 'interrupted';\n\t\t\tretryAfterSeconds: number;\n\t\t\tmessage: string;\n\t }\n\t| { kind: 'compute_error'; durationMs: number; error: unknown };\n\n/** One finished solve, shared by every caller coalesced onto it. */\nexport interface SolvedFlight {\n\tkind: 'solved';\n\tresponse: GrasshopperComputeResponse;\n\tencoder: ResponseEncoder;\n\tsolveMs: number;\n\trhino: ServerTiming | null;\n\tsettle: CacheVerdict;\n\t/** The verdict of the ref that ran: a coalesced caller's own ref never loads anything. */\n\tdefBytes?: DefBytesVerdict;\n}\n\nfunction shed(\n\tdurationMs: number,\n\treason: 'queue_full' | 'queue_timeout' | 'interrupted',\n\tmessage: string\n): SolveFailure {\n\treturn { kind: 'shed', durationMs, reason, retryAfterSeconds: 1, message };\n}\n\n/** The solve lost its client, and no caller asked for that: a config write or a shutdown. */\nexport function interrupted(durationMs: number): SolveFailure {\n\treturn shed(durationMs, 'interrupted', 'The solve was interrupted. Retry shortly.');\n}\n\nfunction classifyFailure(err: unknown, durationMs: number, args: SolvePipelineArgs): SolveFailure {\n\tif (!(err instanceof ComputeError)) return { kind: 'compute_error', durationMs, error: err };\n\tif (err.statusCode === 413) {\n\t\tconst cap = args.client.limits.maxRequestBytes;\n\t\treturn {\n\t\t\tkind: 'request_too_large',\n\t\t\tdurationMs,\n\t\t\tmessage:\n\t\t\t\t`The inputs are larger than the compute server accepts` +\n\t\t\t\t(cap ? ` (${Math.round(cap / (1024 * 1024))} MB)` : '') +\n\t\t\t\t'. Its admin can raise RHINO_COMPUTE_MAX_REQUEST_SIZE.'\n\t\t};\n\t}\n\tswitch (err.code) {\n\t\tcase ErrorCodes.TIMEOUT_ERROR:\n\t\t\treturn {\n\t\t\t\tkind: 'timeout',\n\t\t\t\tdurationMs,\n\t\t\t\tmessage: `Solve exceeded the ${Math.round(args.client.limits.solveDeadlineMs / 1000)}s deadline set by the compute server.`\n\t\t\t};\n\t\tcase ErrorCodes.ABORTED:\n\t\t\treturn args.signal.aborted ? { kind: 'client_abort', durationMs } : interrupted(durationMs);\n\t\tcase ErrorCodes.RESPONSE_TOO_LARGE:\n\t\t\treturn { kind: 'too_large' };\n\t\t// Queue-full or queue-timeout rejects the solve before compute runs — a load\n\t\t// signal, not a failure, so it's `shed` rather than `compute_error`.\n\t\tcase ErrorCodes.QUEUE_FULL:\n\t\t\treturn shed(durationMs, 'queue_full', 'Compute server is at capacity. Retry shortly.');\n\t\tcase ErrorCodes.QUEUE_TIMEOUT:\n\t\t\treturn shed(\n\t\t\t\tdurationMs,\n\t\t\t\t'queue_timeout',\n\t\t\t\t'Compute request waited too long in the queue. Retry shortly.'\n\t\t\t);\n\t\tdefault:\n\t\t\treturn { kind: 'compute_error', durationMs, error: err };\n\t}\n}\n\nfunction defBytesVerdict(source: SolveDefinition): DefBytesVerdict | undefined {\n\tif (!isByteCacheRef(source)) return undefined;\n\tconst { loaded, fromCache } = source.outcome;\n\treturn !loaded ? 'skipped' : fromCache ? 'hit' : 'miss';\n}\n\nexport async function runSolvePipeline(\n\targs: SolvePipelineArgs\n): Promise<SolvedFlight | SolveFailure> {\n\tlet rhino: ServerTiming | null = null;\n\tconst solveStart = performance.now();\n\ttry {\n\t\tconst detail = await args.client.scheduler.solveDetailed(\n\t\t\targs.definitionSource,\n\t\t\targs.inputTree,\n\t\t\t{\n\t\t\t\tsignal: args.signal,\n\t\t\t\tselvaevents: args.selvaEvents,\n\t\t\t\tonServerTiming: (t) => {\n\t\t\t\t\trhino = t;\n\t\t\t\t}\n\t\t\t}\n\t\t);\n\t\tconst defBytes = defBytesVerdict(args.definitionSource);\n\t\treturn {\n\t\t\tkind: 'solved',\n\t\t\tresponse: detail.response,\n\t\t\tencoder: createResponseEncoder(detail.response, args.responseMaxBytes),\n\t\t\tsolveMs: performance.now() - solveStart,\n\t\t\trhino,\n\t\t\tsettle: { fromCache: detail.fromCache, definitionReuploaded: detail.definitionReuploaded },\n\t\t\t...(defBytes ? { defBytes } : {})\n\t\t};\n\t} catch (err) {\n\t\treturn classifyFailure(err, performance.now() - solveStart, args);\n\t}\n}\n","/**\n * Facade over the warm-client cache, definition-byte cache, single-flight\n * coalescing, and the solve pipeline — the primitives an app route would\n * otherwise wire up by hand.\n *\n * Doesn't read env itself: `@selvajs/server` owns `resolveComputeLimits`, and\n * this package stays independent of it. Callers resolve their own limits and\n * pass in the fields below.\n *\n * Stays out of auth, DB reads, share tokens, rate limiting, metrics, and\n * compute-server selection — all app policy, supplied per-call via\n * `SolveEngineSolveArgs.server`.\n */\n\nimport {\n\tComputeError,\n\tErrorCodes,\n\tisDefinitionRef,\n\ttype DefinitionRef,\n\ttype SolveDefinition\n} from '@selvajs/compute/core';\nimport { stableStringify, type SelvaEventTarget } from '@selvajs/compute/grasshopper';\nimport {\n\tcreateClientCache,\n\ttype CachedClient,\n\ttype ClientCache,\n\ttype ClientCacheDebug,\n\ttype ResolvedServer,\n\ttype SolveCacheStats\n} from './client-cache.js';\nimport {\n\tcreateDefinitionByteCache,\n\tisByteCacheRef,\n\ttype ByteCacheRef,\n\ttype ByteCacheStats,\n\ttype DefinitionByteCache\n} from './definition-byte-cache.js';\nimport { buildEnvelope } from './response-encoding.js';\nimport { buildServerTiming, type PrepMark } from './server-timing.js';\nimport {\n\tcreateSolveCacheSingleFlight,\n\ttype SolveCacheSingleFlight\n} from './solve-cache-single-flight.js';\nimport { buildSolveInputTree } from './transform-input.js';\nimport { interrupted, runSolvePipeline, type SolveEngineOutcome } from './solve-pipeline.js';\nimport type { SolveInput } from '../shared/solve-input.js';\n\n/**\n * Subset of `ComputeLimits` (`@selvajs/server/compute`) the engine needs.\n * Pass the resolved object through as-is — extra fields are ignored.\n */\nexport interface SolveEngineLimits {\n\tcomputeResponseMaxBytes: number;\n\tcomputeServerCachesolve: boolean;\n\tcomputeMaxQueueDepth: number;\n\tcomputeQueueWaitMs: number;\n\tcomputeDefinitionCacheBytes: number;\n\tcomputeSolveCacheBytes: number;\n}\n\n/** A deadline of 0 means no deadline, and a `NaN` budget retains without limit, both silently. */\nfunction assertValidLimits(limits: SolveEngineLimits): void {\n\tconst check = (name: keyof SolveEngineLimits, min: number) => {\n\t\tconst value = limits[name] as number;\n\t\tif (!Number.isFinite(value) || value < min) {\n\t\t\tthrow new RangeError(`SolveEngine: limits.${name} must be a number >= ${min}, got ${value}.`);\n\t\t}\n\t};\n\tcheck('computeResponseMaxBytes', 1);\n\tcheck('computeMaxQueueDepth', 0);\n\tcheck('computeQueueWaitMs', 0);\n\tcheck('computeDefinitionCacheBytes', 0);\n\tcheck('computeSolveCacheBytes', 0);\n}\n\nexport interface SolveEngineOptions {\n\tlimits: SolveEngineLimits;\n\t/** Max distinct warm compute servers before the LRU evicts the oldest. */\n\tmaxWarmComputeServers?: number;\n\t/** Concise cache/timing logs when truthy; `'verbose'` also dumps full lib-level requests/responses. Default off. */\n\tdebug?: ClientCacheDebug;\n\t/** Sink for the debug lines. Without it they go nowhere. */\n\tonDebugLog?: (message: string) => void;\n\t/** Fired when a caller joins an already-in-flight solve instead of running its own. */\n\tonSolveCoalesced?: (key: string) => void;\n}\n\n/**\n * A `ByteCacheRef` obtained ahead of time from `engine.definitionRef()` (e.g. to\n * read bytes for schema extraction before solving) is passed through as-is.\n */\nexport type SolveEngineDefinitionSource = Uint8Array | string | DefinitionRef | ByteCacheRef;\n\nexport interface SolveEngineSolveArgs {\n\tserver: ResolvedServer;\n\tdefinitionSource: SolveEngineDefinitionSource;\n\t/**\n\t * Coalesce-key identity for a raw `Uint8Array`/`string` source, which has\n\t * no natural identity the way a `DefinitionRef`'s `.key` does. Required in\n\t * that case — `solve()` throws rather than silently hashing the bytes.\n\t * Ignored for the other source forms.\n\t */\n\tdefinitionKey?: string;\n\t/**\n\t * The definition's inputs, from the schema you stored, never from the request: `id`, `nickname`,\n\t * `paramType` and the bounds decide what reaches the definition, so a body that supplies them\n\t * can drop a limit or fill a slot your host reserved.\n\t */\n\tinputs: SolveInput[];\n\tvalues: Record<string, unknown>;\n\tsignal: AbortSignal;\n\tacceptEncoding?: string;\n\t/**\n\t * Stamped as `X-Selva-Definition` when this solve builds the server's warm client. The header\n\t * is fixed per client, so later solves on that server carry the first definition's guid.\n\t */\n\tdefinitionGuid?: string;\n\tloadStartMs?: number;\n\tdefLoadMs?: number;\n\tprepMarks?: PrepMark[];\n\t/** Per-solve live-event callback. A solve carrying one runs its own compute call, never a shared one. */\n\tselvaEvents?: SelvaEventTarget;\n}\n\nexport interface SolveEngineStats {\n\tclient: SolveCacheStats;\n\tdefinitionBytes: ByteCacheStats;\n\tcoalescing: { inFlight: number };\n}\n\nexport class SolveEngine {\n\tprivate readonly limits: SolveEngineLimits;\n\tprivate readonly clientCache: ClientCache;\n\tprivate readonly byteCache: DefinitionByteCache;\n\tprivate readonly singleFlight: SolveCacheSingleFlight;\n\tprivate readonly clientGenerations = new WeakMap<CachedClient, number>();\n\tprivate lastClientGeneration = 0;\n\n\tconstructor(options: SolveEngineOptions) {\n\t\tassertValidLimits(options.limits);\n\t\tthis.limits = options.limits;\n\n\t\tthis.clientCache = createClientCache({\n\t\t\tcachesolve: this.limits.computeServerCachesolve,\n\t\t\tmaxQueueDepth: this.limits.computeMaxQueueDepth,\n\t\t\tqueueWaitMs: this.limits.computeQueueWaitMs,\n\t\t\tresponseCacheMaxBytes: this.limits.computeSolveCacheBytes,\n\t\t\tmaxResponseBytes: this.limits.computeResponseMaxBytes,\n\t\t\tdebug: options.debug ?? false,\n\t\t\tmaxWarmComputeServers: options.maxWarmComputeServers,\n\t\t\tonDebugLog: options.onDebugLog\n\t\t});\n\n\t\tthis.byteCache = createDefinitionByteCache(this.limits.computeDefinitionCacheBytes);\n\n\t\tthis.singleFlight = createSolveCacheSingleFlight({\n\t\t\tonJoin: options.onSolveCoalesced\n\t\t});\n\t}\n\n\tgetClient(server: ResolvedServer, opts?: { definitionGuid?: string }): Promise<CachedClient> {\n\t\treturn this.clientCache.getClient(server, opts);\n\t}\n\n\tdefinitionRef(\n\t\tversionId: string,\n\t\tload: (signal: AbortSignal) => Promise<Uint8Array>\n\t): ByteCacheRef {\n\t\treturn this.byteCache.getOrLoad(versionId, load);\n\t}\n\n\t/**\n\t * Drops the warm client for `id`; call it when the server's URL or key changes. Solves already\n\t * running finish on the old connection details first, so a revoked key stays in use until then.\n\t */\n\tevictServer(id: string): void {\n\t\tthis.clientCache.evict(id);\n\t}\n\n\tclearSolveCaches(): void {\n\t\tthis.clientCache.clearSolveCaches();\n\t}\n\n\t/**\n\t * Drop every retained `.gh` byte entry. Safe at any time: entries key on an\n\t * immutable version id, so clearing costs a storage re-read and nothing else.\n\t */\n\tclearDefinitionCache(): void {\n\t\tthis.byteCache.clear();\n\t}\n\n\tstats(): SolveEngineStats {\n\t\treturn {\n\t\t\tclient: this.clientCache.solveCacheStats(),\n\t\t\tdefinitionBytes: this.byteCache.stats(),\n\t\t\tcoalescing: { inFlight: this.singleFlight.inFlight() }\n\t\t};\n\t}\n\n\tasync solve(args: SolveEngineSolveArgs): Promise<SolveEngineOutcome> {\n\t\tconst { definitionSource, identity } = this.resolveDefinitionSource(args);\n\t\tconst startedAt = performance.now();\n\t\tconst elapsed = () => performance.now() - startedAt;\n\n\t\t// `addEventListener` never fires for a signal that already aborted, so a client gone during\n\t\t// the host's own awaits would otherwise still pay for a solve.\n\t\tif (args.signal.aborted) return { kind: 'client_abort', durationMs: 0 };\n\n\t\tlet client: CachedClient;\n\t\ttry {\n\t\t\tclient = await this.clientCache.getClient(args.server, {\n\t\t\t\tdefinitionGuid: args.definitionGuid\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tif (args.signal.aborted) return { kind: 'client_abort', durationMs: elapsed() };\n\t\t\tif (error instanceof ComputeError && error.code === ErrorCodes.ABORTED) {\n\t\t\t\treturn interrupted(elapsed());\n\t\t\t}\n\t\t\treturn { kind: 'compute_error', durationMs: elapsed(), error };\n\t\t}\n\t\tif (args.signal.aborted) return { kind: 'client_abort', durationMs: elapsed() };\n\n\t\tconst treeBuildStart = performance.now();\n\t\tlet inputTree;\n\t\ttry {\n\t\t\tinputTree = buildSolveInputTree(args.inputs, args.values);\n\t\t} catch (error) {\n\t\t\treturn { kind: 'compute_error', durationMs: elapsed(), error };\n\t\t}\n\t\tconst treeBuildMs = performance.now() - treeBuildStart;\n\n\t\t// A live solve never shares a flight: the request carries its owner's callback, through which\n\t\t// that owner's Esc aborts the Grasshopper solve. Shared, one user's cancel would abort everyone\n\t\t// who joined, and the joiners would get no progress events.\n\t\tconst live = args.selvaEvents ? `:live:${args.selvaEvents.solveId}` : '';\n\t\tconst flight = await this.singleFlight.run(\n\t\t\t`${identity}:${args.server.id}:${this.clientGeneration(client)}:${stableStringify(inputTree)}${live}`,\n\t\t\t(signal) =>\n\t\t\t\trunSolvePipeline({\n\t\t\t\t\tdefinitionSource,\n\t\t\t\t\tinputTree,\n\t\t\t\t\tclient,\n\t\t\t\t\tresponseMaxBytes: this.limits.computeResponseMaxBytes,\n\t\t\t\t\tselvaEvents: args.selvaEvents,\n\t\t\t\t\tsignal\n\t\t\t\t}),\n\t\t\targs.signal\n\t\t);\n\n\t\t// Before encoding: a caller that left a shared flight must not stringify for nobody.\n\t\tif (args.signal.aborted) return { kind: 'client_abort', durationMs: elapsed() };\n\t\tif (flight.kind !== 'solved') return flight;\n\n\t\tlet encoded;\n\t\ttry {\n\t\t\tencoded = await flight.encoder.encode(args.acceptEncoding ?? '');\n\t\t} catch (error) {\n\t\t\treturn { kind: 'compute_error', durationMs: elapsed(), error };\n\t\t}\n\t\tif (encoded === 'too_large') return { kind: 'too_large' };\n\n\t\tconst { body, serializedBytes, serializeMs, gzipMs } = encoded;\n\t\tconst { solveMs, response } = flight;\n\t\tconst serverTotalMs = performance.now() - (args.loadStartMs ?? startedAt);\n\t\tconst timing = {\n\t\t\tdefLoadMs: args.defLoadMs ?? 0,\n\t\t\ttreeBuildMs,\n\t\t\tsolveMs,\n\t\t\tserializeMs,\n\t\t\tgzipMs,\n\t\t\tserverTotalMs,\n\t\t\trhino: flight.rhino,\n\t\t\tsettle: flight.settle,\n\t\t\t...(flight.defBytes ? { defBytes: flight.defBytes } : {})\n\t\t};\n\t\tconst serverTiming = buildServerTiming({ ...timing, prepMarks: args.prepMarks ?? [] });\n\n\t\treturn {\n\t\t\tkind: 'ok',\n\t\t\tenvelope: buildEnvelope(encoded, response, serverTiming, {\n\t\t\t\t...timing,\n\t\t\t\tserializedBytes,\n\t\t\t\tcompressedBytes: encoded.encoding === 'gzip' ? (body as Uint8Array).byteLength : null\n\t\t\t}),\n\t\t\tsolveMs,\n\t\t\terrorCount: response.errors?.length ?? 0,\n\t\t\twarningCount: response.warnings?.length ?? 0\n\t\t};\n\t}\n\n\t/**\n\t * The response for an outcome; the mapping from outcome to status lives here. Throws\n\t * `compute_error`'s error: what it means for the caller is the host's to decide.\n\t */\n\ttoWebResponse(outcome: SolveEngineOutcome): Response {\n\t\tconst fail = (status: number, body: { message: string; retryAfter?: number }) =>\n\t\t\tnew Response(JSON.stringify(body), {\n\t\t\t\tstatus,\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t...(body.retryAfter !== undefined ? { 'Retry-After': String(body.retryAfter) } : {})\n\t\t\t\t}\n\t\t\t});\n\n\t\tswitch (outcome.kind) {\n\t\t\tcase 'ok':\n\t\t\t\t// The cast is a TS gap: `Uint8Array<ArrayBufferLike>` is not a body type, though it works.\n\t\t\t\treturn new Response(outcome.envelope.body as ConstructorParameters<typeof Response>[0], {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: outcome.envelope.headers\n\t\t\t\t});\n\t\t\tcase 'timeout':\n\t\t\t\treturn fail(504, { message: outcome.message });\n\t\t\tcase 'client_abort':\n\t\t\t\treturn fail(499, { message: 'Client closed request' });\n\t\t\tcase 'too_large':\n\t\t\t\treturn fail(413, {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t'Solve result is too large to return. This usually means a file output exceeds the supported size.'\n\t\t\t\t});\n\t\t\tcase 'request_too_large':\n\t\t\t\treturn fail(413, { message: outcome.message });\n\t\t\tcase 'shed':\n\t\t\t\treturn fail(503, { message: outcome.message, retryAfter: outcome.retryAfterSeconds });\n\t\t\tcase 'compute_error':\n\t\t\t\tthrow outcome.error;\n\t\t}\n\t}\n\n\t/**\n\t * Part of the coalesce key: after an eviction the id stays and the client changes, and a new\n\t * caller joining a draining flight would get an answer from the old URL or key.\n\t */\n\tprivate clientGeneration(client: CachedClient): number {\n\t\tlet generation = this.clientGenerations.get(client);\n\t\tif (generation === undefined) {\n\t\t\tgeneration = ++this.lastClientGeneration;\n\t\t\tthis.clientGenerations.set(client, generation);\n\t\t}\n\t\treturn generation;\n\t}\n\n\tprivate resolveDefinitionSource(args: SolveEngineSolveArgs): {\n\t\tdefinitionSource: SolveDefinition;\n\t\tidentity: string;\n\t} {\n\t\tconst source = args.definitionSource;\n\n\t\tif (isByteCacheRef(source) || isDefinitionRef(source)) {\n\t\t\treturn { definitionSource: source, identity: source.key };\n\t\t}\n\n\t\t// Raw Uint8Array | string — no natural identity; require the caller's own.\n\t\tif (!args.definitionKey) {\n\t\t\tthrow new Error(\n\t\t\t\t'SolveEngine.solve(): definitionKey is required when definitionSource is raw bytes or a string.'\n\t\t\t);\n\t\t}\n\t\treturn { definitionSource: source, identity: args.definitionKey };\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;AAOA,MAAM,uBAAuB;;;;;;;AAQ7B,MAAM,gCAAgC;;;;;;;;;;;AAoBtC,SAAgB,uBACf,QACA,UACmB;CACnB,IAAI,gBAAgB;CACpB,IAAI,cAAc;CAClB,IAAI,UAAU;;CAGd,eAAe,QAAQ,WAAmD;EACzE,IAAI,SAAS;EACb,UAAU;EACV,IAAI;GACH,MAAM,WAAW,MAAM,QAAQ,QAC9B,OAAO,aAAa,kBAAkB,EAAE,YAAY,MAAM,CAAC,CAC5D,CAAC,CAAC,YAAY,IAAI;GAClB,cAAc,KAAK,IAAI;GACvB,MAAM,OAAO,CAAC,YAAY,YAAY,IAAI,uBAAuB;GACjE,IAAI,S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AAa,EAAE,KAAK;GACpB,KAAK,kBAAkB,IAAI,QAAQ,UAAU;EAC9C;EACA,OAAO;CACR;CAEA,wBAAgC,MAG9B;EACD,MAAM,SAAS,KAAK;EAEpB,IAAI,eAAe,MAAM,KAAK,gBAAgB,MAAM,GACnD,OAAO;GAAE,kBAAkB;GAAQ,UAAU,OAAO;EAAI;EAIzD,IAAI,CAAC,KAAK,eACT,MAAM,IAAI,MACT,gGACD;EAED,OAAO;GAAE,kBAAkB;GAAQ,UAAU,KAAK;EAAc;CACjE;AACD"}
|
|
1
|
+
{"version":3,"file":"server.js","names":["undiciFetch"],"sources":["../src/server/concurrency-probe.ts","../src/server/long-fetch.ts","../src/server/server-timing.ts","../src/server/server-limits.ts","../src/server/client-cache.ts","../src/server/definition-byte-cache.ts","../src/server/response-encoding.ts","../src/server/solve-cache-single-flight.ts","../src/server/transform-input.ts","../src/server/solve-pipeline.ts","../src/server/solve-engine.ts"],"sourcesContent":["import type { GrasshopperClient } from '@selvajs/compute/grasshopper';\n\n/**\n * Fallback concurrency while the first probe is still in flight, and the value\n * kept whenever a probe fails or reports 0 — never send more than a single\n * solve to a server whose real capacity is unknown.\n */\nconst FALLBACK_CONCURRENCY = 1;\n\n/**\n * How stale the compute server's child count may get before a solve triggers a\n * re-read. Short enough that a resized pool is picked up within a few solves,\n * long enough that a busy server is probed rarely — the probe rides on solve\n * completion, so an idle server is never polled at all.\n */\nconst CONCURRENCY_PROBE_INTERVAL_MS = 5 * 60 * 1000;\n\nexport interface ConcurrencyProbe {\n\treadonly maxConcurrent: number;\n\t/** One blocking read at connect, so the first solves already use the right cap. */\n\trefresh(): Promise<void>;\n\t/** Piggybacks on real traffic: at most once per window, and never on the request path. */\n\trefreshIfDue(scheduler: { setMaxConcurrent(n: number): void }): void;\n}\n\n/**\n * rhino.compute hands out children in strict rotation and never reports \"busy\", so nothing\n * downstream enforces a concurrency cap — send more than it has children and requests silently\n * double up on one. Rhino.Compute is the only source of truth for its own child count, so this\n * always probes rather than trusting an operator-supplied guess.\n *\n * The count is not static: children exit on their own under `-idlespan`, and `/launch-child` +\n * `/recycle-children` resize the pool at runtime. So the probe re-runs after solves rather than\n * only at connect.\n */\nexport function createConcurrencyProbe(\n\tclient: Pick<GrasshopperClient, 'serverStats'>,\n\tdebugLog: (message: string) => void\n): ConcurrencyProbe {\n\tlet maxConcurrent = FALLBACK_CONCURRENCY;\n\tlet lastProbeAt = 0;\n\tlet probing = false;\n\n\t/** `initialize: false` keeps this from spawning children on an idle server, so a cold one reports 0. */\n\tasync function refresh(scheduler?: { setMaxConcurrent(n: number): void }) {\n\t\tif (probing) return;\n\t\tprobing = true;\n\t\ttry {\n\t\t\tconst children = await Promise.resolve(\n\t\t\t\tclient.serverStats?.getActiveChildren({ initialize: false })\n\t\t\t).catch(() => null);\n\t\t\tlastProbeAt = Date.now();\n\t\t\tconst next = !children || children <= 0 ? FALLBACK_CONCURRENCY : children;\n\t\t\tif (next === maxConcurrent) return;\n\t\t\tdebugLog(\n\t\t\t\t`[Compute/client-cache] concurrency ${maxConcurrent} → ${next} (server child count)`\n\t\t\t);\n\t\t\tmaxConcurrent = next;\n\t\t\tscheduler?.setMaxConcurrent(next);\n\t\t} finally {\n\t\t\tprobing = false;\n\t\t}\n\t}\n\n\treturn {\n\t\tget maxConcurrent() {\n\t\t\treturn maxConcurrent;\n\t\t},\n\t\trefresh: () => refresh(),\n\t\trefreshIfDue(scheduler) {\n\t\t\tif (Date.now() - lastProbeAt >= CONCURRENCY_PROBE_INTERVAL_MS) void refresh(scheduler);\n\t\t}\n\t};\n}\n","import { Agent, fetch as undiciFetch } from 'undici';\n\n/** undici's default `headersTimeout` and `bodyTimeout`, which `timeoutMs` can't extend. */\nconst UNDICI_DEFAULT_LIMIT_MS = 300_000;\n\n/** Both off, not raised: the scheduler's `timeoutMs` stays the one deadline on a solve. */\nconst unlimited = new Agent({ headersTimeout: 0, bodyTimeout: 0 });\n\n// npm undici's own fetch, not the global one: a dispatcher has to match the undici that calls it,\n// and Node's bundled copy is a different version.\n//\n// A call with no signal has no deadline of its own (`getIO` is one), so it keeps undici's limit:\n// switching the timeouts off for it would let a hung server hang a page load forever.\nconst unlimitedFetch = ((input: string, init?: { signal?: AbortSignal }) =>\n\tundiciFetch(input, {\n\t\t...init,\n\t\tsignal: init?.signal ?? AbortSignal.timeout(UNDICI_DEFAULT_LIMIT_MS),\n\t\tdispatcher: unlimited\n\t})) as unknown as typeof fetch;\n\n/** The fetch for a solve deadline: `undefined` means the global one reaches it. */\nexport function fetchForDeadline(deadlineMs: number): typeof fetch | undefined {\n\treturn deadlineMs >= UNDICI_DEFAULT_LIMIT_MS ? unlimitedFetch : undefined;\n}\n","import type { ServerTiming } from '@selvajs/compute/core';\n\n/** A pre-solve prep sub-phase: `[label, ms]`, surfaced as a `p_*` Server-Timing entry. */\nexport type PrepMark = [string, number];\n\nexport interface CacheVerdict {\n\tfromCache: boolean;\n\tdefinitionReuploaded?: boolean;\n}\n\n/**\n * skipped = scheduler never materialized bytes (pointer-known solve, the whole point of this\n * path); hit = warm byte cache; miss = fell through to storage.\n */\nexport type DefBytesVerdict = 'skipped' | 'hit' | 'miss';\n\ninterface ServerTimingParts {\n\tdefLoadMs: number;\n\ttreeBuildMs: number;\n\tsolveMs: number;\n\tserializeMs: number;\n\tgzipMs: number;\n\tserverTotalMs: number;\n\trhino: ServerTiming | null;\n\tsettle: CacheVerdict;\n\t/** Absent for raw-bytes solves. */\n\tdefBytes?: DefBytesVerdict;\n\tprepMarks: PrepMark[];\n}\n\n/**\n * False for a header carrying only the proxy's `affinity`, from a child that sends no timing:\n * read as zeros, it would put the whole solve on `compute_link`.\n */\nexport function reportsTiming(rhino: ServerTiming): boolean {\n\treturn (\n\t\trhino.cacheHit === true ||\n\t\trhino.decode !== undefined ||\n\t\trhino.solve !== undefined ||\n\t\trhino.encode !== undefined\n\t);\n}\n\n/**\n * `total` is the server's headers-to-out wall time, so browser `ttfb − total`\n * ≈ network+send latency. When the compute server reports its own timing\n * (VektorNode fork), `rhino_*` is time on the compute server and `compute_link`\n * is everything between it (network + queue wait). `rhino_cache` is 1 when the\n * compute server answered from its own result cache.\n */\nexport function buildServerTiming(parts: ServerTimingParts): string {\n\tlet header =\n\t\t`load;dur=${parts.defLoadMs.toFixed(1)}, ` +\n\t\t`tree;dur=${parts.treeBuildMs.toFixed(1)}, ` +\n\t\t`solve;dur=${parts.solveMs.toFixed(1)}, ` +\n\t\t`serialize;dur=${parts.serializeMs.toFixed(1)}, ` +\n\t\t`gzip;dur=${parts.gzipMs.toFixed(1)}, ` +\n\t\t`total;dur=${parts.serverTotalMs.toFixed(1)}`;\n\n\tconst rhino = parts.rhino;\n\tif (rhino && reportsTiming(rhino)) {\n\t\tconst decode = rhino.decode ?? 0;\n\t\tconst solve = rhino.solve ?? 0;\n\t\tconst encode = rhino.encode ?? 0;\n\t\tconst computeLinkMs = Math.max(0, parts.solveMs - decode - solve - encode);\n\t\theader +=\n\t\t\t`, rhino_decode;dur=${decode.toFixed(1)}` +\n\t\t\t`, rhino_solve;dur=${solve.toFixed(1)}` +\n\t\t\t`, rhino_encode;dur=${encode.toFixed(1)}` +\n\t\t\t`, compute_link;dur=${computeLinkMs.toFixed(1)}` +\n\t\t\t`, rhino_cache;dur=${rhino.cacheHit ? 1 : 0}`;\n\t\tif (rhino.gh !== undefined) header += `, rhino_gh;dur=${rhino.gh.toFixed(1)}`;\n\t\tif (rhino.recomputed) {\n\t\t\theader += `, rhino_recomputed;desc=\"${rhino.recomputed.ran}/${rhino.recomputed.total}\"`;\n\t\t}\n\t}\n\tif (rhino?.affinity) header += `, rhino_affinity;desc=${rhino.affinity}`;\n\n\tfor (const [label, ms] of parts.prepMarks) {\n\t\theader += `, p_${label};dur=${ms.toFixed(1)}`;\n\t}\n\n\tconst settle = parts.settle;\n\theader += `, selva_cache;dur=${settle.fromCache ? 1 : 0}`;\n\tif (settle.definitionReuploaded !== undefined) {\n\t\theader += `, def_reupload;dur=${settle.definitionReuploaded ? 1 : 0}`;\n\t}\n\n\tif (parts.defBytes) header += `, def_bytes;desc=${parts.defBytes}`;\n\n\treturn header;\n}\n","import type { ServerLimits } from '@selvajs/compute/grasshopper';\n\n/** Rhino.Compute's own default `--timeout`, for a server that can't report its own. */\nexport const DEFAULT_SOLVE_DEADLINE_MS = 100_000;\n\n/**\n * Ceiling on any solve deadline, whatever the server reports. Bounds how long a request, its\n * live-event token and a delegated session stay open; past an hour a solve belongs in a job.\n */\nexport const MAX_SOLVE_DEADLINE_MS = 60 * 60_000;\n\n/** Selva waits this much past the server's timeout, so Compute's own error arrives first. */\nconst DEADLINE_SLACK_MS = 5_000;\n\n/** What Selva holds a solve to on one compute server. */\nexport interface SolveLimits {\n\tsolveDeadlineMs: number;\n\t/** `null` when the server didn't say: its own 413 is then the only check. */\n\tmaxRequestBytes: number | null;\n\t/**\n\t * Send a pointer to the server's cached definition instead of the `.gh`. Only on a server that\n\t * answers a stale pointer with `definition_not_cached`: one that doesn't returns empty geometry.\n\t */\n\treuseDefinitionCache: boolean;\n\t/** The server's deployment (`ServerLimits.deploymentId`); another one may solve differently. */\n\tdeploymentId: string | null;\n}\n\nexport function solveLimitsFor(reported: ServerLimits | null): SolveLimits {\n\tif (!reported) {\n\t\treturn {\n\t\t\tsolveDeadlineMs: DEFAULT_SOLVE_DEADLINE_MS,\n\t\t\tmaxRequestBytes: null,\n\t\t\treuseDefinitionCache: false,\n\t\t\tdeploymentId: null\n\t\t};\n\t}\n\treturn {\n\t\tsolveDeadlineMs: Math.min(\n\t\t\treported.timeoutSeconds * 1000 + DEADLINE_SLACK_MS,\n\t\t\tMAX_SOLVE_DEADLINE_MS\n\t\t),\n\t\tmaxRequestBytes: reported.maxRequestBytes,\n\t\treuseDefinitionCache: reported.signalsStaleDefinition,\n\t\t// An older `@selvajs/compute` leaves it undefined.\n\t\tdeploymentId: reported.deploymentId ?? null\n\t};\n}\n\nexport function sameSolveLimits(a: SolveLimits, b: SolveLimits): boolean {\n\treturn (\n\t\ta.solveDeadlineMs === b.solveDeadlineMs &&\n\t\ta.maxRequestBytes === b.maxRequestBytes &&\n\t\ta.reuseDefinitionCache === b.reuseDefinitionCache &&\n\t\ta.deploymentId === b.deploymentId\n\t);\n}\n","import {\n\tGrasshopperClient,\n\ttype GrasshopperComputeConfig,\n\ttype SolveScheduler\n} from '@selvajs/compute/grasshopper';\nimport { ComputeError, enableDebugLogging, ErrorCodes } from '@selvajs/compute/core';\n\nimport { createConcurrencyProbe } from './concurrency-probe';\nimport { fetchForDeadline } from './long-fetch';\nimport { reportsTiming } from './server-timing';\nimport {\n\tMAX_SOLVE_DEADLINE_MS,\n\tsameSolveLimits,\n\tsolveLimitsFor,\n\ttype SolveLimits\n} from './server-limits';\n\n// ============================================================================\n// Shared compute client cache\n// ============================================================================\n//\n// One warm `GrasshopperClient` (+ its `SolveScheduler`) per distinct compute\n// server, keyed by the server's `id`, never its URL — a rotated URL/apiKey\n// keeps the same key with stale connection details, so the config-write path\n// MUST call `evict(id)` when a server's config changes.\n//\n// Per-server LRU, not a single shared client: definitions can pin different\n// servers, so two definitions on two servers keep two warm clients, with\n// churn from one-off servers bounded by `maxWarmComputeServers`.\n//\n// Both hot paths share this cache — the solve endpoint uses the entry's\n// `scheduler`; the definition-viewer render path uses the entry's `client`\n// for `getIO` — so a definition that was just solved renders from the same\n// warm client instead of re-handshaking Rhino.Compute per page load.\n\n/** Minimal resolved-server shape the cache needs (a subset of the app's `ComputeServerConfig`). */\nexport interface ResolvedServer {\n\tid: string;\n\tserverUrl: string;\n\t/** Sent as the `RhinoComputeKey` header. */\n\tapiKey?: string;\n}\n\nexport interface CachedClient {\n\tclient: GrasshopperClient;\n\tscheduler: SolveScheduler;\n\t/** Read from the server when the client was built; a changed server rebuilds the client. */\n\tlimits: SolveLimits;\n}\n\n/**\n * Debug verbosity: `false` is silent, `true` gives concise cache/timing logs\n * (`onDebugLog`), `'verbose'` also enables full lib-level request/response\n * dumps (incl. geometry) — a superset of `true`, not an independent setting.\n */\nexport type ClientCacheDebug = boolean | 'verbose';\n\n/** Config injected by the consuming app, so this module stays env-agnostic and testable. */\nexport interface ClientCacheConfig {\n\tcachesolve: boolean;\n\t/**\n\t * Max solves that may wait in the FIFO queue, excluding in-flight solves\n\t * (capped at `maxConcurrent`, itself driven by the server's probed child\n\t * count). `0` = unbounded (`ComputeLimits.computeMaxQueueDepth`); a full\n\t * queue rejects new solves with `QUEUE_FULL`.\n\t */\n\tmaxQueueDepth: number;\n\t/**\n\t * Max ms a solve may sit queued before executing; `0` = no deadline\n\t * (`ComputeLimits.computeQueueWaitMs`). Too long a wait rejects with\n\t * `QUEUE_TIMEOUT`.\n\t */\n\tqueueWaitMs: number;\n\t/**\n\t * Byte budget for this client's in-process solve cache. Applies per warm\n\t * client — worst-case heap is this × `maxWarmComputeServers`. `0` disables\n\t * the cache (`ComputeLimits.computeSolveCacheBytes`, env `COMPUTE_SOLVE_CACHE_MB`).\n\t */\n\tresponseCacheMaxBytes: number;\n\t/**\n\t * Largest compute response body read; past it the download is cancelled (`too_large`).\n\t * Default: no limit.\n\t */\n\tmaxResponseBytes?: number;\n\tdebug: ClientCacheDebug;\n\t/** Max distinct warm compute servers before the LRU evicts the oldest. */\n\tmaxWarmComputeServers?: number;\n\t/** Sink for the debug lines. `onDebugLog` only fires when `debug` is not `false`. */\n\tonDebugLog?: (message: string) => void;\n}\n\nexport interface ClientCache {\n\t/**\n\t * Get (or create) the warm client + scheduler for a resolved compute server, keyed by its `id`.\n\t * With the solve cache on, a warm client last checked over 30 s ago first re-reads the server's\n\t * `/limits` (at most 1 s), and one that reports another deployment is rebuilt with an empty cache.\n\t *\n\t * @throws {ComputeError} `ABORTED` when `evict` or `disposeAll` overtakes the build.\n\t */\n\tgetClient(server: ResolvedServer): Promise<CachedClient>;\n\t/**\n\t * Drops the warm client for `id`, so the next request rebuilds, and disposes it once idle. A\n\t * build in flight for `id` is abandoned.\n\t */\n\tevict(id: string): void;\n\t/** Solve-cache counters summed across every warm client. Counters die with the client that owns them, so totals can fall over time. */\n\tsolveCacheStats(): SolveCacheStats;\n\t/**\n\t * Drop every retained solve result, keeping the warm clients (and their\n\t * connections and server-side definition pointers) intact. Nothing expires\n\t * these on its own — the byte budget is the only other pressure — so this is\n\t * the operator's release valve when a definition's inputs no longer describe\n\t * its output, e.g. it reads an external source that has since changed.\n\t */\n\tclearSolveCaches(): void;\n\t/** Dispose every warm client now, running solves included. Test seam / shutdown hook. */\n\tdisposeAll(): void;\n}\n\nexport interface SolveCacheStats {\n\twarmClients: number;\n\tentries: number;\n\tbytes: number;\n\thits: number;\n\tmisses: number;\n\t/** Entries dropped under size/byte pressure (not replacement or manual clears). */\n\tevictions: number;\n}\n\nconst DEFAULT_MAX_WARM_COMPUTE_SERVERS = 16;\n\n/**\n * How often a warm client re-reads its server's limits, riding on solve completion like the\n * concurrency probe. An operator who changes `--timeout` sees it within this window.\n */\nconst LIMITS_RECHECK_MS = 5 * 60 * 1000;\n\n/**\n * How stale the server's `deploymentId` may be before a warm client whose solve cache could\n * answer is handed out again. A cache hit never reaches the server, so riding on solve completion\n * would let a redeployed plugin's old results be served until the next miss.\n */\nconst DEPLOYMENT_RECHECK_MS = 30 * 1000;\n\n/**\n * Longest a request waits on that check. The proxy answers `/limits` itself, in milliseconds; an\n * unreachable server must not hold a request that the cache could answer. The check finishes\n * behind it and retires the client if the deployment changed.\n */\nconst DEPLOYMENT_CHECK_WAIT_MS = 1000;\n\n/**\n * How long a failed build is remembered before the next `getClient` retries the\n * handshake. Without this, every solve against a down server pays the full probe\n * ladder again, so a user clicking Solve twice waits twice.\n *\n * Short on purpose: this is a convenience for the rapid-retry case, not a\n * circuit breaker. An operator who starts the compute VM should not have to wait\n * out a long penalty window before the app notices.\n */\nconst FAILED_BUILD_TTL_MS = 5000;\n\n/**\n * A build failure worth replaying to the next caller, with the time it happened\n * so {@link FAILED_BUILD_TTL_MS} can expire it.\n */\ninterface FailedBuild {\n\terror: unknown;\n\tat: number;\n}\n\ninterface PendingBuild {\n\tpromise: Promise<CachedClient>;\n\t/** Set when the connection details this build started from stopped being true. */\n\ttoken: { stale: boolean };\n}\n\ninterface SettledSolve {\n\tfromCache?: boolean;\n\tdurationMs: number;\n\tdefinitionReuploaded?: boolean;\n\tresponse?: { errors?: unknown };\n}\n\nfunction describeSettledSolve(result: SettledSolve): string[] {\n\tconst verdict = result.fromCache\n\t\t? 'HIT — served from cache (no compute call)'\n\t\t: 'miss — went to Rhino.Compute';\n\tconst lines = [`[Compute/selva-cache] ${verdict} (${Math.round(result.durationMs)}ms)`];\n\tif (result.fromCache) return lines;\n\n\tlines.push(\n\t\tresult.definitionReuploaded === true\n\t\t\t? '[Compute/def-cache] miss — definition RE-UPLOADED (pointer was cold/stale)'\n\t\t\t: result.definitionReuploaded === false\n\t\t\t\t? '[Compute/def-cache] HIT — definition reused from server cache (no upload)'\n\t\t\t\t: '[Compute/def-cache] n/a — reuse disabled or non-reusable definition'\n\t);\n\tconst errors = result.response?.errors;\n\tif (Array.isArray(errors) && errors.length > 0) {\n\t\t// A count: solver text can quote user input, and logs are out of erasure's reach.\n\t\tlines.push(`[Compute/rhino-cache] ${errors.length} GH error(s); not cached by Rhino.Compute.`);\n\t}\n\treturn lines;\n}\n\nexport function createClientCache(config: ClientCacheConfig): ClientCache {\n\tconst maxWarmComputeServers = config.maxWarmComputeServers ?? DEFAULT_MAX_WARM_COMPUTE_SERVERS;\n\tconst cacheEnabled = config.responseCacheMaxBytes > 0;\n\tconst cache = new Map<string, CachedClient>();\n\t// Builds in flight, keyed like `cache`. Concurrent `getClient` calls for the\n\t// same id must share ONE build: without this, both miss, both handshake, and\n\t// the loser's entry is overwritten in the map without ever being disposed.\n\tconst pending = new Map<string, PendingBuild>();\n\t// Recent build failures, keyed like `cache`. Cleared by `evict` so a config\n\t// fix takes effect immediately rather than waiting out the TTL.\n\tconst failed = new Map<string, FailedBuild>();\n\t// Out of `cache`, waiting for their running solves to finish.\n\tconst draining = new Set<CachedClient>();\n\t// Per warm client: resolves false once the server reports another deployment (see\n\t// DEPLOYMENT_RECHECK_MS), true while it may still serve from its solve cache.\n\tconst deploymentChecks = new WeakMap<CachedClient, () => Promise<boolean>>();\n\n\tif (config.debug === 'verbose') enableDebugLogging();\n\n\tconst debugLog = (message: string) => {\n\t\tif (config.debug) config.onDebugLog?.(message);\n\t};\n\n\tfunction isIdle({ scheduler }: CachedClient) {\n\t\treturn scheduler.inFlightCount === 0 && scheduler.queueDepth === 0;\n\t}\n\n\t/** Disposing a busy scheduler aborts its solves, so a config write would fail them all. */\n\tfunction retire(entry: CachedClient) {\n\t\tif (isIdle(entry)) entry.scheduler.dispose();\n\t\telse draining.add(entry);\n\t}\n\n\tfunction finishDraining(entry: CachedClient) {\n\t\tif (!draining.has(entry) || !isIdle(entry)) return;\n\t\tdraining.delete(entry);\n\t\tentry.scheduler.dispose();\n\t}\n\n\tasync function build(server: ResolvedServer): Promise<CachedClient> {\n\t\tconst debugVerbose = config.debug === 'verbose';\n\t\t// The deadline comes from the server, read only once the client exists: pick the fetch\n\t\t// for the longest one. Calls without a signal keep undici's limits either way.\n\t\tconst longFetch = fetchForDeadline(MAX_SOLVE_DEADLINE_MS);\n\n\t\tconst clientConfig: GrasshopperComputeConfig = {\n\t\t\tserverUrl: server.serverUrl,\n\t\t\tapiKey: server.apiKey,\n\t\t\t// Lib-level debug is the verbose one; keep it on the verbose flag so the\n\t\t\t// concise cache logs can run without the noise.\n\t\t\tdebug: debugVerbose,\n\t\t\tcachesolve: config.cachesolve,\n\t\t\tmaxResponseBytes: config.maxResponseBytes,\n\t\t\t...(longFetch && { fetch: longFetch })\n\t\t};\n\n\t\tif (config.debug) {\n\t\t\tclientConfig.onServerTiming = (t) => {\n\t\t\t\tconst routed = t.affinity ? ` | session ${t.affinity}` : '';\n\t\t\t\tif (t.cacheHit) {\n\t\t\t\t\tdebugLog(\n\t\t\t\t\t\t`[Compute/rhino-cache] solve-cache (cachesolve): HIT (cached result, solve skipped)${routed}`\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (!reportsTiming(t)) {\n\t\t\t\t\tdebugLog(`[Compute/rhino-cache] no solve timing${routed}`);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst decode = t.decode ?? 0;\n\t\t\t\tconst solve = t.solve ?? 0;\n\t\t\t\tconst encode = t.encode ?? 0;\n\t\t\t\t// Heuristic threshold: a genuine parse is many ms; a reused definition is\n\t\t\t\t// sub-millisecond. Only meaningful relative to the first (cold) solve.\n\t\t\t\tconst defCache =\n\t\t\t\t\tdecode <= 1 ? 'HIT (definition reused, no re-upload/parse)' : 'miss (full parse)';\n\t\t\t\tconst solveCache = config.cachesolve ? 'miss (solved fresh)' : 'off';\n\t\t\t\tconst ran = t.recomputed ? ` | ran ${t.recomputed.ran}/${t.recomputed.total} objects` : '';\n\t\t\t\tdebugLog(\n\t\t\t\t\t`[Compute/rhino-cache] decode=${decode}ms solve=${solve}ms encode=${encode}ms ` +\n\t\t\t\t\t\t`| definition-cache: ${defCache} | solve-cache (cachesolve): ${solveCache}${ran}${routed}`\n\t\t\t\t);\n\t\t\t};\n\t\t}\n\n\t\tconst client = await GrasshopperClient.create(clientConfig);\n\n\t\tconst probe = createConcurrencyProbe(client, debugLog);\n\t\tconst readReported = async () =>\n\t\t\t(await Promise.resolve(client.serverStats?.getLimits?.()).catch(() => null)) ?? null;\n\t\tconst [reported] = await Promise.all([readReported(), probe.refresh()]);\n\t\tconst limits = solveLimitsFor(reported);\n\t\tdebugLog(\n\t\t\t`[Compute/client-cache] server ${server.id}: deadline ${limits.solveDeadlineMs}ms, ` +\n\t\t\t\t`request cap ${limits.maxRequestBytes ?? 'not reported'}, pointer reuse ${limits.reuseDefinitionCache}`\n\t\t);\n\t\tlet limitsCheckedAt = Date.now();\n\t\tlet recheck: Promise<boolean> | null = null;\n\t\t/** Re-reads the limits; true when they changed, which retires this client. */\n\t\tconst recheckLimits = () =>\n\t\t\t(recheck ??= readReported()\n\t\t\t\t.then((next) => {\n\t\t\t\t\tlimitsCheckedAt = Date.now();\n\t\t\t\t\t// Unanswered by a server that did answer: down or restarting, not reconfigured.\n\t\t\t\t\t// Its defaults would switch pointer reuse off until the next recheck.\n\t\t\t\t\tif (next === null && reported !== null) return false;\n\t\t\t\t\tif (sameSolveLimits(solveLimitsFor(next), limits)) return false;\n\t\t\t\t\tdebugLog(`[Compute/client-cache] server ${server.id} limits changed — rebuilding`);\n\t\t\t\t\tif (cache.get(server.id) === entry) evictEntry(server.id);\n\t\t\t\t\treturn true;\n\t\t\t\t})\n\t\t\t\t.finally(() => {\n\t\t\t\t\trecheck = null;\n\t\t\t\t}));\n\t\tconst recheckLimitsIfDue = () => {\n\t\t\tif (Date.now() - limitsCheckedAt >= LIMITS_RECHECK_MS) void recheckLimits();\n\t\t};\n\n\t\tconst scheduler = client.createScheduler({\n\t\t\tmode: 'queue',\n\t\t\t// Queue mode defaults maxConcurrent to 1, serializing every solve on\n\t\t\t// this server through a single slot while the compute VM's other\n\t\t\t// compute.geometry children idle — always pass it explicitly.\n\t\t\tmaxConcurrent: probe.maxConcurrent,\n\t\t\t// The scheduler treats undefined as unbounded/no-deadline, so map our\n\t\t\t// `0`-means-off convention to undefined rather than passing 0 (which\n\t\t\t// would reject EVERY queued solve).\n\t\t\tmaxQueueDepth: config.maxQueueDepth || undefined,\n\t\t\tqueueWaitMs: config.queueWaitMs || undefined,\n\t\t\ttimeoutMs: limits.solveDeadlineMs,\n\t\t\t// The byte budget is the only eviction pressure. A solve is a pure function of\n\t\t\t// (definition, inputs) on one deployment: expiring a result only forces a paid\n\t\t\t// re-solve of the same answer. A redeployed plugin is another deployment, so the\n\t\t\t// client is retired with its cache (`deploymentChecks`).\n\t\t\tcache: cacheEnabled ? { maxBytes: config.responseCacheMaxBytes } : false,\n\t\t\treuseServerDefinitionCache: limits.reuseDefinitionCache,\n\t\t\tonSettle: (_ctx, result) => {\n\t\t\t\t// Deferred: this hook fires before the scheduler drops the solve from its in-flight set.\n\t\t\t\tqueueMicrotask(() => finishDraining(entry));\n\t\t\t\tif (result.status !== 'success') return;\n\t\t\t\t// A cache hit means we never reached the server, so it says nothing about the pool.\n\t\t\t\t// `scheduler` is safe despite being referenced from inside its own initializer:\n\t\t\t\t// onSettle only ever fires during a solve, long after binding.\n\t\t\t\tif (result.fromCache !== true) {\n\t\t\t\t\tprobe.refreshIfDue(scheduler);\n\t\t\t\t\trecheckLimitsIfDue();\n\t\t\t\t}\n\t\t\t\tif (config.debug) describeSettledSolve(result).forEach(debugLog);\n\t\t\t}\n\t\t});\n\t\tconst entry: CachedClient = { client, scheduler, limits };\n\t\tdeploymentChecks.set(entry, async () => {\n\t\t\tif (Date.now() - limitsCheckedAt < DEPLOYMENT_RECHECK_MS) return true;\n\t\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\t\t\tconst waited = new Promise<false>((resolve) => {\n\t\t\t\ttimer = setTimeout(() => resolve(false), DEPLOYMENT_CHECK_WAIT_MS);\n\t\t\t});\n\t\t\ttry {\n\t\t\t\treturn !(await Promise.race([recheckLimits(), waited]));\n\t\t\t} finally {\n\t\t\t\tclearTimeout(timer);\n\t\t\t}\n\t\t});\n\t\treturn entry;\n\t}\n\n\tfunction evictEntry(id: string) {\n\t\tconst entry = cache.get(id);\n\t\tif (!entry) return;\n\t\tcache.delete(id);\n\t\tretire(entry);\n\t}\n\n\tconst self: ClientCache = {\n\t\tasync getClient(server): Promise<CachedClient> {\n\t\t\tconst key = server.id;\n\t\t\tconst existing = cache.get(key);\n\t\t\tif (existing) {\n\t\t\t\t// Refresh LRU position.\n\t\t\t\tcache.delete(key);\n\t\t\t\tcache.set(key, existing);\n\t\t\t\t// Only a client whose cache could answer waits on its server's deployment.\n\t\t\t\tif (!cacheEnabled || (await deploymentChecks.get(existing)?.()) !== false) return existing;\n\t\t\t\t// Retired by the check: whatever now stands for `key` serves, or a fresh build.\n\t\t\t\treturn self.getClient(server);\n\t\t\t}\n\n\t\t\tconst inFlight = pending.get(key);\n\t\t\tif (inFlight) return inFlight.promise;\n\n\t\t\t// Replay a recent failure instead of re-running the probe ladder. The\n\t\t\t// original error is rethrown verbatim so the caller still sees the real\n\t\t\t// cause (refused / unauthorized / timeout), not a generic cache miss.\n\t\t\tconst recentFailure = failed.get(key);\n\t\t\tif (recentFailure) {\n\t\t\t\tif (Date.now() - recentFailure.at < FAILED_BUILD_TTL_MS) {\n\t\t\t\t\tdebugLog(\n\t\t\t\t\t\t`[Compute/client-cache] server ${key} failed to build <${FAILED_BUILD_TTL_MS}ms ago — replaying error`\n\t\t\t\t\t);\n\t\t\t\t\tthrow recentFailure.error;\n\t\t\t\t}\n\t\t\t\tfailed.delete(key);\n\t\t\t}\n\n\t\t\t// A build is a Rhino.Compute handshake — expensive, and repeated builds for\n\t\t\t// the same id mean churn (LRU thrash or config-rotation evictions).\n\t\t\tdebugLog(`[Compute/client-cache] miss — building warm client for server ${key}`);\n\t\t\tconst token = { stale: false };\n\t\t\tconst promise = (async () => {\n\t\t\t\tlet entry: CachedClient;\n\t\t\t\ttry {\n\t\t\t\t\tentry = await build(server);\n\t\t\t\t} catch (err) {\n\t\t\t\t\tif (!token.stale) failed.set(key, { error: err, at: Date.now() });\n\t\t\t\t\tthrow err;\n\t\t\t\t}\n\n\t\t\t\t// Installing it would carry the old URL or key past the eviction, with nothing left to\n\t\t\t\t// evict it again. Its caller holds a client built from those same details, so it fails too.\n\t\t\t\tif (token.stale) {\n\t\t\t\t\tentry.scheduler.dispose();\n\t\t\t\t\tthrow new ComputeError(\n\t\t\t\t\t\t`Compute server ${key} changed while connecting.`,\n\t\t\t\t\t\tErrorCodes.ABORTED\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tif (cache.size >= maxWarmComputeServers) {\n\t\t\t\t\tconst oldestKey = cache.keys().next().value;\n\t\t\t\t\tif (oldestKey !== undefined) {\n\t\t\t\t\t\tdebugLog(\n\t\t\t\t\t\t\t`[Compute/client-cache] LRU evicted warm client for server ${oldestKey} (cap ${maxWarmComputeServers})`\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst oldest = cache.get(oldestKey);\n\t\t\t\t\t\tcache.delete(oldestKey);\n\t\t\t\t\t\tif (oldest) retire(oldest);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tcache.set(key, entry);\n\t\t\t\treturn entry;\n\t\t\t})().finally(() => {\n\t\t\t\tif (pending.get(key)?.token === token) pending.delete(key);\n\t\t\t});\n\t\t\tpending.set(key, { promise, token });\n\t\t\treturn promise;\n\t\t},\n\n\t\tevict(id): void {\n\t\t\t// Drop any remembered failure too: `evict` means the connection details\n\t\t\t// changed, so the reason the last build failed no longer applies.\n\t\t\tfailed.delete(id);\n\t\t\tconst build = pending.get(id);\n\t\t\tif (build) {\n\t\t\t\tbuild.token.stale = true;\n\t\t\t\tpending.delete(id);\n\t\t\t}\n\t\t\tif (cache.has(id)) {\n\t\t\t\tdebugLog(`[Compute/client-cache] evicted warm client for server ${id} (config change)`);\n\t\t\t\tevictEntry(id);\n\t\t\t}\n\t\t},\n\n\t\tsolveCacheStats(): SolveCacheStats {\n\t\t\tconst total: SolveCacheStats = {\n\t\t\t\twarmClients: cache.size,\n\t\t\t\tentries: 0,\n\t\t\t\tbytes: 0,\n\t\t\t\thits: 0,\n\t\t\t\tmisses: 0,\n\t\t\t\tevictions: 0\n\t\t\t};\n\t\t\tfor (const entry of cache.values()) {\n\t\t\t\tconst s = entry.scheduler.cacheStats();\n\t\t\t\ttotal.entries += s.entries;\n\t\t\t\ttotal.bytes += s.bytes;\n\t\t\t\ttotal.hits += s.hits;\n\t\t\t\ttotal.misses += s.misses;\n\t\t\t\ttotal.evictions += s.evictions;\n\t\t\t}\n\t\t\treturn total;\n\t\t},\n\n\t\tclearSolveCaches(): void {\n\t\t\tdebugLog(`[Compute/client-cache] cleared solve caches on ${cache.size} warm client(s)`);\n\t\t\tfor (const entry of cache.values()) entry.scheduler.clearCache();\n\t\t},\n\n\t\tdisposeAll(): void {\n\t\t\tdebugLog(`[Compute/client-cache] disposing all ${cache.size} warm client(s)`);\n\t\t\tfor (const entry of [...cache.values(), ...draining]) entry.scheduler.dispose();\n\t\t\tfor (const build of pending.values()) build.token.stale = true;\n\t\t\tpending.clear();\n\t\t\tcache.clear();\n\t\t\tdraining.clear();\n\t\t\tfailed.clear();\n\t\t}\n\t};\n\treturn self;\n}\n","/**\n * In-process cache of `.gh` definition bytes, keyed by immutable **version id**.\n *\n * Turns an eager `storage.get(fileKey)` on every solve request into a lazy\n * `DefinitionRef.load()`: the scheduler calls `load()` only when an upload is\n * unavoidable (a pointer-known re-solve never touches storage or this cache).\n *\n * The key MUST be the version id, never a `fileKey` — a delete-latest-then-\n * reupload can reuse a `fileKey` for different content, which would silently\n * serve one version's bytes for another with no diagnostic. Callers pass\n * `version.id`, and the returned `DefinitionRef.key` is that same id, so the\n * scheduler's own result/pointer cache keys on the same immutable identity.\n *\n * Eviction is LRU by total byte budget, not entry count — definitions range\n * from tens of KB to hundreds of MB, so a count cap would either pin\n * gigabytes or evict uselessly. `COMPUTE_DEFINITION_CACHE_MB` sets the\n * budget (see `createDefinitionByteCache`). No TTL: version ids are\n * immutable, so a cached entry can never go stale. An entry larger than the\n * whole budget is served but never retained.\n */\n\nimport { raceAbort } from '../shared/abort.js';\n\n/**\n * Per-`getOrLoad` outcome, observable after the scheduler has (or hasn't)\n * called `load()` — lets the solve pipeline emit a `def_bytes` Server-Timing\n * verdict (`skipped` / `hit` / `miss`).\n */\nexport interface ByteRefOutcome {\n\tloaded: boolean;\n\tfromCache: boolean;\n}\n\n/** A definition reference shaped for `@selvajs/compute`'s `DefinitionRef`. */\nexport interface ByteCacheRef {\n\tkey: string;\n\tload: (signal?: AbortSignal) => Promise<Uint8Array>;\n\toutcome: ByteRefOutcome;\n}\n\nexport interface ByteCacheStats {\n\thits: number;\n\tmisses: number;\n\tevictions: number;\n\tentries: number;\n\tbytes: number;\n}\n\nexport interface DefinitionByteCache {\n\t/** `load` gets a signal that fires once every caller waiting on this read has gone. */\n\tgetOrLoad(versionId: string, load: (signal: AbortSignal) => Promise<Uint8Array>): ByteCacheRef;\n\tstats(): ByteCacheStats;\n\tclear(): void;\n}\n\n/** A `ByteCacheRef` carries `outcome`, which a plain `DefinitionRef` never has. */\nexport function isByteCacheRef(source: unknown): source is ByteCacheRef {\n\treturn typeof source === 'object' && source !== null && 'outcome' in source;\n}\n\n/** One storage read, shared by every caller that misses while it runs. */\ninterface Loading {\n\tpromise: Promise<Uint8Array>;\n\tcontroller: AbortController;\n\twaiters: number;\n}\n\n/**\n * @param maxBytes total retained-byte budget; `0` (or negative) disables caching\n * entirely — every `load()` calls the loader and nothing is retained.\n */\nexport function createDefinitionByteCache(maxBytes: number): DefinitionByteCache {\n\tconst budget = Math.max(0, Math.floor(maxBytes));\n\t// Map iteration order is insertion order, so it doubles as LRU order:\n\t// re-inserting a key on access moves it to the most-recently-used end.\n\tconst cache = new Map<string, Uint8Array>();\n\tconst loading = new Map<string, Loading>();\n\tlet retainedBytes = 0;\n\tconst stats = { hits: 0, misses: 0, evictions: 0 };\n\n\tfunction evictToFit(): void {\n\t\tfor (const [key, bytes] of cache) {\n\t\t\tif (retainedBytes <= budget) break;\n\t\t\tcache.delete(key);\n\t\t\tretainedBytes -= bytes.byteLength;\n\t\t\tstats.evictions += 1;\n\t\t}\n\t}\n\n\tfunction store(key: string, bytes: Uint8Array): void {\n\t\tif (budget === 0) return;\n\t\t// Caching an entry bigger than the budget would evict everything\n\t\t// (including itself) on the next evictToFit — serve it, cache nothing.\n\t\tif (bytes.byteLength > budget) return;\n\t\tcache.set(key, bytes);\n\t\tretainedBytes += bytes.byteLength;\n\t\tevictToFit();\n\t}\n\n\tfunction startLoad(key: string, load: (signal: AbortSignal) => Promise<Uint8Array>): Loading {\n\t\tconst controller = new AbortController();\n\t\tconst entry: Loading = {\n\t\t\tcontroller,\n\t\t\twaiters: 0,\n\t\t\tpromise: (async () => {\n\t\t\t\tconst bytes = await load(controller.signal);\n\t\t\t\tstore(key, bytes);\n\t\t\t\treturn bytes;\n\t\t\t})().finally(() => {\n\t\t\t\tif (loading.get(key) === entry) loading.delete(key);\n\t\t\t})\n\t\t};\n\t\tloading.set(key, entry);\n\t\treturn entry;\n\t}\n\n\t/**\n\t * A caller gone stops waiting, but the read runs on for whoever else is on it. A caller with no\n\t * signal can't leave, so it stays counted and the read is never aborted under it.\n\t */\n\tasync function wait(entry: Loading, signal?: AbortSignal): Promise<Uint8Array> {\n\t\tentry.waiters += 1;\n\t\tif (!signal) return entry.promise;\n\t\ttry {\n\t\t\treturn await raceAbort(entry.promise, signal);\n\t\t} finally {\n\t\t\tentry.waiters -= 1;\n\t\t\tif (entry.waiters === 0 && signal.aborted) entry.controller.abort();\n\t\t}\n\t}\n\n\treturn {\n\t\tgetOrLoad(versionId, load) {\n\t\t\tconst outcome: ByteRefOutcome = { loaded: false, fromCache: false };\n\t\t\treturn {\n\t\t\t\tkey: versionId,\n\t\t\t\toutcome,\n\t\t\t\tload: async function read(signal): Promise<Uint8Array> {\n\t\t\t\t\toutcome.loaded = true;\n\t\t\t\t\tconst hit = cache.get(versionId);\n\t\t\t\t\tif (hit) {\n\t\t\t\t\t\tcache.delete(versionId);\n\t\t\t\t\t\tcache.set(versionId, hit);\n\t\t\t\t\t\tstats.hits += 1;\n\t\t\t\t\t\toutcome.fromCache = true;\n\t\t\t\t\t\treturn hit;\n\t\t\t\t\t}\n\t\t\t\t\tconst running = loading.get(versionId);\n\t\t\t\t\tif (running?.controller.signal.aborted) {\n\t\t\t\t\t\t// Abandoned, and a store that ignores its signal may never finish. Waiting it out\n\t\t\t\t\t\t// rather than starting a second read keeps a hung store to one read per version.\n\t\t\t\t\t\tconst woundDown = running.promise.then(\n\t\t\t\t\t\t\t() => undefined,\n\t\t\t\t\t\t\t() => undefined\n\t\t\t\t\t\t);\n\t\t\t\t\t\tawait (signal ? raceAbort(woundDown, signal) : woundDown);\n\t\t\t\t\t\treturn read(signal);\n\t\t\t\t\t}\n\t\t\t\t\tif (running) {\n\t\t\t\t\t\tstats.hits += 1;\n\t\t\t\t\t\toutcome.fromCache = true;\n\t\t\t\t\t\treturn wait(running, signal);\n\t\t\t\t\t}\n\t\t\t\t\tstats.misses += 1;\n\t\t\t\t\treturn wait(startLoad(versionId, load), signal);\n\t\t\t\t}\n\t\t\t};\n\t\t},\n\t\tstats() {\n\t\t\treturn {\n\t\t\t\thits: stats.hits,\n\t\t\t\tmisses: stats.misses,\n\t\t\t\tevictions: stats.evictions,\n\t\t\t\tentries: cache.size,\n\t\t\t\tbytes: retainedBytes\n\t\t\t};\n\t\t},\n\t\tclear() {\n\t\t\tcache.clear();\n\t\t\tretainedBytes = 0;\n\t\t}\n\t};\n}\n","/**\n * Turns one solve's response into each caller's wire form. Runs after single-flight, not inside\n * it: coalesced callers can want different encodings, and each reports its own timing.\n */\n\nimport { gzip } from 'node:zlib';\nimport { promisify } from 'node:util';\nimport type { ServerTiming } from '@selvajs/compute/core';\nimport type { GrasshopperComputeResponse } from '@selvajs/compute/grasshopper';\nimport type { CacheVerdict, DefBytesVerdict } from './server-timing.js';\n\n// gzipSync blocks the event loop for multi-MB bodies; promisify(gzip) doesn't.\nconst gzipAsync = promisify(gzip);\n\n/**\n * Display output is base64 of already-deflated bytes, where higher levels find nothing more:\n * level 1 lands within 1% of the default's size, in up to a fifth of the time.\n */\nconst GZIP_LEVEL = 1;\n\n/** Below this gzip isn't worth the CPU for the wire savings. */\nconst GZIP_MIN_BYTES = 1024;\n\n// ============================================================================\n// Wire contract version\n// ============================================================================\n\n/** Compute-response contract version. Bump (and document the change) whenever the envelope's shape changes in a way a consumer could observe. */\nexport const COMPUTE_CONTRACT_VERSION = 1 as const;\nexport const COMPUTE_VERSION_HEADER = 'X-Selva-Compute-Version';\n\n// ============================================================================\n// Envelope\n// ============================================================================\n\n/** What the `Server-Timing` header carries for one caller, as data: read this, don't parse that. */\nexport interface SolvePhaseMetrics {\n\tdefLoadMs: number;\n\ttreeBuildMs: number;\n\tsolveMs: number;\n\tserializeMs: number;\n\tgzipMs: number;\n\tserverTotalMs: number;\n\tserializedBytes: number;\n\t/** Null when compression was skipped. */\n\tcompressedBytes: number | null;\n\t/** The compute server's own timing; null when it reports none. */\n\trhino: ServerTiming | null;\n\tsettle: CacheVerdict;\n\t/** Absent for raw-bytes solves. */\n\tdefBytes?: DefBytesVerdict;\n}\n\n/** A ready-to-send response: body + headers + the solve result + phase metrics. */\nexport interface SolveEnvelope {\n\t/** A gzip `Uint8Array` when `encoding === 'gzip'`, else the JSON string. */\n\tbody: string | Uint8Array;\n\tencoding?: 'gzip';\n\theaders: Record<string, string>;\n\tresult: GrasshopperComputeResponse;\n\tmetrics: SolvePhaseMetrics;\n}\n\nexport interface EncodedBody {\n\tbody: string | Uint8Array;\n\tencoding?: 'gzip';\n\tserializedBytes: number;\n\tserializeMs: number;\n\tgzipMs: number;\n}\n\nexport type TooLarge = 'too_large';\n\nexport function buildEnvelope(\n\tencoded: EncodedBody,\n\tresult: GrasshopperComputeResponse,\n\tserverTiming: string,\n\tmetrics: SolvePhaseMetrics\n): SolveEnvelope {\n\tconst headers: Record<string, string> = {\n\t\t'Content-Type': 'application/json',\n\t\t'Server-Timing': serverTiming,\n\t\t[COMPUTE_VERSION_HEADER]: String(COMPUTE_CONTRACT_VERSION),\n\t\tVary: 'Accept-Encoding'\n\t};\n\tconst { body, encoding } = encoded;\n\tif (encoding === 'gzip') {\n\t\t// Content-Encoding tells a downstream proxy the body is already compressed,\n\t\t// so it won't gzip it again.\n\t\theaders['Content-Encoding'] = 'gzip';\n\t}\n\theaders['Content-Length'] = String(\n\t\ttypeof body === 'string' ? Buffer.byteLength(body) : body.byteLength\n\t);\n\treturn { body, ...(encoding ? { encoding } : {}), headers, result, metrics };\n}\n\n// ============================================================================\n// Encoding\n// ============================================================================\n\n/** Whether an `Accept-Encoding` header takes gzip. `gzip;q=0` is a client refusing it. */\nexport function acceptsGzip(acceptEncoding: string): boolean {\n\treturn acceptEncoding.split(',').some((part) => {\n\t\tconst [coding, ...params] = part.split(';').map((s) => s.trim().toLowerCase());\n\t\tif (coding !== 'gzip') return false;\n\t\tconst q = params.find((p) => p.startsWith('q='));\n\t\treturn q === undefined || Number(q.slice(2)) > 0;\n\t});\n}\n\ninterface GzippedBody {\n\tbody: Uint8Array;\n\tserializedBytes: number;\n}\n\n/**\n * A scheduler cache hit returns the same response object, so a repeat solve reuses its bytes rather\n * than paying to serialize and compress them again. Entries live exactly as long as the response.\n * Only the gzip form is kept: browsers always take it, and the JSON string would double the memory.\n * Holds the promise, so callers arriving during the compression share it.\n */\nconst gzippedBodies = new WeakMap<object, Promise<GzippedBody | TooLarge>>();\n\nfunction gzipOnce(response: object, json: string, serializedBytes: number) {\n\tconst pending = gzipAsync(Buffer.from(json), { level: GZIP_LEVEL }).then((body) => ({\n\t\tbody,\n\t\tserializedBytes\n\t}));\n\tgzippedBodies.set(response, pending);\n\t// A failed compression must not stay memoized while the response is cached.\n\tpending.catch(() => gzippedBodies.delete(response));\n\treturn pending;\n}\n\nexport interface ResponseEncoder {\n\t/** Buffered, not streamed, so Content-Length is known upfront and a connection cut mid-transfer fails hard instead of silently truncating the JSON. */\n\tencode(acceptEncoding: string): Promise<EncodedBody | TooLarge>;\n}\n\n/**\n * One per solve. The JSON is kept here rather than beside the gzip body: it only has to outlive the\n * callers of this flight, who would otherwise each stringify the same response and hold a copy.\n */\nexport function createResponseEncoder(response: object, maxBytes: number): ResponseEncoder {\n\tlet serialized: { json: string; bytes: number } | TooLarge | undefined;\n\n\tfunction serialize() {\n\t\tif (serialized !== undefined) return serialized;\n\t\ttry {\n\t\t\tconst json = JSON.stringify(response);\n\t\t\tconst bytes = Buffer.byteLength(json);\n\t\t\tserialized = bytes > maxBytes ? 'too_large' : { json, bytes };\n\t\t} catch (err) {\n\t\t\t// V8 caps a string at ~512 MB; an oversized `file` output trips a RangeError\n\t\t\t// here. Treat it the same as the byte cap.\n\t\t\tif (!(err instanceof RangeError)) throw err;\n\t\t\tserialized = 'too_large';\n\t\t}\n\t\treturn serialized;\n\t}\n\n\treturn {\n\t\tasync encode(acceptEncoding) {\n\t\t\tconst wantsGzip = acceptsGzip(acceptEncoding);\n\t\t\tlet gzipped = wantsGzip ? gzippedBodies.get(response) : undefined;\n\t\t\tlet serializeMs = 0;\n\n\t\t\tif (!gzipped) {\n\t\t\t\tconst serializeStart = performance.now();\n\t\t\t\tconst plain = serialize();\n\t\t\t\tserializeMs = performance.now() - serializeStart;\n\t\t\t\tif (plain === 'too_large') {\n\t\t\t\t\tif (wantsGzip) gzippedBodies.set(response, Promise.resolve('too_large'));\n\t\t\t\t\treturn 'too_large';\n\t\t\t\t}\n\t\t\t\tif (!wantsGzip || plain.bytes <= GZIP_MIN_BYTES) {\n\t\t\t\t\treturn { body: plain.json, serializedBytes: plain.bytes, serializeMs, gzipMs: 0 };\n\t\t\t\t}\n\t\t\t\tgzipped = gzipOnce(response, plain.json, plain.bytes);\n\t\t\t}\n\n\t\t\tconst gzipStart = performance.now();\n\t\t\tconst verdict = await gzipped;\n\t\t\tif (verdict === 'too_large') return verdict;\n\t\t\treturn {\n\t\t\t\tbody: verdict.body,\n\t\t\t\tencoding: 'gzip',\n\t\t\t\tserializedBytes: verdict.serializedBytes,\n\t\t\t\tserializeMs,\n\t\t\t\tgzipMs: performance.now() - gzipStart\n\t\t\t};\n\t\t}\n\t};\n}\n","/**\n * In-process single-flight — dogpile protection for every solve.\n *\n * Always on, not gated behind a result cache being configured: the dogpile is worst\n * when nothing else is caching, since N concurrent identical solves each pay a full\n * Rhino round trip (the scheduler doesn't coalesce in-flight requests itself, and\n * Rhino's `cachesolve` still costs a round trip per repeat).\n *\n * Per app instance. No cross-instance lease (Redis `SET NX`) yet.\n *\n * The shared promise resolves to one value for every caller, so `work` must return\n * something safe to share by reference.\n */\n\nexport interface SolveCacheSingleFlight {\n\t/**\n\t * Coalesces concurrent calls under the same key: the first caller runs `work`, later\n\t * callers await the same promise, and the key frees as soon as it settles.\n\t *\n\t * The flight belongs to all its callers. It aborts `work`'s signal when the last one's\n\t * `callerSignal` fires, and frees the key right then, so a caller arriving after that starts\n\t * a fresh flight instead of joining one that is dying. A caller that leaves earlier still\n\t * resolves with the flight: check its own signal before using the result.\n\t */\n\trun<T>(\n\t\tkey: string,\n\t\twork: (signal: AbortSignal) => Promise<T>,\n\t\tcallerSignal: AbortSignal\n\t): Promise<T>;\n\tinFlight(): number;\n}\n\nexport interface SolveCacheSingleFlightOptions {\n\tonJoin?: (key: string) => void;\n}\n\ninterface Flight {\n\tpromise: Promise<unknown>;\n\tcontroller: AbortController;\n\tcallers: number;\n}\n\nexport function createSolveCacheSingleFlight(\n\toptions: SolveCacheSingleFlightOptions = {}\n): SolveCacheSingleFlight {\n\tconst inflight = new Map<string, Flight>();\n\n\tfunction free(key: string, flight: Flight) {\n\t\t// Guard against deleting a key a later caller has already re-taken.\n\t\tif (inflight.get(key) === flight) inflight.delete(key);\n\t}\n\n\tfunction attend<T>(key: string, flight: Flight, signal: AbortSignal): Promise<T> {\n\t\tflight.callers += 1;\n\t\t// `once: true` doesn't detach on a normal completion, and the caller's signal outlives\n\t\t// this call, so without the `finally` every solve leaks a listener onto it.\n\t\tconst leave = () => {\n\t\t\tflight.callers -= 1;\n\t\t\tif (flight.callers > 0) return;\n\t\t\tfree(key, flight);\n\t\t\tflight.controller.abort();\n\t\t};\n\t\tsignal.addEventListener('abort', leave, { once: true });\n\t\treturn (flight.promise as Promise<T>).finally(() => signal.removeEventListener('abort', leave));\n\t}\n\n\treturn {\n\t\trun<T>(key: string, work: (signal: AbortSignal) => Promise<T>, callerSignal: AbortSignal) {\n\t\t\tconst existing = inflight.get(key);\n\t\t\tif (existing) {\n\t\t\t\ttry {\n\t\t\t\t\toptions.onJoin?.(key);\n\t\t\t\t} catch (err) {\n\t\t\t\t\t// Telemetry must not fail a solve that is otherwise fine.\n\t\t\t\t\tconsole.error('[Solve/single-flight] onJoin threw:', err);\n\t\t\t\t}\n\t\t\t\treturn attend<T>(key, existing, callerSignal);\n\t\t\t}\n\n\t\t\tconst controller = new AbortController();\n\t\t\tconst flight: Flight = {\n\t\t\t\tcontroller,\n\t\t\t\tcallers: 0,\n\t\t\t\t// Wrapped so a synchronous throw in `work` still rejects instead of escaping `run`.\n\t\t\t\tpromise: (async () => work(controller.signal))().finally(() => free(key, flight))\n\t\t\t};\n\t\t\tinflight.set(key, flight);\n\t\t\treturn attend<T>(key, flight, callerSignal);\n\t\t},\n\t\tinFlight(): number {\n\t\t\treturn inflight.size;\n\t\t}\n\t};\n}\n","import { TreeBuilder, type DataTree } from '@selvajs/compute/grasshopper';\nimport { getInputItems, type UISchema } from '@selvajs/schemas';\nimport type { SolveInput } from '../shared/solve-input.js';\nimport { serverSourcedInputIds } from '../shared/server-sourced.js';\n\n/**\n * What `SolveEngine.solve` takes as `inputs`, from a schema the host stored. The layout, not\n * `schema.inputs`, carries the number widgets' bounds and each input's source: without this,\n * nothing clamps and a `server` input takes the caller's value.\n */\nexport function solveInputsFromSchema(schema: UISchema): SolveInput[] {\n\tconst bounds = new Map<string, { minimum?: number; maximum?: number }>();\n\tfor (const item of getInputItems(schema)) {\n\t\tif (item.widgetType === 'number' && item.config) bounds.set(item.paramId, item.config);\n\t}\n\tconst serverSourced = serverSourcedInputIds(schema);\n\treturn schema.inputs.map((input) => {\n\t\tconst widget = bounds.get(input.id);\n\t\tif (!widget && !serverSourced.has(input.id)) return input;\n\t\treturn {\n\t\t\t...input,\n\t\t\t...(widget ? { minimum: widget.minimum, maximum: widget.maximum } : {}),\n\t\t\t...(serverSourced.has(input.id) ? { serverSourced: true } : {})\n\t\t};\n\t});\n}\n\n/**\n * Ids in `values` that set a server-sourced input. A host refuses a request carrying any: the\n * input tree would ignore them, and the caller should hear so rather than solve on the default.\n */\nexport function postedServerSourcedIds(\n\tinputs: SolveInput[],\n\tvalues: Record<string, unknown>\n): string[] {\n\treturn inputs\n\t\t.filter((input) => input.serverSourced && Object.hasOwn(values, input.id))\n\t\t.map((input) => input.id);\n}\n\n/**\n * The input tree a solve sends: each input's value, or its schema default when it has none. A\n * server-sourced input always takes its default.\n */\nexport function buildSolveInputTree(\n\tinputs: SolveInput[],\n\tvalues: Record<string, unknown>\n): DataTree[] {\n\treturn TreeBuilder.fromValues(\n\t\tinputs\n\t\t\t// Stored schemas are validated at upload only, so an older one can lack `paramType`.\n\t\t\t.filter((input) => input.paramType)\n\t\t\t.map((input) => ({\n\t\t\t\tparamName: input.nickname,\n\t\t\t\tparamType: input.paramType,\n\t\t\t\t// `hasOwn`: an input id of `constructor` would read the inherited function.\n\t\t\t\tvalue:\n\t\t\t\t\t(!input.serverSourced && Object.hasOwn(values, input.id)\n\t\t\t\t\t\t? values[input.id]\n\t\t\t\t\t\t: undefined) ?? input.default,\n\t\t\t\tminimum: input.minimum,\n\t\t\t\tmaximum: input.maximum\n\t\t\t}))\n\t);\n}\n","/**\n * Runs one solve against a warm scheduler and classifies how it ended: a solved response, or an\n * expected failure as a value instead of a throw. Single-flight shares this part, so its args come\n * from the caller that started the flight. Anything per caller must be in the coalesce key, or,\n * like `sessionId`, be harmless for the joiners to inherit.\n */\n\nimport {\n\tErrorCodes,\n\tComputeError,\n\ttype ServerTiming,\n\ttype SolveDefinition\n} from '@selvajs/compute/core';\nimport type {\n\tDataTree,\n\tGrasshopperComputeResponse,\n\tSelvaEventTarget\n} from '@selvajs/compute/grasshopper';\nimport type { CachedClient } from './client-cache.js';\nimport { isByteCacheRef } from './definition-byte-cache.js';\nimport {\n\tcreateResponseEncoder,\n\ttype ResponseEncoder,\n\ttype SolveEnvelope\n} from './response-encoding.js';\nimport type { CacheVerdict, DefBytesVerdict } from './server-timing.js';\n\nexport interface SolvePipelineArgs {\n\t/**\n\t * Raw `.gh` bytes, or a byte-cache `DefinitionRef` whose bytes the scheduler\n\t * materializes only when an upload is unavoidable — a pointer-known solve\n\t * moves zero bytes.\n\t */\n\tdefinitionSource: SolveDefinition;\n\tinputTree: DataTree[];\n\tclient: CachedClient;\n\tresponseMaxBytes: number;\n\t/** Per-solve live-event callback the compute server hands to the definition; absent when the caller opened no stream. */\n\tselvaEvents?: SelvaEventTarget;\n\t/** The starting caller's. Joiners route on it too, which only costs them a warm child. */\n\tsessionId?: string;\n\t/** Request headers for this solve only, e.g. `X-Selva-Definition`. */\n\theaders?: Record<string, string>;\n\t/**\n\t * Forwarded to the scheduler so a client disconnect cancels the upstream\n\t * compute call. Its `aborted` flag also tells that apart from an abort nobody asked for.\n\t */\n\tsignal: AbortSignal;\n}\n\n/**\n * `ok` carries the envelope; every other variant names an expected failure:\n * `SolveEngine.toWebResponse` maps each to a status. `durationMs` on error variants is the solve\n * wall time up to the failure, for the metric record.\n */\nexport type SolveEngineOutcome =\n\t| {\n\t\t\tkind: 'ok';\n\t\t\tenvelope: SolveEnvelope;\n\t\t\tsolveMs: number;\n\t\t\terrorCount: number;\n\t\t\twarningCount: number;\n\t }\n\t| SolveFailure;\n\nexport type SolveFailure =\n\t| { kind: 'timeout'; durationMs: number; message: string }\n\t| { kind: 'client_abort'; durationMs: number }\n\t| { kind: 'too_large' }\n\t/** The compute server refused the request body (its `RHINO_COMPUTE_MAX_REQUEST_SIZE`). */\n\t| { kind: 'request_too_large'; durationMs: number; message: string }\n\t/**\n\t * Retryable: no result, through no fault of the request, and `retryAfterSeconds` is a backoff\n\t * hint. Scheduler backpressure rejected the solve before it ran (`queue_full`,\n\t * `queue_timeout`), or its client was shut down under it (`interrupted`).\n\t */\n\t| {\n\t\t\tkind: 'shed';\n\t\t\tdurationMs: number;\n\t\t\treason: 'queue_full' | 'queue_timeout' | 'interrupted';\n\t\t\tretryAfterSeconds: number;\n\t\t\tmessage: string;\n\t }\n\t| { kind: 'compute_error'; durationMs: number; error: unknown };\n\n/** One finished solve, shared by every caller coalesced onto it. */\nexport interface SolvedFlight {\n\tkind: 'solved';\n\tresponse: GrasshopperComputeResponse;\n\tencoder: ResponseEncoder;\n\tsolveMs: number;\n\trhino: ServerTiming | null;\n\tsettle: CacheVerdict;\n\t/** The verdict of the ref that ran: a coalesced caller's own ref never loads anything. */\n\tdefBytes?: DefBytesVerdict;\n}\n\nfunction shed(\n\tdurationMs: number,\n\treason: 'queue_full' | 'queue_timeout' | 'interrupted',\n\tmessage: string\n): SolveFailure {\n\treturn { kind: 'shed', durationMs, reason, retryAfterSeconds: 1, message };\n}\n\n/** The solve lost its client, and no caller asked for that: a config write or a shutdown. */\nexport function interrupted(durationMs: number): SolveFailure {\n\treturn shed(durationMs, 'interrupted', 'The solve was interrupted. Retry shortly.');\n}\n\nfunction classifyFailure(err: unknown, durationMs: number, args: SolvePipelineArgs): SolveFailure {\n\tif (!(err instanceof ComputeError)) return { kind: 'compute_error', durationMs, error: err };\n\tif (err.statusCode === 413) {\n\t\tconst cap = args.client.limits.maxRequestBytes;\n\t\treturn {\n\t\t\tkind: 'request_too_large',\n\t\t\tdurationMs,\n\t\t\tmessage:\n\t\t\t\t`The inputs are larger than the compute server accepts` +\n\t\t\t\t(cap ? ` (${Math.round(cap / (1024 * 1024))} MB)` : '') +\n\t\t\t\t'. Its admin can raise RHINO_COMPUTE_MAX_REQUEST_SIZE.'\n\t\t};\n\t}\n\tswitch (err.code) {\n\t\tcase ErrorCodes.TIMEOUT_ERROR:\n\t\t\treturn {\n\t\t\t\tkind: 'timeout',\n\t\t\t\tdurationMs,\n\t\t\t\tmessage: `Solve exceeded the ${Math.round(args.client.limits.solveDeadlineMs / 1000)}s deadline set by the compute server.`\n\t\t\t};\n\t\tcase ErrorCodes.ABORTED:\n\t\t\treturn args.signal.aborted ? { kind: 'client_abort', durationMs } : interrupted(durationMs);\n\t\tcase ErrorCodes.RESPONSE_TOO_LARGE:\n\t\t\treturn { kind: 'too_large' };\n\t\t// Queue-full or queue-timeout rejects the solve before compute runs — a load\n\t\t// signal, not a failure, so it's `shed` rather than `compute_error`.\n\t\tcase ErrorCodes.QUEUE_FULL:\n\t\t\treturn shed(durationMs, 'queue_full', 'Compute server is at capacity. Retry shortly.');\n\t\tcase ErrorCodes.QUEUE_TIMEOUT:\n\t\t\treturn shed(\n\t\t\t\tdurationMs,\n\t\t\t\t'queue_timeout',\n\t\t\t\t'Compute request waited too long in the queue. Retry shortly.'\n\t\t\t);\n\t\tdefault:\n\t\t\treturn { kind: 'compute_error', durationMs, error: err };\n\t}\n}\n\nfunction defBytesVerdict(source: SolveDefinition): DefBytesVerdict | undefined {\n\tif (!isByteCacheRef(source)) return undefined;\n\tconst { loaded, fromCache } = source.outcome;\n\treturn !loaded ? 'skipped' : fromCache ? 'hit' : 'miss';\n}\n\nexport async function runSolvePipeline(\n\targs: SolvePipelineArgs\n): Promise<SolvedFlight | SolveFailure> {\n\tlet rhino: ServerTiming | null = null;\n\tconst solveStart = performance.now();\n\ttry {\n\t\tconst detail = await args.client.scheduler.solveDetailed(\n\t\t\targs.definitionSource,\n\t\t\targs.inputTree,\n\t\t\t{\n\t\t\t\tsignal: args.signal,\n\t\t\t\tselvaevents: args.selvaEvents,\n\t\t\t\tsessionId: args.sessionId,\n\t\t\t\theaders: args.headers,\n\t\t\t\tonServerTiming: (t) => {\n\t\t\t\t\trhino = t;\n\t\t\t\t}\n\t\t\t}\n\t\t);\n\t\tconst defBytes = defBytesVerdict(args.definitionSource);\n\t\treturn {\n\t\t\tkind: 'solved',\n\t\t\tresponse: detail.response,\n\t\t\tencoder: createResponseEncoder(detail.response, args.responseMaxBytes),\n\t\t\tsolveMs: performance.now() - solveStart,\n\t\t\trhino,\n\t\t\tsettle: { fromCache: detail.fromCache, definitionReuploaded: detail.definitionReuploaded },\n\t\t\t...(defBytes ? { defBytes } : {})\n\t\t};\n\t} catch (err) {\n\t\treturn classifyFailure(err, performance.now() - solveStart, args);\n\t}\n}\n","/**\n * Facade over the warm-client cache, definition-byte cache, single-flight\n * coalescing, and the solve pipeline — the primitives an app route would\n * otherwise wire up by hand.\n *\n * Doesn't read env itself: `@selvajs/server` owns `resolveComputeLimits`, and\n * this package stays independent of it. Callers resolve their own limits and\n * pass in the fields below.\n *\n * Stays out of auth, DB reads, share tokens, rate limiting, metrics, and\n * compute-server selection — all app policy, supplied per-call via\n * `SolveEngineSolveArgs.server`.\n */\n\nimport {\n\tComputeError,\n\tErrorCodes,\n\tisDefinitionRef,\n\ttype DefinitionRef,\n\ttype SolveDefinition\n} from '@selvajs/compute/core';\nimport { stableStringify, type SelvaEventTarget } from '@selvajs/compute/grasshopper';\nimport {\n\tcreateClientCache,\n\ttype CachedClient,\n\ttype ClientCache,\n\ttype ClientCacheDebug,\n\ttype ResolvedServer,\n\ttype SolveCacheStats\n} from './client-cache.js';\nimport {\n\tcreateDefinitionByteCache,\n\tisByteCacheRef,\n\ttype ByteCacheRef,\n\ttype ByteCacheStats,\n\ttype DefinitionByteCache\n} from './definition-byte-cache.js';\nimport { buildEnvelope } from './response-encoding.js';\nimport { buildServerTiming, type PrepMark } from './server-timing.js';\nimport {\n\tcreateSolveCacheSingleFlight,\n\ttype SolveCacheSingleFlight\n} from './solve-cache-single-flight.js';\nimport { buildSolveInputTree } from './transform-input.js';\nimport { interrupted, runSolvePipeline, type SolveEngineOutcome } from './solve-pipeline.js';\nimport type { SolveInput } from '../shared/solve-input.js';\n\n/**\n * Subset of `ComputeLimits` (`@selvajs/server/compute`) the engine needs.\n * Pass the resolved object through as-is — extra fields are ignored.\n */\nexport interface SolveEngineLimits {\n\tcomputeResponseMaxBytes: number;\n\tcomputeServerCachesolve: boolean;\n\tcomputeMaxQueueDepth: number;\n\tcomputeQueueWaitMs: number;\n\tcomputeDefinitionCacheBytes: number;\n\tcomputeSolveCacheBytes: number;\n}\n\n/** A deadline of 0 means no deadline, and a `NaN` budget retains without limit, both silently. */\nfunction assertValidLimits(limits: SolveEngineLimits): void {\n\tconst check = (name: keyof SolveEngineLimits, min: number) => {\n\t\tconst value = limits[name] as number;\n\t\tif (!Number.isFinite(value) || value < min) {\n\t\t\tthrow new RangeError(`SolveEngine: limits.${name} must be a number >= ${min}, got ${value}.`);\n\t\t}\n\t};\n\tcheck('computeResponseMaxBytes', 1);\n\tcheck('computeMaxQueueDepth', 0);\n\tcheck('computeQueueWaitMs', 0);\n\tcheck('computeDefinitionCacheBytes', 0);\n\tcheck('computeSolveCacheBytes', 0);\n}\n\nexport interface SolveEngineOptions {\n\tlimits: SolveEngineLimits;\n\t/** Max distinct warm compute servers before the LRU evicts the oldest. */\n\tmaxWarmComputeServers?: number;\n\t/** Concise cache/timing logs when truthy; `'verbose'` also dumps full lib-level requests/responses. Default off. */\n\tdebug?: ClientCacheDebug;\n\t/** Sink for the debug lines. Without it they go nowhere. */\n\tonDebugLog?: (message: string) => void;\n\t/** Fired when a caller joins an already-in-flight solve instead of running its own. */\n\tonSolveCoalesced?: (key: string) => void;\n}\n\n/**\n * A `ByteCacheRef` obtained ahead of time from `engine.definitionRef()` (e.g. to\n * read bytes for schema extraction before solving) is passed through as-is.\n */\nexport type SolveEngineDefinitionSource = Uint8Array | string | DefinitionRef | ByteCacheRef;\n\nexport interface SolveEngineSolveArgs {\n\tserver: ResolvedServer;\n\tdefinitionSource: SolveEngineDefinitionSource;\n\t/**\n\t * Coalesce-key identity for a raw `Uint8Array`/`string` source, which has\n\t * no natural identity the way a `DefinitionRef`'s `.key` does. Required in\n\t * that case — `solve()` throws rather than silently hashing the bytes.\n\t * Ignored for the other source forms.\n\t */\n\tdefinitionKey?: string;\n\t/**\n\t * The definition's inputs, from the schema you stored, never from the request: `id`, `nickname`,\n\t * `paramType` and the bounds decide what reaches the definition, so a body that supplies them\n\t * can drop a limit or fill a slot your host reserved.\n\t */\n\tinputs: SolveInput[];\n\tvalues: Record<string, unknown>;\n\tsignal: AbortSignal;\n\tacceptEncoding?: string;\n\t/** Sent as `X-Selva-Definition`, a key a router can keep one definition's solves together on. */\n\tdefinitionGuid?: string;\n\tloadStartMs?: number;\n\tdefLoadMs?: number;\n\tprepMarks?: PrepMark[];\n\t/** Per-solve live-event callback. A solve carrying one runs its own compute call, never a shared one. */\n\tselvaEvents?: SelvaEventTarget;\n\t/**\n\t * Sent as `X-Session-Id` so the compute fork routes the solve to the child holding this\n\t * session's warm document. Not part of the coalesce key: a joined flight routes on the id of\n\t * the caller that started it.\n\t */\n\tsessionId?: string;\n}\n\nexport interface SolveEngineStats {\n\tclient: SolveCacheStats;\n\tdefinitionBytes: ByteCacheStats;\n\tcoalescing: { inFlight: number };\n}\n\nexport class SolveEngine {\n\tprivate readonly limits: SolveEngineLimits;\n\tprivate readonly clientCache: ClientCache;\n\tprivate readonly byteCache: DefinitionByteCache;\n\tprivate readonly singleFlight: SolveCacheSingleFlight;\n\tprivate readonly clientGenerations = new WeakMap<CachedClient, number>();\n\tprivate lastClientGeneration = 0;\n\n\tconstructor(options: SolveEngineOptions) {\n\t\tassertValidLimits(options.limits);\n\t\tthis.limits = options.limits;\n\n\t\tthis.clientCache = createClientCache({\n\t\t\tcachesolve: this.limits.computeServerCachesolve,\n\t\t\tmaxQueueDepth: this.limits.computeMaxQueueDepth,\n\t\t\tqueueWaitMs: this.limits.computeQueueWaitMs,\n\t\t\tresponseCacheMaxBytes: this.limits.computeSolveCacheBytes,\n\t\t\tmaxResponseBytes: this.limits.computeResponseMaxBytes,\n\t\t\tdebug: options.debug ?? false,\n\t\t\tmaxWarmComputeServers: options.maxWarmComputeServers,\n\t\t\tonDebugLog: options.onDebugLog\n\t\t});\n\n\t\tthis.byteCache = createDefinitionByteCache(this.limits.computeDefinitionCacheBytes);\n\n\t\tthis.singleFlight = createSolveCacheSingleFlight({\n\t\t\tonJoin: options.onSolveCoalesced\n\t\t});\n\t}\n\n\tgetClient(server: ResolvedServer): Promise<CachedClient> {\n\t\treturn this.clientCache.getClient(server);\n\t}\n\n\tdefinitionRef(\n\t\tversionId: string,\n\t\tload: (signal: AbortSignal) => Promise<Uint8Array>\n\t): ByteCacheRef {\n\t\treturn this.byteCache.getOrLoad(versionId, load);\n\t}\n\n\t/**\n\t * Drops the warm client for `id`; call it when the server's URL or key changes. Solves already\n\t * running finish on the old connection details first, so a revoked key stays in use until then.\n\t */\n\tevictServer(id: string): void {\n\t\tthis.clientCache.evict(id);\n\t}\n\n\tclearSolveCaches(): void {\n\t\tthis.clientCache.clearSolveCaches();\n\t}\n\n\t/**\n\t * Drop every retained `.gh` byte entry. Safe at any time: entries key on an\n\t * immutable version id, so clearing costs a storage re-read and nothing else.\n\t */\n\tclearDefinitionCache(): void {\n\t\tthis.byteCache.clear();\n\t}\n\n\tstats(): SolveEngineStats {\n\t\treturn {\n\t\t\tclient: this.clientCache.solveCacheStats(),\n\t\t\tdefinitionBytes: this.byteCache.stats(),\n\t\t\tcoalescing: { inFlight: this.singleFlight.inFlight() }\n\t\t};\n\t}\n\n\tasync solve(args: SolveEngineSolveArgs): Promise<SolveEngineOutcome> {\n\t\tconst { definitionSource, identity } = this.resolveDefinitionSource(args);\n\t\tconst startedAt = performance.now();\n\t\tconst elapsed = () => performance.now() - startedAt;\n\n\t\t// `addEventListener` never fires for a signal that already aborted, so a client gone during\n\t\t// the host's own awaits would otherwise still pay for a solve.\n\t\tif (args.signal.aborted) return { kind: 'client_abort', durationMs: 0 };\n\n\t\tlet client: CachedClient;\n\t\ttry {\n\t\t\tclient = await this.clientCache.getClient(args.server);\n\t\t} catch (error) {\n\t\t\tif (args.signal.aborted) return { kind: 'client_abort', durationMs: elapsed() };\n\t\t\tif (error instanceof ComputeError && error.code === ErrorCodes.ABORTED) {\n\t\t\t\treturn interrupted(elapsed());\n\t\t\t}\n\t\t\treturn { kind: 'compute_error', durationMs: elapsed(), error };\n\t\t}\n\t\tif (args.signal.aborted) return { kind: 'client_abort', durationMs: elapsed() };\n\n\t\tconst treeBuildStart = performance.now();\n\t\tlet inputTree;\n\t\ttry {\n\t\t\tinputTree = buildSolveInputTree(args.inputs, args.values);\n\t\t} catch (error) {\n\t\t\treturn { kind: 'compute_error', durationMs: elapsed(), error };\n\t\t}\n\t\tconst treeBuildMs = performance.now() - treeBuildStart;\n\n\t\t// A live solve never shares a flight: the request carries its owner's callback, through which\n\t\t// that owner's Esc aborts the Grasshopper solve. Shared, one user's cancel would abort everyone\n\t\t// who joined, and the joiners would get no progress events.\n\t\tconst live = args.selvaEvents ? `:live:${args.selvaEvents.solveId}` : '';\n\t\tconst flight = await this.singleFlight.run(\n\t\t\t`${identity}:${args.server.id}:${this.clientGeneration(client)}:${stableStringify(inputTree)}${live}`,\n\t\t\t(signal) =>\n\t\t\t\trunSolvePipeline({\n\t\t\t\t\tdefinitionSource,\n\t\t\t\t\tinputTree,\n\t\t\t\t\tclient,\n\t\t\t\t\tresponseMaxBytes: this.limits.computeResponseMaxBytes,\n\t\t\t\t\tselvaEvents: args.selvaEvents,\n\t\t\t\t\tsessionId: args.sessionId,\n\t\t\t\t\t...(args.definitionGuid && {\n\t\t\t\t\t\theaders: { 'X-Selva-Definition': args.definitionGuid }\n\t\t\t\t\t}),\n\t\t\t\t\tsignal\n\t\t\t\t}),\n\t\t\targs.signal\n\t\t);\n\n\t\t// Before encoding: a caller that left a shared flight must not stringify for nobody.\n\t\tif (args.signal.aborted) return { kind: 'client_abort', durationMs: elapsed() };\n\t\tif (flight.kind !== 'solved') return flight;\n\n\t\tlet encoded;\n\t\ttry {\n\t\t\tencoded = await flight.encoder.encode(args.acceptEncoding ?? '');\n\t\t} catch (error) {\n\t\t\treturn { kind: 'compute_error', durationMs: elapsed(), error };\n\t\t}\n\t\tif (encoded === 'too_large') return { kind: 'too_large' };\n\n\t\tconst { body, serializedBytes, serializeMs, gzipMs } = encoded;\n\t\tconst { solveMs, response } = flight;\n\t\tconst serverTotalMs = performance.now() - (args.loadStartMs ?? startedAt);\n\t\tconst timing = {\n\t\t\tdefLoadMs: args.defLoadMs ?? 0,\n\t\t\ttreeBuildMs,\n\t\t\tsolveMs,\n\t\t\tserializeMs,\n\t\t\tgzipMs,\n\t\t\tserverTotalMs,\n\t\t\trhino: flight.rhino,\n\t\t\tsettle: flight.settle,\n\t\t\t...(flight.defBytes ? { defBytes: flight.defBytes } : {})\n\t\t};\n\t\tconst serverTiming = buildServerTiming({ ...timing, prepMarks: args.prepMarks ?? [] });\n\n\t\treturn {\n\t\t\tkind: 'ok',\n\t\t\tenvelope: buildEnvelope(encoded, response, serverTiming, {\n\t\t\t\t...timing,\n\t\t\t\tserializedBytes,\n\t\t\t\tcompressedBytes: encoded.encoding === 'gzip' ? (body as Uint8Array).byteLength : null\n\t\t\t}),\n\t\t\tsolveMs,\n\t\t\terrorCount: response.errors?.length ?? 0,\n\t\t\twarningCount: response.warnings?.length ?? 0\n\t\t};\n\t}\n\n\t/**\n\t * The response for an outcome; the mapping from outcome to status lives here. Throws\n\t * `compute_error`'s error: what it means for the caller is the host's to decide.\n\t */\n\ttoWebResponse(outcome: SolveEngineOutcome): Response {\n\t\tconst fail = (status: number, body: { message: string; retryAfter?: number }) =>\n\t\t\tnew Response(JSON.stringify(body), {\n\t\t\t\tstatus,\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t...(body.retryAfter !== undefined ? { 'Retry-After': String(body.retryAfter) } : {})\n\t\t\t\t}\n\t\t\t});\n\n\t\tswitch (outcome.kind) {\n\t\t\tcase 'ok':\n\t\t\t\t// The cast is a TS gap: `Uint8Array<ArrayBufferLike>` is not a body type, though it works.\n\t\t\t\treturn new Response(outcome.envelope.body as ConstructorParameters<typeof Response>[0], {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: outcome.envelope.headers\n\t\t\t\t});\n\t\t\tcase 'timeout':\n\t\t\t\treturn fail(504, { message: outcome.message });\n\t\t\tcase 'client_abort':\n\t\t\t\treturn fail(499, { message: 'Client closed request' });\n\t\t\tcase 'too_large':\n\t\t\t\treturn fail(413, {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t'Solve result is too large to return. This usually means a file output exceeds the supported size.'\n\t\t\t\t});\n\t\t\tcase 'request_too_large':\n\t\t\t\treturn fail(413, { message: outcome.message });\n\t\t\tcase 'shed':\n\t\t\t\treturn fail(503, { message: outcome.message, retryAfter: outcome.retryAfterSeconds });\n\t\t\tcase 'compute_error':\n\t\t\t\tthrow outcome.error;\n\t\t}\n\t}\n\n\t/**\n\t * Part of the coalesce key: after an eviction the id stays and the client changes, and a new\n\t * caller joining a draining flight would get an answer from the old URL or key.\n\t */\n\tprivate clientGeneration(client: CachedClient): number {\n\t\tlet generation = this.clientGenerations.get(client);\n\t\tif (generation === undefined) {\n\t\t\tgeneration = ++this.lastClientGeneration;\n\t\t\tthis.clientGenerations.set(client, generation);\n\t\t}\n\t\treturn generation;\n\t}\n\n\tprivate resolveDefinitionSource(args: SolveEngineSolveArgs): {\n\t\tdefinitionSource: SolveDefinition;\n\t\tidentity: string;\n\t} {\n\t\tconst source = args.definitionSource;\n\n\t\tif (isByteCacheRef(source) || isDefinitionRef(source)) {\n\t\t\treturn { definitionSource: source, identity: source.key };\n\t\t}\n\n\t\t// Raw Uint8Array | string — no natural identity; require the caller's own.\n\t\tif (!args.definitionKey) {\n\t\t\tthrow new Error(\n\t\t\t\t'SolveEngine.solve(): definitionKey is required when definitionSource is raw bytes or a string.'\n\t\t\t);\n\t\t}\n\t\treturn { definitionSource: source, identity: args.definitionKey };\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;AAOA,MAAM,uBAAuB;;;;;;;AAQ7B,MAAM,gCAAgC;;;;;;;;;;;AAoBtC,SAAgB,uBACf,QACA,UACmB;CACnB,IAAI,gBAAgB;CACpB,IAAI,cAAc;CAClB,IAAI,UAAU;;CAGd,eAAe,QAAQ,WAAmD;EACzE,IAAI,SAAS;EACb,UAAU;EACV,IAAI;GACH,MAAM,WAAW,MAAM,QAAQ,QAC9B,OAAO,aAAa,kBAAkB,EAAE,YAAY,MAAM,CAAC,CAC5D,CAAC,CAAC,YAAY,IAAI;GAClB,cAAc,KAAK,IAAI;GACvB,MAAM,OAAO,CAAC,YAAY,YAAY,IAAI,uBAAuB;GACjE,IAAI,SAAS,eAAe;GAC5B,SACC,sCAAsC,cAAc,KAAK,KAAK,sBAC/D;GACA,gBAAgB;GAChB,WAAW,iBAAiB,IAAI;EACjC,UAAU;GACT,UAAU;EACX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AAS,QAAQ;IAAS,YAAY,QAAQ;GAAkB,CAAC;GACrF,KAAK,iBACJ,MAAM,QAAQ;EAChB;CACD;;;;;CAMA,iBAAyB,QAA8B;EACtD,IAAI,aAAa,KAAK,kBAAkB,IAAI,MAAM;EAClD,IAAI,eAAe,KAAA,GAAW;GAC7B,aAAa,EAAE,KAAK;GACpB,KAAK,kBAAkB,IAAI,QAAQ,UAAU;EAC9C;EACA,OAAO;CACR;CAEA,wBAAgC,MAG9B;EACD,MAAM,SAAS,KAAK;EAEpB,IAAI,eAAe,MAAM,KAAK,gBAAgB,MAAM,GACnD,OAAO;GAAE,kBAAkB;GAAQ,UAAU,OAAO;EAAI;EAIzD,IAAI,CAAC,KAAK,eACT,MAAM,IAAI,MACT,gGACD;EAED,OAAO;GAAE,kBAAkB;GAAQ,UAAU,KAAK;EAAc;CACjE;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@selvajs/solve",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.3",
|
|
4
4
|
"description": "One owner for the Selva solve flow — input change to solve result, on both sides of the wire, with no transport and no UI",
|
|
5
5
|
"author": "VektorNode",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"sideEffects": false,
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"undici": "^8.12.2",
|
|
45
|
-
"@selvajs/compute": "^5.0.
|
|
45
|
+
"@selvajs/compute": "^5.1.0-beta.0",
|
|
46
46
|
"@selvajs/schemas": "^5.3.1-beta.1"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|