@segmentstream/ontology 0.168.0 → 0.169.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/CHANGELOG.md +18 -0
- package/dist/agent-draft.js +2 -0
- package/dist/analyze.js +13 -0
- package/dist/diagnostics.d.ts +3 -3
- package/dist/diagnostics.js +1 -0
- package/dist/entity.d.ts +9 -0
- package/dist/manifest.d.ts +33 -0
- package/dist/manifest.js +16 -1
- package/dist/migrate/apply.js +5 -1
- package/dist/migrate/model.d.ts +9 -0
- package/dist/migrate/preview.js +1 -0
- package/dist/replication/erasure-schema.d.ts +11 -0
- package/dist/replication/erasure-schema.js +23 -0
- package/dist/replication/erasure.d.ts +56 -0
- package/dist/replication/erasure.js +127 -0
- package/dist/replication/schema.js +21 -0
- package/dist/service/schemas.d.ts +4 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.169.0
|
|
4
|
+
|
|
5
|
+
An entity can declare `eraseFromWarehouseOnDelete: true`: deleting one of its records then erases
|
|
6
|
+
the record's replicated history from the workspace warehouse, in every copy the platform keeps,
|
|
7
|
+
leaving only the content-free fact of its deletion. Every deletion counts, whether by an action, a
|
|
8
|
+
cascade or a retention purge, and so do the deletions recorded before the setting was turned on.
|
|
9
|
+
Erasure runs daily; the record is gone from everything the workspace can query within about two
|
|
10
|
+
days. It is off by default. The built-in `agent_draft` turns it on, so drafts the chat retention
|
|
11
|
+
deletes no longer remain readable in the warehouse.
|
|
12
|
+
|
|
13
|
+
## 0.168.1
|
|
14
|
+
|
|
15
|
+
The replication schema adds a content-free erasure journal, `_ontology_replication_erasures`.
|
|
16
|
+
Each row is a deleted record whose replicated history the platform erases from the workspace
|
|
17
|
+
warehouse, together with the audit of that erasure: when the record was deleted, when its history
|
|
18
|
+
left the warehouse, by which warehouse job, how many rows that job removed, and when a later check
|
|
19
|
+
found nothing left. This release queues nothing yet; preview databases start with an empty journal.
|
|
20
|
+
|
|
3
21
|
## 0.168.0
|
|
4
22
|
|
|
5
23
|
A mailbox in `chat` takes `signature: true` beside its connection: the ontology's replies to mail,
|
package/dist/agent-draft.js
CHANGED
|
@@ -162,6 +162,8 @@ export const AgentDraft = entity("agent_draft", {
|
|
|
162
162
|
id: "ent_agent_review",
|
|
163
163
|
// @legacy ontology.agent-review-entity-name
|
|
164
164
|
renamedFrom: "agent_review",
|
|
165
|
+
// A draft the chat retention deletes takes its text, notes and reasons out of the warehouse too.
|
|
166
|
+
eraseFromWarehouseOnDelete: true,
|
|
165
167
|
label: "Agent draft",
|
|
166
168
|
plural: "Agent drafts",
|
|
167
169
|
title: "draft",
|
package/dist/analyze.js
CHANGED
|
@@ -2298,6 +2298,7 @@ function resolveEntity(candidate, scope, resolvedProperties) {
|
|
|
2298
2298
|
report("invalid-guide", `${name}.guide`, "must be a relative path to a Markdown file, such as ./lead.md.");
|
|
2299
2299
|
}
|
|
2300
2300
|
checkWrites(name, definition, report);
|
|
2301
|
+
checkErasure(name, definition, report);
|
|
2301
2302
|
const properties = resolvedProperties ?? resolveProperties(candidate, scope);
|
|
2302
2303
|
const relationships = resolveRelationships(candidate, scope);
|
|
2303
2304
|
if (isBlank(definition.title)) {
|
|
@@ -2337,6 +2338,18 @@ function writesOf(candidate) {
|
|
|
2337
2338
|
? "actions"
|
|
2338
2339
|
: "generated";
|
|
2339
2340
|
}
|
|
2341
|
+
function checkErasure(name, definition, report) {
|
|
2342
|
+
const erase = definition.eraseFromWarehouseOnDelete;
|
|
2343
|
+
if (erase === undefined) {
|
|
2344
|
+
return;
|
|
2345
|
+
}
|
|
2346
|
+
if (typeof erase !== "boolean") {
|
|
2347
|
+
report("invalid-erasure", `${name}.eraseFromWarehouseOnDelete`, "eraseFromWarehouseOnDelete is true or false.");
|
|
2348
|
+
}
|
|
2349
|
+
else if (erase && definition.source !== undefined) {
|
|
2350
|
+
report("invalid-erasure", `${name}.eraseFromWarehouseOnDelete`, "a warehouse-backed entity's records are the warehouse's own; remove eraseFromWarehouseOnDelete.");
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2340
2353
|
function checkWrites(name, definition, report) {
|
|
2341
2354
|
const writes = definition.writes;
|
|
2342
2355
|
if (writes === undefined) {
|
package/dist/diagnostics.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
-
export declare const DIAGNOSTIC_CODES: readonly ["invalid-source", "missing-column", "invalid-column", "invalid-scope", "invalid-scope-path", "scope-reads-all", "scope-unused", "scope-member-overlap", "source-with-actions", "source-with-index", "invalid-metric", "unknown-metric", "circular-metric", "invalid-join", "missing-join", "invalid-entity", "invalid-property", "invalid-relationship", "invalid-actions", "invalid-action", "invalid-action-input", "invalid-role", "invalid-name", "reserved-name", "duplicate-name", "invalid-entity-id", "duplicate-entity-id", "missing-label", "missing-description", "invalid-title", "invalid-options", "invalid-default", "invalid-constraint", "unknown-relationship-target", "inverse-collision", "invalid-on-delete", "invalid-phrase", "invalid-index", "unknown-reference", "invalid-renamed-from", "earlier-name", "invalid-guide", "invalid-writes", "generated-update-empty", "managed-without-effect", "missing-permissions", "invalid-permission", "unknown-permission", "removed-allow", "unreachable-action", "wildcard-permission", "invalid-sign-in", "invalid-inbox", "invalid-chat", "invalid-connection", "invalid-feedback", "invalid-discovery", "invalid-email", "feedback-unanswerable", "feedback-unreportable", "agent-tools-implicit", "named-secret-deprecated", "unknown-connection", "unused-connection", "invalid-prepare", "invalid-tool-name", "invalid-toolset", "toolset-lock-mismatch", "invalid-alignment", "single-entity-model-metric", "invalid-workflow", "workflow_role_unknown", "workflow_role_is_member", "workflow_role_grants_nothing", "workflow_role_wildcard", "workflow_role_scoped", "workflow_cron_never_fires", "workflow_input_invalid", "workflow_cron_invalid", "workflow_run_missing", "invalid-trigger", "trigger_action_unknown", "trigger_function_async", "trigger_runs_invalid", "workflow_trigger_limit_exceeded", "workflow_webhook_duplicate", "workflow_webhook_input_invalid", "workflow_webhook_mixed_triggers", "workflow_webhook_signature_invalid", "workflow_webhook_verification_invalid", "workflow_webhook_headers_invalid", "workflow_webhook_limit_exceeded"];
|
|
2
|
+
export declare const DIAGNOSTIC_CODES: readonly ["invalid-source", "missing-column", "invalid-column", "invalid-scope", "invalid-scope-path", "scope-reads-all", "scope-unused", "scope-member-overlap", "source-with-actions", "source-with-index", "invalid-metric", "unknown-metric", "circular-metric", "invalid-join", "missing-join", "invalid-entity", "invalid-property", "invalid-relationship", "invalid-actions", "invalid-action", "invalid-action-input", "invalid-role", "invalid-name", "reserved-name", "duplicate-name", "invalid-entity-id", "duplicate-entity-id", "missing-label", "missing-description", "invalid-title", "invalid-options", "invalid-default", "invalid-constraint", "unknown-relationship-target", "inverse-collision", "invalid-on-delete", "invalid-phrase", "invalid-index", "unknown-reference", "invalid-renamed-from", "earlier-name", "invalid-guide", "invalid-writes", "invalid-erasure", "generated-update-empty", "managed-without-effect", "missing-permissions", "invalid-permission", "unknown-permission", "removed-allow", "unreachable-action", "wildcard-permission", "invalid-sign-in", "invalid-inbox", "invalid-chat", "invalid-connection", "invalid-feedback", "invalid-discovery", "invalid-email", "feedback-unanswerable", "feedback-unreportable", "agent-tools-implicit", "named-secret-deprecated", "unknown-connection", "unused-connection", "invalid-prepare", "invalid-tool-name", "invalid-toolset", "toolset-lock-mismatch", "invalid-alignment", "single-entity-model-metric", "invalid-workflow", "workflow_role_unknown", "workflow_role_is_member", "workflow_role_grants_nothing", "workflow_role_wildcard", "workflow_role_scoped", "workflow_cron_never_fires", "workflow_input_invalid", "workflow_cron_invalid", "workflow_run_missing", "invalid-trigger", "trigger_action_unknown", "trigger_function_async", "trigger_runs_invalid", "workflow_trigger_limit_exceeded", "workflow_webhook_duplicate", "workflow_webhook_input_invalid", "workflow_webhook_mixed_triggers", "workflow_webhook_signature_invalid", "workflow_webhook_verification_invalid", "workflow_webhook_headers_invalid", "workflow_webhook_limit_exceeded"];
|
|
3
3
|
export type DiagnosticCode = (typeof DIAGNOSTIC_CODES)[number];
|
|
4
4
|
/** A problem `check` found in an ontology definition. */
|
|
5
5
|
export declare const Diagnostic: Schema.Struct<{
|
|
6
6
|
readonly severity: Schema.Literals<readonly ["error", "warning", "info"]>;
|
|
7
|
-
readonly code: Schema.Literals<readonly ["invalid-source", "missing-column", "invalid-column", "invalid-scope", "invalid-scope-path", "scope-reads-all", "scope-unused", "scope-member-overlap", "source-with-actions", "source-with-index", "invalid-metric", "unknown-metric", "circular-metric", "invalid-join", "missing-join", "invalid-entity", "invalid-property", "invalid-relationship", "invalid-actions", "invalid-action", "invalid-action-input", "invalid-role", "invalid-name", "reserved-name", "duplicate-name", "invalid-entity-id", "duplicate-entity-id", "missing-label", "missing-description", "invalid-title", "invalid-options", "invalid-default", "invalid-constraint", "unknown-relationship-target", "inverse-collision", "invalid-on-delete", "invalid-phrase", "invalid-index", "unknown-reference", "invalid-renamed-from", "earlier-name", "invalid-guide", "invalid-writes", "generated-update-empty", "managed-without-effect", "missing-permissions", "invalid-permission", "unknown-permission", "removed-allow", "unreachable-action", "wildcard-permission", "invalid-sign-in", "invalid-inbox", "invalid-chat", "invalid-connection", "invalid-feedback", "invalid-discovery", "invalid-email", "feedback-unanswerable", "feedback-unreportable", "agent-tools-implicit", "named-secret-deprecated", "unknown-connection", "unused-connection", "invalid-prepare", "invalid-tool-name", "invalid-toolset", "toolset-lock-mismatch", "invalid-alignment", "single-entity-model-metric", "invalid-workflow", "workflow_role_unknown", "workflow_role_is_member", "workflow_role_grants_nothing", "workflow_role_wildcard", "workflow_role_scoped", "workflow_cron_never_fires", "workflow_input_invalid", "workflow_cron_invalid", "workflow_run_missing", "invalid-trigger", "trigger_action_unknown", "trigger_function_async", "trigger_runs_invalid", "workflow_trigger_limit_exceeded", "workflow_webhook_duplicate", "workflow_webhook_input_invalid", "workflow_webhook_mixed_triggers", "workflow_webhook_signature_invalid", "workflow_webhook_verification_invalid", "workflow_webhook_headers_invalid", "workflow_webhook_limit_exceeded"]>;
|
|
7
|
+
readonly code: Schema.Literals<readonly ["invalid-source", "missing-column", "invalid-column", "invalid-scope", "invalid-scope-path", "scope-reads-all", "scope-unused", "scope-member-overlap", "source-with-actions", "source-with-index", "invalid-metric", "unknown-metric", "circular-metric", "invalid-join", "missing-join", "invalid-entity", "invalid-property", "invalid-relationship", "invalid-actions", "invalid-action", "invalid-action-input", "invalid-role", "invalid-name", "reserved-name", "duplicate-name", "invalid-entity-id", "duplicate-entity-id", "missing-label", "missing-description", "invalid-title", "invalid-options", "invalid-default", "invalid-constraint", "unknown-relationship-target", "inverse-collision", "invalid-on-delete", "invalid-phrase", "invalid-index", "unknown-reference", "invalid-renamed-from", "earlier-name", "invalid-guide", "invalid-writes", "invalid-erasure", "generated-update-empty", "managed-without-effect", "missing-permissions", "invalid-permission", "unknown-permission", "removed-allow", "unreachable-action", "wildcard-permission", "invalid-sign-in", "invalid-inbox", "invalid-chat", "invalid-connection", "invalid-feedback", "invalid-discovery", "invalid-email", "feedback-unanswerable", "feedback-unreportable", "agent-tools-implicit", "named-secret-deprecated", "unknown-connection", "unused-connection", "invalid-prepare", "invalid-tool-name", "invalid-toolset", "toolset-lock-mismatch", "invalid-alignment", "single-entity-model-metric", "invalid-workflow", "workflow_role_unknown", "workflow_role_is_member", "workflow_role_grants_nothing", "workflow_role_wildcard", "workflow_role_scoped", "workflow_cron_never_fires", "workflow_input_invalid", "workflow_cron_invalid", "workflow_run_missing", "invalid-trigger", "trigger_action_unknown", "trigger_function_async", "trigger_runs_invalid", "workflow_trigger_limit_exceeded", "workflow_webhook_duplicate", "workflow_webhook_input_invalid", "workflow_webhook_mixed_triggers", "workflow_webhook_signature_invalid", "workflow_webhook_verification_invalid", "workflow_webhook_headers_invalid", "workflow_webhook_limit_exceeded"]>;
|
|
8
8
|
/** Where the problem is, for example `lead.properties.status.options[1]`. */
|
|
9
9
|
readonly path: Schema.String;
|
|
10
10
|
readonly message: Schema.String;
|
|
@@ -14,7 +14,7 @@ declare const InvalidOntology_base: Schema.Class<InvalidOntology, Schema.TaggedS
|
|
|
14
14
|
readonly message: Schema.String;
|
|
15
15
|
readonly diagnostics: Schema.$Array<Schema.Struct<{
|
|
16
16
|
readonly severity: Schema.Literals<readonly ["error", "warning", "info"]>;
|
|
17
|
-
readonly code: Schema.Literals<readonly ["invalid-source", "missing-column", "invalid-column", "invalid-scope", "invalid-scope-path", "scope-reads-all", "scope-unused", "scope-member-overlap", "source-with-actions", "source-with-index", "invalid-metric", "unknown-metric", "circular-metric", "invalid-join", "missing-join", "invalid-entity", "invalid-property", "invalid-relationship", "invalid-actions", "invalid-action", "invalid-action-input", "invalid-role", "invalid-name", "reserved-name", "duplicate-name", "invalid-entity-id", "duplicate-entity-id", "missing-label", "missing-description", "invalid-title", "invalid-options", "invalid-default", "invalid-constraint", "unknown-relationship-target", "inverse-collision", "invalid-on-delete", "invalid-phrase", "invalid-index", "unknown-reference", "invalid-renamed-from", "earlier-name", "invalid-guide", "invalid-writes", "generated-update-empty", "managed-without-effect", "missing-permissions", "invalid-permission", "unknown-permission", "removed-allow", "unreachable-action", "wildcard-permission", "invalid-sign-in", "invalid-inbox", "invalid-chat", "invalid-connection", "invalid-feedback", "invalid-discovery", "invalid-email", "feedback-unanswerable", "feedback-unreportable", "agent-tools-implicit", "named-secret-deprecated", "unknown-connection", "unused-connection", "invalid-prepare", "invalid-tool-name", "invalid-toolset", "toolset-lock-mismatch", "invalid-alignment", "single-entity-model-metric", "invalid-workflow", "workflow_role_unknown", "workflow_role_is_member", "workflow_role_grants_nothing", "workflow_role_wildcard", "workflow_role_scoped", "workflow_cron_never_fires", "workflow_input_invalid", "workflow_cron_invalid", "workflow_run_missing", "invalid-trigger", "trigger_action_unknown", "trigger_function_async", "trigger_runs_invalid", "workflow_trigger_limit_exceeded", "workflow_webhook_duplicate", "workflow_webhook_input_invalid", "workflow_webhook_mixed_triggers", "workflow_webhook_signature_invalid", "workflow_webhook_verification_invalid", "workflow_webhook_headers_invalid", "workflow_webhook_limit_exceeded"]>;
|
|
17
|
+
readonly code: Schema.Literals<readonly ["invalid-source", "missing-column", "invalid-column", "invalid-scope", "invalid-scope-path", "scope-reads-all", "scope-unused", "scope-member-overlap", "source-with-actions", "source-with-index", "invalid-metric", "unknown-metric", "circular-metric", "invalid-join", "missing-join", "invalid-entity", "invalid-property", "invalid-relationship", "invalid-actions", "invalid-action", "invalid-action-input", "invalid-role", "invalid-name", "reserved-name", "duplicate-name", "invalid-entity-id", "duplicate-entity-id", "missing-label", "missing-description", "invalid-title", "invalid-options", "invalid-default", "invalid-constraint", "unknown-relationship-target", "inverse-collision", "invalid-on-delete", "invalid-phrase", "invalid-index", "unknown-reference", "invalid-renamed-from", "earlier-name", "invalid-guide", "invalid-writes", "invalid-erasure", "generated-update-empty", "managed-without-effect", "missing-permissions", "invalid-permission", "unknown-permission", "removed-allow", "unreachable-action", "wildcard-permission", "invalid-sign-in", "invalid-inbox", "invalid-chat", "invalid-connection", "invalid-feedback", "invalid-discovery", "invalid-email", "feedback-unanswerable", "feedback-unreportable", "agent-tools-implicit", "named-secret-deprecated", "unknown-connection", "unused-connection", "invalid-prepare", "invalid-tool-name", "invalid-toolset", "toolset-lock-mismatch", "invalid-alignment", "single-entity-model-metric", "invalid-workflow", "workflow_role_unknown", "workflow_role_is_member", "workflow_role_grants_nothing", "workflow_role_wildcard", "workflow_role_scoped", "workflow_cron_never_fires", "workflow_input_invalid", "workflow_cron_invalid", "workflow_run_missing", "invalid-trigger", "trigger_action_unknown", "trigger_function_async", "trigger_runs_invalid", "workflow_trigger_limit_exceeded", "workflow_webhook_duplicate", "workflow_webhook_input_invalid", "workflow_webhook_mixed_triggers", "workflow_webhook_signature_invalid", "workflow_webhook_verification_invalid", "workflow_webhook_headers_invalid", "workflow_webhook_limit_exceeded"]>;
|
|
18
18
|
/** Where the problem is, for example `lead.properties.status.options[1]`. */
|
|
19
19
|
readonly path: Schema.String;
|
|
20
20
|
readonly message: Schema.String;
|
package/dist/diagnostics.js
CHANGED
package/dist/entity.d.ts
CHANGED
|
@@ -179,6 +179,15 @@ export interface EntityDefinition<P extends Properties, R extends Relationships,
|
|
|
179
179
|
* Reads stay the same. Stored entities only.
|
|
180
180
|
*/
|
|
181
181
|
readonly writes?: EntityWrites;
|
|
182
|
+
/**
|
|
183
|
+
* Whether deleting a record also erases its replicated history from the workspace warehouse:
|
|
184
|
+
* every earlier value the warehouse holds for it, in each copy the platform keeps. The record's
|
|
185
|
+
* deletion itself stays, with no content. Erasure runs daily; see the replication docs for its
|
|
186
|
+
* timing. Every deletion counts, by an action, a cascade or a retention purge, as do the
|
|
187
|
+
* deletions recorded before it was turned on. Off by default: the warehouse keeps a deleted
|
|
188
|
+
* record's history for queries over the past. Stored entities only.
|
|
189
|
+
*/
|
|
190
|
+
readonly eraseFromWarehouseOnDelete?: boolean;
|
|
182
191
|
}
|
|
183
192
|
/** Who writes an entity's records; see {@link EntityDefinition.writes}. */
|
|
184
193
|
export type EntityWrites = "generated" | "actions";
|
package/dist/manifest.d.ts
CHANGED
|
@@ -110,6 +110,9 @@ import { type ResolvedGuest, type ResolvedSignInOptions, type SignInPolicy } fro
|
|
|
110
110
|
* `private` without members. Older readers ignore it and register no chat without members.
|
|
111
111
|
* Within version 16, a mailbox in `chat` may carry `signature: true`, written only when declared:
|
|
112
112
|
* the ontology's replies to mail end with the mailbox's own signature. Older readers ignore it.
|
|
113
|
+
* Within version 16, an entity may carry `erase_from_warehouse_on_delete: true`, written only when
|
|
114
|
+
* declared: deleting a record erases its replicated history from the warehouse. The SDK's own
|
|
115
|
+
* replication capture acts on it; readers ignore it.
|
|
113
116
|
*/
|
|
114
117
|
export declare const MANIFEST_VERSION = 16;
|
|
115
118
|
/** The phrase used for a direction the author did not phrase. */
|
|
@@ -418,6 +421,11 @@ export declare const EntityManifest: Schema.Struct<{
|
|
|
418
421
|
* writes and its `permissions` hold only `read`. Absent means the generated writes exist.
|
|
419
422
|
*/
|
|
420
423
|
readonly writes: Schema.optionalKey<Schema.Literal<"actions">>;
|
|
424
|
+
/**
|
|
425
|
+
* `true` when deleting a record erases its replicated history from the warehouse; written only
|
|
426
|
+
* then. The SDK's replication capture acts on it; the platform does not read it.
|
|
427
|
+
*/
|
|
428
|
+
readonly eraseFromWarehouseOnDelete: Schema.optionalKey<Schema.Literal<true>>;
|
|
421
429
|
}>;
|
|
422
430
|
export type EntityManifest = typeof EntityManifest.Type;
|
|
423
431
|
/**
|
|
@@ -1151,9 +1159,15 @@ export declare const OntologyManifestWire: Schema.encodeKeys<Schema.Struct<{
|
|
|
1151
1159
|
* writes and its `permissions` hold only `read`. Absent means the generated writes exist.
|
|
1152
1160
|
*/
|
|
1153
1161
|
readonly writes: Schema.optionalKey<Schema.Literal<"actions">>;
|
|
1162
|
+
/**
|
|
1163
|
+
* `true` when deleting a record erases its replicated history from the warehouse; written only
|
|
1164
|
+
* then. The SDK's replication capture acts on it; the platform does not read it.
|
|
1165
|
+
*/
|
|
1166
|
+
readonly eraseFromWarehouseOnDelete: Schema.optionalKey<Schema.Literal<true>>;
|
|
1154
1167
|
}>, {
|
|
1155
1168
|
readonly builtIn: "built_in";
|
|
1156
1169
|
readonly renamedFrom: "renamed_from";
|
|
1170
|
+
readonly eraseFromWarehouseOnDelete: "erase_from_warehouse_on_delete";
|
|
1157
1171
|
}>>;
|
|
1158
1172
|
readonly roles: Schema.$Array<Schema.encodeKeys<Schema.Struct<{
|
|
1159
1173
|
readonly name: Schema.String;
|
|
@@ -1650,6 +1664,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
|
|
|
1650
1664
|
readonly balanceOver?: string | undefined;
|
|
1651
1665
|
}[] | undefined;
|
|
1652
1666
|
readonly guide?: string | undefined;
|
|
1667
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
1653
1668
|
}[];
|
|
1654
1669
|
readonly chat?: {
|
|
1655
1670
|
readonly telegram?: {
|
|
@@ -2027,6 +2042,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
|
|
|
2027
2042
|
readonly balanceOver?: string | undefined;
|
|
2028
2043
|
}[] | undefined;
|
|
2029
2044
|
readonly guide?: string | undefined;
|
|
2045
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
2030
2046
|
}[];
|
|
2031
2047
|
readonly chat?: {
|
|
2032
2048
|
readonly telegram?: {
|
|
@@ -2404,6 +2420,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
|
|
|
2404
2420
|
}[] | undefined;
|
|
2405
2421
|
readonly guide?: string | undefined;
|
|
2406
2422
|
readonly renamed_from?: string | undefined;
|
|
2423
|
+
readonly erase_from_warehouse_on_delete?: true | undefined;
|
|
2407
2424
|
}[];
|
|
2408
2425
|
readonly chat?: {
|
|
2409
2426
|
readonly telegram?: {
|
|
@@ -2781,6 +2798,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
|
|
|
2781
2798
|
readonly balanceOver?: string | undefined;
|
|
2782
2799
|
}[] | undefined;
|
|
2783
2800
|
readonly guide?: string | undefined;
|
|
2801
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
2784
2802
|
}[];
|
|
2785
2803
|
readonly chat?: {
|
|
2786
2804
|
readonly telegram?: {
|
|
@@ -3185,9 +3203,15 @@ export declare const OntologyManifest: Schema.declareConstructor<{
|
|
|
3185
3203
|
* writes and its `permissions` hold only `read`. Absent means the generated writes exist.
|
|
3186
3204
|
*/
|
|
3187
3205
|
readonly writes: Schema.optionalKey<Schema.Literal<"actions">>;
|
|
3206
|
+
/**
|
|
3207
|
+
* `true` when deleting a record erases its replicated history from the warehouse; written only
|
|
3208
|
+
* then. The SDK's replication capture acts on it; the platform does not read it.
|
|
3209
|
+
*/
|
|
3210
|
+
readonly eraseFromWarehouseOnDelete: Schema.optionalKey<Schema.Literal<true>>;
|
|
3188
3211
|
}>, {
|
|
3189
3212
|
readonly builtIn: "built_in";
|
|
3190
3213
|
readonly renamedFrom: "renamed_from";
|
|
3214
|
+
readonly eraseFromWarehouseOnDelete: "erase_from_warehouse_on_delete";
|
|
3191
3215
|
}>>;
|
|
3192
3216
|
readonly roles: Schema.$Array<Schema.encodeKeys<Schema.Struct<{
|
|
3193
3217
|
readonly name: Schema.String;
|
|
@@ -3718,6 +3742,11 @@ export declare const OntologyManifest: Schema.declareConstructor<{
|
|
|
3718
3742
|
* writes and its `permissions` hold only `read`. Absent means the generated writes exist.
|
|
3719
3743
|
*/
|
|
3720
3744
|
readonly writes: Schema.optionalKey<Schema.Literal<"actions">>;
|
|
3745
|
+
/**
|
|
3746
|
+
* `true` when deleting a record erases its replicated history from the warehouse; written only
|
|
3747
|
+
* then. The SDK's replication capture acts on it; the platform does not read it.
|
|
3748
|
+
*/
|
|
3749
|
+
readonly eraseFromWarehouseOnDelete: Schema.optionalKey<Schema.Literal<true>>;
|
|
3721
3750
|
}>>;
|
|
3722
3751
|
readonly roles: Schema.$Array<Schema.Struct<{
|
|
3723
3752
|
readonly name: Schema.String;
|
|
@@ -4188,6 +4217,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
|
|
|
4188
4217
|
readonly balanceOver?: string | undefined;
|
|
4189
4218
|
}[] | undefined;
|
|
4190
4219
|
readonly guide?: string | undefined;
|
|
4220
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
4191
4221
|
}[];
|
|
4192
4222
|
readonly chat?: {
|
|
4193
4223
|
readonly telegram?: {
|
|
@@ -4565,6 +4595,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
|
|
|
4565
4595
|
readonly balanceOver?: string | undefined;
|
|
4566
4596
|
}[] | undefined;
|
|
4567
4597
|
readonly guide?: string | undefined;
|
|
4598
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
4568
4599
|
}[];
|
|
4569
4600
|
readonly chat?: {
|
|
4570
4601
|
readonly telegram?: {
|
|
@@ -4948,6 +4979,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
|
|
|
4948
4979
|
readonly balanceOver?: string | undefined;
|
|
4949
4980
|
}[] | undefined;
|
|
4950
4981
|
readonly guide?: string | undefined;
|
|
4982
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
4951
4983
|
}[];
|
|
4952
4984
|
readonly chat?: {
|
|
4953
4985
|
readonly telegram?: {
|
|
@@ -5325,6 +5357,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
|
|
|
5325
5357
|
readonly balanceOver?: string | undefined;
|
|
5326
5358
|
}[] | undefined;
|
|
5327
5359
|
readonly guide?: string | undefined;
|
|
5360
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
5328
5361
|
}[];
|
|
5329
5362
|
readonly chat?: {
|
|
5330
5363
|
readonly telegram?: {
|
package/dist/manifest.js
CHANGED
|
@@ -111,6 +111,9 @@ import { CHANNEL_PROVIDERS, DEFAULT_SIGN_IN_POLICY, isGuestBotCheckScheme, isGue
|
|
|
111
111
|
* `private` without members. Older readers ignore it and register no chat without members.
|
|
112
112
|
* Within version 16, a mailbox in `chat` may carry `signature: true`, written only when declared:
|
|
113
113
|
* the ontology's replies to mail end with the mailbox's own signature. Older readers ignore it.
|
|
114
|
+
* Within version 16, an entity may carry `erase_from_warehouse_on_delete: true`, written only when
|
|
115
|
+
* declared: deleting a record erases its replicated history from the warehouse. The SDK's own
|
|
116
|
+
* replication capture acts on it; readers ignore it.
|
|
114
117
|
*/
|
|
115
118
|
export const MANIFEST_VERSION = 16;
|
|
116
119
|
/** The first version whose documents use snake_case names on the wire. */
|
|
@@ -296,6 +299,11 @@ export const EntityManifest = Schema.Struct({
|
|
|
296
299
|
* writes and its `permissions` hold only `read`. Absent means the generated writes exist.
|
|
297
300
|
*/
|
|
298
301
|
writes: Schema.optionalKey(Schema.Literal("actions")),
|
|
302
|
+
/**
|
|
303
|
+
* `true` when deleting a record erases its replicated history from the warehouse; written only
|
|
304
|
+
* then. The SDK's replication capture acts on it; the platform does not read it.
|
|
305
|
+
*/
|
|
306
|
+
eraseFromWarehouseOnDelete: Schema.optionalKey(Schema.Literal(true)),
|
|
299
307
|
});
|
|
300
308
|
/**
|
|
301
309
|
* A scoped permission of a role: `<entity>:<verb>` and the `where` of the records it covers, in the
|
|
@@ -696,7 +704,11 @@ const EntityManifestWire = Schema.Struct({
|
|
|
696
704
|
metrics: Schema.optionalKey(Schema.Array(MetricManifestWire)),
|
|
697
705
|
properties: Schema.Array(PropertyManifestWire),
|
|
698
706
|
relationships: Schema.Array(RelationshipManifestWire),
|
|
699
|
-
}).pipe(Schema.encodeKeys({
|
|
707
|
+
}).pipe(Schema.encodeKeys({
|
|
708
|
+
builtIn: "built_in",
|
|
709
|
+
renamedFrom: "renamed_from",
|
|
710
|
+
eraseFromWarehouseOnDelete: "erase_from_warehouse_on_delete",
|
|
711
|
+
}));
|
|
700
712
|
const RoleManifestWire = RoleManifest.pipe(Schema.encodeKeys({ builtIn: "built_in" }));
|
|
701
713
|
/** The JSON contract written since version 4; decoded SDK models remain camelCase. */
|
|
702
714
|
export const OntologyManifestWire = Schema.Struct({
|
|
@@ -1100,6 +1112,9 @@ function entityManifest(input) {
|
|
|
1100
1112
|
})),
|
|
1101
1113
|
permissions: { ...resolved.permissions },
|
|
1102
1114
|
...(resolved.writes === "actions" ? { writes: "actions" } : {}),
|
|
1115
|
+
...(definition.eraseFromWarehouseOnDelete === true && definition.source === undefined
|
|
1116
|
+
? { eraseFromWarehouseOnDelete: true }
|
|
1117
|
+
: {}),
|
|
1103
1118
|
};
|
|
1104
1119
|
}
|
|
1105
1120
|
/** Builds the manifest of a resolved ontology that passed `check`. */
|
package/dist/migrate/apply.js
CHANGED
|
@@ -255,7 +255,10 @@ export const applyMigration = (plan, options) => Effect.gen(function* () {
|
|
|
255
255
|
const targetPresent = presence.targets && isNoopMigration(plan)
|
|
256
256
|
? "EXISTS (SELECT 1 FROM _ontology_replication_targets WHERE generation = active_generation)"
|
|
257
257
|
: "false";
|
|
258
|
-
const [state] = yield* sql.unsafe(
|
|
258
|
+
const [state] = yield* sql.unsafe(
|
|
259
|
+
// Tables a release adds within the schema version are installed by its first migration.
|
|
260
|
+
`SELECT version, enabled, ${targetPresent} AS target_present, selection,
|
|
261
|
+
to_regclass('_ontology_replication_erasures') IS NOT NULL AS erasures
|
|
259
262
|
FROM _ontology_replication WHERE id`);
|
|
260
263
|
if (state?.enabled &&
|
|
261
264
|
state.version >= 6 &&
|
|
@@ -273,6 +276,7 @@ export const applyMigration = (plan, options) => Effect.gen(function* () {
|
|
|
273
276
|
}
|
|
274
277
|
if (isNoopMigration(plan) &&
|
|
275
278
|
state?.version === REPLICATION_SCHEMA_VERSION &&
|
|
279
|
+
state.erasures &&
|
|
276
280
|
(!state.enabled || (state.target_present && replicatesAll(plan.target, state.selection)))) {
|
|
277
281
|
if (options.workspaceId !== undefined || syncsIdentities(plan)) {
|
|
278
282
|
// Under the migration lock, and only while the catalog still records this target.
|
package/dist/migrate/model.d.ts
CHANGED
|
@@ -404,6 +404,7 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
404
404
|
readonly balanceOver?: string | undefined;
|
|
405
405
|
}[] | undefined;
|
|
406
406
|
readonly guide?: string | undefined;
|
|
407
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
407
408
|
}[];
|
|
408
409
|
readonly chat?: {
|
|
409
410
|
readonly telegram?: {
|
|
@@ -781,6 +782,7 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
781
782
|
readonly balanceOver?: string | undefined;
|
|
782
783
|
}[] | undefined;
|
|
783
784
|
readonly guide?: string | undefined;
|
|
785
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
784
786
|
}[];
|
|
785
787
|
readonly chat?: {
|
|
786
788
|
readonly telegram?: {
|
|
@@ -1158,6 +1160,7 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
1158
1160
|
}[] | undefined;
|
|
1159
1161
|
readonly guide?: string | undefined;
|
|
1160
1162
|
readonly renamed_from?: string | undefined;
|
|
1163
|
+
readonly erase_from_warehouse_on_delete?: true | undefined;
|
|
1161
1164
|
}[];
|
|
1162
1165
|
readonly chat?: {
|
|
1163
1166
|
readonly telegram?: {
|
|
@@ -1535,6 +1538,7 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
1535
1538
|
readonly balanceOver?: string | undefined;
|
|
1536
1539
|
}[] | undefined;
|
|
1537
1540
|
readonly guide?: string | undefined;
|
|
1541
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
1538
1542
|
}[];
|
|
1539
1543
|
readonly chat?: {
|
|
1540
1544
|
readonly telegram?: {
|
|
@@ -1907,9 +1911,11 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
1907
1911
|
readonly unlink: Schema.optionalKey<Schema.String>;
|
|
1908
1912
|
}>>;
|
|
1909
1913
|
readonly writes: Schema.optionalKey<Schema.Literal<"actions">>;
|
|
1914
|
+
readonly eraseFromWarehouseOnDelete: Schema.optionalKey<Schema.Literal<true>>;
|
|
1910
1915
|
}>, {
|
|
1911
1916
|
readonly builtIn: "built_in";
|
|
1912
1917
|
readonly renamedFrom: "renamed_from";
|
|
1918
|
+
readonly eraseFromWarehouseOnDelete: "erase_from_warehouse_on_delete";
|
|
1913
1919
|
}>>;
|
|
1914
1920
|
readonly roles: Schema.$Array<Schema.encodeKeys<Schema.Struct<{
|
|
1915
1921
|
readonly name: Schema.String;
|
|
@@ -2300,6 +2306,7 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
2300
2306
|
readonly unlink: Schema.optionalKey<Schema.String>;
|
|
2301
2307
|
}>>;
|
|
2302
2308
|
readonly writes: Schema.optionalKey<Schema.Literal<"actions">>;
|
|
2309
|
+
readonly eraseFromWarehouseOnDelete: Schema.optionalKey<Schema.Literal<true>>;
|
|
2303
2310
|
}>>;
|
|
2304
2311
|
readonly roles: Schema.$Array<Schema.Struct<{
|
|
2305
2312
|
readonly name: Schema.String;
|
|
@@ -2676,6 +2683,7 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
2676
2683
|
readonly balanceOver?: string | undefined;
|
|
2677
2684
|
}[] | undefined;
|
|
2678
2685
|
readonly guide?: string | undefined;
|
|
2686
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
2679
2687
|
}[];
|
|
2680
2688
|
readonly chat?: {
|
|
2681
2689
|
readonly telegram?: {
|
|
@@ -3053,6 +3061,7 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
3053
3061
|
readonly balanceOver?: string | undefined;
|
|
3054
3062
|
}[] | undefined;
|
|
3055
3063
|
readonly guide?: string | undefined;
|
|
3064
|
+
readonly eraseFromWarehouseOnDelete?: true | undefined;
|
|
3056
3065
|
}[];
|
|
3057
3066
|
readonly chat?: {
|
|
3058
3067
|
readonly telegram?: {
|
package/dist/migrate/preview.js
CHANGED
|
@@ -52,6 +52,7 @@ export function preparePreviewDatabase({ sql, }) {
|
|
|
52
52
|
"_ontology_replication_scans",
|
|
53
53
|
"_ontology_replication_baselines",
|
|
54
54
|
"_ontology_replication_receipts",
|
|
55
|
+
"_ontology_replication_erasures",
|
|
55
56
|
"_ontology_replication_targets",
|
|
56
57
|
];
|
|
57
58
|
if (present.has("_ontology_replication")) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Records whose replicated history the warehouse must lose: one row per deleted record of an
|
|
3
|
+
* entity that erases its history on delete. A row is the audit of its erasure as well, holding no
|
|
4
|
+
* content: which record, when it was deleted, when its history left the warehouse, by which
|
|
5
|
+
* warehouse job, how many rows that job removed, and when a later pass found nothing left.
|
|
6
|
+
*
|
|
7
|
+
* `due_at` batches the work by day: a deletion is erased from the next day's first pass, a failed
|
|
8
|
+
* attempt is retried an hour later, and an erased record is checked again once Pub/Sub can no
|
|
9
|
+
* longer deliver an event captured before the deletion.
|
|
10
|
+
*/
|
|
11
|
+
export declare const REPLICATION_ERASURES_DDL: string[];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Records whose replicated history the warehouse must lose: one row per deleted record of an
|
|
3
|
+
* entity that erases its history on delete. A row is the audit of its erasure as well, holding no
|
|
4
|
+
* content: which record, when it was deleted, when its history left the warehouse, by which
|
|
5
|
+
* warehouse job, how many rows that job removed, and when a later pass found nothing left.
|
|
6
|
+
*
|
|
7
|
+
* `due_at` batches the work by day: a deletion is erased from the next day's first pass, a failed
|
|
8
|
+
* attempt is retried an hour later, and an erased record is checked again once Pub/Sub can no
|
|
9
|
+
* longer deliver an event captured before the deletion.
|
|
10
|
+
*/
|
|
11
|
+
export const REPLICATION_ERASURES_DDL = [
|
|
12
|
+
`CREATE TABLE IF NOT EXISTS _ontology_replication_erasures (
|
|
13
|
+
entity_id text NOT NULL, record_id uuid NOT NULL,
|
|
14
|
+
requested_at timestamptz NOT NULL DEFAULT now(),
|
|
15
|
+
due_at timestamptz NOT NULL DEFAULT date_trunc('day', now()) + interval '1 day',
|
|
16
|
+
attempts integer NOT NULL DEFAULT 0, failure text,
|
|
17
|
+
erased_at timestamptz, erased_job text, erased_rows bigint NOT NULL DEFAULT 0,
|
|
18
|
+
confirmed_at timestamptz,
|
|
19
|
+
PRIMARY KEY (entity_id, record_id)
|
|
20
|
+
)`,
|
|
21
|
+
`CREATE INDEX IF NOT EXISTS _ontology_replication_erasures_due
|
|
22
|
+
ON _ontology_replication_erasures (due_at, entity_id, record_id) WHERE confirmed_at IS NULL`,
|
|
23
|
+
];
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { SqlClient } from "effect/unstable/sql";
|
|
3
|
+
export { REPLICATION_ERASURES_DDL } from "./erasure-schema.js";
|
|
4
|
+
/** The most records one warehouse statement erases. */
|
|
5
|
+
export declare const REPLICATION_ERASURE_BATCH = 1000;
|
|
6
|
+
/**
|
|
7
|
+
* How long after its copies were scrubbed an event published before can still reach the warehouse:
|
|
8
|
+
* Pub/Sub keeps an undelivered message seven days. An erased record is confirmed by a pass after.
|
|
9
|
+
*/
|
|
10
|
+
export declare const REPLICATION_ERASURE_CONFIRM_AFTER = "8 days";
|
|
11
|
+
export interface ReplicationErasureBatch {
|
|
12
|
+
readonly entityId: string;
|
|
13
|
+
readonly recordIds: readonly string[];
|
|
14
|
+
}
|
|
15
|
+
export interface ReplicationErasureScrub {
|
|
16
|
+
/** Staging objects of settled copy segments that held the records' events. */
|
|
17
|
+
readonly objectUris: readonly string[];
|
|
18
|
+
/** Copy segments that hold the records' events but have not settled yet: scrubbed later. */
|
|
19
|
+
readonly unsettled: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The next records due for erasure, of one entity. A source an SDK without erasure installed has
|
|
23
|
+
* none.
|
|
24
|
+
*/
|
|
25
|
+
export declare const dueReplicationErasures: (sql: SqlClient.SqlClient, limit?: number) => Effect.Effect<{
|
|
26
|
+
entityId: string;
|
|
27
|
+
recordIds: string[];
|
|
28
|
+
} | undefined, import("effect/unstable/sql/SqlError").SqlError, never>;
|
|
29
|
+
/**
|
|
30
|
+
* Removes the records' content from every replication copy the source keeps: the events their
|
|
31
|
+
* history captured, delivery payloads kept for dropped history, baseline scan events and settled
|
|
32
|
+
* copy segments. Each copy keeps the event's identity and revision as a deletion, so a copy sent
|
|
33
|
+
* again can never show the record. Relationship events carry only ids and stay. A segment still
|
|
34
|
+
* being loaded keeps its events until it settles; the next pass scrubs it.
|
|
35
|
+
*/
|
|
36
|
+
export declare const scrubReplicationErasures: (sql: SqlClient.SqlClient, batch: ReplicationErasureBatch) => Effect.Effect<{
|
|
37
|
+
objectUris: string[];
|
|
38
|
+
unsettled: number;
|
|
39
|
+
}, import("effect/unstable/sql/SqlError").SqlError, never>;
|
|
40
|
+
/**
|
|
41
|
+
* Records a warehouse erasure of the batch. The first erases the records. Their copies were scrubbed
|
|
42
|
+
* before it, so any event published since carries no content; one published earlier stays in
|
|
43
|
+
* Pub/Sub at most until the confirmation delay after that first erasure. A pass after that delay
|
|
44
|
+
* that removed nothing and found no unsettled copy confirms the records. Otherwise they are checked
|
|
45
|
+
* again the next day, or within the hour while a copy segment that holds them has not settled: it
|
|
46
|
+
* may still load their content, and its staging object is deleted once it has.
|
|
47
|
+
*/
|
|
48
|
+
export declare const recordReplicationErasure: (sql: SqlClient.SqlClient, batch: ReplicationErasureBatch, outcome: {
|
|
49
|
+
readonly jobId: string;
|
|
50
|
+
readonly rows: number;
|
|
51
|
+
readonly unsettled: number;
|
|
52
|
+
}) => import("effect/unstable/sql/Statement").Statement<{
|
|
53
|
+
record_id: string;
|
|
54
|
+
}>;
|
|
55
|
+
/** A failed attempt is retried an hour later; its reason is a code, never content. */
|
|
56
|
+
export declare const recordReplicationErasureFailure: (sql: SqlClient.SqlClient, batch: ReplicationErasureBatch, failure: string) => Effect.Effect<number, import("effect/unstable/sql/SqlError").SqlError, never>;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { historyTableName, quoteIdentifier as qi } from "../migrate/storage.js";
|
|
3
|
+
import { activeReplicationTarget } from "./store.js";
|
|
4
|
+
export { REPLICATION_ERASURES_DDL } from "./erasure-schema.js";
|
|
5
|
+
/** The most records one warehouse statement erases. */
|
|
6
|
+
export const REPLICATION_ERASURE_BATCH = 1000;
|
|
7
|
+
/**
|
|
8
|
+
* How long after its copies were scrubbed an event published before can still reach the warehouse:
|
|
9
|
+
* Pub/Sub keeps an undelivered message seven days. An erased record is confirmed by a pass after.
|
|
10
|
+
*/
|
|
11
|
+
export const REPLICATION_ERASURE_CONFIRM_AFTER = "8 days";
|
|
12
|
+
/** What scrubbing a replicated event leaves: its identity and revision, no content, a deletion. */
|
|
13
|
+
const SCRUBBED = `'{"payload":"{}","deleted":true}'::jsonb`;
|
|
14
|
+
const present = (sql) => sql
|
|
15
|
+
.unsafe("SELECT to_regclass('_ontology_replication_erasures') IS NOT NULL AS present")
|
|
16
|
+
.pipe(Effect.map((rows) => rows[0]?.present === true));
|
|
17
|
+
/**
|
|
18
|
+
* The next records due for erasure, of one entity. A source an SDK without erasure installed has
|
|
19
|
+
* none.
|
|
20
|
+
*/
|
|
21
|
+
export const dueReplicationErasures = (sql, limit = REPLICATION_ERASURE_BATCH) => Effect.gen(function* () {
|
|
22
|
+
if (!(yield* present(sql))) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const [first] = yield* sql.unsafe(`SELECT entity_id FROM _ontology_replication_erasures
|
|
26
|
+
WHERE confirmed_at IS NULL AND due_at <= now() ORDER BY due_at LIMIT 1`);
|
|
27
|
+
if (!first) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const rows = yield* sql.unsafe(`SELECT record_id::text FROM _ontology_replication_erasures
|
|
31
|
+
WHERE confirmed_at IS NULL AND due_at <= now() AND entity_id = $1
|
|
32
|
+
ORDER BY record_id LIMIT $2::int`, [first.entity_id, limit]);
|
|
33
|
+
return {
|
|
34
|
+
entityId: first.entity_id,
|
|
35
|
+
recordIds: rows.map((row) => row.record_id),
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Removes the records' content from every replication copy the source keeps: the events their
|
|
40
|
+
* history captured, delivery payloads kept for dropped history, baseline scan events and settled
|
|
41
|
+
* copy segments. Each copy keeps the event's identity and revision as a deletion, so a copy sent
|
|
42
|
+
* again can never show the record. Relationship events carry only ids and stay. A segment still
|
|
43
|
+
* being loaded keeps its events until it settles; the next pass scrubs it.
|
|
44
|
+
*/
|
|
45
|
+
export const scrubReplicationErasures = (sql, batch) => sql.withTransaction(Effect.gen(function* () {
|
|
46
|
+
const target = yield* activeReplicationTarget(sql);
|
|
47
|
+
const active = target?.manifest.entities.find((item) => item.id === batch.entityId)?.name;
|
|
48
|
+
// An entity removed from the ontology keeps its records and history under its last name,
|
|
49
|
+
// which the latest replication target that declared it records.
|
|
50
|
+
const [retained] = active
|
|
51
|
+
? []
|
|
52
|
+
: yield* sql.unsafe(`SELECT e->>'name' AS name FROM _ontology_replication_targets t
|
|
53
|
+
CROSS JOIN LATERAL jsonb_array_elements(t.manifest->'entities') e
|
|
54
|
+
WHERE e->>'id' = $1 ORDER BY t.created_at DESC LIMIT 1`, [batch.entityId]);
|
|
55
|
+
const name = active ?? retained?.name;
|
|
56
|
+
const history = name ? historyTableName(name) : undefined;
|
|
57
|
+
const ids = JSON.stringify(batch.recordIds);
|
|
58
|
+
let events = [];
|
|
59
|
+
if (history) {
|
|
60
|
+
const [exists] = yield* sql.unsafe("SELECT to_regclass($1) IS NOT NULL AS present", [qi(history)]);
|
|
61
|
+
if (exists?.present) {
|
|
62
|
+
const scrubbed = yield* sql.unsafe(`UPDATE ${qi(history)} SET replication = replication || ${SCRUBBED}
|
|
63
|
+
WHERE record_id IN (SELECT value::uuid FROM jsonb_array_elements_text($1::jsonb))
|
|
64
|
+
AND replication IS NOT NULL AND replication->>'record_kind' = 'entity'
|
|
65
|
+
RETURNING id::text`, [ids]);
|
|
66
|
+
events = scrubbed.map((row) => row.id);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const delivered = yield* sql.unsafe(`UPDATE _ontology_replication_delivery d SET payload = d.payload || ${SCRUBBED}
|
|
70
|
+
WHERE d.payload IS NOT NULL AND d.payload->>'entity_id' = $1
|
|
71
|
+
AND d.payload->>'record_kind' = 'entity'
|
|
72
|
+
AND d.payload->>'record_key' IN (SELECT value FROM jsonb_array_elements_text($2::jsonb))
|
|
73
|
+
RETURNING d.event_id::text AS id`, [batch.entityId, ids]);
|
|
74
|
+
const baselines = yield* sql.unsafe(`UPDATE _ontology_replication_baselines SET event = event || ${SCRUBBED}
|
|
75
|
+
WHERE event->>'entity_id' = $1 AND event->>'record_kind' = 'entity'
|
|
76
|
+
AND event->>'record_key' IN (SELECT value FROM jsonb_array_elements_text($2::jsonb))
|
|
77
|
+
RETURNING id::text`, [batch.entityId, ids]);
|
|
78
|
+
events = [...events, ...delivered.map((row) => row.id), ...baselines.map((row) => row.id)];
|
|
79
|
+
if (!events.length) {
|
|
80
|
+
return { objectUris: [], unsettled: 0 };
|
|
81
|
+
}
|
|
82
|
+
const matches = `e->>'entity_id' = $1 AND e->>'record_kind' = 'entity'
|
|
83
|
+
AND e->>'record_key' IN (SELECT value FROM jsonb_array_elements_text($2::jsonb))`;
|
|
84
|
+
const settled = yield* sql.unsafe(`UPDATE _ontology_replication_segments s SET events = (
|
|
85
|
+
SELECT jsonb_agg(CASE WHEN ${matches} THEN e || ${SCRUBBED} ELSE e END ORDER BY n)
|
|
86
|
+
FROM jsonb_array_elements(s.events) WITH ORDINALITY AS item(e, n))
|
|
87
|
+
WHERE s.state <> 'pending' AND s.event_ids ?| ARRAY(SELECT jsonb_array_elements_text($3::jsonb))
|
|
88
|
+
RETURNING s.object_uri`, [batch.entityId, ids, JSON.stringify(events)]);
|
|
89
|
+
const [pending] = yield* sql.unsafe(`SELECT count(*)::int AS count FROM _ontology_replication_segments s
|
|
90
|
+
WHERE s.state = 'pending' AND s.event_ids ?| ARRAY(SELECT jsonb_array_elements_text($1::jsonb))`, [JSON.stringify(events)]);
|
|
91
|
+
return {
|
|
92
|
+
objectUris: settled.map((row) => row.object_uri),
|
|
93
|
+
unsettled: pending?.count ?? 0,
|
|
94
|
+
};
|
|
95
|
+
}));
|
|
96
|
+
/**
|
|
97
|
+
* Records a warehouse erasure of the batch. The first erases the records. Their copies were scrubbed
|
|
98
|
+
* before it, so any event published since carries no content; one published earlier stays in
|
|
99
|
+
* Pub/Sub at most until the confirmation delay after that first erasure. A pass after that delay
|
|
100
|
+
* that removed nothing and found no unsettled copy confirms the records. Otherwise they are checked
|
|
101
|
+
* again the next day, or within the hour while a copy segment that holds them has not settled: it
|
|
102
|
+
* may still load their content, and its staging object is deleted once it has.
|
|
103
|
+
*/
|
|
104
|
+
export const recordReplicationErasure = (sql, batch, outcome) => sql.unsafe(`UPDATE _ontology_replication_erasures SET attempts = attempts + 1, failure = NULL,
|
|
105
|
+
erased_at = COALESCE(erased_at, now()), erased_job = $3, erased_rows = erased_rows + $4::bigint,
|
|
106
|
+
confirmed_at = CASE WHEN erased_at IS NOT NULL AND $4::bigint = 0 AND $5::int = 0
|
|
107
|
+
AND now() >= erased_at + interval '${REPLICATION_ERASURE_CONFIRM_AFTER}' THEN now() END,
|
|
108
|
+
due_at = CASE WHEN $5::int > 0 THEN now() + interval '1 hour'
|
|
109
|
+
WHEN erased_at IS NULL THEN now() + interval '${REPLICATION_ERASURE_CONFIRM_AFTER}'
|
|
110
|
+
ELSE now() + interval '1 day' END
|
|
111
|
+
WHERE entity_id = $1 AND confirmed_at IS NULL
|
|
112
|
+
AND record_id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))
|
|
113
|
+
RETURNING record_id::text`, [
|
|
114
|
+
batch.entityId,
|
|
115
|
+
JSON.stringify(batch.recordIds),
|
|
116
|
+
outcome.jobId,
|
|
117
|
+
outcome.rows,
|
|
118
|
+
outcome.unsettled,
|
|
119
|
+
]);
|
|
120
|
+
/** A failed attempt is retried an hour later; its reason is a code, never content. */
|
|
121
|
+
export const recordReplicationErasureFailure = (sql, batch, failure) => sql
|
|
122
|
+
.unsafe(`UPDATE _ontology_replication_erasures SET attempts = attempts + 1, failure = $3,
|
|
123
|
+
due_at = now() + interval '1 hour'
|
|
124
|
+
WHERE entity_id = $1 AND confirmed_at IS NULL
|
|
125
|
+
AND record_id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))
|
|
126
|
+
RETURNING attempts`, [batch.entityId, JSON.stringify(batch.recordIds), failure])
|
|
127
|
+
.pipe(Effect.map((rows) => Math.max(0, ...rows.map((row) => row.attempts))));
|
|
@@ -3,6 +3,7 @@ import { columnName, historyTableName, quoteIdentifier as qi, quoteLiteral as ql
|
|
|
3
3
|
import { replicatesRecords } from "../replicated-entities.js";
|
|
4
4
|
import { replicatedFields, REPLICATION_RECORD_BYTES } from "./model.js";
|
|
5
5
|
import { SHADOW_REPLICATION_DDL, SHADOW_CONTROL_JOBS_DDL, SHADOW_SOURCE_REPLACEMENTS_DDL, } from "./shadow-schema.js";
|
|
6
|
+
import { REPLICATION_ERASURES_DDL } from "./erasure-schema.js";
|
|
6
7
|
export const REPLICATION_SCHEMA_VERSION = 6;
|
|
7
8
|
/** Targets of sources not migrated since fills were introduced have no `fills` column. */
|
|
8
9
|
export const REPLICATION_TARGET_FILLS = `COALESCE(to_jsonb(_ontology_replication_targets) -> 'fills',
|
|
@@ -111,6 +112,7 @@ export const REPLICATION_DDL = [
|
|
|
111
112
|
...SHADOW_REPLICATION_DDL,
|
|
112
113
|
SHADOW_CONTROL_JOBS_DDL,
|
|
113
114
|
SHADOW_SOURCE_REPLACEMENTS_DDL,
|
|
115
|
+
...REPLICATION_ERASURES_DDL,
|
|
114
116
|
`CREATE OR REPLACE FUNCTION _ontology_replication_guard() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
115
117
|
BEGIN
|
|
116
118
|
IF (SELECT enabled FROM _ontology_replication WHERE id) AND
|
|
@@ -156,6 +158,15 @@ export function replicationEntityDdl(manifest, previous) {
|
|
|
156
158
|
const declared = entity.relationships
|
|
157
159
|
.filter((r) => r.cardinality === "many")
|
|
158
160
|
.map((r) => r.name);
|
|
161
|
+
// A deletion of an entity that erases its history is journaled whatever replication's state,
|
|
162
|
+
// in the deleting transaction, so no path of deletion escapes it.
|
|
163
|
+
const erases = entity.eraseFromWarehouseOnDelete === true;
|
|
164
|
+
const journal = erases
|
|
165
|
+
? `IF NEW.kind = 'delete' THEN
|
|
166
|
+
INSERT INTO _ontology_replication_erasures (entity_id, record_id)
|
|
167
|
+
VALUES (${ql(entity.id)}, NEW.record_id) ON CONFLICT DO NOTHING;
|
|
168
|
+
END IF;`
|
|
169
|
+
: "";
|
|
159
170
|
return [
|
|
160
171
|
`ALTER TABLE ${history} ADD COLUMN IF NOT EXISTS replication jsonb`,
|
|
161
172
|
`CREATE INDEX IF NOT EXISTS ${qi(`_replication_${key}_idx`)} ON ${history}
|
|
@@ -165,6 +176,7 @@ export function replicationEntityDdl(manifest, previous) {
|
|
|
165
176
|
event_revision bigint; record_key text; record_kind text := 'entity';
|
|
166
177
|
is_deleted boolean := NEW.kind = 'delete';
|
|
167
178
|
BEGIN
|
|
179
|
+
${journal}
|
|
168
180
|
SELECT * INTO state FROM _ontology_replication WHERE id;
|
|
169
181
|
policy := state.selection->${ql(entity.id)};
|
|
170
182
|
IF NOT state.enabled OR policy IS NULL THEN RETURN NEW; END IF;
|
|
@@ -207,6 +219,15 @@ export function replicationEntityDdl(manifest, previous) {
|
|
|
207
219
|
END IF;
|
|
208
220
|
RETURN NEW;
|
|
209
221
|
END $$`,
|
|
222
|
+
// Deletions recorded before the entity erased its history, such as drafts a retention purge
|
|
223
|
+
// already deleted, are erased as well: due at once, requested when they were deleted.
|
|
224
|
+
...(erases
|
|
225
|
+
? [
|
|
226
|
+
`INSERT INTO _ontology_replication_erasures (entity_id, record_id, requested_at, due_at)
|
|
227
|
+
SELECT ${ql(entity.id)}, record_id, min(at), now() FROM ${history}
|
|
228
|
+
WHERE kind = 'delete' GROUP BY record_id ON CONFLICT DO NOTHING`,
|
|
229
|
+
]
|
|
230
|
+
: []),
|
|
210
231
|
`DROP TRIGGER IF EXISTS _ontology_capture ON ${history}`,
|
|
211
232
|
`CREATE TRIGGER _ontology_capture BEFORE INSERT ON ${history} FOR EACH ROW EXECUTE FUNCTION ${fn}()`,
|
|
212
233
|
`DROP TRIGGER IF EXISTS _ontology_writer_guard ON ${table}`,
|
|
@@ -411,9 +411,11 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
411
411
|
readonly unlink: Schema.optionalKey<Schema.String>;
|
|
412
412
|
}>>;
|
|
413
413
|
readonly writes: Schema.optionalKey<Schema.Literal<"actions">>;
|
|
414
|
+
readonly eraseFromWarehouseOnDelete: Schema.optionalKey<Schema.Literal<true>>;
|
|
414
415
|
}>, {
|
|
415
416
|
readonly builtIn: "built_in";
|
|
416
417
|
readonly renamedFrom: "renamed_from";
|
|
418
|
+
readonly eraseFromWarehouseOnDelete: "erase_from_warehouse_on_delete";
|
|
417
419
|
}>>;
|
|
418
420
|
readonly roles: Schema.$Array<Schema.encodeKeys<Schema.Struct<{
|
|
419
421
|
readonly name: Schema.String;
|
|
@@ -800,9 +802,11 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
800
802
|
readonly unlink: Schema.optionalKey<Schema.String>;
|
|
801
803
|
}>>;
|
|
802
804
|
readonly writes: Schema.optionalKey<Schema.Literal<"actions">>;
|
|
805
|
+
readonly eraseFromWarehouseOnDelete: Schema.optionalKey<Schema.Literal<true>>;
|
|
803
806
|
}>, {
|
|
804
807
|
readonly builtIn: "built_in";
|
|
805
808
|
readonly renamedFrom: "renamed_from";
|
|
809
|
+
readonly eraseFromWarehouseOnDelete: "erase_from_warehouse_on_delete";
|
|
806
810
|
}>>>;
|
|
807
811
|
readonly roles: Schema.toEncoded<Schema.$Array<Schema.encodeKeys<Schema.Struct<{
|
|
808
812
|
readonly name: Schema.String;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/ontology",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.169.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",
|