@malloy-publisher/server 0.0.199 → 0.0.201

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/app/api-doc.yaml +110 -118
  2. package/dist/app/assets/{EnvironmentPage-Dpee_Kn6.js → EnvironmentPage-KoP4wt8H.js} +1 -1
  3. package/dist/app/assets/HomePage-HbPwKL84.js +1 -0
  4. package/dist/app/assets/MainPage-DfK4zDYO.js +2 -0
  5. package/dist/app/assets/{ModelPage-AwAugZ37.js → ModelPage-CUgSwGXg.js} +1 -1
  6. package/dist/app/assets/{PackagePage-XQ-EWGTC.js → PackagePage-CUDQNL5k.js} +1 -1
  7. package/dist/app/assets/{RouteError-3Mv8JQw7.js → RouteError-sgmtBdg8.js} +1 -1
  8. package/dist/app/assets/{WorkbookPage-DHYYpcYc.js → WorkbookPage-tnWmLcrW.js} +1 -1
  9. package/dist/app/assets/{core-DfcpQGVP.es-DQggNOdX.js → core-B3IQNPBD.es-foBNuT8L.js} +10 -10
  10. package/dist/app/assets/{index-D1pdwrUW.js → index-B5We8x8r.js} +1 -1
  11. package/dist/app/assets/{index-BUp81Qdm.js → index-KIvi9k3F.js} +1 -1
  12. package/dist/app/assets/index-PNYovl3E.js +452 -0
  13. package/dist/app/assets/{index.umd-CQH4LZU8.js → index.umd-BXcsl2XW.js} +1 -1
  14. package/dist/app/index.html +1 -1
  15. package/dist/package_load_worker.mjs +1 -1
  16. package/dist/server.mjs +1556 -1018
  17. package/package.json +1 -1
  18. package/publisher.config.json +4 -0
  19. package/src/config.spec.ts +246 -0
  20. package/src/config.ts +121 -1
  21. package/src/constants.ts +84 -1
  22. package/src/controller/connection.controller.spec.ts +803 -0
  23. package/src/controller/connection.controller.ts +207 -20
  24. package/src/controller/model.controller.ts +16 -5
  25. package/src/controller/query.controller.ts +20 -7
  26. package/src/controller/watch-mode.controller.ts +11 -2
  27. package/src/errors.spec.ts +44 -0
  28. package/src/errors.ts +34 -0
  29. package/src/filter_deprecation.spec.ts +64 -0
  30. package/src/filter_deprecation.ts +42 -0
  31. package/src/heap_check.spec.ts +144 -0
  32. package/src/heap_check.ts +144 -0
  33. package/src/mcp/handler_utils.ts +14 -0
  34. package/src/mcp/tools/execute_query_tool.ts +44 -14
  35. package/src/oom_guards.integration.spec.ts +261 -0
  36. package/src/path_safety.ts +9 -3
  37. package/src/query_cap_metrics.spec.ts +89 -0
  38. package/src/query_cap_metrics.ts +115 -0
  39. package/src/query_concurrency.spec.ts +247 -0
  40. package/src/query_concurrency.ts +236 -0
  41. package/src/query_timeout.spec.ts +224 -0
  42. package/src/query_timeout.ts +178 -0
  43. package/src/server-old.ts +20 -0
  44. package/src/server.ts +57 -72
  45. package/src/service/connection.spec.ts +244 -0
  46. package/src/service/connection.ts +14 -4
  47. package/src/service/environment.ts +124 -4
  48. package/src/service/environment_admission.spec.ts +165 -1
  49. package/src/service/environment_store.spec.ts +103 -0
  50. package/src/service/environment_store.ts +74 -23
  51. package/src/service/filter_integration.spec.ts +69 -0
  52. package/src/service/model.spec.ts +193 -3
  53. package/src/service/model.ts +95 -14
  54. package/src/service/model_limits.spec.ts +181 -0
  55. package/src/service/model_limits.ts +110 -0
  56. package/src/service/package.spec.ts +2 -6
  57. package/src/service/package.ts +6 -1
  58. package/src/service/path_injection.spec.ts +39 -0
  59. package/src/stream_helpers.spec.ts +280 -0
  60. package/src/stream_helpers.ts +162 -0
  61. package/src/test_helpers/metrics_harness.ts +126 -0
  62. package/dist/app/assets/HomePage-DLRWTNoL.js +0 -1
  63. package/dist/app/assets/MainPage-DsVt5QGM.js +0 -2
  64. package/dist/app/assets/index-Dv5bF4Ii.js +0 -451
@@ -44,7 +44,9 @@ const MAX_ENVIRONMENT_PATH_LEN = 4096;
44
44
  * production package name we've seen fits within it, and tightening
45
45
  * here costs nothing.
46
46
  */
47
- export function assertSafePackageName(packageName: unknown): void {
47
+ export function assertSafePackageName(
48
+ packageName: unknown,
49
+ ): asserts packageName is string {
48
50
  if (typeof packageName !== "string" || !SAFE_NAME_RE.test(packageName)) {
49
51
  throw new BadRequestError(
50
52
  `Invalid package name: must be 1-255 characters of letters, digits, "-", "_", or "." and must not start with "."`,
@@ -58,7 +60,9 @@ export function assertSafePackageName(packageName: unknown): void {
58
60
  * live in subdirectories like `models/foo.malloy`); backslashes,
59
61
  * absolute paths, NUL bytes, and `..` / `.` segments are not.
60
62
  */
61
- export function assertSafeRelativeModelPath(modelPath: unknown): void {
63
+ export function assertSafeRelativeModelPath(
64
+ modelPath: unknown,
65
+ ): asserts modelPath is string {
62
66
  if (
63
67
  typeof modelPath !== "string" ||
64
68
  modelPath.length === 0 ||
@@ -90,7 +94,9 @@ export function assertSafeRelativeModelPath(modelPath: unknown): void {
90
94
  * sanitizer-barrier pattern CodeQL's `js/path-injection` query
91
95
  * recognises.
92
96
  */
93
- export function assertSafeEnvironmentPath(environmentPath: unknown): void {
97
+ export function assertSafeEnvironmentPath(
98
+ environmentPath: unknown,
99
+ ): asserts environmentPath is string {
94
100
  if (typeof environmentPath !== "string") {
95
101
  throw new BadRequestError(`Invalid environment path: must be a string`);
96
102
  }
@@ -0,0 +1,89 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
2
+
3
+ import {
4
+ recordQueryCapExceeded,
5
+ resetQueryCapTelemetryForTesting,
6
+ } from "./query_cap_metrics";
7
+ import {
8
+ startMetricsHarness,
9
+ type MetricsHarness,
10
+ } from "./test_helpers/metrics_harness";
11
+
12
+ describe("query_cap_metrics", () => {
13
+ let harness: MetricsHarness;
14
+
15
+ beforeEach(async () => {
16
+ harness = await startMetricsHarness();
17
+ // Drop cached instruments so they re-init against the new
18
+ // provider; otherwise this test's writes go to a counter
19
+ // bound to the previous provider's reader.
20
+ resetQueryCapTelemetryForTesting();
21
+ });
22
+
23
+ afterEach(async () => {
24
+ delete process.env.PUBLISHER_MAX_QUERY_ROWS;
25
+ delete process.env.PUBLISHER_MAX_RESPONSE_BYTES;
26
+ resetQueryCapTelemetryForTesting();
27
+ await harness.shutdown();
28
+ });
29
+
30
+ it("publisher_query_cap_exceeded_total ticks per call, labeled by cap_type and source", async () => {
31
+ recordQueryCapExceeded("rows", "connection_sql");
32
+ recordQueryCapExceeded("rows", "connection_sql");
33
+ recordQueryCapExceeded("bytes", "model_query");
34
+ recordQueryCapExceeded("rows", "notebook_cell");
35
+
36
+ expect(
37
+ await harness.collectCounter("publisher_query_cap_exceeded_total", {
38
+ cap_type: "rows",
39
+ source: "connection_sql",
40
+ }),
41
+ ).toBe(2);
42
+ expect(
43
+ await harness.collectCounter("publisher_query_cap_exceeded_total", {
44
+ cap_type: "bytes",
45
+ source: "model_query",
46
+ }),
47
+ ).toBe(1);
48
+ expect(
49
+ await harness.collectCounter("publisher_query_cap_exceeded_total", {
50
+ cap_type: "rows",
51
+ source: "notebook_cell",
52
+ }),
53
+ ).toBe(1);
54
+ });
55
+
56
+ it("publisher_max_query_rows gauge reports the live env-var value", async () => {
57
+ process.env.PUBLISHER_MAX_QUERY_ROWS = "12345";
58
+ // Prime telemetry — the gauges install on the first
59
+ // counter-emitting call (`recordQueryCapExceeded`); in
60
+ // production that's the first 413, in tests we trigger it
61
+ // explicitly so the gauge is observable without a 413.
62
+ recordQueryCapExceeded("rows", "connection_sql");
63
+ expect(await harness.collectGauge("publisher_max_query_rows")).toBe(
64
+ 12345,
65
+ );
66
+ });
67
+
68
+ it("publisher_max_response_bytes gauge reports the live env-var value", async () => {
69
+ process.env.PUBLISHER_MAX_RESPONSE_BYTES = "9876543";
70
+ recordQueryCapExceeded("bytes", "connection_sql");
71
+ expect(await harness.collectGauge("publisher_max_response_bytes")).toBe(
72
+ 9876543,
73
+ );
74
+ });
75
+
76
+ it("publisher_max_query_rows gauge reports 0 when the cap is opted out", async () => {
77
+ process.env.PUBLISHER_MAX_QUERY_ROWS = "0";
78
+ recordQueryCapExceeded("rows", "connection_sql");
79
+ expect(await harness.collectGauge("publisher_max_query_rows")).toBe(0);
80
+ });
81
+
82
+ it("publisher_max_query_rows gauge reports -1 on misconfig so dashboards reveal the bad value", async () => {
83
+ process.env.PUBLISHER_MAX_QUERY_ROWS = "not-a-number";
84
+ // Misconfig must not crash the scrape; -1 is the agreed
85
+ // signal mirroring `publisher_query_timeout_ms`.
86
+ recordQueryCapExceeded("rows", "connection_sql");
87
+ expect(await harness.collectGauge("publisher_max_query_rows")).toBe(-1);
88
+ });
89
+ });
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Centralized telemetry for row-cap / byte-cap rejections (HTTP 413).
3
+ *
4
+ * Without this, operators tuning {@link PUBLISHER_MAX_QUERY_ROWS} /
5
+ * {@link PUBLISHER_MAX_RESPONSE_BYTES} can only see undifferentiated
6
+ * `http_server_requests_total{status_code="413"}` — they can't tell
7
+ * which cap is firing or which query surface is hottest. The counter
8
+ * here carries `cap_type` (`rows` / `bytes`) and `source`
9
+ * (`connection_sql` / `model_query` / `notebook_cell`) so a single
10
+ * dashboard panel can answer "what should I tune and on which
11
+ * endpoint?".
12
+ *
13
+ * Observable gauges expose the current effective caps so dashboards
14
+ * can render `actual_rows_returned / max_rows` utilization without
15
+ * a separate config feed — same pattern the memory governor uses
16
+ * for high/low water bytes and the concurrency middleware uses for
17
+ * its slot cap.
18
+ *
19
+ * Lazy init for the same reason as `query_timeout.ts` /
20
+ * `query_concurrency.ts`: instruments created before
21
+ * `setGlobalMeterProvider` bind to a NoOp meter
22
+ * (https://github.com/open-telemetry/opentelemetry-js/issues/3505).
23
+ * The first throw site initializes the counter; the gauges are
24
+ * installed alongside on the same call so the production hot path
25
+ * is one boolean check after the first 413.
26
+ */
27
+
28
+ import { metrics, type Counter } from "@opentelemetry/api";
29
+
30
+ import { getMaxQueryRows, getMaxResponseBytes } from "./config";
31
+
32
+ export type QueryCapType = "rows" | "bytes";
33
+ export type QueryCapSource = "connection_sql" | "model_query" | "notebook_cell";
34
+
35
+ let capExceededCounter: Counter | null = null;
36
+ let configGaugesInstalled = false;
37
+
38
+ function ensureCapTelemetry(): Counter {
39
+ if (capExceededCounter && configGaugesInstalled) {
40
+ return capExceededCounter;
41
+ }
42
+ const meter = metrics.getMeter("publisher");
43
+ if (!capExceededCounter) {
44
+ capExceededCounter = meter.createCounter(
45
+ "publisher_query_cap_exceeded_total",
46
+ {
47
+ description:
48
+ "Queries rejected with 413 because the row or byte cap was exceeded. Labels: cap_type ('rows'|'bytes'), source ('connection_sql'|'model_query'|'notebook_cell').",
49
+ },
50
+ );
51
+ }
52
+ if (!configGaugesInstalled) {
53
+ // Live config readouts so dashboards can render
54
+ // "actual / max" utilization for the row and byte caps the
55
+ // same way `publisher_memory_*_bytes` does for the governor.
56
+ // Read on every scrape so a runtime env-var change is
57
+ // visible without a restart; an env-var parse failure
58
+ // reports -1 so misconfig is visible rather than silently
59
+ // dropped (mirrors `publisher_query_timeout_ms`).
60
+ meter
61
+ .createObservableGauge("publisher_max_query_rows", {
62
+ description:
63
+ "Current effective PUBLISHER_MAX_QUERY_ROWS cap (0 = disabled, -1 = misconfigured)",
64
+ })
65
+ .addCallback((observation) => {
66
+ try {
67
+ observation.observe(getMaxQueryRows());
68
+ } catch {
69
+ observation.observe(-1);
70
+ }
71
+ });
72
+ meter
73
+ .createObservableGauge("publisher_max_response_bytes", {
74
+ description:
75
+ "Current effective PUBLISHER_MAX_RESPONSE_BYTES cap (0 = disabled, -1 = misconfigured)",
76
+ unit: "By",
77
+ })
78
+ .addCallback((observation) => {
79
+ try {
80
+ observation.observe(getMaxResponseBytes());
81
+ } catch {
82
+ observation.observe(-1);
83
+ }
84
+ });
85
+ configGaugesInstalled = true;
86
+ }
87
+ return capExceededCounter;
88
+ }
89
+
90
+ /**
91
+ * Record a single 413 cap-exceeded event. Call BEFORE throwing
92
+ * `PayloadTooLargeError` so the metric ticks even if a downstream
93
+ * `catch` swallows the error (MCP tools surface failures as content
94
+ * payloads rather than letting them bubble to the HTTP error
95
+ * mapper).
96
+ *
97
+ * `cap_type` must be one of `rows` / `bytes`; `source` identifies
98
+ * the query surface that detected the overflow.
99
+ */
100
+ export function recordQueryCapExceeded(
101
+ capType: QueryCapType,
102
+ source: QueryCapSource,
103
+ ): void {
104
+ ensureCapTelemetry().add(1, { cap_type: capType, source });
105
+ }
106
+
107
+ /**
108
+ * Visible for tests. Drops the cached instruments so a fresh
109
+ * `MeterProvider` (installed via `startMetricsHarness`) can capture
110
+ * future emissions. Do NOT call from production code.
111
+ */
112
+ export function resetQueryCapTelemetryForTesting(): void {
113
+ capExceededCounter = null;
114
+ configGaugesInstalled = false;
115
+ }
@@ -0,0 +1,247 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
2
+ import { EventEmitter } from "events";
3
+ import type { NextFunction, Request, Response } from "express";
4
+
5
+ import { ServiceUnavailableError } from "./errors";
6
+ import {
7
+ getActiveQueryCount,
8
+ queryConcurrencyMiddleware,
9
+ resetActiveQueryCountForTesting,
10
+ resetQueryConcurrencyTelemetryForTesting,
11
+ } from "./query_concurrency";
12
+ import {
13
+ startMetricsHarness,
14
+ type MetricsHarness,
15
+ } from "./test_helpers/metrics_harness";
16
+
17
+ function makeReq(path = "/api/v0/test"): Request {
18
+ return { path } as unknown as Request;
19
+ }
20
+
21
+ /**
22
+ * Minimal Response stub: just needs `on` to capture the release
23
+ * listeners and `emit` for tests to fire them. Wraps an
24
+ * EventEmitter so the on/emit semantics match real Express
25
+ * responses (multiple listeners, listener order, etc.).
26
+ */
27
+ function makeRes(): Response & {
28
+ fireFinish: () => void;
29
+ fireClose: () => void;
30
+ } {
31
+ const ee = new EventEmitter();
32
+ const res = ee as unknown as Response & {
33
+ fireFinish: () => void;
34
+ fireClose: () => void;
35
+ };
36
+ res.fireFinish = (): void => {
37
+ ee.emit("finish");
38
+ };
39
+ res.fireClose = (): void => {
40
+ ee.emit("close");
41
+ };
42
+ return res;
43
+ }
44
+
45
+ function callMiddleware(
46
+ req: Request,
47
+ res: Response,
48
+ ): { next: NextFunction; error: { value: unknown } } {
49
+ const errorBox: { value: unknown } = { value: undefined };
50
+ const next: NextFunction = (err) => {
51
+ errorBox.value = err;
52
+ };
53
+ queryConcurrencyMiddleware(req, res, next);
54
+ return { next, error: errorBox };
55
+ }
56
+
57
+ describe("queryConcurrencyMiddleware", () => {
58
+ beforeEach(() => {
59
+ // Belt-and-suspenders: every test starts from a clean gauge.
60
+ resetActiveQueryCountForTesting();
61
+ delete process.env.PUBLISHER_MAX_CONCURRENT_QUERIES;
62
+ });
63
+ afterEach(() => {
64
+ delete process.env.PUBLISHER_MAX_CONCURRENT_QUERIES;
65
+ resetActiveQueryCountForTesting();
66
+ });
67
+
68
+ it("passes through when the limit is 0 (opt-out)", () => {
69
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "0";
70
+ const res = makeRes();
71
+ const { error } = callMiddleware(makeReq(), res);
72
+ expect(error.value).toBeUndefined();
73
+ // Crucially: the counter stays at zero so opt-out really is
74
+ // opt-out — not "still tracks, just never rejects".
75
+ expect(getActiveQueryCount()).toBe(0);
76
+ });
77
+
78
+ it("admits the first request under the cap and increments the gauge", () => {
79
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "2";
80
+ const { error } = callMiddleware(makeReq(), makeRes());
81
+ expect(error.value).toBeUndefined();
82
+ expect(getActiveQueryCount()).toBe(1);
83
+ });
84
+
85
+ it("rejects the (cap+1)-th in-flight request with ServiceUnavailableError", () => {
86
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "2";
87
+ callMiddleware(makeReq(), makeRes());
88
+ callMiddleware(makeReq(), makeRes());
89
+ // Two in flight; the third must be turned away.
90
+ const { error } = callMiddleware(makeReq(), makeRes());
91
+ expect(error.value).toBeInstanceOf(ServiceUnavailableError);
92
+ expect((error.value as Error).message).toContain(
93
+ "PUBLISHER_MAX_CONCURRENT_QUERIES",
94
+ );
95
+ // Gauge is unchanged by the rejection (we never claimed the
96
+ // slot), so subsequent legitimate completions don't go
97
+ // negative.
98
+ expect(getActiveQueryCount()).toBe(2);
99
+ });
100
+
101
+ it("decrements on response 'finish' (normal completion)", () => {
102
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "2";
103
+ const res = makeRes();
104
+ callMiddleware(makeReq(), res);
105
+ expect(getActiveQueryCount()).toBe(1);
106
+ res.fireFinish();
107
+ expect(getActiveQueryCount()).toBe(0);
108
+ });
109
+
110
+ it("decrements on response 'close' (client disconnect)", () => {
111
+ // A client tearing down the socket before the response
112
+ // finishes is the failure case that, without 'close'
113
+ // handling, would leak slots until the process restart.
114
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "2";
115
+ const res = makeRes();
116
+ callMiddleware(makeReq(), res);
117
+ res.fireClose();
118
+ expect(getActiveQueryCount()).toBe(0);
119
+ });
120
+
121
+ it("decrements only once even when both 'finish' and 'close' fire", () => {
122
+ // Express + Node fire both events in some versions when a
123
+ // long-poll response wraps up just as the client disconnects.
124
+ // The release must be idempotent or the counter goes negative
125
+ // and we hand out one extra slot than the operator configured.
126
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "1";
127
+ const res = makeRes();
128
+ callMiddleware(makeReq(), res);
129
+ res.fireFinish();
130
+ res.fireClose();
131
+ expect(getActiveQueryCount()).toBe(0);
132
+
133
+ // A second request after the double-fire must still be
134
+ // admitted (proving the counter didn't underflow into a
135
+ // permanently-rejecting state).
136
+ const second = makeRes();
137
+ const { error } = callMiddleware(makeReq(), second);
138
+ expect(error.value).toBeUndefined();
139
+ expect(getActiveQueryCount()).toBe(1);
140
+ });
141
+
142
+ it("re-admits after an in-flight request completes (gauge rolls forward)", () => {
143
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "1";
144
+ const firstRes = makeRes();
145
+ callMiddleware(makeReq(), firstRes);
146
+ // Second request is over the cap and rejected.
147
+ const second = callMiddleware(makeReq(), makeRes());
148
+ expect(second.error.value).toBeInstanceOf(ServiceUnavailableError);
149
+
150
+ // First request finishes; the next call should now be
151
+ // admitted — proving the cap is not a one-shot fuse.
152
+ firstRes.fireFinish();
153
+ const third = callMiddleware(makeReq(), makeRes());
154
+ expect(third.error.value).toBeUndefined();
155
+ expect(getActiveQueryCount()).toBe(1);
156
+ });
157
+
158
+ it("reads the env var on every call so the limit can change without restart", () => {
159
+ // Operators can adjust PUBLISHER_MAX_CONCURRENT_QUERIES at
160
+ // runtime (e.g. via a config-reload SIGHUP wired elsewhere).
161
+ // The middleware must respect the new value on the next
162
+ // request, not cache the original module-load value.
163
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "1";
164
+ callMiddleware(makeReq(), makeRes());
165
+ // At the cap.
166
+ const denied = callMiddleware(makeReq(), makeRes());
167
+ expect(denied.error.value).toBeInstanceOf(ServiceUnavailableError);
168
+
169
+ // Operator bumps the cap; the next request is admitted.
170
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "3";
171
+ const admitted = callMiddleware(makeReq(), makeRes());
172
+ expect(admitted.error.value).toBeUndefined();
173
+ expect(getActiveQueryCount()).toBe(2);
174
+ });
175
+
176
+ describe("telemetry", () => {
177
+ let harness: MetricsHarness;
178
+ beforeEach(async () => {
179
+ harness = await startMetricsHarness();
180
+ resetActiveQueryCountForTesting();
181
+ resetQueryConcurrencyTelemetryForTesting();
182
+ });
183
+ afterEach(async () => {
184
+ delete process.env.PUBLISHER_MAX_CONCURRENT_QUERIES;
185
+ resetActiveQueryCountForTesting();
186
+ resetQueryConcurrencyTelemetryForTesting();
187
+ await harness.shutdown();
188
+ });
189
+
190
+ it("publisher_query_concurrency_rejections_total ticks on each 503", async () => {
191
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "1";
192
+ callMiddleware(makeReq("/api/v0/test"), makeRes());
193
+ // At the cap; this one must be rejected.
194
+ const denied = callMiddleware(makeReq("/api/v0/test"), makeRes());
195
+ expect(denied.error.value).toBeInstanceOf(ServiceUnavailableError);
196
+ expect(
197
+ await harness.collectCounter(
198
+ "publisher_query_concurrency_rejections_total",
199
+ ),
200
+ ).toBe(1);
201
+ });
202
+
203
+ it("publisher_query_active_slots gauge reflects the live in-flight count", async () => {
204
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "5";
205
+ callMiddleware(makeReq(), makeRes());
206
+ callMiddleware(makeReq(), makeRes());
207
+ // The middleware's lazy telemetry init runs on every
208
+ // request, so by now the gauge callback should be
209
+ // attached and the next scrape reads 2.
210
+ expect(
211
+ await harness.collectGauge("publisher_query_active_slots"),
212
+ ).toBe(2);
213
+ });
214
+
215
+ it("publisher_query_active_slots gauge follows releases (decrements on res.finish)", async () => {
216
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "5";
217
+ const res = makeRes();
218
+ callMiddleware(makeReq(), res);
219
+ expect(
220
+ await harness.collectGauge("publisher_query_active_slots"),
221
+ ).toBe(1);
222
+ res.fireFinish();
223
+ // A scrape after release must reflect the new value;
224
+ // otherwise an operator can't tell "leaking slot" from
225
+ // "real load".
226
+ expect(
227
+ await harness.collectGauge("publisher_query_active_slots"),
228
+ ).toBe(0);
229
+ });
230
+
231
+ it("publisher_query_max_slots gauge reports the current cap", async () => {
232
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "17";
233
+ callMiddleware(makeReq(), makeRes());
234
+ expect(await harness.collectGauge("publisher_query_max_slots")).toBe(
235
+ 17,
236
+ );
237
+ });
238
+
239
+ it("publisher_query_max_slots gauge reports 0 when concurrency is opted out", async () => {
240
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "0";
241
+ callMiddleware(makeReq(), makeRes());
242
+ expect(await harness.collectGauge("publisher_query_max_slots")).toBe(
243
+ 0,
244
+ );
245
+ });
246
+ });
247
+ });