@malloy-publisher/server 0.0.202 → 0.0.204

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 (51) hide show
  1. package/dist/app/api-doc.yaml +25 -3
  2. package/dist/app/assets/{EnvironmentPage-CNQYDaxR.js → EnvironmentPage-CX06cjOF.js} +1 -1
  3. package/dist/app/assets/HomePage-CNFt_eUU.js +1 -0
  4. package/dist/app/assets/{MainPage-B0kNpkxT.js → MainPage-nUJ9YatG.js} +1 -1
  5. package/dist/app/assets/{PackagePage-yAh0TrOV.js → MaterializationsPage-B5goxVXW.js} +1 -1
  6. package/dist/app/assets/{ModelPage-DcVElc9L.js → ModelPage-Ba7Xh4lL.js} +1 -1
  7. package/dist/app/assets/PackagePage-BaEVdEAG.js +1 -0
  8. package/dist/app/assets/{RouteError-DknUbx_s.js → RouteError-BShQjZio.js} +1 -1
  9. package/dist/app/assets/{WorkbookPage-CCqc8otA.js → WorkbookPage-CBn6ZjJW.js} +1 -1
  10. package/dist/app/assets/{core-B3A61KGJ.es-iOUZ6RJL.js → core-DECXYL4E.es-OaRfXwuQ.js} +1 -1
  11. package/dist/app/assets/{index-W0bOLKGl.js → index-BLfPC1gy.js} +2 -2
  12. package/dist/app/assets/index-DqiJ0bWp.js +455 -0
  13. package/dist/app/assets/index-Dy3YhAZQ.js +1812 -0
  14. package/dist/app/assets/index.umd-DAN9K8yC.js +2469 -0
  15. package/dist/app/index.html +1 -1
  16. package/dist/package_load_worker.mjs +392 -67
  17. package/dist/server.mjs +418 -153
  18. package/package.json +11 -11
  19. package/src/ducklake_version.spec.ts +43 -0
  20. package/src/ducklake_version.ts +26 -0
  21. package/src/errors.ts +18 -1
  22. package/src/package_load/package_load_pool.ts +0 -5
  23. package/src/package_load/package_load_worker.ts +41 -99
  24. package/src/package_load/protocol.ts +1 -7
  25. package/src/service/annotations.spec.ts +118 -0
  26. package/src/service/annotations.ts +91 -0
  27. package/src/service/authorize.spec.ts +132 -0
  28. package/src/service/authorize.ts +241 -0
  29. package/src/service/authorize_integration.spec.ts +838 -0
  30. package/src/service/connection.ts +1 -1
  31. package/src/service/environment.ts +4 -4
  32. package/src/service/environment_store.ts +14 -2
  33. package/src/service/filter.spec.ts +14 -3
  34. package/src/service/filter.ts +5 -1
  35. package/src/service/filter_bypass.spec.ts +418 -0
  36. package/src/service/given.ts +37 -12
  37. package/src/service/givens_integration.spec.ts +34 -7
  38. package/src/service/materialization_service.ts +25 -20
  39. package/src/service/materialized_table_gc.spec.ts +6 -5
  40. package/src/service/materialized_table_gc.ts +2 -50
  41. package/src/service/model.spec.ts +203 -8
  42. package/src/service/model.ts +305 -155
  43. package/src/service/package_worker_path.spec.ts +113 -0
  44. package/src/service/quoting.ts +0 -20
  45. package/src/service/restricted_mode.spec.ts +299 -0
  46. package/src/service/source_extraction.ts +226 -0
  47. package/src/storage/StorageManager.ts +73 -0
  48. package/dist/app/assets/HomePage-DBFTIoD8.js +0 -1
  49. package/dist/app/assets/index-F_o127LC.js +0 -454
  50. package/dist/app/assets/index-QeX_e740.js +0 -1803
  51. package/dist/app/assets/index.umd-CEDRw4TK.js +0 -1145
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@malloy-publisher/server",
3
3
  "description": "Malloy Publisher Server",
4
- "version": "0.0.202",
4
+ "version": "0.0.204",
5
5
  "main": "dist/server.mjs",
6
6
  "bin": {
7
7
  "malloy-publisher": "dist/server.mjs"
@@ -34,16 +34,16 @@
34
34
  "@azure/identity": "^4.13.0",
35
35
  "@azure/storage-blob": "^12.26.0",
36
36
  "@google-cloud/storage": "^7.16.0",
37
- "@malloydata/db-bigquery": "^0.0.394",
38
- "@malloydata/db-databricks": "^0.0.394",
39
- "@malloydata/db-duckdb": "^0.0.394",
40
- "@malloydata/db-mysql": "^0.0.394",
41
- "@malloydata/db-postgres": "^0.0.394",
42
- "@malloydata/db-snowflake": "^0.0.394",
43
- "@malloydata/db-trino": "^0.0.394",
44
- "@malloydata/malloy": "^0.0.394",
45
- "@malloydata/malloy-sql": "^0.0.394",
46
- "@malloydata/render-validator": "^0.0.394",
37
+ "@malloydata/db-bigquery": "^0.0.405",
38
+ "@malloydata/db-databricks": "^0.0.405",
39
+ "@malloydata/db-duckdb": "^0.0.405",
40
+ "@malloydata/db-mysql": "^0.0.405",
41
+ "@malloydata/db-postgres": "^0.0.405",
42
+ "@malloydata/db-snowflake": "^0.0.405",
43
+ "@malloydata/db-trino": "^0.0.405",
44
+ "@malloydata/malloy": "^0.0.405",
45
+ "@malloydata/malloy-sql": "^0.0.405",
46
+ "@malloydata/render-validator": "^0.0.405",
47
47
  "@modelcontextprotocol/sdk": "^1.13.2",
48
48
  "@opentelemetry/api": "^1.9.0",
49
49
  "@opentelemetry/auto-instrumentations-node": "^0.57.0",
@@ -0,0 +1,43 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import {
3
+ isCatalogVersionSupported,
4
+ SUPPORTED_CATALOG_VERSIONS,
5
+ } from "./ducklake_version";
6
+
7
+ describe("ducklake_version", () => {
8
+ describe("SUPPORTED_CATALOG_VERSIONS", () => {
9
+ it("matches the 0.3-line DuckLake extension's internal list", () => {
10
+ expect([...SUPPORTED_CATALOG_VERSIONS]).toEqual([
11
+ "0.1",
12
+ "0.2",
13
+ "0.3-dev1",
14
+ "0.3",
15
+ ]);
16
+ });
17
+ });
18
+
19
+ describe("isCatalogVersionSupported", () => {
20
+ it("accepts each documented 0.3-line version", () => {
21
+ for (const v of SUPPORTED_CATALOG_VERSIONS) {
22
+ expect(isCatalogVersionSupported(v)).toBe(true);
23
+ }
24
+ });
25
+
26
+ it("rejects 1.0-line versions", () => {
27
+ expect(isCatalogVersionSupported("1.0")).toBe(false);
28
+ expect(isCatalogVersionSupported("1.0.0")).toBe(false);
29
+ expect(isCatalogVersionSupported("1.1.0")).toBe(false);
30
+ });
31
+
32
+ it("rejects newer pre-1.0 versions not in the supported list", () => {
33
+ expect(isCatalogVersionSupported("0.4")).toBe(false);
34
+ expect(isCatalogVersionSupported("0.3-dev2")).toBe(false);
35
+ });
36
+
37
+ it("rejects empty and malformed input without throwing", () => {
38
+ expect(isCatalogVersionSupported("")).toBe(false);
39
+ expect(isCatalogVersionSupported("not a version")).toBe(false);
40
+ expect(isCatalogVersionSupported("0.3 ")).toBe(false);
41
+ });
42
+ });
43
+ });
@@ -0,0 +1,26 @@
1
+ // DuckLake catalog format compatibility check. Lives at `src/` root so
2
+ // both `service/` (user-facing connections) and `storage/` (materialization
3
+ // catalog) can pre-flight a catalog's recorded version without bringing in
4
+ // the other layer's helpers. See CLAUDE.md's "Two parallel DuckLake/PG
5
+ // attach paths" note.
6
+ //
7
+ // The Publisher Docker image bakes a specific DuckLake extension version at
8
+ // build time (see Dockerfile). If a catalog's on-disk format version is
9
+ // newer than what the baked extension supports, ATTACH fails deep inside
10
+ // DuckDB with a generic 500. The preflight catches that case and surfaces
11
+ // it as a non-retryable 422 instead.
12
+
13
+ // Versions the currently-baked DuckLake extension (0.3 line, paired with
14
+ // duckdb@1.4.4) can read. When the Publisher upgrades to the DuckLake 1.0
15
+ // line (separate PR, requires duckdb@1.5+), extend this list to include
16
+ // "1.0" and later format versions.
17
+ export const SUPPORTED_CATALOG_VERSIONS: readonly string[] = [
18
+ "0.1",
19
+ "0.2",
20
+ "0.3-dev1",
21
+ "0.3",
22
+ ];
23
+
24
+ export function isCatalogVersionSupported(version: string): boolean {
25
+ return SUPPORTED_CATALOG_VERSIONS.includes(version);
26
+ }
package/src/errors.ts CHANGED
@@ -6,6 +6,8 @@ export function internalErrorToHttpError(error: Error) {
6
6
  return httpError(400, error.message);
7
7
  } else if (error instanceof FrozenConfigError) {
8
8
  return httpError(403, error.message);
9
+ } else if (error instanceof AccessDeniedError) {
10
+ return httpError(403, error.message);
9
11
  } else if (error instanceof EnvironmentNotFoundError) {
10
12
  return httpError(404, error.message);
11
13
  } else if (error instanceof PackageNotFoundError) {
@@ -98,7 +100,10 @@ export class ConnectionAuthError extends Error {
98
100
  }
99
101
 
100
102
  export class ModelCompilationError extends Error {
101
- constructor(error: MalloyError) {
103
+ // Accepts a MalloyError or any message-bearing object, so callers that add
104
+ // context around a compile failure (e.g. naming the source whose authorize
105
+ // annotation failed) can reuse this 424 mapping without a separate class.
106
+ constructor(error: { message: string }) {
102
107
  super(error.message);
103
108
  }
104
109
  }
@@ -111,6 +116,18 @@ export class FrozenConfigError extends Error {
111
116
  }
112
117
  }
113
118
 
119
+ /**
120
+ * A request was denied by a source's `#(authorize)` gate (HTTP 403). Thrown by
121
+ * the runtime authorize check when no in-scope expression evaluates true for
122
+ * the supplied givens (including when a referenced given has no value).
123
+ */
124
+ export class AccessDeniedError extends Error {
125
+ constructor(message: string) {
126
+ super(message);
127
+ this.name = "AccessDeniedError";
128
+ }
129
+ }
130
+
114
131
  export class MaterializationNotFoundError extends Error {
115
132
  constructor(message: string) {
116
133
  super(message);
@@ -67,7 +67,6 @@
67
67
  * it resolves.
68
68
  */
69
69
  import type {
70
- Annotation,
71
70
  FetchSchemaOptions,
72
71
  InfoConnection,
73
72
  LookupConnection,
@@ -811,15 +810,11 @@ export class PackageLoadPool {
811
810
 
812
811
  function buildFetchOptions(options: {
813
812
  refreshTimestamp?: number;
814
- modelAnnotation?: Annotation;
815
813
  }): FetchSchemaOptions {
816
814
  const out: FetchSchemaOptions = {};
817
815
  if (options.refreshTimestamp !== undefined) {
818
816
  out.refreshTimestamp = options.refreshTimestamp;
819
817
  }
820
- if (options.modelAnnotation !== undefined) {
821
- out.modelAnnotation = options.modelAnnotation;
822
- }
823
818
  return out;
824
819
  }
825
820
 
@@ -46,7 +46,6 @@
46
46
  */
47
47
  import {
48
48
  contextOverlay,
49
- type Annotation,
50
49
  type BuildManifestEntry,
51
50
  type Connection,
52
51
  type FetchSchemaOptions,
@@ -56,16 +55,12 @@ import {
56
55
  type ModelDef,
57
56
  type ModelMaterializer,
58
57
  modelDefToModelInfo,
59
- type NamedModelObject,
60
58
  type NamedQueryDef,
61
59
  type Query,
62
60
  Runtime,
63
61
  type SQLSourceDef,
64
62
  type SQLSourceRequest,
65
- type StructDef,
66
63
  type TableSourceDef,
67
- type TurtleDef,
68
- isSourceDef,
69
64
  } from "@malloydata/malloy";
70
65
  import * as Malloy from "@malloydata/malloy-interfaces";
71
66
  import {
@@ -84,7 +79,13 @@ import {
84
79
  PACKAGE_MANIFEST_NAME,
85
80
  } from "../constants";
86
81
  import { HackyDataStylesAccumulator } from "../data_styles";
87
- import { parseFilters, type FilterDefinition } from "../service/filter";
82
+ import { ModelCompilationError } from "../errors";
83
+ import { validateAuthorizeProbes } from "../service/authorize";
84
+ import { type FilterDefinition } from "../service/filter";
85
+ import {
86
+ extractQueriesFromModelDef,
87
+ extractSourcesFromModelDef,
88
+ } from "../service/source_extraction";
88
89
  import {
89
90
  malloyGivenToApi,
90
91
  type MalloyGiven,
@@ -271,15 +272,13 @@ class ProxyConnection {
271
272
 
272
273
  function serializeFetchOptions(options: FetchSchemaOptions): {
273
274
  refreshTimestamp?: number;
274
- modelAnnotation?: Annotation;
275
275
  } {
276
- const out: { refreshTimestamp?: number; modelAnnotation?: Annotation } = {};
276
+ const out: {
277
+ refreshTimestamp?: number;
278
+ } = {};
277
279
  if (options.refreshTimestamp !== undefined) {
278
280
  out.refreshTimestamp = options.refreshTimestamp;
279
281
  }
280
- if (options.modelAnnotation !== undefined) {
281
- out.modelAnnotation = options.modelAnnotation;
282
- }
283
282
  return out;
284
283
  }
285
284
 
@@ -411,6 +410,7 @@ interface ApiSourceWire {
411
410
  views?: { name: string; annotations?: string[] }[];
412
411
  filters?: unknown[];
413
412
  givens?: unknown[];
413
+ authorize?: string[];
414
414
  }
415
415
  interface ApiQueryWire {
416
416
  name: string;
@@ -472,96 +472,20 @@ function appendLocalSourceInfos(
472
472
  }
473
473
  }
474
474
 
475
+ // Source / query introspection is shared with the in-process path; see
476
+ // service/source_extraction.ts. The worker has no logger, so a filter parse
477
+ // failure is swallowed silently (no `onParseError` callback) — matching the
478
+ // prior worker-local behavior.
475
479
  function extractSources(
476
- modelPath: string,
477
480
  modelDef: ModelDef,
478
481
  givens: ApiGivenWire[] | undefined,
479
482
  ): { sources: ApiSourceWire[]; filterMap: Map<string, FilterDefinition[]> } {
480
- const filterMap = new Map<string, FilterDefinition[]>();
481
- const sources: ApiSourceWire[] = Object.values(modelDef.contents)
482
- .filter((obj) => isSourceDef(obj))
483
- .map((sourceObj) => {
484
- const sourceName =
485
- (sourceObj as StructDef).as || (sourceObj as StructDef).name;
486
- const annotations = (sourceObj as StructDef).annotation?.blockNotes
487
- ?.filter((note) => note.at.url.includes(modelPath))
488
- .map((note) => note.text);
489
-
490
- const collected: string[][] = [];
491
- let cur: Annotation | undefined = (sourceObj as StructDef).annotation;
492
- while (cur) {
493
- if (cur.blockNotes) {
494
- collected.push(cur.blockNotes.map((note) => note.text));
495
- }
496
- cur = cur.inherits;
497
- }
498
- const allAnnotations = collected.reverse().flat();
499
- let filters: unknown[] | undefined;
500
- if (allAnnotations.length > 0) {
501
- try {
502
- const parsed = parseFilters(allAnnotations);
503
- if (parsed.length > 0) {
504
- filterMap.set(sourceName, parsed);
505
- const fields = (sourceObj as StructDef).fields;
506
- filters = parsed.map((f) => {
507
- const field = fields.find(
508
- (fd) => (fd.as || fd.name) === f.dimension,
509
- );
510
- return {
511
- name: f.name,
512
- dimension: f.dimension,
513
- type: f.type,
514
- implicit: f.implicit,
515
- required: f.required,
516
- dimensionType: field?.type as string | undefined,
517
- };
518
- });
519
- }
520
- } catch {
521
- /* parse errors are warnings; matches in-process */
522
- }
523
- }
524
-
525
- const views = (sourceObj as StructDef).fields
526
- .filter((f) => f.type === "turtle")
527
- .filter((turtle) =>
528
- (turtle as TurtleDef).pipeline
529
- .map((stage) => stage.type)
530
- .every((type) => type === "reduce"),
531
- )
532
- .map((turtle) => ({
533
- name: turtle.as || turtle.name,
534
- annotations: turtle?.annotation?.blockNotes
535
- ?.filter((note) => note.at.url.includes(modelPath))
536
- .map((note) => note.text),
537
- }));
538
-
539
- return {
540
- name: sourceName,
541
- annotations,
542
- views,
543
- filters,
544
- givens,
545
- } as ApiSourceWire;
546
- });
547
-
548
- return { sources, filterMap };
483
+ const { sources, filterMap } = extractSourcesFromModelDef(modelDef, givens);
484
+ return { sources: sources as unknown as ApiSourceWire[], filterMap };
549
485
  }
550
486
 
551
- function extractQueries(modelPath: string, modelDef: ModelDef): ApiQueryWire[] {
552
- const isNamedQuery = (obj: NamedModelObject): obj is NamedQueryDef =>
553
- obj.type === "query";
554
- return Object.values(modelDef.contents)
555
- .filter(isNamedQuery)
556
- .map((q) => ({
557
- name: q.as || q.name,
558
- sourceName: typeof q.structRef === "string" ? q.structRef : undefined,
559
- annotations: q?.annotation?.blockNotes
560
- ?.filter((note: { at: { url: string } }) =>
561
- note.at.url.includes(modelPath),
562
- )
563
- .map((note: { text: string }) => note.text),
564
- }));
487
+ function extractQueries(modelDef: ModelDef): ApiQueryWire[] {
488
+ return extractQueriesFromModelDef(modelDef) as ApiQueryWire[];
565
489
  }
566
490
 
567
491
  function buildRuntimeForModel(
@@ -621,8 +545,12 @@ async function compileMalloyModel(
621
545
  );
622
546
  appendLocalSourceInfos(modelDef, sourceInfos, importedNames);
623
547
 
624
- const { sources, filterMap } = extractSources(modelPath, modelDef, givens);
625
- const queries = extractQueries(modelPath, modelDef);
548
+ const { sources, filterMap } = extractSources(modelDef, givens);
549
+ const queries = extractQueries(modelDef);
550
+ // Validate #(authorize) at compile time (shared with Model.create). Throws
551
+ // on an unknown given / source-field reference; compileOneModel's catch
552
+ // turns it into this model's compilationError.
553
+ await validateAuthorizeProbes(mm, sources);
626
554
 
627
555
  return {
628
556
  modelPath,
@@ -798,10 +726,12 @@ async function compileNotebookModel(
798
726
  collected.importedNames,
799
727
  );
800
728
  finalSourceInfos = collected.sourceInfos;
801
- const extracted = extractSources(modelPath, finalModelDef, finalGivens);
729
+ const extracted = extractSources(finalModelDef, finalGivens);
802
730
  finalSources = extracted.sources;
803
731
  finalFilterMap = extracted.filterMap;
804
- finalQueries = extractQueries(modelPath, finalModelDef);
732
+ finalQueries = extractQueries(finalModelDef);
733
+ // Validate #(authorize) at compile time (shared with Model.create).
734
+ await validateAuthorizeProbes(mm, finalSources);
805
735
  }
806
736
 
807
737
  return {
@@ -899,6 +829,18 @@ function serializeError(error: unknown): SerializedError {
899
829
  isCompilationError: true,
900
830
  };
901
831
  }
832
+ // ModelCompilationError (e.g. an invalid #(authorize) annotation caught by
833
+ // validateAuthorizeProbes) carries no Malloy `problems`, but it must keep its
834
+ // compilation-error classification across the worker boundary so the main
835
+ // thread re-wraps it as a 424, not a generic 500.
836
+ if (error instanceof ModelCompilationError) {
837
+ return {
838
+ name: error.name,
839
+ message: error.message,
840
+ stack: error.stack,
841
+ isCompilationError: true,
842
+ };
843
+ }
902
844
  if (error instanceof Error) {
903
845
  return {
904
846
  name: error.name,
@@ -64,11 +64,7 @@
64
64
  * cheaper than `JSON.stringify` for the multi-MB modelDef payloads.
65
65
  */
66
66
 
67
- import type {
68
- Annotation,
69
- SQLSourceDef,
70
- TableSourceDef,
71
- } from "@malloydata/malloy";
67
+ import type { SQLSourceDef, TableSourceDef } from "@malloydata/malloy";
72
68
 
73
69
  // ──────────────────────────────────────────────────────────────────────
74
70
  // Direction: main ──▶ worker (load-package job)
@@ -236,7 +232,6 @@ export interface SchemaForTablesRequest {
236
232
  tables: Record<string, string>;
237
233
  options: {
238
234
  refreshTimestamp?: number;
239
- modelAnnotation?: Annotation;
240
235
  };
241
236
  }
242
237
 
@@ -256,7 +251,6 @@ export interface SchemaForSqlRequest {
256
251
  sentence: unknown;
257
252
  options: {
258
253
  refreshTimestamp?: number;
259
- modelAnnotation?: Annotation;
260
254
  };
261
255
  }
262
256
 
@@ -0,0 +1,118 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import type { ModelDef } from "@malloydata/malloy";
3
+ import {
4
+ annotationTexts,
5
+ isReservedRoute,
6
+ modelAnnotations,
7
+ } from "./annotations";
8
+
9
+ // Minimal `ModelDef` carrying only what `modelAnnotations` reads: `modelID`
10
+ // and the `modelAnnotations` registry (modelID → { ownNotes, inheritsFrom }).
11
+ const makeModelDef = (
12
+ modelID: string,
13
+ registry: Record<
14
+ string,
15
+ {
16
+ ownNotes: { notes?: string[]; blockNotes?: string[] };
17
+ inheritsFrom: string[];
18
+ }
19
+ >,
20
+ ): ModelDef =>
21
+ ({
22
+ modelID,
23
+ modelAnnotations: Object.fromEntries(
24
+ Object.entries(registry).map(([id, { ownNotes, inheritsFrom }]) => [
25
+ id,
26
+ {
27
+ ownNotes: {
28
+ notes: ownNotes.notes?.map((text) => ({ text, at: {} })),
29
+ blockNotes: ownNotes.blockNotes?.map((text) => ({
30
+ text,
31
+ at: {},
32
+ })),
33
+ },
34
+ inheritsFrom,
35
+ },
36
+ ]),
37
+ ),
38
+ }) as unknown as ModelDef;
39
+
40
+ const noteTexts = (def: ModelDef): string[] =>
41
+ (modelAnnotations(def).notes ?? []).map((note) => note.text);
42
+
43
+ describe("isReservedRoute", () => {
44
+ it("treats the empty route (MOTLY / render config) as reserved", () => {
45
+ expect(isReservedRoute("")).toBe(true);
46
+ });
47
+
48
+ it("treats Malloy's punctuation sigils as reserved", () => {
49
+ // Form 2 reserves the entire punct-only namespace, not just the
50
+ // currently-claimed sigils (`!` `@` `"` `:`).
51
+ for (const route of ["!", "@", '"', ":", "%", "-", "::"]) {
52
+ expect(isReservedRoute(route)).toBe(true);
53
+ }
54
+ });
55
+
56
+ it("treats bracketed app routes as not reserved", () => {
57
+ for (const route of [
58
+ "doc",
59
+ "label",
60
+ "filter",
61
+ "bar-chart",
62
+ "https://example.com/ns",
63
+ "123",
64
+ "══SECURITY══",
65
+ ]) {
66
+ expect(isReservedRoute(route)).toBe(false);
67
+ }
68
+ });
69
+ });
70
+
71
+ describe("modelAnnotations", () => {
72
+ it("returns the local model's own `##` notes", () => {
73
+ const def = makeModelDef("local", {
74
+ local: { ownNotes: { notes: ["## local"] }, inheritsFrom: [] },
75
+ });
76
+ expect(noteTexts(def)).toEqual(["## local"]);
77
+ });
78
+
79
+ it("falls back to an imported model's notes when the local model has no own `##` (authorize must not fail open)", () => {
80
+ // Regression: a `##(authorize)` declared in an imported file must still
81
+ // flow into the importing file's file-level gate even when the importing
82
+ // file declares no `##` of its own. An earlier fold kept an empty local
83
+ // link at the top, so `.notes` returned [] and the gate failed open.
84
+ const def = makeModelDef("local", {
85
+ local: { ownNotes: {}, inheritsFrom: ["base"] },
86
+ base: {
87
+ ownNotes: { notes: ['## (authorize) request.user == "admin"'] },
88
+ inheritsFrom: [],
89
+ },
90
+ });
91
+ expect(noteTexts(def)).toEqual([
92
+ '## (authorize) request.user == "admin"',
93
+ ]);
94
+ });
95
+
96
+ it("surfaces the local model's notes at the top when both local and import have `##`", () => {
97
+ const def = makeModelDef("local", {
98
+ local: { ownNotes: { notes: ["## local"] }, inheritsFrom: ["base"] },
99
+ base: { ownNotes: { notes: ["## base"] }, inheritsFrom: [] },
100
+ });
101
+ // `.notes` is the top (local); `.texts()` flattens the whole lineage
102
+ // ancestral-first.
103
+ expect(noteTexts(def)).toEqual(["## local"]);
104
+ expect(annotationTexts(modelAnnotations(def))).toEqual([
105
+ "## base",
106
+ "## local",
107
+ ]);
108
+ });
109
+
110
+ it("is cycle-safe and returns an empty bundle for a model with no annotations", () => {
111
+ const def = makeModelDef("local", {
112
+ local: { ownNotes: {}, inheritsFrom: ["base"] },
113
+ base: { ownNotes: {}, inheritsFrom: ["local"] }, // back-edge cycle
114
+ });
115
+ expect(modelAnnotations(def)).toEqual({});
116
+ expect(noteTexts(def)).toEqual([]);
117
+ });
118
+ });
@@ -0,0 +1,91 @@
1
+ import { Annotations, type ModelDef } from "@malloydata/malloy";
2
+
3
+ /**
4
+ * The raw IR annotation bundle. Workaround: `@malloydata/malloy` exports the
5
+ * `Annotations` view but not the underlying `AnnotationsDef` type, so we
6
+ * recover it from the view's constructor parameter. Replace with a direct
7
+ * `import type { AnnotationsDef }` once malloy exports it.
8
+ */
9
+ export type AnnotationsDef = NonNullable<
10
+ ConstructorParameters<typeof Annotations>[0]
11
+ >;
12
+
13
+ /**
14
+ * True if a route belongs to Malloy's own reserved namespace rather than an
15
+ * application. Malloy claims the empty route (`''` — MOTLY tags / render
16
+ * config) and the whole punctuation-sigil namespace (`!` `@` `"` `:`, and
17
+ * any punct-only route — Form 2 reserves all of them). Everything else is a
18
+ * bracketed app route (`#(doc)`, `#<label>`, …).
19
+ *
20
+ * TODO: this route classification belongs in `@malloydata/malloy` core,
21
+ * beside `parsePrefix` — otherwise every consumer reinvents it. Remove when
22
+ * core exports an equivalent.
23
+ */
24
+ export function isReservedRoute(route: string): boolean {
25
+ return route === "" || !/[\p{L}\p{N}]/u.test(route);
26
+ }
27
+
28
+ /**
29
+ * The model (`##`) annotation bundle for one model, folded across its
30
+ * import/extend lineage.
31
+ *
32
+ * Workaround: malloy 0.0.405 moved model annotations off `ModelDef.annotation`
33
+ * and onto `ModelDef.modelAnnotations` (a `modelID → {ownNotes, inheritsFrom}`
34
+ * registry), folded by the `getModelAnnotations` helper — which malloy does
35
+ * NOT export from its public barrel. We replicate it (matching
36
+ * `@malloydata/malloy/dist/model/annotation_utils.js`): a post-order DFS over
37
+ * `inheritsFrom` (cycle-safe, each model emitted once at its most-ancestral
38
+ * slot) yields ancestral-first / local-last order, folded into an
39
+ * `AnnotationsDef` whose `inherits` chain runs most-ancestral-deepest / local
40
+ * at the top.
41
+ *
42
+ * A model that contributes no `##` of its own adds NO link to the chain (we
43
+ * skip empty `ownNotes`), so `.notes` returns the nearest ancestor that
44
+ * actually has notes — not an empty local node. This matters because `.notes`
45
+ * feeds file-level `##(authorize)` enforcement: an imported model's
46
+ * `##(authorize)` must still flow into an importing file that declares no `##`
47
+ * of its own. We also copy only `notes`/`blockNotes` rather than spreading
48
+ * `ownNotes`, whose own `inherits` would otherwise leak in. Replace with a
49
+ * direct `import { getModelAnnotations }` once malloy exports it.
50
+ */
51
+ export function modelAnnotations(modelDef: ModelDef): AnnotationsDef {
52
+ const registry = modelDef.modelAnnotations ?? {};
53
+ const visited = new Set<string>();
54
+ const order: string[] = [];
55
+ const visit = (id: string): void => {
56
+ if (visited.has(id)) return;
57
+ visited.add(id);
58
+ const entry = registry[id];
59
+ if (!entry) return;
60
+ for (const dep of entry.inheritsFrom) visit(dep);
61
+ order.push(id); // post-order: ancestors precede the model itself
62
+ };
63
+ visit(modelDef.modelID);
64
+
65
+ // Fold most-ancestral → local so the local model lands at the top of the
66
+ // resulting `inherits` chain. Models with no own notes add no link.
67
+ let folded: AnnotationsDef | undefined;
68
+ for (const id of order) {
69
+ const own = registry[id].ownNotes;
70
+ if (!own.notes?.length && !own.blockNotes?.length) continue;
71
+ folded = {
72
+ notes: own.notes,
73
+ blockNotes: own.blockNotes,
74
+ inherits: folded,
75
+ };
76
+ }
77
+ return folded ?? {};
78
+ }
79
+
80
+ /**
81
+ * Every annotation text on an entity — its own `notes` and `blockNotes`
82
+ * plus everything inherited from its ancestors. All of an entity's
83
+ * annotations apply; none are dropped by source location. Returns
84
+ * `undefined`, not `[]`, when empty, to match the optional API shape.
85
+ */
86
+ export function annotationTexts(
87
+ annote: AnnotationsDef | undefined,
88
+ ): string[] | undefined {
89
+ const texts = new Annotations(annote).texts();
90
+ return texts.length > 0 ? texts : undefined;
91
+ }