@malloy-publisher/server 0.0.199 → 0.0.200

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 (58) hide show
  1. package/dist/app/api-doc.yaml +76 -111
  2. package/dist/app/assets/{EnvironmentPage-Dpee_Kn6.js → EnvironmentPage-CgKNjySu.js} +1 -1
  3. package/dist/app/assets/HomePage-BPIpMBjW.js +1 -0
  4. package/dist/app/assets/{MainPage-DsVt5QGM.js → MainPage-CAwb8U82.js} +2 -2
  5. package/dist/app/assets/{ModelPage-AwAugZ37.js → ModelPage-C0Uevsw9.js} +1 -1
  6. package/dist/app/assets/{PackagePage-XQ-EWGTC.js → PackagePage-Cu-u9k1g.js} +1 -1
  7. package/dist/app/assets/{RouteError-3Mv8JQw7.js → RouteError-DVwPh2Ql.js} +1 -1
  8. package/dist/app/assets/{WorkbookPage-DHYYpcYc.js → WorkbookPage-DW38R2Zv.js} +1 -1
  9. package/dist/app/assets/{core-DfcpQGVP.es-DQggNOdX.js → core-C0vCMRDQ.es-D_ytHhjS.js} +10 -10
  10. package/dist/app/assets/{index-D1pdwrUW.js → index-BGdcKsFF.js} +1 -1
  11. package/dist/app/assets/{index-BUp81Qdm.js → index-CTx4v4_3.js} +1 -1
  12. package/dist/app/assets/index-DE6d5jEy.js +452 -0
  13. package/dist/app/assets/{index.umd-CQH4LZU8.js → index.umd-C1Mi1uRm.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 +1482 -1010
  17. package/package.json +1 -1
  18. package/src/config.spec.ts +246 -0
  19. package/src/config.ts +121 -1
  20. package/src/constants.ts +84 -1
  21. package/src/controller/connection.controller.spec.ts +803 -0
  22. package/src/controller/connection.controller.ts +207 -20
  23. package/src/controller/model.controller.ts +16 -5
  24. package/src/controller/query.controller.ts +20 -7
  25. package/src/controller/watch-mode.controller.ts +11 -2
  26. package/src/errors.spec.ts +44 -0
  27. package/src/errors.ts +34 -0
  28. package/src/heap_check.spec.ts +144 -0
  29. package/src/heap_check.ts +144 -0
  30. package/src/mcp/handler_utils.ts +14 -0
  31. package/src/mcp/tools/execute_query_tool.ts +44 -14
  32. package/src/oom_guards.integration.spec.ts +261 -0
  33. package/src/path_safety.ts +9 -3
  34. package/src/query_cap_metrics.spec.ts +89 -0
  35. package/src/query_cap_metrics.ts +115 -0
  36. package/src/query_concurrency.spec.ts +247 -0
  37. package/src/query_concurrency.ts +236 -0
  38. package/src/query_timeout.spec.ts +224 -0
  39. package/src/query_timeout.ts +178 -0
  40. package/src/server-old.ts +20 -0
  41. package/src/server.ts +25 -47
  42. package/src/service/connection.ts +8 -2
  43. package/src/service/environment.ts +82 -2
  44. package/src/service/environment_admission.spec.ts +165 -1
  45. package/src/service/environment_store.spec.ts +103 -0
  46. package/src/service/environment_store.ts +74 -23
  47. package/src/service/model.spec.ts +193 -3
  48. package/src/service/model.ts +80 -12
  49. package/src/service/model_limits.spec.ts +181 -0
  50. package/src/service/model_limits.ts +110 -0
  51. package/src/service/package.spec.ts +2 -6
  52. package/src/service/package.ts +6 -1
  53. package/src/service/path_injection.spec.ts +39 -0
  54. package/src/stream_helpers.spec.ts +280 -0
  55. package/src/stream_helpers.ts +162 -0
  56. package/src/test_helpers/metrics_harness.ts +126 -0
  57. package/dist/app/assets/HomePage-DLRWTNoL.js +0 -1
  58. package/dist/app/assets/index-Dv5bF4Ii.js +0 -451
@@ -0,0 +1,144 @@
1
+ import { metrics } from "@opentelemetry/api";
2
+ import * as v8 from "v8";
3
+
4
+ import { logger } from "./logger";
5
+
6
+ /**
7
+ * Subset of the winston logger surface this module needs. Defined
8
+ * structurally so tests can pass a plain object stub and so we
9
+ * don't take a direct dependency on the concrete winston type.
10
+ */
11
+ interface HeapCheckLogger {
12
+ warn: (...args: unknown[]) => unknown;
13
+ info: (...args: unknown[]) => unknown;
14
+ }
15
+
16
+ /**
17
+ * Observable gauges for heap configuration so dashboards can render
18
+ * "configured heap ceiling" alongside the RSS / back-pressure
19
+ * timeseries from `PackageMemoryGovernor`. The values are observed
20
+ * on every scrape rather than cached — `v8.getHeapStatistics()` is
21
+ * cheap (a single VM call) and serving the live value avoids stale
22
+ * reads.
23
+ *
24
+ * Lazy init for the same reason as `query_timeout.ts`: instruments
25
+ * captured before `setGlobalMeterProvider` are bound to NoOp. We
26
+ * call `installHeapGauges` from `checkHeapConfiguration` so it
27
+ * runs once at startup, after the OTel SDK is up.
28
+ *
29
+ * Mirrors the governor's `publisher_*` unlabeled style.
30
+ */
31
+ let heapGaugesInstalled = false;
32
+ function installHeapGauges(): void {
33
+ if (heapGaugesInstalled) return;
34
+ heapGaugesInstalled = true;
35
+ const meter = metrics.getMeter("publisher");
36
+ meter
37
+ .createObservableGauge("publisher_heap_size_limit_bytes", {
38
+ description:
39
+ "V8 heap_size_limit (--max-old-space-size). Compare with PUBLISHER_MAX_MEMORY_BYTES.",
40
+ unit: "By",
41
+ })
42
+ .addCallback((observation) => {
43
+ observation.observe(v8.getHeapStatistics().heap_size_limit);
44
+ });
45
+ meter
46
+ .createObservableGauge("publisher_heap_used_bytes", {
47
+ description:
48
+ "Current V8 used_heap_size in bytes. Watch this alongside publisher_process_rss_bytes; the two diverge under native-allocator pressure (DuckDB, etc.).",
49
+ unit: "By",
50
+ })
51
+ .addCallback((observation) => {
52
+ observation.observe(v8.getHeapStatistics().used_heap_size);
53
+ });
54
+ }
55
+
56
+ /**
57
+ * Visible for tests; production code never calls this. Resets the
58
+ * lazy guard so a re-installation captures into a fresh
59
+ * MeterProvider.
60
+ */
61
+ export function resetHeapTelemetryForTesting(): void {
62
+ heapGaugesInstalled = false;
63
+ }
64
+
65
+ /**
66
+ * Minimum V8 heap ceiling (`--max-old-space-size`) the publisher
67
+ * expects in production. Below this the row/byte caps from earlier
68
+ * steps still apply, but a single buffered model query at the
69
+ * default 50 MB byte cap plus the surrounding `Result` allocation
70
+ * can plausibly chew through the remaining headroom and OOM the
71
+ * process before back-pressure trips.
72
+ *
73
+ * 2 GiB is the smallest value at which the defaults are comfortably
74
+ * survivable. Operators running explicitly tuned-down pods (e.g. a
75
+ * lightweight smoke-test deploy) can ignore the warning.
76
+ */
77
+ const MIN_RECOMMENDED_HEAP_BYTES = 2 * 1024 * 1024 * 1024;
78
+
79
+ /**
80
+ * Probe `v8.getHeapStatistics().heap_size_limit` at startup and
81
+ * emit a single structured warning when the process is configured
82
+ * with a heap ceiling below {@link MIN_RECOMMENDED_HEAP_BYTES}.
83
+ *
84
+ * Why warn (not exit):
85
+ * - Smaller pods are legitimate (CI, local dev, smoke tests).
86
+ * Hard-failing them would be hostile to those workflows.
87
+ * - The earlier steps already bound memory growth per request; this
88
+ * is a "you probably want to know" signal, not a safety
89
+ * interlock.
90
+ * - A warning at boot is grep-able in pod logs / dashboards and
91
+ * surfaces faster than waiting for the first OOMKill.
92
+ *
93
+ * Returns the observed heap limit so the caller (server.ts startup)
94
+ * can also surface it in startup metrics if desired.
95
+ */
96
+ /**
97
+ * Test seam: parameters allow injecting a fake heap-stats getter
98
+ * and logger so the bun/sinon "ES modules cannot be stubbed"
99
+ * restriction doesn't force a module-level mock. Production calls
100
+ * (server.ts startup) use the defaults; tests pass in stubs.
101
+ */
102
+ export interface CheckHeapOptions {
103
+ getHeapStatistics?: () => Pick<v8.HeapInfo, "heap_size_limit">;
104
+ log?: HeapCheckLogger;
105
+ }
106
+
107
+ export function checkHeapConfiguration(options: CheckHeapOptions = {}): {
108
+ heapSizeLimitBytes: number;
109
+ warned: boolean;
110
+ } {
111
+ // Install heap-related observable gauges on the same startup
112
+ // tick. Idempotent — re-calling in tests / warmup paths is
113
+ // safe and re-uses the same instruments.
114
+ installHeapGauges();
115
+ const getStats = options.getHeapStatistics ?? v8.getHeapStatistics;
116
+ const log = options.log ?? logger;
117
+ const stats = getStats();
118
+ const heapSizeLimitBytes = stats.heap_size_limit;
119
+ const limitMiB = Math.round(heapSizeLimitBytes / (1024 * 1024));
120
+ const recommendedMiB = Math.round(
121
+ MIN_RECOMMENDED_HEAP_BYTES / (1024 * 1024),
122
+ );
123
+
124
+ if (heapSizeLimitBytes < MIN_RECOMMENDED_HEAP_BYTES) {
125
+ log.warn(
126
+ `V8 heap_size_limit is ${limitMiB} MiB, below the recommended ${recommendedMiB} MiB. ` +
127
+ `Pass --max-old-space-size=${recommendedMiB} (or higher) on the node process to keep ` +
128
+ `the row/byte caps (PUBLISHER_MAX_QUERY_ROWS / PUBLISHER_MAX_RESPONSE_BYTES) within ` +
129
+ `safe margin. With a smaller heap, a single large query can OOM the pod before the ` +
130
+ `memory governor's back-pressure has a chance to trip.`,
131
+ {
132
+ heapSizeLimitBytes,
133
+ recommendedHeapSizeBytes: MIN_RECOMMENDED_HEAP_BYTES,
134
+ },
135
+ );
136
+ return { heapSizeLimitBytes, warned: true };
137
+ }
138
+
139
+ log.info(
140
+ `V8 heap_size_limit is ${limitMiB} MiB (>= recommended ${recommendedMiB} MiB).`,
141
+ { heapSizeLimitBytes },
142
+ );
143
+ return { heapSizeLimitBytes, warned: false };
144
+ }
@@ -7,6 +7,7 @@ import {
7
7
  ModelNotFoundError,
8
8
  ModelCompilationError,
9
9
  EnvironmentNotFoundError,
10
+ ServiceUnavailableError,
10
11
  } from "../errors";
11
12
  import {
12
13
  getNotFoundError,
@@ -132,6 +133,9 @@ export async function getModelForQuery(
132
133
  environmentName,
133
134
  false,
134
135
  );
136
+ // Shed load before any disk / DB work; mirrors the HTTP query
137
+ // controllers so MCP traffic obeys the same back-pressure rules.
138
+ environment.assertCanAdmitQuery();
135
139
  const pkg = await environment.getPackage(packageName, false);
136
140
  const model = pkg.getModel(modelPath);
137
141
  if (!model || model.getModelType() === "notebook") {
@@ -163,6 +167,16 @@ export async function getModelForQuery(
163
167
  `${environmentName}/${packageName}/${modelPath}`,
164
168
  error,
165
169
  );
170
+ } else if (error instanceof ServiceUnavailableError) {
171
+ // Back-pressure: don't dress this up as a 404/500. Surface the
172
+ // server's own message so the MCP caller knows to retry.
173
+ errorDetails = {
174
+ message: error.message,
175
+ suggestions: [
176
+ "Retry after the publisher's memory usage drops below the configured low-water mark.",
177
+ "If this happens repeatedly, raise PUBLISHER_MAX_MEMORY_BYTES or scale up the pod.",
178
+ ],
179
+ } satisfies ErrorDetails;
166
180
  } else {
167
181
  // Unexpected error during setup
168
182
  errorDetails = getInternalError("executeQuery (Setup)", error);
@@ -2,7 +2,13 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
2
  import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
3
3
  import { z } from "zod";
4
4
  import type { GivenValue } from "@malloydata/malloy";
5
+ import { getQueryTimeoutMs } from "../../config";
5
6
  import { logger } from "../../logger";
7
+ import {
8
+ tryAcquireQuerySlot,
9
+ type QuerySlotHandle,
10
+ } from "../../query_concurrency";
11
+ import { runWithQueryTimeout } from "../../query_timeout";
6
12
  import { EnvironmentStore } from "../../service/environment_store";
7
13
  import { getMalloyErrorDetails, type ErrorDetails } from "../error_messages";
8
14
  import { buildMalloyUri, getModelForQuery } from "../handler_utils";
@@ -128,16 +134,30 @@ export function registerExecuteQueryTool(
128
134
  logger.info(
129
135
  `[MCP Tool executeQuery] Model found. Proceeding to execute query.`,
130
136
  );
137
+ // Per-pod concurrency slot. MCP shares the same slot pool
138
+ // as the HTTP query routes so a hot agent loop can't
139
+ // bypass PUBLISHER_MAX_CONCURRENT_QUERIES. `mcp:executeQuery`
140
+ // is a fixed label so the dashboard can separate MCP load
141
+ // from HTTP route load. Acquisition can throw
142
+ // ServiceUnavailableError; the existing catch below surfaces
143
+ // it as the standard MCP error-content payload.
144
+ let querySlot: QuerySlotHandle | null = null;
131
145
  try {
146
+ querySlot = tryAcquireQuerySlot("mcp:executeQuery");
132
147
  // If ad-hoc query is provided, use it directly in the 3rd arg
133
148
  if (query) {
134
- const { result } = await model.getQueryResults(
135
- undefined,
136
- undefined,
137
- query,
138
- filterParams,
139
- undefined,
140
- givens as Record<string, GivenValue> | undefined,
149
+ const { result } = await runWithQueryTimeout(
150
+ (abortSignal) =>
151
+ model.getQueryResults(
152
+ undefined,
153
+ undefined,
154
+ query,
155
+ filterParams,
156
+ undefined,
157
+ givens as Record<string, GivenValue> | undefined,
158
+ abortSignal,
159
+ ),
160
+ getQueryTimeoutMs(),
141
161
  );
142
162
  const { validateRenderTags } = await import(
143
163
  "@malloydata/render-validator"
@@ -179,13 +199,18 @@ export function registerExecuteQueryTool(
179
199
 
180
200
  return { isError: false, content };
181
201
  } else if (queryName) {
182
- const { result } = await model.getQueryResults(
183
- sourceName,
184
- queryName,
185
- undefined,
186
- filterParams,
187
- undefined,
188
- givens as Record<string, GivenValue> | undefined,
202
+ const { result } = await runWithQueryTimeout(
203
+ (abortSignal) =>
204
+ model.getQueryResults(
205
+ sourceName,
206
+ queryName,
207
+ undefined,
208
+ filterParams,
209
+ undefined,
210
+ givens as Record<string, GivenValue> | undefined,
211
+ abortSignal,
212
+ ),
213
+ getQueryTimeoutMs(),
189
214
  );
190
215
  const { validateRenderTags } = await import(
191
216
  "@malloydata/render-validator"
@@ -271,6 +296,11 @@ export function registerExecuteQueryTool(
271
296
  },
272
297
  ],
273
298
  };
299
+ } finally {
300
+ // Release on every exit path — success, error, or
301
+ // unreachable code-path throw. `release()` is idempotent
302
+ // so a double-fault during cleanup can't double-decrement.
303
+ querySlot?.release();
274
304
  }
275
305
  },
276
306
  );
@@ -0,0 +1,261 @@
1
+ /**
2
+ * End-to-end integration test for the OOM-mitigation guardrails
3
+ * (Steps 1-6). Mounts a real Express app with the same middleware
4
+ * + controller wiring `server.ts` uses, then exercises each layer
5
+ * of the defense via HTTP requests.
6
+ *
7
+ * What's covered, in firing order:
8
+ * 1. Admission gate — memory governor flags back-pressure →
9
+ * `ConnectionController` short-circuits with 503 BEFORE the
10
+ * connector is touched.
11
+ * 2. Concurrency middleware — when the pod is at
12
+ * `PUBLISHER_MAX_CONCURRENT_QUERIES`, new requests get 503
13
+ * BEFORE the controller is invoked.
14
+ * 3. Timeout — when the underlying driver takes longer than
15
+ * `PUBLISHER_QUERY_TIMEOUT_MS`, the request gets 504 and the
16
+ * abort signal fires.
17
+ * 4. Row cap — when the driver returns more rows than
18
+ * `PUBLISHER_MAX_QUERY_ROWS`, the request gets 413.
19
+ *
20
+ * Mocked: only the Malloy `Connection.runSQL`. Everything else
21
+ * (admission gate, concurrency middleware, timeout helper, cap
22
+ * detection, error-to-HTTP mapping) is real production code.
23
+ */
24
+
25
+ import type { Connection, RunSQLOptions } from "@malloydata/malloy";
26
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
27
+ import express from "express";
28
+ import sinon from "sinon";
29
+ import request from "supertest";
30
+
31
+ import { ConnectionController } from "./controller/connection.controller";
32
+ import { internalErrorToHttpError, ServiceUnavailableError } from "./errors";
33
+ import {
34
+ queryConcurrency,
35
+ resetActiveQueryCountForTesting,
36
+ resetQueryConcurrencyTelemetryForTesting,
37
+ } from "./query_concurrency";
38
+ import type { EnvironmentStore } from "./service/environment_store";
39
+
40
+ function buildApp(
41
+ runSQL: sinon.SinonStub,
42
+ opts: { assertCanAdmitQuery?: sinon.SinonStub } = {},
43
+ ): {
44
+ app: express.Express;
45
+ assertCanAdmitQuery: sinon.SinonStub;
46
+ runSQL: sinon.SinonStub;
47
+ } {
48
+ const fakeConnection = { runSQL } as unknown as Connection;
49
+ const assertCanAdmitQuery =
50
+ opts.assertCanAdmitQuery ?? sinon.stub().returns(undefined);
51
+ const fakeEnv = { assertCanAdmitQuery };
52
+ const fakeStore = {
53
+ getEnvironment: sinon.stub().resolves(fakeEnv),
54
+ } as unknown as EnvironmentStore;
55
+ const controller = new ConnectionController(fakeStore);
56
+ // Bypass the connector lookup; the test only cares about the
57
+ // guardrail chain on top of a stub.
58
+ sinon
59
+ .stub(
60
+ controller as unknown as {
61
+ getMalloyConnection: (...args: unknown[]) => Promise<Connection>;
62
+ },
63
+ "getMalloyConnection",
64
+ )
65
+ .resolves(fakeConnection);
66
+
67
+ const app = express();
68
+ app.use(express.json());
69
+
70
+ // Same shape as `server.ts` registers for `/sqlQuery` — middleware
71
+ // first, then a thin route handler that delegates to the controller.
72
+ app.post(
73
+ "/api/v0/environments/:env/connections/:conn/sqlQuery",
74
+ queryConcurrency(),
75
+ async (req, res) => {
76
+ try {
77
+ const result = await controller.getConnectionQueryData(
78
+ req.params.env,
79
+ req.params.conn,
80
+ req.body.sqlStatement as string,
81
+ (req.body.options as string | undefined) ?? "",
82
+ );
83
+ res.status(200).json(result);
84
+ } catch (error) {
85
+ const { json, status } = internalErrorToHttpError(error as Error);
86
+ res.status(status).json(json);
87
+ }
88
+ },
89
+ );
90
+
91
+ // Mirror the global error handler from `server.ts` — required
92
+ // for the concurrency middleware's `next(err)` path to map
93
+ // `ServiceUnavailableError` to a 503 instead of Express's
94
+ // default 500. Without this the integration test would be
95
+ // exercising a different error path than production.
96
+ app.use(
97
+ (
98
+ err: Error,
99
+ _req: express.Request,
100
+ res: express.Response,
101
+ _next: express.NextFunction,
102
+ ): void => {
103
+ const { json, status } = internalErrorToHttpError(err);
104
+ res.status(status).json(json);
105
+ },
106
+ );
107
+
108
+ return { app, assertCanAdmitQuery, runSQL };
109
+ }
110
+
111
+ describe("OOM guardrails: end-to-end chain", () => {
112
+ const savedEnv = {
113
+ max: process.env.PUBLISHER_MAX_QUERY_ROWS,
114
+ timeout: process.env.PUBLISHER_QUERY_TIMEOUT_MS,
115
+ concurrency: process.env.PUBLISHER_MAX_CONCURRENT_QUERIES,
116
+ };
117
+
118
+ beforeEach(() => {
119
+ resetActiveQueryCountForTesting();
120
+ resetQueryConcurrencyTelemetryForTesting();
121
+ });
122
+
123
+ afterEach(() => {
124
+ sinon.restore();
125
+ resetActiveQueryCountForTesting();
126
+ resetQueryConcurrencyTelemetryForTesting();
127
+ // Restore env so later tests don't see this suite's bleed-through.
128
+ const restore = (key: keyof typeof savedEnv, envVar: string): void => {
129
+ if (savedEnv[key] === undefined) delete process.env[envVar];
130
+ else process.env[envVar] = savedEnv[key]!;
131
+ };
132
+ restore("max", "PUBLISHER_MAX_QUERY_ROWS");
133
+ restore("timeout", "PUBLISHER_QUERY_TIMEOUT_MS");
134
+ restore("concurrency", "PUBLISHER_MAX_CONCURRENT_QUERIES");
135
+ });
136
+
137
+ it("admission gate fires FIRST: 503 before the connector is touched", async () => {
138
+ const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
139
+ const assertCanAdmitQuery = sinon
140
+ .stub()
141
+ .throws(
142
+ new ServiceUnavailableError(
143
+ "Publisher is under memory pressure and cannot accept new queries.",
144
+ ),
145
+ );
146
+ const { app } = buildApp(runSQL, { assertCanAdmitQuery });
147
+
148
+ const res = await request(app)
149
+ .post("/api/v0/environments/e/connections/c/sqlQuery")
150
+ .send({ sqlStatement: "SELECT 1" });
151
+
152
+ expect(res.status).toBe(503);
153
+ // Critical load-shedding invariant: connector must NOT have
154
+ // been called when the gate rejects.
155
+ expect(runSQL.called).toBe(false);
156
+ });
157
+
158
+ it("concurrency cap fires SECOND: 503 before the controller is touched, once the slot pool is full", async () => {
159
+ process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "1";
160
+ // First request: runSQL takes ~200 ms so the slot stays
161
+ // held while we fire the second. Using a real timer rather
162
+ // than an unresolved deferred keeps the test deterministic
163
+ // even when supertest spawns one ephemeral http server per
164
+ // `request(app)` call — both sockets will eventually close
165
+ // on their own regardless of orchestration order.
166
+ const runSQL = sinon
167
+ .stub()
168
+ .callsFake(
169
+ () =>
170
+ new Promise((resolve) =>
171
+ setTimeout(() => resolve({ rows: [], totalRows: 0 }), 200),
172
+ ),
173
+ );
174
+ const { app } = buildApp(runSQL);
175
+
176
+ // Drive both requests in parallel — `Promise.all` keeps the
177
+ // event loop turning on the first request's pending timer
178
+ // while the second runs through the middleware.
179
+ const [first, second] = await Promise.all([
180
+ request(app)
181
+ .post("/api/v0/environments/e/connections/c/sqlQuery")
182
+ .send({ sqlStatement: "SELECT 1" }),
183
+ (async () => {
184
+ // Yield a few ticks so the first request actually
185
+ // enters the middleware and holds the slot before
186
+ // the second one is dispatched. 20 ms is far longer
187
+ // than any scheduler hop but well below the 200 ms
188
+ // runSQL timer, leaving the slot definitely held.
189
+ await new Promise((r) => setTimeout(r, 20));
190
+ return request(app)
191
+ .post("/api/v0/environments/e/connections/c/sqlQuery")
192
+ .send({ sqlStatement: "SELECT 2" });
193
+ })(),
194
+ ]);
195
+
196
+ expect(first.status).toBe(200);
197
+ expect(second.status).toBe(503);
198
+ // Connector should still have been called only once — the
199
+ // middleware rejected the second request before it reached
200
+ // the controller.
201
+ expect(runSQL.callCount).toBe(1);
202
+ });
203
+
204
+ it("timeout fires THIRD: 504 when the driver outlasts PUBLISHER_QUERY_TIMEOUT_MS, and the abort signal is delivered to the driver", async () => {
205
+ process.env.PUBLISHER_QUERY_TIMEOUT_MS = "20";
206
+ let observedSignal: AbortSignal | undefined;
207
+ const runSQL = sinon.stub().callsFake(
208
+ (_sql: string, opts: RunSQLOptions) =>
209
+ new Promise((_resolve, reject) => {
210
+ observedSignal = opts.abortSignal;
211
+ opts.abortSignal?.addEventListener("abort", () =>
212
+ // The driver MUST reject when its abort signal
213
+ // fires; otherwise the timeout helper has nothing
214
+ // to convert to 504 (the success-after-timeout
215
+ // race is explicitly allowed).
216
+ reject(new Error("driver: aborted")),
217
+ );
218
+ }),
219
+ );
220
+ const { app } = buildApp(runSQL);
221
+
222
+ const res = await request(app)
223
+ .post("/api/v0/environments/e/connections/c/sqlQuery")
224
+ .send({ sqlStatement: "SELECT pg_sleep(1)" });
225
+
226
+ expect(res.status).toBe(504);
227
+ expect(observedSignal).toBeDefined();
228
+ expect(observedSignal?.aborted).toBe(true);
229
+ });
230
+
231
+ it("row cap fires LAST: 413 when the driver overshoots PUBLISHER_MAX_QUERY_ROWS", async () => {
232
+ process.env.PUBLISHER_MAX_QUERY_ROWS = "2";
233
+ // Driver returns 3 rows even though we asked for cap+1 = 3
234
+ // — the sentinel pattern catches this and 413s.
235
+ const rows = [{ a: 1 }, { a: 2 }, { a: 3 }];
236
+ const runSQL = sinon.stub().resolves({ rows, totalRows: rows.length });
237
+ const { app } = buildApp(runSQL);
238
+
239
+ const res = await request(app)
240
+ .post("/api/v0/environments/e/connections/c/sqlQuery")
241
+ .send({ sqlStatement: "SELECT * FROM big_table" });
242
+
243
+ expect(res.status).toBe(413);
244
+ expect(JSON.stringify(res.body)).toContain("more than 2 rows");
245
+ });
246
+
247
+ it("happy path: all gates pass-through, 200 with the result body", async () => {
248
+ const rows = [{ a: 1 }];
249
+ const runSQL = sinon.stub().resolves({ rows, totalRows: rows.length });
250
+ const { app } = buildApp(runSQL);
251
+
252
+ const res = await request(app)
253
+ .post("/api/v0/environments/e/connections/c/sqlQuery")
254
+ .send({ sqlStatement: "SELECT 1" });
255
+
256
+ expect(res.status).toBe(200);
257
+ expect(res.body).toEqual({
258
+ data: JSON.stringify({ rows, totalRows: rows.length }),
259
+ });
260
+ });
261
+ });
@@ -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
+ });