@malloy-publisher/server 0.0.231 → 0.0.233
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.docker.md +4 -0
- package/dist/app/api-doc.yaml +135 -13
- package/dist/app/assets/{EnvironmentPage-wa_EPkwK.js → EnvironmentPage-DutP7T8h.js} +1 -1
- package/dist/app/assets/{HomePage-jnCrupQp.js → HomePage-BcxDrBfl.js} +1 -1
- package/dist/app/assets/{LightMode-DYbwNULZ.js → LightMode-BJukGxgz.js} +1 -1
- package/dist/app/assets/{MainPage-CuJLrPNI.js → MainPage-DXbwlMeF.js} +2 -2
- package/dist/app/assets/{MaterializationsPage-D_67x2ee.js → MaterializationsPage-BBQksmTU.js} +1 -1
- package/dist/app/assets/{ModelPage-D5JtAWqR.js → ModelPage-C6tK51uU.js} +1 -1
- package/dist/app/assets/{PackagePage-BRwtqUSG.js → PackagePage-Bo3cwwZE.js} +1 -1
- package/dist/app/assets/{RouteError-CBNNrnSD.js → RouteError-BufkcAKE.js} +1 -1
- package/dist/app/assets/{ThemeEditorPage-CTCeBneA.js → ThemeEditorPage-DICvvKpa.js} +1 -1
- package/dist/app/assets/{WorkbookPage-SN6f1RBm.js → WorkbookPage-Dkwt75Nj.js} +1 -1
- package/dist/app/assets/{core-Dp3q5Ieu.es-CD5FvM2s.js → core-C0nunIQT.es-DlMLKZBK.js} +1 -1
- package/dist/app/assets/{index-DU4r7GdU.js → index-BabP-V-S.js} +346 -321
- package/dist/app/assets/{index-BLCx1EdC.js → index-BusxL5Pt.js} +1 -1
- package/dist/app/assets/{index-C_tJstcx.js → index-CmEVVe-8.js} +15 -15
- package/dist/app/assets/{index-CfmBVB6M.js → index-Cs4WVm2z.js} +1 -1
- package/dist/app/assets/{index-B3Nn8Vm2.js → index-qnhU9CGo.js} +266 -265
- package/dist/app/index.html +1 -1
- package/dist/instrumentation.mjs +2 -0
- package/dist/package_load_worker.mjs +11 -1
- package/dist/server.mjs +22627 -1260
- package/package.json +12 -12
- package/scripts/bake-duckdb-extensions.js +4 -1
- package/src/config.spec.ts +39 -0
- package/src/config.ts +135 -0
- package/src/controller/materialization.controller.spec.ts +62 -0
- package/src/controller/materialization.controller.ts +15 -0
- package/src/controller/package.controller.spec.ts +6 -0
- package/src/controller/package.controller.ts +7 -2
- package/src/controller/query.controller.ts +26 -21
- package/src/errors.ts +19 -0
- package/src/json_utils.spec.ts +51 -0
- package/src/json_utils.ts +33 -0
- package/src/logger.spec.ts +18 -1
- package/src/logger.ts +3 -1
- package/src/materialization_metrics.spec.ts +89 -4
- package/src/materialization_metrics.ts +155 -5
- package/src/mcp/query_envelope.spec.ts +229 -0
- package/src/mcp/query_envelope.ts +230 -0
- package/src/mcp/server.protocol.spec.ts +128 -16
- package/src/mcp/skills/build_skills_bundle.ts +94 -4
- package/src/mcp/skills/skills_bundle.json +1 -1
- package/src/mcp/skills/skills_bundle.spec.ts +113 -4
- package/src/mcp/tool_response.spec.ts +108 -0
- package/src/mcp/tool_response.ts +138 -0
- package/src/mcp/tools/compile_tool.spec.ts +112 -4
- package/src/mcp/tools/compile_tool.ts +61 -30
- package/src/mcp/tools/docs_search_tool.ts +6 -16
- package/src/mcp/tools/embedding_index.spec.ts +1236 -0
- package/src/mcp/tools/embedding_index.ts +808 -0
- package/src/mcp/tools/execute_query_tool.spec.ts +23 -3
- package/src/mcp/tools/execute_query_tool.ts +90 -151
- package/src/mcp/tools/get_context_eval.ts +194 -45
- package/src/mcp/tools/get_context_tool.spec.ts +358 -5
- package/src/mcp/tools/get_context_tool.ts +202 -56
- package/src/mcp/tools/reload_package_tool.ts +3 -29
- package/src/pg_helpers.spec.ts +201 -0
- package/src/pg_helpers.ts +44 -5
- package/src/server.ts +24 -0
- package/src/service/build_plan.spec.ts +128 -2
- package/src/service/build_plan.ts +239 -17
- package/src/service/compile_fragment_techniques.spec.ts +156 -0
- package/src/service/connection.spec.ts +371 -1
- package/src/service/connection.ts +339 -20
- package/src/service/connection_config.spec.ts +108 -0
- package/src/service/connection_config.ts +47 -8
- package/src/service/connection_federation.spec.ts +184 -0
- package/src/service/duckdb_instance_isolation.spec.ts +137 -0
- package/src/service/embedding_provider.spec.ts +329 -0
- package/src/service/embedding_provider.ts +236 -0
- package/src/service/environment.ts +274 -12
- package/src/service/environment_store.spec.ts +678 -3
- package/src/service/environment_store.ts +449 -33
- package/src/service/environment_store_clone.spec.ts +350 -0
- package/src/service/manifest_loader.spec.ts +68 -13
- package/src/service/manifest_loader.ts +67 -19
- package/src/service/materialization_build_session.spec.ts +435 -0
- package/src/service/materialization_build_session.ts +681 -0
- package/src/service/materialization_eligibility.spec.ts +158 -0
- package/src/service/materialization_eligibility.ts +305 -0
- package/src/service/materialization_serve_transform.spec.ts +1003 -0
- package/src/service/materialization_serve_transform.ts +779 -0
- package/src/service/materialization_service.spec.ts +774 -7
- package/src/service/materialization_service.ts +1107 -42
- package/src/service/materialization_test_fixtures.ts +7 -0
- package/src/service/model.spec.ts +207 -0
- package/src/service/model.ts +569 -59
- package/src/service/model_limits.spec.ts +28 -0
- package/src/service/model_limits.ts +21 -0
- package/src/service/model_storage_serve.spec.ts +193 -0
- package/src/service/model_storage_serve_joins.spec.ts +193 -0
- package/src/service/package.spec.ts +196 -0
- package/src/service/package.ts +385 -17
- package/src/service/persistence_policy.spec.ts +109 -0
- package/src/storage/duckdb/schema.ts +37 -0
- package/tests/fixtures/xlsx/database.xlsx +0 -0
- package/tests/integration/first_boot/readiness_line.integration.spec.ts +177 -0
- package/tests/integration/materialization/manifest_binding.integration.spec.ts +104 -0
- package/tests/integration/mcp/mcp_execute_query_tool.integration.spec.ts +37 -12
- package/tests/integration/mcp/mcp_get_context_semantic.integration.spec.ts +235 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Real-compiler contract for the materialization-eligibility gate. The gate
|
|
2
|
+
// reads specific compiled-IR shapes (a Parameter's `value: null` for unbound
|
|
3
|
+
// params; `refSummary.givenUsage` / `given` IR nodes for given references), so
|
|
4
|
+
// it is pinned against the real compiler rather than hand-built stubs — a
|
|
5
|
+
// Malloy change to either shape must fail here, not leak an ineligible source
|
|
6
|
+
// (a frozen tenant-filtered table) into the tier.
|
|
7
|
+
import { DuckDBConnection } from "@malloydata/db-duckdb";
|
|
8
|
+
import type { PersistSource } from "@malloydata/malloy";
|
|
9
|
+
import {
|
|
10
|
+
FixedConnectionMap,
|
|
11
|
+
InMemoryURLReader,
|
|
12
|
+
Runtime,
|
|
13
|
+
} from "@malloydata/malloy";
|
|
14
|
+
import { beforeAll, describe, expect, it } from "bun:test";
|
|
15
|
+
import { MaterializationEligibilityError } from "../errors";
|
|
16
|
+
import { assertMaterializationEligible } from "./materialization_eligibility";
|
|
17
|
+
|
|
18
|
+
const ROOT = "file:///elig/";
|
|
19
|
+
let connections: FixedConnectionMap;
|
|
20
|
+
|
|
21
|
+
beforeAll(() => {
|
|
22
|
+
const duckdb = new DuckDBConnection("duckdb", ":memory:");
|
|
23
|
+
connections = new FixedConnectionMap(
|
|
24
|
+
new Map([["duckdb", duckdb]]),
|
|
25
|
+
"duckdb",
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** Compile a single-file model and return its persist sources by name. */
|
|
30
|
+
async function persistSources(
|
|
31
|
+
model: string,
|
|
32
|
+
): Promise<Record<string, PersistSource>> {
|
|
33
|
+
const urlReader = new InMemoryURLReader(
|
|
34
|
+
new Map([[`${ROOT}m.malloy`, model]]),
|
|
35
|
+
);
|
|
36
|
+
const runtime = new Runtime({ urlReader, connections });
|
|
37
|
+
const compiled = await runtime
|
|
38
|
+
.loadModel(new URL(`${ROOT}m.malloy`), { importBaseURL: new URL(ROOT) })
|
|
39
|
+
.getModel();
|
|
40
|
+
const byName: Record<string, PersistSource> = {};
|
|
41
|
+
for (const source of Object.values(compiled.getBuildPlan().sources)) {
|
|
42
|
+
byName[source.name] = source;
|
|
43
|
+
}
|
|
44
|
+
return byName;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe("assertMaterializationEligible", () => {
|
|
48
|
+
it("accepts a plain persist source (no params, no givens)", async () => {
|
|
49
|
+
const sources = await persistSources(`##! experimental.persistence
|
|
50
|
+
source: base is duckdb.sql("SELECT 1 AS amount, 'US' AS region")
|
|
51
|
+
#@ persist name="mz_plain"
|
|
52
|
+
source: mz_plain is base -> { aggregate: c is count() }`);
|
|
53
|
+
expect(sources.mz_plain).toBeDefined();
|
|
54
|
+
expect(() =>
|
|
55
|
+
assertMaterializationEligible(sources.mz_plain),
|
|
56
|
+
).not.toThrow();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("accepts a parameter bound to a constant", async () => {
|
|
60
|
+
const sources = await persistSources(`##! experimental.persistence
|
|
61
|
+
##! experimental.parameters
|
|
62
|
+
source: base is duckdb.sql("SELECT 1 AS amount, 'US' AS region")
|
|
63
|
+
#@ persist name="mz_bound"
|
|
64
|
+
source: mz_bound(threshold::number is 5) is base -> { aggregate: c is count() }`);
|
|
65
|
+
expect(sources.mz_bound).toBeDefined();
|
|
66
|
+
expect(() =>
|
|
67
|
+
assertMaterializationEligible(sources.mz_bound),
|
|
68
|
+
).not.toThrow();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("refuses a source with an unbound (free) parameter", async () => {
|
|
72
|
+
const sources = await persistSources(`##! experimental.persistence
|
|
73
|
+
##! experimental.parameters
|
|
74
|
+
source: base is duckdb.sql("SELECT 1 AS amount, 'US' AS region")
|
|
75
|
+
#@ persist name="mz_free"
|
|
76
|
+
source: mz_free(threshold::number) is base -> { aggregate: c is count() }`);
|
|
77
|
+
expect(sources.mz_free).toBeDefined();
|
|
78
|
+
expect(() => assertMaterializationEligible(sources.mz_free)).toThrow(
|
|
79
|
+
MaterializationEligibilityError,
|
|
80
|
+
);
|
|
81
|
+
expect(() => assertMaterializationEligible(sources.mz_free)).toThrow(
|
|
82
|
+
/unbound parameter/i,
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("refuses a source that references a given (RLAC security refusal)", async () => {
|
|
87
|
+
const sources = await persistSources(`##! experimental.persistence
|
|
88
|
+
##! experimental.givens
|
|
89
|
+
given: tenant :: string is 'acme'
|
|
90
|
+
source: base is duckdb.sql("SELECT 1 AS amount, 'acme' AS tenant")
|
|
91
|
+
#@ persist name="mz_given"
|
|
92
|
+
source: mz_given is base -> { where: tenant = $tenant; aggregate: c is count() }`);
|
|
93
|
+
expect(sources.mz_given).toBeDefined();
|
|
94
|
+
expect(() => assertMaterializationEligible(sources.mz_given)).toThrow(
|
|
95
|
+
MaterializationEligibilityError,
|
|
96
|
+
);
|
|
97
|
+
expect(() => assertMaterializationEligible(sources.mz_given)).toThrow(
|
|
98
|
+
/given/i,
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("refuses a source protected by its own #(authorize) gate", async () => {
|
|
103
|
+
const sources = await persistSources(`##! experimental.persistence
|
|
104
|
+
##! experimental.givens
|
|
105
|
+
given: role :: string is 'analyst'
|
|
106
|
+
source: base is duckdb.sql("SELECT 1 AS amount, 'US' AS region")
|
|
107
|
+
#(authorize) "$role = 'analyst'"
|
|
108
|
+
#@ persist name="mz_authz"
|
|
109
|
+
source: mz_authz is base -> { aggregate: c is count() }`);
|
|
110
|
+
expect(sources.mz_authz).toBeDefined();
|
|
111
|
+
expect(() => assertMaterializationEligible(sources.mz_authz)).toThrow(
|
|
112
|
+
MaterializationEligibilityError,
|
|
113
|
+
);
|
|
114
|
+
expect(() => assertMaterializationEligible(sources.mz_authz)).toThrow(
|
|
115
|
+
/authorize/i,
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("refuses a source that reaches an #(authorize) gate through a JOIN", async () => {
|
|
120
|
+
// The gate is on the joined source, not on mz_authz_joined itself — a join
|
|
121
|
+
// must not launder an authorize-gated source into a frozen table.
|
|
122
|
+
const sources = await persistSources(`##! experimental.persistence
|
|
123
|
+
##! experimental.givens
|
|
124
|
+
given: role :: string is 'analyst'
|
|
125
|
+
#(authorize) "$role = 'analyst'"
|
|
126
|
+
source: gated is duckdb.sql("SELECT 1 AS amount, 'acme' AS tenant")
|
|
127
|
+
source: joiner is duckdb.sql("SELECT 2 AS n, 'acme' AS tenant")
|
|
128
|
+
#@ persist name="mz_authz_joined"
|
|
129
|
+
source: mz_authz_joined is joiner extend {
|
|
130
|
+
join_one: g is gated on tenant = g.tenant
|
|
131
|
+
} -> { aggregate: c is count() }`);
|
|
132
|
+
expect(sources.mz_authz_joined).toBeDefined();
|
|
133
|
+
expect(() =>
|
|
134
|
+
assertMaterializationEligible(sources.mz_authz_joined),
|
|
135
|
+
).toThrow(MaterializationEligibilityError);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("refuses a source that reaches a given through a JOIN (not just its own pipeline)", async () => {
|
|
139
|
+
// The given lives on a joined source, not on mz_joined's own where/fields.
|
|
140
|
+
// The compiled struct embeds the joined SourceDef, so the fail-closed walk
|
|
141
|
+
// must still reach it — a join must not launder a given-filtered source.
|
|
142
|
+
const sources = await persistSources(`##! experimental.persistence
|
|
143
|
+
##! experimental.givens
|
|
144
|
+
given: tenant :: string is 'acme'
|
|
145
|
+
source: gated is duckdb.sql("SELECT 1 AS amount, 'acme' AS tenant") extend {
|
|
146
|
+
where: tenant = $tenant
|
|
147
|
+
}
|
|
148
|
+
source: joiner is duckdb.sql("SELECT 2 AS n, 'acme' AS tenant")
|
|
149
|
+
#@ persist name="mz_joined"
|
|
150
|
+
source: mz_joined is joiner extend {
|
|
151
|
+
join_one: g is gated on tenant = g.tenant
|
|
152
|
+
} -> { aggregate: c is count() }`);
|
|
153
|
+
expect(sources.mz_joined).toBeDefined();
|
|
154
|
+
expect(() => assertMaterializationEligible(sources.mz_joined)).toThrow(
|
|
155
|
+
MaterializationEligibilityError,
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import type { PersistSource } from "@malloydata/malloy";
|
|
2
|
+
import { MaterializationEligibilityError } from "../errors";
|
|
3
|
+
import { recordEligibilityRefused } from "../materialization_metrics";
|
|
4
|
+
import { parseAuthorizeAnnotation } from "./authorize";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Compile-time eligibility gate for materializing a persist source into a
|
|
8
|
+
* *storage* destination (a DuckDB/DuckLake tier table), as opposed to the
|
|
9
|
+
* default colocated path (no `storage=`). This is a HARD REFUSE: an
|
|
10
|
+
* ineligible source never builds an artifact.
|
|
11
|
+
*
|
|
12
|
+
* It exists because a `storage=<duckdb>` table is built once and served frozen
|
|
13
|
+
* to every subsequent query. Two properties that hold for a live query — the
|
|
14
|
+
* relation is recomputed per query, and per-tenant access filters bind at query
|
|
15
|
+
* time — are lost the moment the relation is frozen. So a source is only
|
|
16
|
+
* materializable when its relation is fully determined at build time:
|
|
17
|
+
*
|
|
18
|
+
* 1. **No unbound (free) parameters.** A source declaring an unbound parameter
|
|
19
|
+
* is a *template* instantiated per query — there is no single relation to
|
|
20
|
+
* freeze. Parameters bound to a constant are fine (the relation is fixed,
|
|
21
|
+
* and the bound value already distinguishes the content address).
|
|
22
|
+
* 2. **No given references — a security refusal.** Givens bind at the
|
|
23
|
+
* runtime/query layer and are the documented mechanism for row-level access
|
|
24
|
+
* control (RLAC). A source filtered by a given (`where: tenant_id = $TENANT`)
|
|
25
|
+
* materialized once and served frozen would leak one tenant's rows to every
|
|
26
|
+
* tenant. This check fails closed: if the source references any given, it is
|
|
27
|
+
* refused, no exceptions.
|
|
28
|
+
* 3. **No `#(authorize)` gate — a security refusal.** An authorize expression
|
|
29
|
+
* is a per-request *who-can-query* gate evaluated at query time. The served
|
|
30
|
+
* virtual shape of a materialized source carries no gate to evaluate, so a
|
|
31
|
+
* materialized authorize-gated source would be served to everyone,
|
|
32
|
+
* bypassing the gate. Fails closed and reaches a gate on a JOINED source too
|
|
33
|
+
* (a join must not launder an authorize-gated source), mirroring the
|
|
34
|
+
* transitive `#(authorize)` enforcement on the live serve path (#906).
|
|
35
|
+
*
|
|
36
|
+
* One further eligibility property from the design — the served source must
|
|
37
|
+
* compile in DuckDB (portability) — is enforced at *build* time against the
|
|
38
|
+
* captured table schema (see the DuckDB-compile gate in the build path), not
|
|
39
|
+
* here: it needs the post-build authoritative schema this compile-time pass
|
|
40
|
+
* does not have. This pass covers the properties determinable from the compiled
|
|
41
|
+
* source alone (free parameters, given references, authorize gates), so it can
|
|
42
|
+
* fail fast before any warehouse work.
|
|
43
|
+
*
|
|
44
|
+
* The gate reads the *compiled* source (only the compiled `ModelDef` sees
|
|
45
|
+
* effective annotations, parameters, and given usage through `extend`/imports —
|
|
46
|
+
* a raw-source scan would miss inherited declarations), so it must run after
|
|
47
|
+
* compilation and before the build.
|
|
48
|
+
*
|
|
49
|
+
* @throws {MaterializationEligibilityError} (HTTP 422) naming the source and the
|
|
50
|
+
* specific reason, so the author can either fix the source or drop `storage=`.
|
|
51
|
+
*/
|
|
52
|
+
export function assertMaterializationEligible(
|
|
53
|
+
persistSource: PersistSource,
|
|
54
|
+
): void {
|
|
55
|
+
const sourceName = persistSource.name;
|
|
56
|
+
|
|
57
|
+
// Fail closed, like referencesGiven / referencesAuthorize below: an unreadable
|
|
58
|
+
// parameter surface is a refusal, not an assumed absence of free parameters.
|
|
59
|
+
let unbound: string[];
|
|
60
|
+
try {
|
|
61
|
+
unbound = unboundParameterNames(persistSource);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
recordEligibilityRefused("free_parameter");
|
|
64
|
+
throw new MaterializationEligibilityError({
|
|
65
|
+
message:
|
|
66
|
+
`Source '${sourceName}' cannot be materialized into a storage ` +
|
|
67
|
+
`destination: its parameter surface could not be determined ` +
|
|
68
|
+
`(${err instanceof Error ? err.message : String(err)}), so the ` +
|
|
69
|
+
`publisher cannot prove the source has no free parameters. This is ` +
|
|
70
|
+
`refused for safety. Drop 'storage=' to serve it live from the ` +
|
|
71
|
+
`source warehouse.`,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (unbound.length > 0) {
|
|
75
|
+
recordEligibilityRefused("free_parameter");
|
|
76
|
+
throw new MaterializationEligibilityError({
|
|
77
|
+
message:
|
|
78
|
+
`Source '${sourceName}' cannot be materialized into a storage ` +
|
|
79
|
+
`destination: it has unbound parameter(s) ` +
|
|
80
|
+
`${unbound.map((p) => `'${p}'`).join(", ")}. A source with a free ` +
|
|
81
|
+
`parameter is a template instantiated per query, so there is no ` +
|
|
82
|
+
`single relation to materialize. Bind the parameter to a constant, ` +
|
|
83
|
+
`or drop 'storage=' to serve it live from the source warehouse.`,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (referencesGiven(persistSource)) {
|
|
88
|
+
recordEligibilityRefused("given");
|
|
89
|
+
throw new MaterializationEligibilityError({
|
|
90
|
+
message:
|
|
91
|
+
`Source '${sourceName}' cannot be materialized into a storage ` +
|
|
92
|
+
`destination: it references a given. Givens bind per query and are ` +
|
|
93
|
+
`used for row-level access control, so a materialized-once table ` +
|
|
94
|
+
`served to everyone would leak filtered rows across tenants. This ` +
|
|
95
|
+
`is refused for safety. Serve this source live (drop 'storage=').`,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (referencesAuthorize(persistSource)) {
|
|
100
|
+
recordEligibilityRefused("authorize");
|
|
101
|
+
throw new MaterializationEligibilityError({
|
|
102
|
+
message:
|
|
103
|
+
`Source '${sourceName}' cannot be materialized into a storage ` +
|
|
104
|
+
`destination: it is protected by an #(authorize) gate (its own or a ` +
|
|
105
|
+
`joined source's). An authorize expression is evaluated per request; ` +
|
|
106
|
+
`a materialized-once table served frozen carries no gate, so it would ` +
|
|
107
|
+
`be served to everyone, bypassing authorization. This is refused for ` +
|
|
108
|
+
`safety. Serve this source live (drop 'storage=').`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Names of the source's parameters that are declared but not bound to a value.
|
|
115
|
+
* A Malloy `Parameter` carries `value: ConstantExpr | null`; `null` is an
|
|
116
|
+
* unbound (free) parameter — bound-to-constant parameters have a non-null value.
|
|
117
|
+
*
|
|
118
|
+
* Fail-closed like the given/authorize walks (the caller turns a throw into a
|
|
119
|
+
* refusal): a parameter counts as unbound unless demonstrably bound, so a
|
|
120
|
+
* compiler shift to `value: undefined` can't let a template through. A MISSING
|
|
121
|
+
* `parameters` field is not an error — most sources declare none — so a renamed
|
|
122
|
+
* or relocated field is the one drift this can't catch; the real-compiler
|
|
123
|
+
* eligibility spec is what pins that shape.
|
|
124
|
+
*/
|
|
125
|
+
function unboundParameterNames(persistSource: PersistSource): string[] {
|
|
126
|
+
const def = persistSource._sourceDef as unknown;
|
|
127
|
+
if (def === null || typeof def !== "object") {
|
|
128
|
+
throw new Error("compiled source definition is not readable");
|
|
129
|
+
}
|
|
130
|
+
const parameters = (def as { parameters?: unknown }).parameters;
|
|
131
|
+
if (parameters === undefined || parameters === null) return [];
|
|
132
|
+
if (typeof parameters !== "object" || Array.isArray(parameters)) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`compiled source 'parameters' has an unexpected shape (${
|
|
135
|
+
Array.isArray(parameters) ? "array" : typeof parameters
|
|
136
|
+
})`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
const unbound: string[] = [];
|
|
140
|
+
for (const [name, param] of Object.entries(
|
|
141
|
+
parameters as Record<string, unknown>,
|
|
142
|
+
)) {
|
|
143
|
+
const bound =
|
|
144
|
+
param !== null &&
|
|
145
|
+
typeof param === "object" &&
|
|
146
|
+
(param as { value?: unknown }).value !== null &&
|
|
147
|
+
(param as { value?: unknown }).value !== undefined;
|
|
148
|
+
if (!bound) unbound.push(name);
|
|
149
|
+
}
|
|
150
|
+
return unbound;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Whether the compiled source (transitively) references any given. Fail-closed:
|
|
155
|
+
* walks the compiled source definition for both of Malloy's given signals —
|
|
156
|
+
* a non-empty `refSummary.givenUsage` (populated on filters and expressions by
|
|
157
|
+
* the reference-tracking walker) and any `given` / `givenReference` IR node —
|
|
158
|
+
* anywhere in the source's filter list, field expressions, or nested pipeline.
|
|
159
|
+
*
|
|
160
|
+
* A generic bounded walk (rather than reading a single well-known field) is
|
|
161
|
+
* deliberate: a given can hide in a field expression or a nested view, not just
|
|
162
|
+
* a top-level `where:`, and missing one is a tenant-isolation breach. The walk
|
|
163
|
+
* is depth- and visited-bounded so a cyclic IR graph cannot hang it, and any
|
|
164
|
+
* introspection failure is treated as "references a given" (fail closed).
|
|
165
|
+
*/
|
|
166
|
+
function referencesGiven(persistSource: PersistSource): boolean {
|
|
167
|
+
try {
|
|
168
|
+
return walkForGiven(persistSource._sourceDef, new WeakSet(), 0);
|
|
169
|
+
} catch {
|
|
170
|
+
// Fail closed: if we cannot prove the source is given-free, refuse it.
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Max IR depth to walk; deep enough for real sources, a hang backstop. */
|
|
176
|
+
const MAX_GIVEN_WALK_DEPTH = 200;
|
|
177
|
+
|
|
178
|
+
function walkForGiven(
|
|
179
|
+
node: unknown,
|
|
180
|
+
seen: WeakSet<object>,
|
|
181
|
+
depth: number,
|
|
182
|
+
): boolean {
|
|
183
|
+
if (depth > MAX_GIVEN_WALK_DEPTH) {
|
|
184
|
+
// Refuse rather than risk an unbounded structure hiding a given.
|
|
185
|
+
throw new Error("given-usage walk exceeded max depth");
|
|
186
|
+
}
|
|
187
|
+
if (node === null || typeof node !== "object") return false;
|
|
188
|
+
if (seen.has(node as object)) return false;
|
|
189
|
+
seen.add(node as object);
|
|
190
|
+
|
|
191
|
+
if (Array.isArray(node)) {
|
|
192
|
+
for (const item of node) {
|
|
193
|
+
if (walkForGiven(item, seen, depth + 1)) return true;
|
|
194
|
+
}
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const record = node as Record<string, unknown>;
|
|
199
|
+
|
|
200
|
+
// A given declaration or reference IR node.
|
|
201
|
+
const nodeKind = record.node;
|
|
202
|
+
if (nodeKind === "given" || nodeKind === "givenReference") return true;
|
|
203
|
+
if (record.givenRef !== undefined) return true;
|
|
204
|
+
|
|
205
|
+
// Reference-tracking summary: a non-empty givenUsage means this fragment
|
|
206
|
+
// reads a given (the precise, per-fragment signal). It appears either nested
|
|
207
|
+
// under refSummary (per-fragment) OR as a bare `givenUsage` on a query/struct
|
|
208
|
+
// node — treat a non-empty array in EITHER shape as a hit, so the walk stays
|
|
209
|
+
// correct if a future compiler keeps only the summary and prunes the embedded
|
|
210
|
+
// `node:'given'` leaves.
|
|
211
|
+
const refSummary = record.refSummary as
|
|
212
|
+
| { givenUsage?: unknown[] }
|
|
213
|
+
| undefined;
|
|
214
|
+
if (refSummary?.givenUsage && refSummary.givenUsage.length > 0) return true;
|
|
215
|
+
if (Array.isArray(record.givenUsage) && record.givenUsage.length > 0) {
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
for (const value of Object.values(record)) {
|
|
220
|
+
if (walkForGiven(value, seen, depth + 1)) return true;
|
|
221
|
+
}
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Whether the compiled source (transitively) carries an `#(authorize)` gate —
|
|
227
|
+
* on the source itself or on any source reachable through a join. Fail-closed:
|
|
228
|
+
* walks the compiled source definition for annotation notes (`blockNotes` /
|
|
229
|
+
* `notes`) whose text is an authorize annotation. A join embeds the joined
|
|
230
|
+
* SourceDef (with its own blockNotes), so a gate reached only through a join is
|
|
231
|
+
* still found — a join must not launder an authorize-gated source, matching the
|
|
232
|
+
* transitive enforcement on the live serve path (#906). Any introspection or
|
|
233
|
+
* parse failure is treated as "carries a gate" (fail closed).
|
|
234
|
+
*
|
|
235
|
+
* Deliberately separate from `Model.collectAllReachableGates`, which answers a
|
|
236
|
+
* different question: that walk collects gates to EVALUATE per request, and needs
|
|
237
|
+
* their expressions and the givens they reference. This one only needs to know
|
|
238
|
+
* whether any gate exists at all, at build time, with no request to evaluate
|
|
239
|
+
* against — so it is a generic fail-closed scan rather than a typed collection.
|
|
240
|
+
* They will not inherit each other's fixes; that is the trade accepted for
|
|
241
|
+
* keeping the build-time refusal independent of the serve-time evaluator.
|
|
242
|
+
*/
|
|
243
|
+
function referencesAuthorize(persistSource: PersistSource): boolean {
|
|
244
|
+
try {
|
|
245
|
+
return walkForAuthorize(persistSource._sourceDef, new WeakSet(), 0);
|
|
246
|
+
} catch {
|
|
247
|
+
// Fail closed: if we cannot prove the source is authorize-free, refuse it.
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** True if an annotation string is an `#(authorize)`/`##(authorize)` gate. */
|
|
253
|
+
function isAuthorizeAnnotation(text: string): boolean {
|
|
254
|
+
try {
|
|
255
|
+
return parseAuthorizeAnnotation(text) !== null;
|
|
256
|
+
} catch {
|
|
257
|
+
// A malformed authorize annotation still means the author intended a gate.
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function walkForAuthorize(
|
|
263
|
+
node: unknown,
|
|
264
|
+
seen: WeakSet<object>,
|
|
265
|
+
depth: number,
|
|
266
|
+
): boolean {
|
|
267
|
+
if (depth > MAX_GIVEN_WALK_DEPTH) {
|
|
268
|
+
throw new Error("authorize-usage walk exceeded max depth");
|
|
269
|
+
}
|
|
270
|
+
if (node === null || typeof node !== "object") return false;
|
|
271
|
+
if (seen.has(node as object)) return false;
|
|
272
|
+
seen.add(node as object);
|
|
273
|
+
|
|
274
|
+
if (Array.isArray(node)) {
|
|
275
|
+
for (const item of node) {
|
|
276
|
+
if (walkForAuthorize(item, seen, depth + 1)) return true;
|
|
277
|
+
}
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const record = node as Record<string, unknown>;
|
|
282
|
+
|
|
283
|
+
// Annotation notes live under `blockNotes` (source/statement) or `notes`
|
|
284
|
+
// (model/file), as either bare strings or `{ text }` objects.
|
|
285
|
+
for (const key of ["blockNotes", "notes"]) {
|
|
286
|
+
const arr = record[key];
|
|
287
|
+
if (!Array.isArray(arr)) continue;
|
|
288
|
+
for (const n of arr) {
|
|
289
|
+
const text =
|
|
290
|
+
typeof n === "string"
|
|
291
|
+
? n
|
|
292
|
+
: n &&
|
|
293
|
+
typeof n === "object" &&
|
|
294
|
+
typeof (n as { text?: unknown }).text === "string"
|
|
295
|
+
? (n as { text: string }).text
|
|
296
|
+
: undefined;
|
|
297
|
+
if (text !== undefined && isAuthorizeAnnotation(text)) return true;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (const value of Object.values(record)) {
|
|
302
|
+
if (walkForAuthorize(value, seen, depth + 1)) return true;
|
|
303
|
+
}
|
|
304
|
+
return false;
|
|
305
|
+
}
|