@makoai/app-sdk 2.4.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +35 -6
  2. package/index.js +15 -0
  3. package/package.json +1 -1
  4. package/vite.d.ts +37 -3
  5. package/vite.js +297 -41
package/README.md CHANGED
@@ -96,14 +96,43 @@ export default defineConfig({ plugins: [react(), makoData()] });
96
96
  ```
97
97
 
98
98
  `makoData()` answers `__data/index.json` (the app's `bindings/*.sql`) and
99
- `__data/<name>.parquet` during `vite dev` by streaming each binding's
100
- materialized artifact from the Mako API — a binding that was never
101
- materialized is built on first request, and `POST __data/<name>/refresh`
102
- (the SDK's `refresh()`) rebuilds one on demand. Results are cached under
103
- `node_modules/.mako-data/` for five minutes (`?refresh` bypasses; a stale
104
- copy is served if the API is unreachable). It is `apply: "serve"` only —
99
+ `__data/<name>.parquet` during `vite dev` from the Mako API, and what it asks
100
+ for is **your local binding file**: it sends the text of
101
+ `bindings/<name>.sql` and gets back the parquet of exactly that query, run
102
+ read-only through the workspace connection its front matter names. When the
103
+ text is the committed binding, that is the app's stored artifact (built on
104
+ first request if it never was); when you have edited it, Mako builds a draft
105
+ from your text and hands it back without storing it — nobody else, and no
106
+ published viewer, ever sees uncommitted SQL. Building needs edit access to the
107
+ app; read-only members get committed artifacts. `POST __data/<name>/refresh`
108
+ (the SDK's `refresh()`) rebuilds from the local text on demand.
109
+
110
+ Builds run as jobs: the plugin asks for one (`async`), gets a job id back at
111
+ once, and polls it until the parquet is ready — so a query that runs for
112
+ minutes is waited for instead of failing when a proxy cuts the request at
113
+ 100 s. `pollIntervalMs` / `buildTimeoutMs` tune the wait (1 s, 30 min).
114
+
115
+ Results are cached under `node_modules/.mako-data/` for five minutes
116
+ (`revalidateMs`; `?refresh` bypasses), next to a fingerprint of the text they
117
+ were built from: after an edit the cache is never served, however long
118
+ `revalidateMs` is, and while the API is unreachable a stale copy is served
119
+ only if it was built from the same text. It is `apply: "serve"` only —
105
120
  production builds never load it.
106
121
 
122
+ ### dbt models you are still building
123
+
124
+ A binding linked to dbt (`-- dbt_project: <id>`) writes `{{ dbt_schema }}`,
125
+ which renders to the production schema. To preview models you built into
126
+ your own dbt environment, point the dev server at it:
127
+
128
+ ```ts
129
+ makoData({ dbtEnvironment: "joan" }) // or MAKO_DBT_ENV=joan in the repo's .env
130
+ ```
131
+
132
+ The environment must exist in the linked dbt project (`dbt/environments.yml`),
133
+ and a personal environment (`owner_user_id`) renders only for its owner.
134
+ These builds are drafts too: never stored, never what a published app reads.
135
+
107
136
  It also answers `__data/viewer.json` (you, as Mako sees you), and
108
137
  `MAKO_VIEWER_AS=<email>` — in the environment or the repo's `.env` —
109
138
  previews the app as that member instead (editors of the app only).
package/index.js CHANGED
@@ -451,6 +451,21 @@ export function navigate(to, opts) {
451
451
  window.history.pushState(null, "", url);
452
452
  }
453
453
  emitLocation();
454
+ // Inside Mako's shell the app is a sandboxed iframe with an opaque origin,
455
+ // so the line above moved a URL nobody can see or copy. Tell the host,
456
+ // which projects the query onto its own address bar (/apps/<slug>?...)
457
+ // and seeds it back into the iframe when that link is opened. Only the
458
+ // query travels: the frame's pathname is the preview token, not the app's.
459
+ if (window.parent !== window) {
460
+ try {
461
+ window.parent.postMessage(
462
+ { type: "mako-app:navigate", search: url.search },
463
+ "*",
464
+ );
465
+ } catch {
466
+ // A host that is not Mako, or a frame that forbids it: nothing to tell.
467
+ }
468
+ }
454
469
  }
455
470
 
456
471
  export function useLocation() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makoai/app-sdk",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "Mako app SDK: data bindings (useQuery/useDuckDB), URL state, theme.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/vite.d.ts CHANGED
@@ -11,10 +11,33 @@ export interface MakoDataOptions {
11
11
  slug?: string;
12
12
  /** Repo root. Default: the nearest ancestor holding .mako/ or .git. */
13
13
  repoRoot?: string;
14
- /** Re-fetch a cached parquet after this long (ms). Default: 5 minutes. */
14
+ /**
15
+ * Re-fetch a cached parquet after this long (ms). Default: 5 minutes. A
16
+ * cache built from different binding text (or dbt environment) is never
17
+ * served, whatever this says — `Infinity` is safe while you edit SQL.
18
+ */
15
19
  revalidateMs?: number;
16
- /** Build a never-materialized binding on first request. Default: true. */
20
+ /**
21
+ * Build bindings through the API: the local `bindings/<name>.sql` text
22
+ * (uncommitted edits included), or the committed binding when it was never
23
+ * materialized. `false` serves committed artifacts only. Default: true.
24
+ */
17
25
  materialize?: boolean;
26
+ /**
27
+ * Render `{{ dbt_schema }}` in dbt-linked bindings against this dbt
28
+ * environment (e.g. your personal one, `dbt_<you>`) instead of production.
29
+ * Those builds are yours alone — never stored as the app's data. Default:
30
+ * MAKO_DBT_ENV, else production.
31
+ */
32
+ dbtEnvironment?: string;
33
+ /**
34
+ * A build longer than the API answers synchronously comes back as a job;
35
+ * the plugin polls it starting at this interval (ms, backing off to 5×).
36
+ * Default: 1000.
37
+ */
38
+ pollIntervalMs?: number;
39
+ /** Give up waiting for one build after this long (ms). Default: 30 minutes. */
40
+ buildTimeoutMs?: number;
18
41
  /**
19
42
  * Preview the app as this member (an email): `__data/viewer.json` answers
20
43
  * as Mako would for them. Default: MAKO_VIEWER_AS, else yourself.
@@ -32,6 +55,8 @@ export interface MakoContext {
32
55
  cacheDir: string;
33
56
  /** Email of the member being previewed, or "" for yourself. */
34
57
  viewAs: string;
58
+ /** dbt environment for `{{ dbt_schema }}`, or "" for production. */
59
+ dbtEnvironment: string;
35
60
  }
36
61
 
37
62
  /** Resolve credentials and identity the way `makoData` does. */
@@ -40,9 +65,18 @@ export function resolveMakoContext(
40
65
  options?: MakoDataOptions,
41
66
  ): MakoContext;
42
67
 
68
+ /**
69
+ * What a cached parquet was built from: a hash of the binding file's text and
70
+ * the dbt environment. The plugin serves a cache entry only for a match.
71
+ */
72
+ /** CRLF/CR → LF and no trailing whitespace — what bindingFingerprint hashes. */
73
+ export function normalizeBindingSource(source: string): string;
74
+
75
+ export function bindingFingerprint(source: string, dbtEnvironment?: string): string;
76
+
43
77
  /**
44
78
  * Serve the app's data bindings (`__data/index.json`, `__data/<name>.parquet`)
45
- * during a local `vite dev` by streaming materialized artifacts from Mako.
79
+ * during a local `vite dev`, built by Mako from the local binding files.
46
80
  */
47
81
  export function makoData(options?: MakoDataOptions): Plugin;
48
82
  export default makoData;
package/vite.js CHANGED
@@ -4,12 +4,20 @@
4
4
  // `__data/<name>.parquet` itself. On a laptop nothing does, so Vite's SPA
5
5
  // fallback returns index.html and DuckDB fails with "footer != PAR1". This
6
6
  // plugin is the laptop's answer: it lists the app's bindings/*.sql as
7
- // __data/index.json and streams each binding's materialized parquet from the
8
- // Mako API, authenticated with the workspace API key in the repo's .env.
7
+ // __data/index.json and serves each binding's parquet from the Mako API,
8
+ // authenticated with `mako login` or the workspace API key in the repo's .env.
9
+ //
10
+ // The LOCAL binding file is what gets built: its text goes to the API, which
11
+ // answers with the committed artifact when the text is the committed binding
12
+ // and with a throwaway draft build when it is not — so editing
13
+ // bindings/<name>.sql shows real data before anything is committed. The local
14
+ // cache is keyed by that text, so an edit never serves a parquet built from
15
+ // the old query.
9
16
  //
10
17
  // Plain ESM, Node built-ins only — like the rest of this package.
11
18
  import fs from "node:fs";
12
19
  import path from "node:path";
20
+ import { createHash } from "node:crypto";
13
21
  import { HOSTED_API_URL, findCredential, getAccessToken } from "./credentials.js";
14
22
 
15
23
  const BINDING_NAME = /^[A-Za-z0-9_][A-Za-z0-9_-]*$/;
@@ -85,14 +93,50 @@ export function resolveMakoContext(appDir, options = {}) {
85
93
  apiUrl,
86
94
  apiKey: options.apiKey ?? env("MAKO_API_KEY") ?? "",
87
95
  workspaceId: options.workspaceId ?? env("MAKO_WORKSPACE_ID") ?? ws.workspaceId ?? "",
88
- slug: options.slug ?? path.basename(path.resolve(appDir)),
96
+ // The manifest id is the app's identity (apps.md §29); the folder
97
+ // basename is only a fallback for apps that predate ids — and is
98
+ // ambiguous once apps nest.
99
+ slug: options.slug ?? readManifestId(appDir) ?? path.basename(path.resolve(appDir)),
89
100
  bindingsDir: path.join(appDir, "bindings"),
90
101
  cacheDir: path.join(appDir, "node_modules", ".mako-data"),
91
102
  /** Preview the app as this viewer (email) — see makoData(). */
92
103
  viewAs: options.viewAs ?? env("MAKO_VIEWER_AS") ?? "",
104
+ /** Render `{{ dbt_schema }}` against this dbt environment — see makoData(). */
105
+ dbtEnvironment: options.dbtEnvironment ?? env("MAKO_DBT_ENV") ?? "",
93
106
  };
94
107
  }
95
108
 
109
+ /** CRLF/CR → LF, no trailing whitespace at the end of the file. */
110
+ export function normalizeBindingSource(source) {
111
+ return source.replace(/\r\n?/g, "\n").trimEnd();
112
+ }
113
+
114
+ /**
115
+ * What a cached parquet was built from: the binding file's exact text plus
116
+ * the dbt environment it was rendered against. A cache entry is only ever
117
+ * served for the same fingerprint, whatever its age.
118
+ */
119
+ export function bindingFingerprint(source, dbtEnvironment = "") {
120
+ // Line endings and trailing whitespace are not the query: a Windows
121
+ // checkout (core.autocrlf) must not look like an edit. The API normalises
122
+ // the text it compares the same way.
123
+ return createHash("sha256")
124
+ .update(normalizeBindingSource(source))
125
+ .update("\0")
126
+ .update(dbtEnvironment)
127
+ .digest("hex")
128
+ .slice(0, 32);
129
+ }
130
+
131
+ function readManifestId(appDir) {
132
+ try {
133
+ const raw = JSON.parse(fs.readFileSync(path.join(appDir, "mako.json"), "utf8"));
134
+ return typeof raw.id === "string" && /^[0-9a-f]{24}$/i.test(raw.id) ? raw.id : null;
135
+ } catch {
136
+ return null;
137
+ }
138
+ }
139
+
96
140
  function listBindings(bindingsDir) {
97
141
  try {
98
142
  return fs
@@ -142,7 +186,8 @@ export function makoData(options = {}) {
142
186
  ` mako-data: ${bindings.length} binding(s) for apps/${ctx.slug}` +
143
187
  (problems.length
144
188
  ? ` — NOT CONNECTED: ${problems.join("; ")} (see CLAUDE.md → Credentials)`
145
- : ` via ${ctx.apiUrl} (${ctx.apiKey ? "API key" : "mako login"})`),
189
+ : ` via ${ctx.apiUrl} (${ctx.apiKey ? "API key" : "mako login"})` +
190
+ (ctx.dbtEnvironment ? `, dbt environment "${ctx.dbtEnvironment}"` : "")),
146
191
  );
147
192
 
148
193
  // fetch() with network failures translated into something a person can
@@ -161,19 +206,93 @@ export function makoData(options = {}) {
161
206
  }
162
207
  }
163
208
 
209
+ // Long builds run as jobs: the API answers 202 + a job id (it would
210
+ // otherwise hit the edge's 100 s limit and fail with a 524 while the
211
+ // build carried on), and this polls the job until it lands. An API
212
+ // without jobs just answers synchronously, which works as before.
213
+ const pollMs = options.pollIntervalMs ?? 1000;
214
+ const buildTimeoutMs = options.buildTimeoutMs ?? 30 * 60 * 1000;
215
+ // A poll that fails transiently — the network, a 429, a 5xx, the
216
+ // edge's 524 — is retried with backoff until the overall deadline; only
217
+ // a definitive 4xx (the job is gone, access was revoked) ends the wait.
218
+ const isTransient = (status) => status === 429 || status >= 500;
219
+ async function awaitJob(name, jobId) {
220
+ const url = `${appBase()}/binding-jobs/${encodeURIComponent(jobId)}`;
221
+ const deadline = Date.now() + buildTimeoutMs;
222
+ let wait = pollMs;
223
+ let lastProblem = "";
224
+ for (;;) {
225
+ let res = null;
226
+ try {
227
+ res = await apiFetch(url, { headers: await headers() });
228
+ } catch (error) {
229
+ lastProblem = error instanceof Error ? error.message : String(error);
230
+ }
231
+ if (res) {
232
+ const body = await res.json().catch(() => ({}));
233
+ if (res.ok) {
234
+ if (body.status === "ready") return body;
235
+ if (body.status === "error") {
236
+ const error = new Error(body.error || `build ${name} failed`);
237
+ error.status = body.errorStatus ?? 502;
238
+ throw error;
239
+ }
240
+ lastProblem = "";
241
+ } else if (isTransient(res.status)) {
242
+ lastProblem = `job HTTP ${res.status}`;
243
+ } else {
244
+ const error = new Error(body.error || `build ${name}: job HTTP ${res.status}`);
245
+ error.status = res.status;
246
+ throw error;
247
+ }
248
+ }
249
+ if (Date.now() > deadline) {
250
+ const error = new Error(
251
+ `build ${name} still running after ${Math.round(buildTimeoutMs / 1000)} s (job ${jobId})` +
252
+ (lastProblem ? `; last poll: ${lastProblem}` : ""),
253
+ );
254
+ error.status = 504;
255
+ throw error;
256
+ }
257
+ await new Promise((resolve) => setTimeout(resolve, wait));
258
+ wait = Math.min(wait * 2, pollMs * 5);
259
+ }
260
+ }
261
+ async function jobArtifact(name, jobId) {
262
+ const res = await apiFetch(`${appBase()}/binding-jobs/${encodeURIComponent(jobId)}/artifact`, {
263
+ headers: await headers(),
264
+ });
265
+ if (!res.ok) {
266
+ const text = await res.text().catch(() => "");
267
+ const error = new Error(`build ${name}: result HTTP ${res.status} ${text.slice(0, 300)}`);
268
+ error.status = res.status;
269
+ throw error;
270
+ }
271
+ return Buffer.from(await res.arrayBuffer());
272
+ }
273
+ // POST …/materialize, asynchronously when the API supports it.
274
+ // Resolves to the materialize answer ({ rowCount, byteSize, materializedAt }).
275
+ async function materialize(name) {
276
+ const res = await apiFetch(
277
+ `${appBase()}/bindings/${encodeURIComponent(name)}/materialize?async=1`,
278
+ { method: "POST", headers: await headers() },
279
+ );
280
+ const body = await res.json().catch(() => ({}));
281
+ if (res.status === 202 && body.jobId) return awaitJob(name, body.jobId);
282
+ if (!res.ok) {
283
+ const error = new Error(body.error || `materialize ${name}: HTTP ${res.status}`);
284
+ error.status = res.status;
285
+ throw error;
286
+ }
287
+ return body;
288
+ }
289
+
164
290
  async function fetchArtifact(name) {
165
291
  const url = `${appBase()}/bindings/${encodeURIComponent(name)}/artifact`;
166
292
  let res = await apiFetch(url, { headers: await headers() });
167
293
  if (res.status === 404 && options.materialize !== false) {
168
294
  // Never materialized (or a live binding): build it now, then read.
169
- const built = await apiFetch(
170
- `${appBase()}/bindings/${encodeURIComponent(name)}/materialize`,
171
- { method: "POST", headers: await headers() },
172
- );
173
- if (!built.ok) {
174
- const text = await built.text().catch(() => "");
175
- throw new Error(`materialize ${name}: HTTP ${built.status} ${text.slice(0, 300)}`);
176
- }
295
+ await materialize(name);
177
296
  res = await apiFetch(url, { headers: await headers() });
178
297
  }
179
298
  if (!res.ok) {
@@ -192,24 +311,153 @@ export function makoData(options = {}) {
192
311
  return Buffer.from(await res.arrayBuffer());
193
312
  }
194
313
 
195
- // POST __data/<name>/refresh — the SDK's refresh(): rebuild the
196
- // binding through the API (the same call app_materialize makes), then
197
- // forget the local copy so the next read is the new artifact.
314
+ // The local binding file, or null when the app has none by that name
315
+ // (then the committed binding is all there is to serve).
316
+ function readSource(name) {
317
+ try {
318
+ return fs.readFileSync(path.join(ctx.bindingsDir, `${name}.sql`), "utf8");
319
+ } catch {
320
+ return null;
321
+ }
322
+ }
323
+
324
+ const cachePath = (name) => path.join(ctx.cacheDir, `${name}.parquet`);
325
+ const metaPath = (name) => path.join(ctx.cacheDir, `${name}.parquet.json`);
326
+
327
+ function cachedFingerprint(name) {
328
+ try {
329
+ return JSON.parse(fs.readFileSync(metaPath(name), "utf8")).fingerprint ?? null;
330
+ } catch {
331
+ return null;
332
+ }
333
+ }
334
+
335
+ function writeCache(name, buf, meta) {
336
+ fs.mkdirSync(ctx.cacheDir, { recursive: true });
337
+ fs.writeFileSync(cachePath(name), buf);
338
+ if (meta) fs.writeFileSync(metaPath(name), JSON.stringify(meta));
339
+ else fs.rmSync(metaPath(name), { force: true });
340
+ }
341
+
342
+ // An API that predates dev builds answers the route with its not-found
343
+ // handler (`{"success":false,"error":"Not Found"}`, or plain text from
344
+ // a proxy) — or, for a `mako login` token or scoped key, with the
345
+ // scoped-credential refusal, since the route is not on its allowlist
346
+ // yet. Remembered, so the rest of the session asks for the committed
347
+ // artifact directly (the behaviour before dev builds existed). Any
348
+ // other 404 (no such app) falls back for that request only, so the
349
+ // artifact route reports it.
350
+ let devBuildUnsupported = false;
351
+ const inflight = new Map();
352
+
353
+ // Build the LOCAL binding text through the API. Concurrent asks for the
354
+ // same text share one request (a page mounting several tables at once).
355
+ function devBuild(name, source, refresh) {
356
+ const fingerprint = bindingFingerprint(source, ctx.dbtEnvironment);
357
+ const key = `${name}:${fingerprint}:${refresh ? 1 : 0}`;
358
+ let pending = inflight.get(key);
359
+ if (pending) return pending;
360
+ pending = (async () => {
361
+ const res = await apiFetch(`${appBase()}/bindings/${encodeURIComponent(name)}/dev-build`, {
362
+ method: "POST",
363
+ headers: { ...(await headers()), "content-type": "application/json" },
364
+ body: JSON.stringify({
365
+ source,
366
+ ...(ctx.dbtEnvironment ? { dbtEnvironment: ctx.dbtEnvironment } : {}),
367
+ ...(refresh ? { refresh: true } : {}),
368
+ async: true,
369
+ }),
370
+ });
371
+ let jobDone = null;
372
+ if (res.status === 202) {
373
+ const queued = await res.json().catch(() => ({}));
374
+ if (!queued.jobId) throw new Error(`build ${name}: HTTP 202 without a job id`);
375
+ jobDone = await awaitJob(name, queued.jobId);
376
+ }
377
+ if (!jobDone && !res.ok) {
378
+ const text = await res.text().catch(() => "");
379
+ let body = null;
380
+ try {
381
+ body = JSON.parse(text);
382
+ } catch {
383
+ // Not the API's JSON error envelope.
384
+ }
385
+ const predatesDevBuild =
386
+ (res.status === 404 && (!body || body.error === "Not Found")) ||
387
+ (res.status === 403 && /restricted to the (\/api\/mcp|Mako MCP) endpoint/.test(body?.error ?? ""));
388
+ if (predatesDevBuild || res.status === 404) {
389
+ if (!predatesDevBuild) return null;
390
+ devBuildUnsupported = true;
391
+ server.config.logger.warn?.(
392
+ ` mako-data: ${ctx.apiUrl} does not build local binding edits yet; serving committed bindings`,
393
+ );
394
+ return null;
395
+ }
396
+ const error = new Error(
397
+ res.status === 401
398
+ ? `the Mako API at ${ctx.apiUrl} refused the credential (HTTP 401) — ` +
399
+ (ctx.apiKey
400
+ ? "the MAKO_API_KEY in .env is invalid for this host/workspace"
401
+ : "run `npx @makoai/cli login` in this repo again")
402
+ : body?.error || `build ${name}: HTTP ${res.status}`,
403
+ );
404
+ error.status = res.status;
405
+ throw error;
406
+ }
407
+ const buf = jobDone
408
+ ? await jobArtifact(name, jobDone.jobId)
409
+ : Buffer.from(await res.arrayBuffer());
410
+ const meta = jobDone
411
+ ? {
412
+ fingerprint,
413
+ dbtEnvironment: ctx.dbtEnvironment || null,
414
+ build: jobDone.build ?? "artifact",
415
+ rowCount: jobDone.rowCount ?? null,
416
+ builtAt: jobDone.materializedAt ?? new Date().toISOString(),
417
+ }
418
+ : {
419
+ fingerprint,
420
+ dbtEnvironment: ctx.dbtEnvironment || null,
421
+ // A redirect to the stored artifact loses the API's headers;
422
+ // that answer is always the committed artifact.
423
+ build: res.headers.get("x-mako-build") ?? "artifact",
424
+ rowCount: Number(res.headers.get("x-mako-row-count") ?? "") || null,
425
+ builtAt: res.headers.get("x-mako-materialized-at") ?? new Date().toISOString(),
426
+ };
427
+ writeCache(name, buf, meta);
428
+ return { buf, meta };
429
+ })().finally(() => inflight.delete(key));
430
+ inflight.set(key, pending);
431
+ return pending;
432
+ }
433
+
434
+ // POST __data/<name>/refresh — the SDK's refresh(): rebuild the LOCAL
435
+ // binding through the API (the committed one is re-materialized and
436
+ // stored, an edited one is built as a draft), and cache what came back.
198
437
  async function refreshBinding(name, res) {
199
- const cached = path.join(ctx.cacheDir, `${name}.parquet`);
438
+ const source = options.materialize === false || devBuildUnsupported ? null : readSource(name);
200
439
  try {
201
- const built = await apiFetch(
202
- `${appBase()}/bindings/${encodeURIComponent(name)}/materialize`,
203
- { method: "POST", headers: await headers() },
204
- );
205
- const body = await built.json().catch(() => ({}));
206
- if (!built.ok) {
207
- return json(res, built.status, {
440
+ const built = source === null ? null : await devBuild(name, source, true);
441
+ if (built) {
442
+ return json(res, 200, {
443
+ success: true,
444
+ binding: name,
445
+ materialization: "parquet",
446
+ rowCount: built.meta.rowCount ?? undefined,
447
+ byteSize: built.buf.length,
448
+ materializedAt: built.meta.builtAt,
449
+ });
450
+ }
451
+ let body;
452
+ try {
453
+ body = await materialize(name);
454
+ } catch (error) {
455
+ return json(res, error?.status ?? 502, {
208
456
  success: false,
209
- error: body.error || `materialize ${name}: HTTP ${built.status}`,
457
+ error: error instanceof Error ? error.message : String(error),
210
458
  });
211
459
  }
212
- fs.rmSync(cached, { force: true });
460
+ fs.rmSync(cachePath(name), { force: true });
213
461
  json(res, 200, {
214
462
  success: true,
215
463
  binding: name,
@@ -221,7 +469,7 @@ export function makoData(options = {}) {
221
469
  } catch (error) {
222
470
  const message = error instanceof Error ? error.message : String(error);
223
471
  server.config.logger.error(` mako-data: ${message}`);
224
- json(res, 502, { success: false, error: message });
472
+ json(res, error?.status ?? 502, { success: false, error: message });
225
473
  }
226
474
  }
227
475
 
@@ -265,19 +513,30 @@ export function makoData(options = {}) {
265
513
  });
266
514
  }
267
515
  if (isRefresh) return refreshBinding(name, res);
268
- const cached = path.join(ctx.cacheDir, `${name}.parquet`);
516
+ const cached = cachePath(name);
269
517
  const refresh = /(^|&)refresh(=|&|$)/.test(query);
518
+ const source = options.materialize === false || devBuildUnsupported ? null : readSource(name);
519
+ // Null without a local file: the committed artifact, cached by age alone.
520
+ const fingerprint = source === null ? null : bindingFingerprint(source, ctx.dbtEnvironment);
521
+ // A cache built from other text (or another dbt environment) is not
522
+ // this binding's data, however fresh — serving it is how an edited
523
+ // query ended up read with the old query's columns.
524
+ const cacheMatches = () => fingerprint === null || cachedFingerprint(name) === fingerprint;
525
+ const serveCached = (how) => {
526
+ res.setHeader("content-type", "application/vnd.apache.parquet");
527
+ res.setHeader("x-mako-data", how);
528
+ return fs.createReadStream(cached).pipe(res);
529
+ };
270
530
  try {
271
531
  const stat = fs.statSync(cached, { throwIfNoEntry: false });
272
532
  const fresh = stat && Date.now() - stat.mtimeMs < revalidateMs;
273
- if (!refresh && fresh) {
274
- res.setHeader("content-type", "application/vnd.apache.parquet");
275
- res.setHeader("x-mako-data", "cache");
276
- return fs.createReadStream(cached).pipe(res);
533
+ if (!refresh && fresh && cacheMatches()) return serveCached("cache");
534
+ let buf = null;
535
+ if (source !== null) buf = (await devBuild(name, source, false))?.buf ?? null;
536
+ if (!buf) {
537
+ buf = await fetchArtifact(name);
538
+ writeCache(name, buf, null);
277
539
  }
278
- const buf = await fetchArtifact(name);
279
- fs.mkdirSync(ctx.cacheDir, { recursive: true });
280
- fs.writeFileSync(cached, buf);
281
540
  res.setHeader("content-type", "application/vnd.apache.parquet");
282
541
  res.setHeader("content-length", String(buf.length));
283
542
  res.setHeader("x-mako-data", "api");
@@ -285,13 +544,10 @@ export function makoData(options = {}) {
285
544
  } catch (error) {
286
545
  const message = error instanceof Error ? error.message : String(error);
287
546
  server.config.logger.error(` mako-data: ${message}`);
288
- if (fs.existsSync(cached)) {
289
- // Stale beats nothing while offline.
290
- res.setHeader("content-type", "application/vnd.apache.parquet");
291
- res.setHeader("x-mako-data", "stale");
292
- return fs.createReadStream(cached).pipe(res);
293
- }
294
- json(res, 502, { error: message });
547
+ // Stale beats nothing while offline — but only data built from THIS
548
+ // text; an old query's parquet is not a stale copy of a new one.
549
+ if (fs.existsSync(cached) && cacheMatches()) return serveCached("stale");
550
+ json(res, error?.status ?? 502, { error: message });
295
551
  }
296
552
  });
297
553
  },