@malloy-publisher/server 0.0.219 → 0.0.221

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 (69) hide show
  1. package/dist/app/api-doc.yaml +291 -20
  2. package/dist/app/assets/{EnvironmentPage-gehnjfC6.js → EnvironmentPage-D6G5n6mY.js} +1 -1
  3. package/dist/app/assets/{HomePage-8LQBytE4.js → HomePage-DgZluD8u.js} +1 -1
  4. package/dist/app/assets/LightMode-w9v0pEbg.js +1 -0
  5. package/dist/app/assets/MainPage-Bi_Tukvr.js +2 -0
  6. package/dist/app/assets/{MaterializationsPage-D7P1Kp6O.js → MaterializationsPage-BVERjnmG.js} +1 -1
  7. package/dist/app/assets/ModelPage-2OjS259-.js +1 -0
  8. package/dist/app/assets/{PackagePage-Cz9fVwSG.js → PackagePage-C8L2On4z.js} +1 -1
  9. package/dist/app/assets/{RouteError-UONCloyN.js → RouteError-DJoNC_Vl.js} +1 -1
  10. package/dist/app/assets/ThemeEditorPage-B-hJ1zgd.js +1 -0
  11. package/dist/app/assets/{WorkbookPage-Bhzqvbq_.js → WorkbookPage-CTbDgGeJ.js} +1 -1
  12. package/dist/app/assets/{core-BiGj7BML.es-kMHAa8tP.js → core-D9Hl0IDY.es-CmgzHn4c.js} +1 -1
  13. package/dist/app/assets/github-dark-DenFmJkN.es-DHJKELXO.js +1 -0
  14. package/dist/app/assets/index-8E2uLeV9.js +2541 -0
  15. package/dist/app/assets/index-BEjJCJjX.js +1761 -0
  16. package/dist/app/assets/index-BF8PkFm8.js +1 -0
  17. package/dist/app/assets/index-Cr-asgoV.js +527 -0
  18. package/dist/app/assets/index-DcCw_qxr.js +18 -0
  19. package/dist/app/assets/index-w_0OQJgZ.js +23 -0
  20. package/dist/app/index.html +1 -1
  21. package/dist/package_load_worker.mjs +20 -2
  22. package/dist/runtime/publisher.js +6 -1
  23. package/dist/server.mjs +412 -55
  24. package/dist/sshcrypto-8m50vnmb.node +0 -0
  25. package/package.json +12 -12
  26. package/src/config.theme.spec.ts +178 -0
  27. package/src/config.ts +179 -0
  28. package/src/controller/materialization.controller.spec.ts +4 -4
  29. package/src/controller/materialization.controller.ts +2 -2
  30. package/src/controller/package.controller.spec.ts +89 -1
  31. package/src/controller/package.controller.ts +28 -3
  32. package/src/controller/theme.controller.ts +83 -0
  33. package/src/dto/connection.dto.ts +6 -0
  34. package/src/materialization_metrics.ts +1 -1
  35. package/src/package_load/package_load_pool.ts +2 -1
  36. package/src/package_load/package_load_worker.ts +5 -2
  37. package/src/package_load/protocol.ts +2 -1
  38. package/src/runtime/publisher.js +6 -1
  39. package/src/server.ts +58 -1
  40. package/src/service/build_plan.spec.ts +61 -17
  41. package/src/service/build_plan.ts +35 -11
  42. package/src/service/connection.ts +52 -6
  43. package/src/service/connection_fingerprint.spec.ts +102 -0
  44. package/src/service/manifest_loader.spec.ts +5 -5
  45. package/src/service/manifest_loader.ts +4 -4
  46. package/src/service/materialization_cron_gate.spec.ts +137 -0
  47. package/src/service/materialization_schedule_surface.spec.ts +30 -1
  48. package/src/service/materialization_service.spec.ts +47 -37
  49. package/src/service/materialization_service.ts +59 -50
  50. package/src/service/materialization_test_fixtures.ts +22 -6
  51. package/src/service/model.ts +1 -1
  52. package/src/service/package.ts +50 -4
  53. package/src/service/package_manifest.spec.ts +42 -4
  54. package/src/service/package_manifest.ts +53 -4
  55. package/src/service/theme_store.ts +199 -0
  56. package/src/storage/DatabaseInterface.ts +1 -1
  57. package/src/storage/StorageManager.ts +17 -0
  58. package/src/storage/duckdb/schema.ts +27 -6
  59. package/src/theme_key_parity.spec.ts +57 -0
  60. package/tests/integration/materialization/manifest_binding.integration.spec.ts +1 -1
  61. package/tests/integration/materialization/materialization_lifecycle.integration.spec.ts +5 -5
  62. package/tests/unit/controller/theme.controller.spec.ts +141 -0
  63. package/tests/unit/service/theme_store.spec.ts +139 -0
  64. package/dist/app/assets/MainPage-DDaZLJw-.js +0 -2
  65. package/dist/app/assets/ModelPage-Do_vhxOc.js +0 -1
  66. package/dist/app/assets/index-VzRbxcF7.js +0 -455
  67. package/dist/app/assets/index-ddq4-5hu.js +0 -1760
  68. package/dist/app/assets/index-qOQF9CXq.js +0 -40
  69. package/dist/app/assets/index.umd-B2kmxDh7.js +0 -2467
@@ -34,7 +34,7 @@ import { errMessage } from "../utils";
34
34
  import {
35
35
  CompiledBuildPlan,
36
36
  compilePackageBuildPlan,
37
- computeBuildId,
37
+ computeSourceEntityId,
38
38
  deriveAnnotationFields,
39
39
  iterGraphSources,
40
40
  } from "./build_plan";
@@ -43,16 +43,17 @@ import { bareTableName, quoteIdentifier, quoteTablePath } from "./quoting";
43
43
  import { resolveEnvironmentId } from "./resolve_environment";
44
44
 
45
45
  /**
46
- * Length of the buildId prefix used when synthesizing staging table names.
47
- * buildId is a 64-char SHA-256 hex string; 12 hex chars is 48 bits of
48
- * entropy, well inside every dialect's identifier limit (Postgres is the
49
- * tightest at 63).
46
+ * Length of the sourceEntityId prefix used when synthesizing staging table
47
+ * names. 12 hex chars is 48 bits of entropy, well inside every dialect's
48
+ * identifier limit (Postgres is the tightest at 63).
50
49
  */
51
- const STAGING_BUILD_ID_LEN = 12;
50
+ const STAGING_ID_LEN = 12;
52
51
 
53
52
  /** Staging suffix appended to a table name while it is being built. */
54
- export function stagingSuffix(buildId: string): string {
55
- return `_${buildId.substring(0, STAGING_BUILD_ID_LEN)}`;
53
+ export function stagingSuffix(sourceEntityId: string): string {
54
+ // Drop hyphens so the suffix stays a bare identifier fragment when the id
55
+ // becomes a UUID5 (a no-op for the current hex ids).
56
+ return `_${sourceEntityId.replace(/-/g, "").substring(0, STAGING_ID_LEN)}`;
56
57
  }
57
58
 
58
59
  /**
@@ -334,7 +335,7 @@ export class MaterializationService {
334
335
  carried = {};
335
336
  } else {
336
337
  // Skip-if-unchanged: reuse tables from the most recent successful
337
- // manifest for sources whose buildId is unchanged, unless
338
+ // manifest for sources whose sourceEntityId is unchanged, unless
338
339
  // forceRefresh.
339
340
  const priorEntries = opts.forceRefresh
340
341
  ? {}
@@ -396,7 +397,7 @@ export class MaterializationService {
396
397
  /**
397
398
  * Derive the publisher's own build instructions for auto-run. Each persist
398
399
  * source (respecting the optional sourceNames filter) gets a self-assigned
399
- * physical table name and COPY realization, unless its buildId is unchanged
400
+ * physical table name and COPY realization, unless its sourceEntityId is unchanged
400
401
  * since `priorEntries` — those are carried forward (reused) instead of
401
402
  * rebuilt.
402
403
  */
@@ -420,24 +421,24 @@ export class MaterializationService {
420
421
  )) {
421
422
  if (include && !include.has(persistSource.name)) continue;
422
423
 
423
- const buildId = computeBuildId(
424
+ const sourceEntityId = computeSourceEntityId(
424
425
  persistSource,
425
426
  compiled.connectionDigests,
426
427
  );
427
- if (seen.has(buildId)) continue;
428
- seen.add(buildId);
428
+ if (seen.has(sourceEntityId)) continue;
429
+ seen.add(sourceEntityId);
429
430
 
430
- const prior = priorEntries[buildId];
431
+ const prior = priorEntries[sourceEntityId];
431
432
  if (prior && prior.physicalTableName) {
432
- carried[buildId] = prior;
433
+ carried[sourceEntityId] = prior;
433
434
  continue;
434
435
  }
435
436
 
436
437
  instructions.push({
437
- buildId,
438
- materializedTableId: `local-${buildId.substring(
438
+ sourceEntityId,
439
+ materializedTableId: `local-${sourceEntityId.substring(
439
440
  0,
440
- STAGING_BUILD_ID_LEN,
441
+ STAGING_ID_LEN,
441
442
  )}`,
442
443
  physicalTableName: selfAssignTableName(persistSource),
443
444
  realization: "COPY",
@@ -486,9 +487,11 @@ export class MaterializationService {
486
487
  entries: Record<string, ManifestEntry>,
487
488
  ): Promise<void> {
488
489
  const manifestEntries: BuildManifest["entries"] = {};
489
- for (const [buildId, entry] of Object.entries(entries)) {
490
+ for (const [sourceEntityId, entry] of Object.entries(entries)) {
490
491
  if (entry.physicalTableName) {
491
- manifestEntries[buildId] = { tableName: entry.physicalTableName };
492
+ manifestEntries[sourceEntityId] = {
493
+ tableName: entry.physicalTableName,
494
+ };
492
495
  }
493
496
  }
494
497
  try {
@@ -512,7 +515,7 @@ export class MaterializationService {
512
515
 
513
516
  /**
514
517
  * Validate caller-supplied build instructions against the package's compiled
515
- * build plan: every instructed buildId must be a planned source, and only
518
+ * build plan: every instructed sourceEntityId must be a planned source, and only
516
519
  * COPY realization is supported. Throws when the package declares no persist
517
520
  * source (no plan to build against).
518
521
  */
@@ -525,15 +528,15 @@ export class MaterializationService {
525
528
  "Package has no persist sources; buildInstructions cannot be applied",
526
529
  );
527
530
  }
528
- const plannedBuildIds = new Set<string>();
531
+ const plannedSourceEntityIds = new Set<string>();
529
532
  for (const source of Object.values(plan.sources)) {
530
- plannedBuildIds.add(source.buildId);
533
+ plannedSourceEntityIds.add(source.sourceEntityId);
531
534
  }
532
535
 
533
536
  for (const instruction of instructions) {
534
- if (!plannedBuildIds.has(instruction.buildId)) {
537
+ if (!plannedSourceEntityIds.has(instruction.sourceEntityId)) {
535
538
  throw new BadRequestError(
536
- `Instruction references unknown buildId '${instruction.buildId}'`,
539
+ `Instruction references unknown sourceEntityId '${instruction.sourceEntityId}'`,
537
540
  );
538
541
  }
539
542
  // COPY-only for now; SNAPSHOT lands once clone semantics are defined.
@@ -561,20 +564,20 @@ export class MaterializationService {
561
564
  const { graphs, sources, connectionDigests, connections } = compiled;
562
565
 
563
566
  // Index instructions by sourceID (the stable per-source handle) so the
564
- // build no longer recomputes the buildId to find an instruction.
565
- // Recomputing it here forced a caller's buildId to equal the publisher's
566
- // content hash, so a caller that derives buildIds by any other scheme
567
- // would have its sources silently skipped (the recomputed buildId would
568
- // not match the instruction). buildId is treated as opaque, caller-assigned
569
- // identity. A buildId index is kept as a fallback for instructions without
567
+ // build no longer recomputes the sourceEntityId to find an instruction.
568
+ // Recomputing it here forced a caller's sourceEntityId to equal the publisher's
569
+ // content hash, so a caller that derives sourceEntityIds by any other scheme
570
+ // would have its sources silently skipped (the recomputed sourceEntityId would
571
+ // not match the instruction). sourceEntityId is treated as opaque, caller-assigned
572
+ // identity. A sourceEntityId index is kept as a fallback for instructions without
570
573
  // a sourceID (e.g. standalone auto-run).
571
574
  const bySourceID = new Map<string, BuildInstruction>();
572
- const byBuildId = new Map<string, BuildInstruction>();
575
+ const bySourceEntityId = new Map<string, BuildInstruction>();
573
576
  for (const instruction of instructions) {
574
577
  if (instruction.sourceID) {
575
578
  bySourceID.set(instruction.sourceID, instruction);
576
579
  }
577
- byBuildId.set(instruction.buildId, instruction);
580
+ bySourceEntityId.set(instruction.sourceEntityId, instruction);
578
581
  }
579
582
 
580
583
  // Accumulates physical names as sources are built so downstream sources
@@ -582,11 +585,13 @@ export class MaterializationService {
582
585
  // it with carried-forward entries so reused upstreams resolve too.
583
586
  const manifest = new Manifest();
584
587
  const entries: Record<string, ManifestEntry> = {};
585
- for (const [buildId, entry] of Object.entries(seedEntries)) {
588
+ for (const [sourceEntityId, entry] of Object.entries(seedEntries)) {
586
589
  if (entry.physicalTableName) {
587
- manifest.update(buildId, { tableName: entry.physicalTableName });
590
+ manifest.update(sourceEntityId, {
591
+ tableName: entry.physicalTableName,
592
+ });
588
593
  }
589
- entries[buildId] = entry;
594
+ entries[sourceEntityId] = entry;
590
595
  }
591
596
 
592
597
  for (const graph of graphs) {
@@ -599,15 +604,19 @@ export class MaterializationService {
599
604
  for (const persistSource of iterGraphSources(graph, sources)) {
600
605
  if (signal.aborted) throw new Error("Build cancelled");
601
606
 
602
- // The manifest is keyed by the content buildId — what Malloy
607
+ // The manifest is keyed by the content sourceEntityId — what Malloy
603
608
  // recomputes to resolve upstream persist references during SQL
604
- // generation — independent of the instruction's identity buildId.
605
- const buildId = computeBuildId(persistSource, connectionDigests);
606
- // Prefer sourceID matching (so the caller's buildId scheme stays
607
- // opaque to the build); fall back to buildId for instructions
609
+ // generation — independent of the instruction's identity sourceEntityId.
610
+ const sourceEntityId = computeSourceEntityId(
611
+ persistSource,
612
+ connectionDigests,
613
+ );
614
+ // Prefer sourceID matching (so the caller's sourceEntityId scheme stays
615
+ // opaque to the build); fall back to sourceEntityId for instructions
608
616
  // without a sourceID (auto-run).
609
617
  const instruction =
610
- bySourceID.get(persistSource.sourceID) ?? byBuildId.get(buildId);
618
+ bySourceID.get(persistSource.sourceID) ??
619
+ bySourceEntityId.get(sourceEntityId);
611
620
  if (!instruction) continue;
612
621
 
613
622
  const entry = await this.buildOneSource(
@@ -617,7 +626,7 @@ export class MaterializationService {
617
626
  connectionDigests,
618
627
  manifest,
619
628
  );
620
- entries[buildId] = entry;
629
+ entries[sourceEntityId] = entry;
621
630
  }
622
631
  }
623
632
 
@@ -627,7 +636,7 @@ export class MaterializationService {
627
636
  /**
628
637
  * Build a single instructed source into its assigned physical table.
629
638
  * COPY uses a staging table + atomic rename for crash-safety; the staging
630
- * name derives from the buildId. Records and returns the manifest entry.
639
+ * name derives from the sourceEntityId. Records and returns the manifest entry.
631
640
  */
632
641
  private async buildOneSource(
633
642
  persistSource: PersistSource,
@@ -636,7 +645,7 @@ export class MaterializationService {
636
645
  connectionDigests: Record<string, string>,
637
646
  manifest: Manifest,
638
647
  ): Promise<ManifestEntry> {
639
- const buildId = instruction.buildId;
648
+ const sourceEntityId = instruction.sourceEntityId;
640
649
  const physicalTableName = instruction.physicalTableName;
641
650
  const buildSQL = persistSource.getSQL({
642
651
  buildManifest: manifest.buildManifest,
@@ -644,7 +653,7 @@ export class MaterializationService {
644
653
  });
645
654
 
646
655
  const bareName = bareTableName(physicalTableName);
647
- const stagingTableName = `${physicalTableName}${stagingSuffix(buildId)}`;
656
+ const stagingTableName = `${physicalTableName}${stagingSuffix(sourceEntityId)}`;
648
657
  // The control plane sends the logical (unquoted) physical name; dialect-
649
658
  // quote each identifier here so a container path or quote-requiring name
650
659
  // (e.g. a hyphenated BigQuery project id) produces valid DDL. The manifest
@@ -681,7 +690,7 @@ export class MaterializationService {
681
690
  }
682
691
 
683
692
  // Make this table visible to downstream sources built later in this run.
684
- manifest.update(buildId, { tableName: physicalTableName });
693
+ manifest.update(sourceEntityId, { tableName: physicalTableName });
685
694
 
686
695
  const durationMs = Math.round(performance.now() - startTime);
687
696
  recordSourceBuildDuration(durationMs);
@@ -691,7 +700,7 @@ export class MaterializationService {
691
700
  });
692
701
 
693
702
  return {
694
- buildId,
703
+ sourceEntityId,
695
704
  sourceName: persistSource.name,
696
705
  materializedTableId: instruction.materializedTableId,
697
706
  physicalTableName,
@@ -799,7 +808,7 @@ export class MaterializationService {
799
808
  if (!connectionName || !physicalTableName) {
800
809
  logger.warn("Skipping manifest entry with no connection/table", {
801
810
  materializationId: m.id,
802
- buildId: entry.buildId,
811
+ sourceEntityId: entry.sourceEntityId,
803
812
  });
804
813
  continue;
805
814
  }
@@ -825,7 +834,7 @@ export class MaterializationService {
825
834
  // table behind; clean it up too while we hold the connection.
826
835
  await connection.runSQL(
827
836
  `DROP TABLE IF EXISTS ${quoteTablePath(
828
- `${physicalTableName}${stagingSuffix(entry.buildId)}`,
837
+ `${physicalTableName}${stagingSuffix(entry.sourceEntityId)}`,
829
838
  dialect,
830
839
  )}`,
831
840
  );
@@ -48,7 +48,7 @@ export function makeBuildPlan(overrides: Partial<BuildPlan> = {}): BuildPlan {
48
48
  sourceID: "orders@m.malloy",
49
49
  connectionName: "duckdb",
50
50
  dialect: "duckdb",
51
- buildId: "build-orders",
51
+ sourceEntityId: "build-orders",
52
52
  sql: "SELECT 1",
53
53
  columns: [],
54
54
  },
@@ -62,7 +62,7 @@ export function makeInstruction(
62
62
  overrides: Partial<BuildInstruction> = {},
63
63
  ): BuildInstruction {
64
64
  return {
65
- buildId: "build-orders",
65
+ sourceEntityId: "build-orders",
66
66
  materializedTableId: "mt-1",
67
67
  physicalTableName: '"orders_v1"',
68
68
  realization: "COPY",
@@ -72,25 +72,41 @@ export function makeInstruction(
72
72
 
73
73
  /**
74
74
  * A minimal stand-in for a Malloy {@link PersistSource} exposing only what the
75
- * build internals touch (name/id, deterministic buildId, SQL, and the
75
+ * build internals touch (name/id, deterministic sourceEntityId, SQL, and the
76
76
  * `#@ persist name=` annotation reader, defaulted to "unset").
77
77
  */
78
78
  export function fakeSource(opts: {
79
79
  name: string;
80
- buildId: string;
80
+ sourceEntityId: string;
81
81
  sql?: string;
82
82
  connectionName?: string;
83
83
  dialectName?: string;
84
+ /** key=value fields of the `#@ persist` annotation (e.g. sharing). */
85
+ annotationFields?: Record<string, string>;
84
86
  }): PersistSource {
87
+ const fields = opts.annotationFields;
85
88
  return {
86
89
  name: opts.name,
87
90
  sourceID: opts.name,
88
91
  connectionName: opts.connectionName ?? "duckdb",
89
92
  dialectName: opts.dialectName ?? "duckdb",
90
- makeBuildId: () => opts.buildId,
93
+ makeBuildId: () => opts.sourceEntityId,
91
94
  getSQL: () => opts.sql ?? "SELECT 1",
92
95
  annotations: {
93
- parseAsTag: () => ({ tag: { text: () => undefined } }),
96
+ parseAsTag: () =>
97
+ fields
98
+ ? {
99
+ tag: {
100
+ *entries() {
101
+ for (const [key, value] of Object.entries(fields)) {
102
+ yield [key, { text: () => value }];
103
+ }
104
+ },
105
+ },
106
+ }
107
+ : // No annotation fields: entries() is absent, and
108
+ // deriveAnnotationFields degrades to {}.
109
+ { tag: { text: () => undefined } },
94
110
  },
95
111
  } as unknown as PersistSource;
96
112
  }
@@ -1931,7 +1931,7 @@ function makeHydrationRuntime(
1931
1931
  // (getSQL) time, gated on `prepareResultOptions.buildManifest`; without this
1932
1932
  // the hydrated model always recomputes from the base tables even though the
1933
1933
  // manifest was bound at load. `strict: false` keeps serving live for any
1934
- // source whose buildId is absent from the manifest.
1934
+ // source whose sourceEntityId is absent from the manifest.
1935
1935
  return new Runtime({
1936
1936
  urlReader,
1937
1937
  config,
@@ -61,7 +61,7 @@ export class Package {
61
61
  private packagePath: string;
62
62
  private malloyConfig: MalloyConfig;
63
63
  // Build-manifest binding state (Malloy Persistence v0). When bound, these
64
- // entries (buildId -> { tableName }) are what served queries use to route
64
+ // entries (sourceEntityId -> { tableName }) are what served queries use to route
65
65
  // persist sources to their materialized physical tables; they are also reused
66
66
  // by the /compile preview so previewed SQL matches executed SQL. Surfaced on
67
67
  // /status (via getPackageMetadata) so the control plane can confirm a worker
@@ -72,7 +72,7 @@ export class Package {
72
72
  private manifestEntryCount = 0;
73
73
  private boundManifestUri: string | null = null;
74
74
  // The package's persist build plan: a deterministic property of the compiled
75
- // package (per-source buildId, columns, build SQL, dependency graphs),
75
+ // package (per-source sourceEntityId, columns, build SQL, dependency graphs),
76
76
  // computed once at load from the live (unbound) models so it is stable for a
77
77
  // given (package version, connection config). Null when the package declares
78
78
  // no persist source. Surfaced read-only on getPackageMetadata() so a caller
@@ -347,6 +347,7 @@ export class Package {
347
347
  // absence as a schedule removal. See `parsePackageMaterialization`.
348
348
  materialization: outcome.packageMetadata.materialization ?? {
349
349
  schedule: null,
350
+ freshness: null,
350
351
  },
351
352
  };
352
353
 
@@ -450,6 +451,16 @@ export class Package {
450
451
  detail: invalidMsg,
451
452
  });
452
453
  }
454
+ // Same fail-safe split for the materialization cron gate: an existing
455
+ // package whose manifest violates the private-only cron rule still loads
456
+ // (warn), but a publish of it is rejected (see package.controller).
457
+ const invalidSchedule = pkg.formatInvalidSchedule();
458
+ if (invalidSchedule) {
459
+ logger.warn(`Package ${packageName} has an invalid cron schedule`, {
460
+ packageName,
461
+ detail: invalidSchedule,
462
+ });
463
+ }
453
464
  pkg.logEmptyDiscoveryWarnings();
454
465
 
455
466
  return pkg;
@@ -460,7 +471,7 @@ export class Package {
460
471
  }
461
472
 
462
473
  /**
463
- * The package's persist build plan (per-source buildId, columns, build SQL,
474
+ * The package's persist build plan (per-source sourceEntityId, columns, build SQL,
464
475
  * dependency graphs), or null when the package declares no persist source.
465
476
  * A deterministic property of the compiled package; callers derive build
466
477
  * instructions from it for an orchestrated materialization.
@@ -503,7 +514,7 @@ export class Package {
503
514
  }
504
515
 
505
516
  /**
506
- * The currently-bound build-manifest entries (buildId -> { tableName }), or
517
+ * The currently-bound build-manifest entries (sourceEntityId -> { tableName }), or
507
518
  * undefined when the package is serving live. Reused by the /compile preview
508
519
  * so previewed SQL gets the same persist-source -> physical-table routing as
509
520
  * execution.
@@ -603,6 +614,41 @@ export class Package {
603
614
  return this.exploreWarnings(exploresOverride).join("\n");
604
615
  }
605
616
 
617
+ /**
618
+ * Publish-gate for the package-level materialization cron (the power tier
619
+ * of artifact-anchored scheduling): a declared `materialization.schedule`
620
+ * governs every persist source in the package, so it is valid only when
621
+ * each source in the compiled build plan resolves to an explicit
622
+ * `sharing=private`. A cron over any shared/unset source would let one
623
+ * package dictate the refresh cadence of an artifact other packages share —
624
+ * those packages declare `materialization.freshness.window` instead and the
625
+ * control plane schedules the shared artifact from the tightest window.
626
+ * One actionable message per offending source (empty when the cron is
627
+ * valid or no cron is declared). Strict at publish (package.controller),
628
+ * warn-only at load/reload (loadViaWorker) — same split as explores.
629
+ */
630
+ public scheduleWarnings(): string[] {
631
+ const schedule = this.packageMetadata.materialization?.schedule;
632
+ if (!schedule) return [];
633
+ const sources = Object.values(this.buildPlan?.sources ?? {});
634
+ return sources
635
+ .filter((source) => source.sharing !== "private")
636
+ .map(
637
+ (source) =>
638
+ `materialization.schedule (cron) in ${PACKAGE_MANIFEST_NAME} requires every ` +
639
+ `persist source to declare '#@ persist ... sharing=private'; source ` +
640
+ `'${source.name}' resolves to ${
641
+ source.sharing ? `'${source.sharing}'` : "unset"
642
+ }. Declare 'materialization.freshness.window' instead (the control plane ` +
643
+ `schedules refreshes from it), or mark every persist source sharing=private.`,
644
+ );
645
+ }
646
+
647
+ /** The {@link scheduleWarnings} joined into one string, or "" if none. */
648
+ public formatInvalidSchedule(): string {
649
+ return this.scheduleWarnings().join("\n");
650
+ }
651
+
606
652
  /**
607
653
  * One message per LISTED model whose discovery surface is empty because it
608
654
  * is import-only (imports other files, declares/re-exports nothing). Such a
@@ -5,7 +5,7 @@ describe("service/package_manifest", () => {
5
5
  describe("parsePackageMaterialization", () => {
6
6
  it("extracts a string schedule", () => {
7
7
  expect(parsePackageMaterialization({ schedule: "0 6 * * *" })).toEqual(
8
- { schedule: "0 6 * * *" },
8
+ { schedule: "0 6 * * *", freshness: null },
9
9
  );
10
10
  });
11
11
 
@@ -18,21 +18,59 @@ describe("service/package_manifest", () => {
18
18
  expect(
19
19
  parsePackageMaterialization({
20
20
  schedule: "*/15 * * * *",
21
- freshness: { window: "24h" },
21
+ retries: 3,
22
22
  }),
23
- ).toEqual({ schedule: "*/15 * * * *" });
23
+ ).toEqual({ schedule: "*/15 * * * *", freshness: null });
24
24
  });
25
25
 
26
26
  it("degrades a non-string schedule to null", () => {
27
27
  expect(parsePackageMaterialization({ schedule: 42 })).toEqual({
28
28
  schedule: null,
29
+ freshness: null,
30
+ });
31
+ expect(parsePackageMaterialization({})).toEqual({
32
+ schedule: null,
33
+ freshness: null,
29
34
  });
30
- expect(parsePackageMaterialization({})).toEqual({ schedule: null });
31
35
  });
32
36
 
33
37
  it("returns null for non-object input", () => {
34
38
  expect(parsePackageMaterialization("0 6 * * *")).toBeNull();
35
39
  expect(parsePackageMaterialization(7)).toBeNull();
36
40
  });
41
+
42
+ it("extracts a full freshness block verbatim", () => {
43
+ expect(
44
+ parsePackageMaterialization({
45
+ freshness: { window: "24h", fallback: "stale_ok" },
46
+ }),
47
+ ).toEqual({
48
+ schedule: null,
49
+ freshness: { window: "24h", fallback: "stale_ok" },
50
+ });
51
+ });
52
+
53
+ it("keeps a partial freshness block (window only)", () => {
54
+ expect(
55
+ parsePackageMaterialization({ freshness: { window: "1h" } }),
56
+ ).toEqual({ schedule: null, freshness: { window: "1h" } });
57
+ });
58
+
59
+ it("drops invalid freshness fields rather than defaulting them", () => {
60
+ // A bad value is reported as absent — never substituted — so the
61
+ // control plane sees exactly what was (validly) declared.
62
+ expect(
63
+ parsePackageMaterialization({
64
+ freshness: { window: 24, fallback: "retry" },
65
+ }),
66
+ ).toEqual({ schedule: null, freshness: {} });
67
+ });
68
+
69
+ it("degrades a non-object freshness to null", () => {
70
+ expect(parsePackageMaterialization({ freshness: "24h" })).toEqual({
71
+ schedule: null,
72
+ freshness: null,
73
+ });
74
+ });
37
75
  });
38
76
  });
@@ -4,18 +4,61 @@
4
4
  * testable in isolation from the package-load worker that consumes it.
5
5
  */
6
6
 
7
+ const FRESHNESS_FALLBACKS = ["live", "stale_ok", "fail"] as const;
8
+ export type FreshnessFallback = (typeof FRESHNESS_FALLBACKS)[number];
9
+
10
+ /**
11
+ * The manifest's `materialization.freshness` block, surfaced verbatim for the
12
+ * control plane (which owns the scheduling and query-time gating logic).
13
+ * Fields are kept only when valid — an invalid value is dropped, never
14
+ * defaulted, so absence on the wire always means "not declared".
15
+ */
16
+ export interface PackageFreshnessConfig {
17
+ /** Maximum acceptable staleness, as a duration string (e.g. "24h"). */
18
+ window?: string;
19
+ /** Declared query-time behavior when the window is missed. */
20
+ fallback?: FreshnessFallback;
21
+ }
22
+
7
23
  export interface PackageMaterializationConfig {
8
24
  /**
9
25
  * 5-field UNIX cron the control plane uses to schedule version-level
10
- * re-materialization. Null when absent or not a string.
26
+ * re-materialization. Null when absent or not a string. Publish-gated:
27
+ * only valid when every persist source resolves to explicit
28
+ * `sharing=private` (see Package.scheduleWarnings).
11
29
  */
12
30
  schedule: string | null;
31
+ /**
32
+ * Freshness policy ({ window, fallback }). Null when the manifest declares
33
+ * none — distinct from an empty object, so the control plane can tell
34
+ * "no policy" from "policy with no valid fields".
35
+ */
36
+ freshness: PackageFreshnessConfig | null;
37
+ }
38
+
39
+ function parseFreshness(raw: unknown): PackageFreshnessConfig | null {
40
+ if (!raw || typeof raw !== "object") {
41
+ return null;
42
+ }
43
+ const { window, fallback } = raw as { window?: unknown; fallback?: unknown };
44
+ const freshness: PackageFreshnessConfig = {};
45
+ if (typeof window === "string") {
46
+ freshness.window = window;
47
+ }
48
+ if (
49
+ typeof fallback === "string" &&
50
+ (FRESHNESS_FALLBACKS as readonly string[]).includes(fallback)
51
+ ) {
52
+ freshness.fallback = fallback as FreshnessFallback;
53
+ }
54
+ return freshness;
13
55
  }
14
56
 
15
57
  /**
16
58
  * Read the manifest's `materialization` object, keeping only recognized fields.
17
59
  * Returns null when the block is absent so the API field is null rather than an
18
- * empty object; a non-string schedule degrades to null.
60
+ * empty object; a non-string schedule degrades to null, and an absent or
61
+ * non-object freshness degrades to null.
19
62
  */
20
63
  export function parsePackageMaterialization(
21
64
  raw: unknown,
@@ -23,6 +66,12 @@ export function parsePackageMaterialization(
23
66
  if (!raw || typeof raw !== "object") {
24
67
  return null;
25
68
  }
26
- const schedule = (raw as { schedule?: unknown }).schedule;
27
- return { schedule: typeof schedule === "string" ? schedule : null };
69
+ const { schedule, freshness } = raw as {
70
+ schedule?: unknown;
71
+ freshness?: unknown;
72
+ };
73
+ return {
74
+ schedule: typeof schedule === "string" ? schedule : null,
75
+ freshness: parseFreshness(freshness),
76
+ };
28
77
  }