@segmentstream/ontology 0.106.2 → 0.106.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.106.3
4
+
5
+ The ontology service reads less from the workspace database. Its status endpoint reads the hash of
6
+ the applied ontology instead of the whole applied manifest, and warehouse replication skips its
7
+ bookkeeping reads when it has nothing to deliver. Responses and replication results are unchanged.
8
+
3
9
  ## 0.106.2
4
10
 
5
11
  `emailReplyMessage` from `@segmentstream/ontology/email` takes two more options:
@@ -47,6 +47,12 @@ export type ApplyMigrationError = MigrationRefused | CatalogMismatch | CatalogIn
47
47
  * decoded with the manifest contract, so fields this SDK does not know are dropped.
48
48
  */
49
49
  export declare const readCatalog: (input: CatalogInput) => Effect.Effect<OntologyManifest | undefined, CatalogInvalid | SqlError>;
50
+ /**
51
+ * The hash recorded by the last applied migration, or undefined when none was applied. A migration
52
+ * writes it in the statement that writes the manifest it names, so this reads the hash alone, not
53
+ * the manifest that `readCatalog` reads and checks, which is megabytes for a large ontology.
54
+ */
55
+ export declare const readCatalogHash: (input: CatalogInput) => Effect.Effect<string | undefined, CatalogInvalid | SqlError>;
50
56
  /**
51
57
  * The storage the catalog records as kept: what removals retained, which declaring the entity or
52
58
  * @legacy ontology.pre-upgrade-database-tolerance
@@ -36,13 +36,12 @@ const DEFAULT_STATEMENT_TIMEOUT = "5 minutes";
36
36
  const DEFAULT_LOCK_TIMEOUT = "5 seconds";
37
37
  const Presence = Schema.Struct({ present: Schema.Boolean });
38
38
  const CatalogRow = Schema.Struct({ hash: Schema.String, manifest: Schema.String });
39
+ const CatalogHashRow = Schema.Struct({ hash: Schema.String });
39
40
  const RetainedRow = Schema.Struct({ retained: Schema.NullOr(Schema.String) });
40
41
  const decodeManifest = decodeManifestDocument;
42
+ const catalogPresent = (sql) => sql.unsafe(`SELECT to_regclass($1) IS NOT NULL AS present`, [CATALOG]).pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.Array(Presence))), Effect.map((presence) => presence[0]?.present === true));
41
43
  const readCatalogRow = (sql, options) => Effect.gen(function* () {
42
- const presence = yield* sql
43
- .unsafe(`SELECT to_regclass($1) IS NOT NULL AS present`, [CATALOG])
44
- .pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.Array(Presence))));
45
- if (presence[0]?.present !== true) {
44
+ if (!(yield* catalogPresent(sql))) {
46
45
  return undefined;
47
46
  }
48
47
  const lock = options.forUpdate ? "FOR UPDATE" : "";
@@ -82,6 +81,20 @@ export const readCatalog = (input) => Effect.gen(function* () {
82
81
  }
83
82
  return manifest;
84
83
  }).pipe(Effect.withSpan("ontology.migrate.readCatalog"));
84
+ /**
85
+ * The hash recorded by the last applied migration, or undefined when none was applied. A migration
86
+ * writes it in the statement that writes the manifest it names, so this reads the hash alone, not
87
+ * the manifest that `readCatalog` reads and checks, which is megabytes for a large ontology.
88
+ */
89
+ export const readCatalogHash = (input) => Effect.gen(function* () {
90
+ if (!(yield* catalogPresent(input.sql))) {
91
+ return undefined;
92
+ }
93
+ const rows = yield* input.sql
94
+ .unsafe(`SELECT "hash" FROM ${CATALOG} WHERE "id"`)
95
+ .pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.Array(CatalogHashRow))));
96
+ return rows[0]?.hash;
97
+ }).pipe(Effect.catchTag("SchemaError", (cause) => Effect.fail(new CatalogInvalid({ message: "The _ontology catalog has an unexpected shape.", cause }))), Effect.withSpan("ontology.migrate.readCatalogHash"));
85
98
  /**
86
99
  * The storage the catalog records as kept: what removals retained, which declaring the entity or
87
100
  * @legacy ontology.pre-upgrade-database-tolerance
@@ -112,8 +112,10 @@ export declare const replaceDestroyedShadowDestination: (sql: SqlClient.SqlClien
112
112
  export declare const installShadowEpoch: (sql: SqlClient.SqlClient, id: string, owner: string) => Effect.Effect<boolean, import("effect/unstable/sql/SqlError").SqlError, never>;
113
113
  /** Strict warehouse job completion, not Pub/Sub acceptance, is the publication prerequisite. */
114
114
  export declare const shadowPublicationReady: (sql: SqlClient.SqlClient, epoch: ShadowEpoch, owner: string) => Effect.Effect<boolean, import("effect/unstable/sql/SqlError").SqlError, never>;
115
- export declare const unfinishedRetiredSegments: (sql: SqlClient.SqlClient) => import("effect/unstable/sql/Statement").Statement<ShadowSegment>;
116
- export declare const settleRetiredSegment: (sql: SqlClient.SqlClient, segment: ShadowSegment, committed: boolean) => import("effect/unstable/sql/Statement").Statement<object>;
115
+ /** A retired segment is settled by its load job alone; its events are never loaded again. */
116
+ export type RetiredShadowSegment = Pick<ShadowSegment, "id" | "job_id">;
117
+ export declare const unfinishedRetiredSegments: (sql: SqlClient.SqlClient) => import("effect/unstable/sql/Statement").Statement<RetiredShadowSegment>;
118
+ export declare const settleRetiredSegment: (sql: SqlClient.SqlClient, segment: RetiredShadowSegment, committed: boolean) => import("effect/unstable/sql/Statement").Statement<object>;
117
119
  /** Restriction-only refresh is independent of build leases and cannot confer publication eligibility. */
118
120
  export declare const shadowPolicyViewOwnership: (sql: SqlClient.SqlClient, generation: string, source: string, exclusions: unknown) => Effect.Effect<boolean, import("effect/unstable/sql/SqlError").SqlError, never>;
119
121
  export declare const recordShadowPolicyViews: (sql: SqlClient.SqlClient, generation: string, source: string, exclusions: unknown) => import("effect/unstable/sql/Statement").Statement<object>;
@@ -573,7 +573,7 @@ export const shadowPublicationReady = (sql, epoch, owner) => sql
573
573
  AND NOT EXISTS(SELECT 1 FROM _ontology_replication_delivery WHERE generation=e.id
574
574
  AND revision<=e.boundary AND committed_at IS NULL AND suppressed_at IS NULL)) AS ready`, [epoch.id, owner, epoch.token, epoch.boundary])
575
575
  .pipe(Effect.map((rows) => rows[0].ready));
576
- export const unfinishedRetiredSegments = (sql) => sql.unsafe(`SELECT ${segmentFields} FROM _ontology_replication_segments
576
+ export const unfinishedRetiredSegments = (sql) => sql.unsafe(`SELECT id::text, job_id FROM _ontology_replication_segments
577
577
  WHERE state='pending' AND created_at<now()-interval '7 days' AND epoch IN (
578
578
  SELECT id FROM _ontology_replication_epochs WHERE state IN ('retired','cancelled'))
579
579
  ORDER BY created_at,id LIMIT 10`);
@@ -166,6 +166,10 @@ export const pendingReplicationEvents = (sql, target, options = {}) => Effect.ge
166
166
  ORDER BY revision,event_id LIMIT ${REPLICATION_BATCH_SIZE}`, shadow
167
167
  ? [target.generation, options.bulk ?? false, options.through ?? null]
168
168
  : [target.generation]);
169
+ // An idle pass ends here: the bindings and the export policy below shape only queued events.
170
+ if (references.length === 0) {
171
+ return [];
172
+ }
169
173
  const groups = new Map();
170
174
  for (const reference of references) {
171
175
  if (reference.payload) {
@@ -9,7 +9,7 @@ import { attachmentRuntime } from "../storage/attachments.js";
9
9
  import { blobRuntime } from "../storage/sessions.js";
10
10
  import { FileAccess, unavailableFiles } from "../storage/file-access.js";
11
11
  import { validateDeploymentStorage } from "../storage/provider.js";
12
- import { readCatalog } from "../migrate/apply.js";
12
+ import { readCatalogHash } from "../migrate/apply.js";
13
13
  import { RuntimeDeployment } from "./deployment.js";
14
14
  import { NotInvited, OntologyBackend, } from "../service/runtime-contract.js";
15
15
  import { WorkflowRecoveryUnavailable } from "./errors.js";
@@ -228,7 +228,7 @@ export function backendLayer(ontology, blobStorage, workflowStarter, deployment,
228
228
  : withSql(checkSignIn(ontology, email)),
229
229
  resolveApiKey: (key) => withSql(resolvePrincipalByApiKey(ontology, Redacted.value(key))).pipe(Effect.catchTag("PrincipalRejected", rejectedAs("api_key"))),
230
230
  resolveApiKeyCredential: (key) => withSql(resolveApiKeyCredential(ontology, Redacted.value(key))).pipe(Effect.catchTag("PrincipalRejected", rejectedAs("api_key"))),
231
- appliedHash: readCatalog({ sql }).pipe(Effect.map((manifest) => manifest?.hash), Effect.orDie),
231
+ appliedHash: readCatalogHash({ sql }).pipe(Effect.orDie),
232
232
  replicationEnabled: Effect.gen(function* () {
233
233
  const [presence] = yield* sql.unsafe("SELECT to_regclass('_ontology_replication') IS NOT NULL AS present");
234
234
  if (!presence?.present) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.106.2",
3
+ "version": "0.106.3",
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",