@segmentstream-cloud/ontology 0.21.0 → 0.23.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.
package/dist/render.js CHANGED
@@ -1,4 +1,4 @@
1
- import { shortestRoutesFrom } from "./aggregate-routes.js";
1
+ import { inputEntities, shortestRoutesFrom } from "./aggregate-routes.js";
2
2
  import { isAllowed, permissionOf, READ_VERB, rolesGranting } from "./permissions.js";
3
3
  import { REFERENCE_ANNOTATION } from "./property.js";
4
4
  /** What an action of the manifest requires; version 1 manifests leave it implicit. */
@@ -389,6 +389,51 @@ export function measuresFor(manifest, permissions) {
389
389
  measure,
390
390
  })));
391
391
  }
392
+ /**
393
+ * The measures of the model whose every input entity the reader may read, in declaration order.
394
+ * Reading a model measure is reading its inputs. Without permissions, every one.
395
+ */
396
+ export function modelMeasuresFor(manifest, permissions) {
397
+ return (manifest.measures ?? []).filter((measure) => inputEntities(measure).every((entity) => readable(entity, permissions)));
398
+ }
399
+ /** `a`, `a and b`, `a, b and c`. */
400
+ function listed(values) {
401
+ return values.length < 2
402
+ ? values.join("")
403
+ : `${values.slice(0, -1).join(", ")} and ${values[values.length - 1]}`;
404
+ }
405
+ /**
406
+ * How one alignment group reads: `sales.order_date aligns with plans.month`, and for an entity
407
+ * without a counterpart `listings has no counterpart for sales.order_date`. Members follow the
408
+ * measure's input entities, whatever order the document keeps them in.
409
+ */
410
+ function alignmentClauses(measure, group) {
411
+ const order = inputEntities(measure);
412
+ const members = Object.entries(group).sort(([left], [right]) => order.indexOf(left) - order.indexOf(right));
413
+ const present = members.flatMap(([entity, property]) => property === null ? [] : [`${entity}.${property}`]);
414
+ const absent = members.flatMap(([entity, property]) => (property === null ? [entity] : []));
415
+ const [first, ...rest] = present;
416
+ if (first === undefined) {
417
+ return [];
418
+ }
419
+ return [
420
+ ...(rest.length > 0 ? [`${first} aligns with ${listed(rest)}`] : []),
421
+ ...absent.map((entity) => `${entity} has no counterpart for ${first}`),
422
+ ];
423
+ }
424
+ /**
425
+ * A measure of the model as `describe`, the MCP tool and the instructions show it: its description,
426
+ * the entity measures it reads and how their properties align. `Ordered over planned units. Reads
427
+ * sales.ordered and plans.planned_units; sales.order_date aligns with plans.month.`
428
+ */
429
+ export function modelMeasureDescription(measure) {
430
+ const clauses = [
431
+ `Reads ${listed(measure.inputs)}`,
432
+ ...measure.align.flatMap((group) => alignmentClauses(measure, group)),
433
+ ];
434
+ const described = measure.description === "" ? [] : [sentence(measure.description)];
435
+ return [...described, `${clauses.join("; ")}.`].join(" ");
436
+ }
392
437
  /**
393
438
  * The entities one entity reaches, with the shortest routes to each: exactly the routes aggregate
394
439
  * resolution weighs ({@link shortestRoutesFrom}). Only then is the reader applied: a target they may
@@ -416,8 +461,9 @@ function reachLine(entity, reaches) {
416
461
  return `${entity} reaches ${targets.join(", ")}.`;
417
462
  }
418
463
  /**
419
- * The aggregate part of the catalog's prose: every measure the reader may read, model-wide, and
420
- * per measure entity the entities it reaches. Empty when the reader may read no measure.
464
+ * The aggregate part of the catalog's prose: every measure the reader may read, model-wide, the
465
+ * measures of the model whose every input they may read, and per measure entity the entities it
466
+ * reaches. Empty when the reader may read no measure.
421
467
  */
422
468
  export function renderAggregates(manifest, permissions) {
423
469
  const measures = measuresFor(manifest, permissions);
@@ -430,10 +476,17 @@ export function renderAggregates(manifest, permissions) {
430
476
  return reaches.length === 0 ? [] : [reachLine(owner, reaches)];
431
477
  });
432
478
  const measureLines = measures.map(({ name, entity, measure }) => `${name} (${measure.unit}): ${sentence(measureDescription(entity, measure))}`);
479
+ const modelLines = modelMeasuresFor(manifest, permissions).map((measure) => `${measure.name} (${measure.unit}): ${modelMeasureDescription(measure)}`);
433
480
  return [
434
481
  `Aggregates: ${RECORDS_OR_AGGREGATES} (POST /api/ontology/aggregate, or the aggregate tool).`,
435
482
  `${INDENT}Measures, as <entity>.<measure>:`,
436
483
  ...measureLines.map((line) => `${INDENT}${INDENT}${line}`),
484
+ ...(modelLines.length === 0
485
+ ? []
486
+ : [
487
+ `${INDENT}Measures of the model, by name:`,
488
+ ...modelLines.map((line) => `${INDENT}${INDENT}${line}`),
489
+ ]),
437
490
  ...(reachLines.length === 0
438
491
  ? []
439
492
  : [
@@ -1,4 +1,4 @@
1
- import { Effect } from "effect";
1
+ import { Effect, Schema } from "effect";
2
2
  import type { SqlClient } from "effect/unstable/sql";
3
3
  import type { OntologyManifest } from "../manifest.js";
4
4
  import { type ReplicationSelection, type ReplicationExclusion } from "./model.js";
@@ -9,6 +9,13 @@ export interface ReplicationTarget {
9
9
  readonly manifest: OntologyManifest;
10
10
  readonly selection: ReadonlyArray<ReplicationSelection>;
11
11
  }
12
+ declare const ReplicationBaselineFailed_base: Schema.Class<ReplicationBaselineFailed, Schema.TaggedStruct<"ReplicationBaselineFailed", {
13
+ readonly message: Schema.String;
14
+ readonly cause: Schema.optionalKey<Schema.Unknown>;
15
+ }>, import("effect/Cause").YieldableError>;
16
+ /** An initial snapshot cannot be created with the requested state or record projection. */
17
+ export declare class ReplicationBaselineFailed extends ReplicationBaselineFailed_base {
18
+ }
12
19
  /** All baseline rows are committed with capture enablement; interrupted setup can safely resume. */
13
20
  export declare const createReplicationBaseline: (sql: SqlClient.SqlClient, input: {
14
21
  readonly generation: string;
@@ -16,4 +23,5 @@ export declare const createReplicationBaseline: (sql: SqlClient.SqlClient, input
16
23
  readonly exclusions?: ReadonlyArray<ReplicationExclusion>;
17
24
  readonly selection: ReadonlyArray<ReplicationSelection>;
18
25
  readonly newSource?: boolean;
19
- }) => Effect.Effect<ReplicationTarget, import("effect/unstable/sql/SqlError").SqlError, never>;
26
+ }) => Effect.Effect<ReplicationTarget, ReplicationBaselineFailed | import("effect/unstable/sql/SqlError").SqlError, never>;
27
+ export {};
@@ -1,26 +1,35 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import { Effect } from "effect";
2
+ import { Effect, Schema } from "effect";
3
3
  import { joinColumnNames, joinTableName, quoteIdentifier as qi, quoteLiteral as ql, tableName, } from "../migrate/storage.js";
4
4
  import { REPLICATION_RECORD_BYTES, validateReplicationSelection, } from "./model.js";
5
5
  import { snapshotExpression } from "./schema.js";
6
- const MAX_BASELINE_RECORDS = 50_000;
6
+ /** An initial snapshot cannot be created with the requested state or record projection. */
7
+ export class ReplicationBaselineFailed extends Schema.TaggedError()("ReplicationBaselineFailed", { message: Schema.String, cause: Schema.optionalKey(Schema.Unknown) }) {
8
+ }
7
9
  /** All baseline rows are committed with capture enablement; interrupted setup can safely resume. */
8
10
  export const createReplicationBaseline = (sql, input) => sql.withTransaction(Effect.gen(function* () {
9
11
  yield* sql.unsafe("SET LOCAL statement_timeout = '30s'");
10
12
  yield* sql.unsafe("SET LOCAL lock_timeout = '5s'");
11
13
  const [state] = yield* sql.unsafe("SELECT source_id::text, revision::text, enabled FROM _ontology_replication WHERE id FOR UPDATE");
12
14
  if (!state) {
13
- return yield* Effect.die(new Error("Run the ontology SDK migration before enabling replication."));
15
+ return yield* new ReplicationBaselineFailed({
16
+ message: "Run the ontology SDK migration before enabling replication.",
17
+ });
14
18
  }
15
19
  const existing = yield* sql.unsafe("SELECT * FROM _ontology_replication_targets WHERE generation = $1::uuid", [input.generation]);
16
20
  if (existing[0]) {
17
21
  return existing[0];
18
22
  }
19
23
  if (state.enabled) {
20
- return yield* Effect.die(new Error("Pause replication before starting a new generation."));
24
+ return yield* new ReplicationBaselineFailed({
25
+ message: "Pause replication before starting a new generation.",
26
+ });
21
27
  }
22
28
  const manifest = input.manifest;
23
- yield* Effect.sync(() => validateReplicationSelection(manifest, input.selection));
29
+ yield* Effect.try({
30
+ try: () => validateReplicationSelection(manifest, input.selection),
31
+ catch: (cause) => new ReplicationBaselineFailed({ message: "Invalid replication projection.", cause }),
32
+ });
24
33
  // Drain older SDK transactions that started before the compatibility fence was enabled.
25
34
  const tables = manifest.entities
26
35
  .filter((e) => e.source === undefined && !e.builtIn)
@@ -57,17 +66,17 @@ export const createReplicationBaseline = (sql, input) => sql.withTransaction(Eff
57
66
  JSON.stringify(manifest),
58
67
  JSON.stringify(input.selection),
59
68
  ]);
60
- let total = 0;
61
69
  const baseline = (entityId, kind, records) => Effect.gen(function* () {
62
70
  // Keep the snapshot in PostgreSQL; large baselines must not fill a serverless process's heap.
63
- const [size] = yield* sql.unsafe(`SELECT count(*)::int AS count, max(octet_length(payload::text)) AS largest
64
- FROM (${records} LIMIT ${MAX_BASELINE_RECORDS + 1}) snapshot`);
65
- total += size.count;
66
- if (total > MAX_BASELINE_RECORDS) {
67
- return yield* Effect.die(new Error("The replication baseline exceeds 50,000 records; use a bulk bootstrap."));
68
- }
69
- if ((size.largest ?? 0) > REPLICATION_RECORD_BYTES) {
70
- return yield* Effect.die(new Error("A projected baseline record exceeds the replication size limit."));
71
+ const [size] = yield* sql.unsafe(`SELECT EXISTS (
72
+ SELECT 1 FROM (${records}) snapshot
73
+ WHERE octet_length(payload::text) > ${REPLICATION_RECORD_BYTES}
74
+ ) AS oversized`);
75
+ if (size.oversized) {
76
+ return yield* new ReplicationBaselineFailed({
77
+ message: `A projected baseline record exceeds ${REPLICATION_RECORD_BYTES} bytes; ` +
78
+ "exclude large fields before enabling replication.",
79
+ });
71
80
  }
72
81
  yield* sql.unsafe(`INSERT INTO _ontology_replication_baselines (generation, event)
73
82
  SELECT $1::uuid, jsonb_build_object('source_id', $2::text, 'entity_id', $3::text,
@@ -0,0 +1,6 @@
1
+ import type { OntologyManifest } from "../manifest.js";
2
+ import type { MigrationRefusal } from "../migrate/model.js";
3
+ import type { ReplicationTarget } from "./baseline.js";
4
+ import type { ReplicationSelection } from "./model.js";
5
+ /** A live stream can change its projection only when retained JSON still describes current rows. */
6
+ export declare function replicationMigrationRefusals(current: ReplicationTarget, manifest: OntologyManifest, selection: ReadonlyArray<ReplicationSelection>): ReadonlyArray<MigrationRefusal>;
@@ -0,0 +1,64 @@
1
+ import { toSnakeCase } from "../names.js";
2
+ import { queryFields } from "../query.js";
3
+ /** A live stream can change its projection only when retained JSON still describes current rows. */
4
+ export function replicationMigrationRefusals(current, manifest, selection) {
5
+ const refusals = [];
6
+ const refuse = (path, reason) => {
7
+ refusals.push({
8
+ code: "replication-rebuild-required",
9
+ path,
10
+ message: `${reason} Pause replication, apply the migration, then explicitly rebuild replication.`,
11
+ });
12
+ };
13
+ for (const previous of current.selection) {
14
+ const entity = current.manifest.entities.find((item) => item.id === previous.entityId);
15
+ const next = selection.find((item) => item.entityId === entity.id);
16
+ if (!next) {
17
+ refuse(entity.name, "Removing a replicated entity would discard its pending history.");
18
+ continue;
19
+ }
20
+ for (const field of previous.fields) {
21
+ if (!next.fields.includes(field)) {
22
+ refuse(`${entity.name}.${field}`, "Removing or renaming a replicated field would leave retained JSON under its old name.");
23
+ }
24
+ }
25
+ for (const relationship of previous.relationships ?? []) {
26
+ if (!next.relationships?.includes(relationship)) {
27
+ refuse(`${entity.name}.${relationship}`, "Removing a replicated relationship would leave its old edges in retained JSON.");
28
+ }
29
+ }
30
+ }
31
+ for (const next of selection) {
32
+ const entity = manifest.entities.find((item) => item.id === next.entityId);
33
+ const previousEntity = current.manifest.entities.find((item) => item.id === entity.id);
34
+ // A newly created table starts empty and all subsequent writes are captured normally.
35
+ if (!previousEntity) {
36
+ continue;
37
+ }
38
+ const previous = current.selection.find((item) => item.entityId === entity.id);
39
+ if (!previous) {
40
+ refuse(entity.name, "Previously excluded records are absent from retained JSON.");
41
+ continue;
42
+ }
43
+ const previousFields = new Set(queryFields(previousEntity).map((field) => field.name));
44
+ for (const field of next.fields.filter((name) => !previous.fields.includes(name))) {
45
+ const property = entity.properties.find((item) => item.name === field);
46
+ const renamed = property?.renamedFrom;
47
+ if (previousFields.has(field) ||
48
+ (renamed !== undefined &&
49
+ (previousFields.has(renamed) || previousFields.has(toSnakeCase(renamed))))) {
50
+ refuse(`${entity.name}.${field}`, "Existing values are absent from this JSON field.");
51
+ }
52
+ else if (property?.default !== undefined && property.default !== null) {
53
+ refuse(`${entity.name}.${field}`, "Adding a field with a non-null default changes existing rows without captured writes.");
54
+ }
55
+ }
56
+ for (const relationship of next.relationships ?? []) {
57
+ if (!previous.relationships?.includes(relationship) &&
58
+ previousEntity.relationships.some((item) => item.name === relationship)) {
59
+ refuse(`${entity.name}.${relationship}`, "Previously excluded relationship edges are absent from retained JSON.");
60
+ }
61
+ }
62
+ }
63
+ return refusals;
64
+ }
@@ -1,5 +1,5 @@
1
1
  import { type OntologyManifest } from "../manifest.js";
2
- /** The resolved projection for one immutable generation. `id` is always included. */
2
+ /** The current projection of a continuous replication stream. `id` is always included. */
3
3
  export interface ReplicationSelection {
4
4
  readonly entityId: string;
5
5
  readonly fields: ReadonlyArray<string>;
@@ -1,14 +1,14 @@
1
1
  import { Effect } from "effect";
2
2
  import type { SqlClient } from "effect/unstable/sql";
3
3
  import { type ReplicationEvent, type ReplicationSelection, type ReplicationExclusion } from "./model.js";
4
- import { type ReplicationTarget } from "./baseline.js";
4
+ import { ReplicationBaselineFailed, type ReplicationTarget } from "./baseline.js";
5
5
  export type { ReplicationTarget } from "./baseline.js";
6
6
  export declare const startReplication: (sql: SqlClient.SqlClient, input: {
7
7
  readonly generation: string;
8
8
  readonly exclusions?: ReadonlyArray<ReplicationExclusion>;
9
9
  readonly selection?: ReadonlyArray<ReplicationSelection>;
10
10
  readonly newSource?: boolean;
11
- }) => Effect.Effect<ReplicationTarget, import("../index.js").CatalogInvalid | import("effect/unstable/sql/SqlError").SqlError, never>;
11
+ }) => Effect.Effect<ReplicationTarget, import("../index.js").CatalogInvalid | ReplicationBaselineFailed | import("effect/unstable/sql/SqlError").SqlError, never>;
12
12
  export declare const activeReplicationTarget: (sql: SqlClient.SqlClient) => Effect.Effect<ReplicationTarget, import("effect/unstable/sql/SqlError").SqlError, never>;
13
13
  export declare const pauseReplication: (sql: SqlClient.SqlClient) => import("effect/unstable/sql/Statement").Statement<object>;
14
14
  export declare const replicationTarget: (sql: SqlClient.SqlClient, generation: string) => Effect.Effect<ReplicationTarget, import("effect/unstable/sql/SqlError").SqlError, never>;
@@ -2,17 +2,22 @@ import { Effect } from "effect";
2
2
  import { readCatalog } from "../migrate/apply.js";
3
3
  import { historyTableName, quoteIdentifier as qi } from "../migrate/storage.js";
4
4
  import { REPLICATION_BATCH_BYTES, REPLICATION_BATCH_SIZE, publishMessage, replicationEventId, resolveReplicationSelection, } from "./model.js";
5
- import { createReplicationBaseline } from "./baseline.js";
5
+ import { createReplicationBaseline, ReplicationBaselineFailed, } from "./baseline.js";
6
6
  export const startReplication = (sql, input) => sql.withTransaction(Effect.gen(function* () {
7
7
  yield* sql.unsafe("SET LOCAL statement_timeout = '30s'");
8
8
  yield* sql.unsafe("SET LOCAL lock_timeout = '5s'");
9
9
  yield* sql.unsafe("SELECT id FROM _ontology_replication WHERE id FOR UPDATE");
10
10
  const manifest = yield* readCatalog({ sql });
11
11
  if (!manifest) {
12
- return yield* Effect.die(new Error("The ontology has not been migrated."));
12
+ return yield* new ReplicationBaselineFailed({
13
+ message: "The ontology has not been migrated.",
14
+ });
13
15
  }
14
16
  const selection = input.selection ??
15
- (yield* Effect.sync(() => resolveReplicationSelection(manifest, input.exclusions)));
17
+ (yield* Effect.try({
18
+ try: () => resolveReplicationSelection(manifest, input.exclusions),
19
+ catch: (cause) => new ReplicationBaselineFailed({ message: "Invalid replication exclusions.", cause }),
20
+ }));
16
21
  return yield* createReplicationBaseline(sql, { ...input, manifest, selection });
17
22
  }));
18
23
  export const activeReplicationTarget = (sql) => sql
@@ -1,7 +1,8 @@
1
1
  import { type OntologyManifest } from "../manifest.js";
2
2
  /**
3
3
  * The catalog for a reader: the manifest, with each entity's actions narrowed to the ones the given
4
- * permissions may run. Without permissions it is the manifest itself.
4
+ * permissions may run, and only the measures of the model whose every input entity they may read.
5
+ * Without permissions it is the manifest itself.
5
6
  */
6
7
  export declare function catalogFor(manifest: OntologyManifest, permissions?: ReadonlyArray<string>): OntologyManifest;
7
8
  export interface GraphRelationship {
@@ -1,16 +1,20 @@
1
1
  import { isValueMatched } from "../manifest.js";
2
2
  import { isAllowed } from "../permissions.js";
3
- import { actionPermission } from "../render.js";
3
+ import { actionPermission, modelMeasuresFor } from "../render.js";
4
4
  /**
5
5
  * The catalog for a reader: the manifest, with each entity's actions narrowed to the ones the given
6
- * permissions may run. Without permissions it is the manifest itself.
6
+ * permissions may run, and only the measures of the model whose every input entity they may read.
7
+ * Without permissions it is the manifest itself.
7
8
  */
8
9
  export function catalogFor(manifest, permissions) {
9
10
  if (permissions === undefined) {
10
11
  return manifest;
11
12
  }
13
+ const { measures: declared, ...rest } = manifest;
14
+ const measures = declared === undefined ? [] : modelMeasuresFor(manifest, permissions);
12
15
  return {
13
- ...manifest,
16
+ ...rest,
17
+ ...(measures.length === 0 ? {} : { measures }),
14
18
  entities: manifest.entities
15
19
  .filter((entity) => entity.source === undefined ||
16
20
  isAllowed(entity.permissions?.read ?? `${entity.name}:read`, { permissions }))
@@ -7,7 +7,7 @@ import { SCOPE_MISMATCH } from "../aggregate-platform.js";
7
7
  import { CurrentPrincipal, withPrincipal } from "../authentication.js";
8
8
  import { catalogFor } from "../catalog.js";
9
9
  import { ConversationSearch } from "../agent/conversations.js";
10
- import { describeCatalog, measureDescription, measuresFor, RECORDS_OR_AGGREGATES, } from "../../render.js";
10
+ import { describeCatalog, measureDescription, measuresFor, modelMeasureDescription, modelMeasuresFor, RECORDS_OR_AGGREGATES, } from "../../render.js";
11
11
  import { RequestId, errorBodyForWire, expectedFailure, internalError, schemaIssues, } from "../errors.js";
12
12
  import { publicOrigin } from "../oauth/protocol.js";
13
13
  import { mcpChallenge, mcpResource } from "../oauth/server.js";
@@ -313,7 +313,10 @@ function instructions({ built }, permissions) {
313
313
  return (definition.source === undefined && isAllowed(permissionOf(name, READ_VERB), { permissions }));
314
314
  })
315
315
  .map(({ name, resolved }) => `- ${name}: ${resolved.entity.definition.description}`);
316
- const measures = measuresFor(manifest, permissions).map(({ name, entity, measure }) => `- ${name}: ${measureDescription(entity, measure)}`);
316
+ const measures = [
317
+ ...measuresFor(manifest, permissions).map(({ name, entity, measure }) => `- ${name}: ${measureDescription(entity, measure)}`),
318
+ ...modelMeasuresFor(manifest, permissions).map((measure) => `- ${measure.name}: ${modelMeasureDescription(measure)}`),
319
+ ];
317
320
  return [
318
321
  "This server is the app's ontology: the records the company works with and the actions " +
319
322
  "that change them. Every change goes through an action, is checked against your " +
@@ -1,11 +1,11 @@
1
1
  import { Schema } from "effect";
2
2
  import { AGGREGATE_NAME_PATTERN } from "../../aggregate-routes.js";
3
3
  import { InvalidOntology } from "../../diagnostics.js";
4
- import { isValueMatched } from "../../manifest.js";
4
+ import { isValueMatched, } from "../../manifest.js";
5
5
  import { GENERATED_UPSERT_PREFIX, humanize, mcpToolName } from "../../names.js";
6
6
  import { isAllowed, permissionOf, READ_VERB } from "../../permissions.js";
7
7
  import { queryJsonSchema } from "../../query.js";
8
- import { measureDescription, measuresFor, RECORDS_OR_AGGREGATES, } from "../../render.js";
8
+ import { measureDescription, measuresFor, modelMeasureDescription, modelMeasuresFor, RECORDS_OR_AGGREGATES, } from "../../render.js";
9
9
  import { actionInputJsonSchema } from "../../runtime/generated-actions.js";
10
10
  import { uploadFields } from "../../storage/attachments.js";
11
11
  import { actionDescription, requires } from "../descriptions.js";
@@ -281,8 +281,15 @@ function measureChoice({ name, entity, measure }) {
281
281
  measureDescription(entity, measure),
282
282
  };
283
283
  }
284
+ /** A measure of the model, by its bare name, read only by a caller who may read every input. */
285
+ function modelMeasureChoice(measure) {
286
+ return {
287
+ const: measure.name,
288
+ description: `Model · ${measure.label} (${measure.unit}): ` + modelMeasureDescription(measure),
289
+ };
290
+ }
284
291
  /** The aggregate tool's input for a caller: the request of `POST /api/ontology/aggregate`. */
285
- function aggregateInput(measures) {
292
+ function aggregateInput({ measures, model }) {
286
293
  return {
287
294
  type: "object",
288
295
  properties: {
@@ -291,9 +298,11 @@ function aggregateInput(measures) {
291
298
  minItems: 1,
292
299
  maxItems: AGGREGATE_LIMITS.measures,
293
300
  uniqueItems: true,
294
- items: { anyOf: measures.map(measureChoice) },
295
- description: "What to compute, as <entity>.<measure>. Measures of several entities are each " +
296
- "computed over their own entity's rows, then combined on the dimensions.",
301
+ items: { anyOf: [...measures.map(measureChoice), ...model.map(modelMeasureChoice)] },
302
+ description: "What to compute, as <entity>.<measure>, or a measure of the model by its name. " +
303
+ "Measures of several entities are each computed over their own entity's rows, then " +
304
+ "combined on the dimensions; a measure of the model is computed on the combined rows " +
305
+ "from the entity measures it reads.",
297
306
  },
298
307
  dimensions: {
299
308
  type: "array",
@@ -385,17 +394,23 @@ const AGGREGATE_DESCRIPTION = "Numbers about the model: measures from one or sev
385
394
  "which dates were used. " +
386
395
  "Answers come from the warehouse, and stored records from its replica: execution.sources says " +
387
396
  "how fresh each is, and execution.warnings says when a filter did not apply to a measure. " +
388
- "Requires reading every entity the request reads, path intermediates included. Call describe " +
389
- "for the model's measures and relationships.";
397
+ "Requires reading every entity the request reads, path intermediates included, and every " +
398
+ "entity a measure of the model reads. A measure of the model is named without an entity; a " +
399
+ "filter must reach every entity it reads or none of them, unless its alignment gives an entity " +
400
+ "no counterpart. Call describe for the model's measures and relationships.";
390
401
  function aggregateTool(manifest) {
391
402
  return {
392
403
  name: "aggregate",
393
404
  title: "Aggregate measures",
394
405
  description: AGGREGATE_DESCRIPTION,
395
- inputSchema: aggregateInput(measuresFor(manifest)),
406
+ inputSchema: aggregateInput({
407
+ measures: measuresFor(manifest),
408
+ model: modelMeasuresFor(manifest),
409
+ }),
396
410
  inputSchemaFor: (caller) => {
397
411
  const measures = measuresFor(manifest, caller.permissions);
398
- return measures.length === 0 ? undefined : aggregateInput(measures);
412
+ const model = modelMeasuresFor(manifest, caller.permissions);
413
+ return measures.length === 0 ? undefined : aggregateInput({ measures, model });
399
414
  },
400
415
  annotations: READ,
401
416
  permissions: [],
@@ -268,6 +268,16 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
268
268
  readonly key: Schema.String;
269
269
  readonly description: Schema.String;
270
270
  }>>>;
271
+ readonly measures: Schema.optionalKey<Schema.$Array<Schema.Struct<{
272
+ readonly name: Schema.String;
273
+ readonly label: Schema.String;
274
+ readonly description: Schema.String;
275
+ readonly unit: Schema.Literals<readonly ["number", "currency", "ratio", "percent"]>;
276
+ readonly format: Schema.Literals<readonly ["decimal", "currency", "percent"]>;
277
+ readonly expression: Schema.Codec<Schema.Json, Schema.Json, never, never>;
278
+ readonly inputs: Schema.$Array<Schema.String>;
279
+ readonly align: Schema.$Array<Schema.$Record<Schema.String, Schema.NullOr<Schema.String>>>;
280
+ }>>>;
271
281
  }>, Schema.Struct<{
272
282
  readonly entities: Schema.toEncoded<Schema.$Array<Schema.encodeKeys<Schema.Struct<{
273
283
  readonly measures: Schema.optionalKey<Schema.$Array<Schema.encodeKeys<Schema.Struct<{
@@ -387,6 +397,16 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
387
397
  readonly key: Schema.String;
388
398
  readonly description: Schema.String;
389
399
  }>>>>;
400
+ readonly measures: Schema.toEncoded<Schema.optionalKey<Schema.$Array<Schema.Struct<{
401
+ readonly name: Schema.String;
402
+ readonly label: Schema.String;
403
+ readonly description: Schema.String;
404
+ readonly unit: Schema.Literals<readonly ["number", "currency", "ratio", "percent"]>;
405
+ readonly format: Schema.Literals<readonly ["decimal", "currency", "percent"]>;
406
+ readonly expression: Schema.Codec<Schema.Json, Schema.Json, never, never>;
407
+ readonly inputs: Schema.$Array<Schema.String>;
408
+ readonly align: Schema.$Array<Schema.$Record<Schema.String, Schema.NullOr<Schema.String>>>;
409
+ }>>>>;
390
410
  }>, never, never>;
391
411
  export declare const Graph: Schema.Struct<{
392
412
  readonly hash: Schema.String;
@@ -505,6 +525,7 @@ export declare const AggregateResult: Schema.Struct<{
505
525
  readonly additive: Schema.optionalKey<Schema.Boolean>;
506
526
  readonly filters: Schema.optionalKey<Schema.$Array<Schema.String>>;
507
527
  readonly balanceOver: Schema.optionalKey<Schema.String>;
528
+ readonly inputs: Schema.optionalKey<Schema.$Array<Schema.String>>;
508
529
  }>>;
509
530
  readonly rows: Schema.$Array<Schema.$Record<Schema.String, Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.Boolean]>>>>;
510
531
  readonly truncated: Schema.Boolean;
@@ -366,7 +366,10 @@ const aggregateFields = {
366
366
  export const AggregateRequest = closedStruct({
367
367
  measures: Schema.Array(Schema.String)
368
368
  .check(Schema.isMaxLength(AGGREGATE_LIMITS.measures))
369
- .annotate({ description: "<entity>.<measure>, at least one." }),
369
+ .annotate({
370
+ description: "<entity>.<measure>, or a measure of the model by its name, such as plan_attainment; " +
371
+ "at least one, at most 30 of both together.",
372
+ }),
370
373
  dimensions: Schema.optionalKey(Schema.Array(Schema.String).check(Schema.isMaxLength(AGGREGATE_LIMITS.dimensions))).annotate({
371
374
  description: "<entity>.<property>, or a route from a measure's entity: " +
372
375
  "<entity>.<relationship>[.<relationship>].<property>.",
@@ -408,7 +411,9 @@ export const EntityAggregateRequest = closedStruct({
408
411
  /** SegmentStream's answer to an aggregate, which the service forwards unchanged. */
409
412
  export const AggregateResult = Schema.Struct({
410
413
  fields: Schema.Array(Schema.Struct({
411
- name: Schema.String.annotate({ description: "A dimension identity or <entity>.<measure>." }),
414
+ name: Schema.String.annotate({
415
+ description: "A dimension identity, <entity>.<measure>, or the name of a measure of the model.",
416
+ }),
412
417
  label: Schema.String,
413
418
  type: Schema.String,
414
419
  unit: Schema.optionalKey(Schema.String),
@@ -425,6 +430,10 @@ export const AggregateResult = Schema.Struct({
425
430
  description: "The date or timestamp dimension a balance measure was grouped by or filtered to one " +
426
431
  "day of.",
427
432
  }),
433
+ inputs: Schema.optionalKey(Schema.Array(Schema.String)).annotate({
434
+ description: "The entity measures a measure of the model reads, <entity>.<measure>, in declaration " +
435
+ "order; present only on such a measure.",
436
+ }),
428
437
  })),
429
438
  rows: Schema.Array(Schema.Record(Schema.String, Schema.NullOr(Schema.Union([Schema.String, Schema.Boolean])))),
430
439
  truncated: Schema.Boolean,
@@ -253,6 +253,10 @@ function checkMeasures(input) {
253
253
  !["decimal", "currency", "percent"].includes(measure.format)) {
254
254
  report("invalid-measure", field, "Invalid measure format.");
255
255
  }
256
+ if (measure.align !== undefined) {
257
+ report("invalid-measure", field, "Entity measures take no align; a measure that aligns the properties of several " +
258
+ "entities belongs in defineOntology({ measures }).");
259
+ }
256
260
  let remaining = 10_000;
257
261
  /** The balance properties of the measure's aggregates and of the measures it references. */
258
262
  const balances = new Set();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream-cloud/ontology",
3
- "version": "0.21.0",
3
+ "version": "0.23.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",