@segmentstream/ontology 0.156.1 → 0.159.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.
Files changed (67) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +12 -9
  3. package/dist/agent-chat.d.ts +1 -1
  4. package/dist/agent-chat.js +8 -8
  5. package/dist/agent-draft.d.ts +287 -0
  6. package/dist/{agent-review.js → agent-draft.js} +103 -82
  7. package/dist/analyze.d.ts +1 -1
  8. package/dist/analyze.js +71 -14
  9. package/dist/channel-files.d.ts +70 -0
  10. package/dist/channel-files.js +58 -0
  11. package/dist/chat.d.ts +2 -2
  12. package/dist/earlier-entity-names.d.ts +35 -0
  13. package/dist/earlier-entity-names.js +88 -0
  14. package/dist/email.d.ts +2 -0
  15. package/dist/email.js +1 -0
  16. package/dist/gmail-send.d.ts +39 -0
  17. package/dist/gmail-send.js +179 -0
  18. package/dist/index.d.ts +1 -1
  19. package/dist/index.js +3 -1
  20. package/dist/migrate/apply.d.ts +3 -2
  21. package/dist/migrate/apply.js +11 -4
  22. package/dist/migrate/plan.js +42 -8
  23. package/dist/migrate/preview.js +4 -1
  24. package/dist/permissions.d.ts +5 -5
  25. package/dist/permissions.js +13 -10
  26. package/dist/replicated-entities.d.ts +1 -1
  27. package/dist/replicated-entities.js +4 -1
  28. package/dist/replication/bigquery.d.ts +24 -2
  29. package/dist/replication/bigquery.js +72 -12
  30. package/dist/runtime/agents.d.ts +1 -1
  31. package/dist/runtime/agents.js +43 -38
  32. package/dist/runtime/chat-messages.d.ts +2 -2
  33. package/dist/runtime/chat-messages.js +1 -1
  34. package/dist/runtime/service-binding.js +1 -1
  35. package/dist/service/agent/protocol.d.ts +4 -4
  36. package/dist/service/agent/protocol.js +7 -7
  37. package/dist/service/agent/server.js +14 -9
  38. package/dist/service/chat/envelope.d.ts +12 -12
  39. package/dist/service/chat/review-delivery.js +23 -22
  40. package/dist/service/chat/review-revision.d.ts +1 -1
  41. package/dist/service/chat/review-revision.js +13 -13
  42. package/dist/service/mcp/runtime-policy.js +1 -1
  43. package/dist/service/mcp/server.js +9 -1
  44. package/dist/service/mcp/tools.js +1 -1
  45. package/dist/service/operations.js +11 -5
  46. package/dist/service/runtime-contract.d.ts +3 -3
  47. package/dist/service/service.d.ts +6 -0
  48. package/dist/service/service.js +21 -3
  49. package/dist/slack-files.d.ts +53 -0
  50. package/dist/slack-files.js +183 -0
  51. package/dist/slack.d.ts +3 -0
  52. package/dist/slack.js +2 -0
  53. package/dist/storage/attachments.js +15 -16
  54. package/dist/storage/tables.d.ts +18 -11
  55. package/dist/storage/tables.js +87 -12
  56. package/dist/telegram-files.d.ts +81 -0
  57. package/dist/telegram-files.js +244 -0
  58. package/dist/telegram.d.ts +3 -1
  59. package/dist/telegram.js +2 -0
  60. package/dist/whatsapp-files.d.ts +54 -0
  61. package/dist/whatsapp-files.js +138 -0
  62. package/dist/whatsapp.d.ts +3 -0
  63. package/dist/whatsapp.js +2 -0
  64. package/dist/workspace-channel.d.ts +20 -0
  65. package/dist/workspace-channel.js +10 -0
  66. package/package.json +1 -1
  67. package/dist/agent-review.d.ts +0 -154
@@ -1,7 +1,7 @@
1
1
  import { isValueMatched, } from "../manifest.js";
2
2
  import { FEDERATION_TABLES, FEDERATION_TABLES_DDL } from "../runtime/federation-tables.js";
3
3
  import { EMAIL_LINK_FENCE_SQL, SEGMENTSTREAM_LINK_FENCE_SQL, USER_IDENTITY_ENTITY_ID, USER_IDENTITY_SYNC_SQL, } from "../runtime/user-identity-sql.js";
4
- import { AGENT_TASKS_DDL, AGENT_TASKS_TABLE, SIGN_IN_EMAILS_DDL, SIGN_IN_EMAILS_TABLE, ATTACHMENTS_TABLE, ATTACHMENT_TABLES_DDL, BLOB_TABLES_DDL, WORKFLOW_STARTS_DDL, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
4
+ import { AGENT_TASKS_DDL, AGENT_TASKS_TABLE, SIGN_IN_EMAILS_DDL, SIGN_IN_EMAILS_TABLE, ATTACHMENTS_TABLE, ATTACHMENT_TABLES_DDL, LEGACY_ATTACHMENTS_TABLE, BLOB_TABLES_DDL, WORKFLOW_STARTS_DDL, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
5
5
  import { SNAKE_CASE_NAME, isSystemProperty, toSnakeCase } from "../names.js";
6
6
  import { NO_RETAINED_STORAGE, retainedEntityManifest, } from "./model.js";
7
7
  import { ACTION_ATTRIBUTION_TABLE, ACTION_ATTRIBUTION_DDL, AGENT_TABLES, AGENT_TABLES_DDL, API_KEYS_DDL, API_KEYS_TABLE, CATALOG_TABLE_DDL, MAX_IDENTIFIER_LENGTH, OAUTH_TABLES, OAUTH_TABLES_DDL, USER_TABLE, addConstraintStatement, columnName, columnDefinition, constraintDefinition, createEnumStatement, createIndexStatement, createTableStatement, defaultExpression, entityStorage, historyActivityIndex, enumTypeName, isStoredProperty, fieldColumnName, joinTable, propertyCheck, blobForeignKey, propertyColumn, propertyEnum, quoteIdentifier, quoteLiteral, relationshipColumn, relationshipColumnName, RUNTIME_TABLES_DDL, relationshipForeignKey, SYSTEM_COLUMNS, storedEntity, tableName, } from "./storage.js";
@@ -225,17 +225,22 @@ export function planMigration(input) {
225
225
  draft.runtimeTables.push(step({
226
226
  kind: "createCatalog",
227
227
  classification: "additive",
228
- description: "Create the ontology's file revision, upload and agent attachment tables.",
228
+ description: "Create the ontology's file revision, upload and chat attachment tables.",
229
229
  sql: [...BLOB_TABLES_DDL, ...ATTACHMENT_TABLES_DDL],
230
230
  }));
231
231
  }
232
- else if (storesFiles && input.existingTables?.has(ATTACHMENTS_TABLE) === false) {
233
- // File storage an earlier SDK created lacks the agent's attachments, or their newer sources.
232
+ else if (
233
+ // @legacy ontology.agent-attachments-view: a table under the earlier name is renamed even
234
+ // where the ontology no longer stores files, so that no row stays behind under that name.
235
+ (storesFiles || input.existingTables?.has(LEGACY_ATTACHMENTS_TABLE)) &&
236
+ input.existingTables?.has(ATTACHMENTS_TABLE) === false) {
237
+ // File storage an earlier SDK created lacks the chats' attachments, their newer sources or
238
+ // their current name.
234
239
  draft.runtimeTables.push(step({
235
240
  kind: "createCatalog",
236
241
  classification: "additive",
237
- description: "Create the agent's temporary attachment table beside the file tables, or widen the " +
238
- "sources it accepts.",
242
+ description: "Create the chats' temporary attachment table beside the file tables, rename it from " +
243
+ `${LEGACY_ATTACHMENTS_TABLE}, or widen the sources it accepts.`,
239
244
  sql: ATTACHMENT_TABLES_DDL,
240
245
  }));
241
246
  }
@@ -958,12 +963,28 @@ function matchProperties(current, target, kept) {
958
963
  const properties = [];
959
964
  const addedProperties = [];
960
965
  const restoredProperties = new Set();
966
+ // Restored fields whose rename source is declared too, by the source's name.
967
+ const filledFrom = new Map();
961
968
  for (const property of target.properties) {
962
969
  const sameName = byName.get(property.name);
963
970
  const source = property.renamedFrom === undefined
964
971
  ? undefined
965
972
  : (byName.get(property.renamedFrom) ?? byName.get(toSnakeCase(property.renamedFrom)));
966
973
  const previous = source !== undefined && !claimed.has(source.name) ? source : undefined;
974
+ const keptUnderName = keptByName.get(property.name);
975
+ // A field renamed back and forth: an earlier SDK's deploy removed it, keeping its column, and
976
+ // declared it under the name it renames from again. The kept field declared the same rename,
977
+ // so it is this field, not another one of the same name. Its kept column is restored and takes the
978
+ // values written under that name meanwhile; that name's column is kept in turn.
979
+ if (sameName === undefined &&
980
+ previous !== undefined &&
981
+ keptUnderName?.renamedFrom !== undefined &&
982
+ keptUnderName.renamedFrom === property.renamedFrom) {
983
+ restoredProperties.add(property.name);
984
+ filledFrom.set(property.name, previous.name);
985
+ properties.push({ current: keptUnderName, target: property });
986
+ continue;
987
+ }
967
988
  const match = sameName ?? previous;
968
989
  if (match === undefined) {
969
990
  const keptProperty = keptByName.get(property.name);
@@ -986,6 +1007,7 @@ function matchProperties(current, target, kept) {
986
1007
  addedProperties,
987
1008
  removedProperties,
988
1009
  restoredProperties,
1010
+ filledFrom,
989
1011
  keptProperties: kept.filter((property) => !restoredProperties.has(property.name)),
990
1012
  };
991
1013
  }
@@ -1723,11 +1745,23 @@ function planEntityChanges(input) {
1723
1745
  }
1724
1746
  const restored = pair.restoredFields.properties.has(property.target.name);
1725
1747
  if (restored) {
1748
+ const source = pair.filledFrom.get(property.target.name);
1749
+ const sourceProperty = source === undefined ? undefined : before.properties.find(({ name }) => name === source);
1726
1750
  draft.changes.push(step({
1727
1751
  kind: "restoreProperty",
1728
1752
  classification: "change",
1729
- description: `Restore property ${entity}.${property.target.name} with the values its column kept.`,
1730
- sql: [],
1753
+ description: sourceProperty === undefined
1754
+ ? `Restore property ${entity}.${property.target.name} with the values its column kept.`
1755
+ : `Restore property ${entity}.${property.target.name} with the values its column ` +
1756
+ `kept, and those of ${sourceProperty.name} where it kept none.`,
1757
+ sql: sourceProperty === undefined
1758
+ ? []
1759
+ : [
1760
+ `UPDATE ${quoteIdentifier(tableName(entity))} SET ` +
1761
+ `${quoteIdentifier(propertyColumn(entity, property.target).name)} = ` +
1762
+ `${quoteIdentifier(propertyColumn(entity, sourceProperty).name)} WHERE ` +
1763
+ `${quoteIdentifier(propertyColumn(entity, property.target).name)} IS NULL`,
1764
+ ],
1731
1765
  }));
1732
1766
  }
1733
1767
  planPropertyChange({
@@ -1,5 +1,5 @@
1
1
  import { Effect } from "effect";
2
- import { AGENT_TASKS_TABLE, ATTACHMENTS_TABLE, SIGN_IN_EMAILS_TABLE, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
2
+ import { AGENT_TASKS_TABLE, ATTACHMENTS_TABLE, LEGACY_ATTACHMENTS_TABLE, SIGN_IN_EMAILS_TABLE, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
3
3
  import { CONFIRMATION_TABLE } from "../runtime/confirmations.js";
4
4
  import { CHANNEL_ORIGIN_SQL, USER_IDENTITY_TABLE } from "../runtime/user-identity-sql.js";
5
5
  import { AGENT_COUNTERS_TABLE, AGENT_GUEST_LINKS_TABLE, AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, API_KEYS_TABLE, CHAT_MESSAGES_TABLE, IDENTITY_NOTICES_TABLE, OAUTH_TABLES, quoteIdentifier, } from "./storage.js";
@@ -39,6 +39,9 @@ export function preparePreviewDatabase({ sql, }) {
39
39
  SIGN_IN_EMAILS_TABLE,
40
40
  WORKFLOW_STARTS_TABLE,
41
41
  ATTACHMENTS_TABLE,
42
+ // @legacy ontology.agent-attachments-view: a copy of a database not yet migrated holds
43
+ // them under the earlier name; once renamed, that name is a view and not listed here.
44
+ LEGACY_ATTACHMENTS_TABLE,
42
45
  "_blob_action_results",
43
46
  "_blob_uploads",
44
47
  "_ontology_replication_delivery",
@@ -18,9 +18,9 @@ export declare const READ_VERB = "read";
18
18
  * App feedback is a member's own message to the builders: a member reads theirs through a scoped
19
19
  * `app_feedback:read`, and only the builders' roles name it unscoped. The built-in sign-in
20
20
  * identities hold every member's external accounts, such as their Telegram id and username: only a
21
- * role that names `user_identity` reads them. The agent's held replies hold the workspace's
22
- * chats with the people it serves, and approving one sends it: only a role that names
23
- * `agent_review` reads, approves or rejects them. The workspace's deployed code is no entity, but
21
+ * role that names `user_identity` reads them. The agent's drafts hold the workspace's chats with
22
+ * the people it serves, and approving one sends it: only a role that names `agent_draft` reads,
23
+ * approves or rejects them. The workspace's deployed code is no entity, but
24
24
  * {@link WORKSPACE_SOURCE_PERMISSION} is covered the same way: `workspace_source:read` or
25
25
  * `workspace_source:*` grants it, `*:read` and `*:*` do not. So are the agent's own tools
26
26
  * ({@link AGENT_TOOL}): `agent_tool:sandbox` or `agent_tool:*` grants the sandbox, `*:*` does not.
@@ -189,8 +189,8 @@ export interface PermissionHolder {
189
189
  }
190
190
  /**
191
191
  * Whether the principal may do what `required` names: one of its permissions equals it, or covers it
192
- * through a wildcard. Workload wildcards exclude user administration, feedback, the agent's held
193
- * replies, the workspace's code, the agent's own tools and writes of sign-in identities.
192
+ * through a wildcard. Workload wildcards exclude user administration, feedback, the agent's
193
+ * drafts, the workspace's code, the agent's own tools and writes of sign-in identities.
194
194
  */
195
195
  export declare function isAllowed(required: string, principal: PermissionHolder): boolean;
196
196
  /**
@@ -1,3 +1,4 @@
1
+ import { currentEntityName } from "./earlier-entity-names.js";
1
2
  import { GENERATED_UPSERT_PREFIX, toSnakeCase } from "./names.js";
2
3
  /**
3
4
  * Permissions are `<entity>:<verb>` strings. The verbs are `read`, the generated `create`, `update`,
@@ -17,16 +18,16 @@ export const READ_VERB = "read";
17
18
  * App feedback is a member's own message to the builders: a member reads theirs through a scoped
18
19
  * `app_feedback:read`, and only the builders' roles name it unscoped. The built-in sign-in
19
20
  * identities hold every member's external accounts, such as their Telegram id and username: only a
20
- * role that names `user_identity` reads them. The agent's held replies hold the workspace's
21
- * chats with the people it serves, and approving one sends it: only a role that names
22
- * `agent_review` reads, approves or rejects them. The workspace's deployed code is no entity, but
21
+ * role that names `user_identity` reads them. The agent's drafts hold the workspace's chats with
22
+ * the people it serves, and approving one sends it: only a role that names `agent_draft` reads,
23
+ * approves or rejects them. The workspace's deployed code is no entity, but
23
24
  * {@link WORKSPACE_SOURCE_PERMISSION} is covered the same way: `workspace_source:read` or
24
25
  * `workspace_source:*` grants it, `*:read` and `*:*` do not. So are the agent's own tools
25
26
  * ({@link AGENT_TOOL}): `agent_tool:sandbox` or `agent_tool:*` grants the sandbox, `*:*` does not.
26
27
  */
27
28
  export const EXPLICIT_GRANT_ENTITIES = new Set([
28
29
  "agent_chat",
29
- "agent_review",
30
+ "agent_draft",
30
31
  "agent_tool",
31
32
  "app_feedback",
32
33
  "app_feedback_file",
@@ -166,10 +167,12 @@ export function grants(granted, required) {
166
167
  if (held === undefined || wanted === undefined) {
167
168
  return false;
168
169
  }
169
- const entity = toSnakeCase(wanted.entity);
170
+ // @legacy ontology.agent-review-entity-name
171
+ // A credential or caller may still name a built-in entity by its earlier name.
172
+ const entity = currentEntityName(toSnakeCase(wanted.entity));
170
173
  return ((held.entity === WILDCARD
171
174
  ? wildcardEntityCovers(entity, toSnakeCase(wanted.verb))
172
- : toSnakeCase(held.entity) === entity) &&
175
+ : currentEntityName(toSnakeCase(held.entity)) === entity) &&
173
176
  (held.verb === WILDCARD || toSnakeCase(held.verb) === toSnakeCase(wanted.verb)));
174
177
  }
175
178
  /**
@@ -194,7 +197,7 @@ function isUserAdministration(required) {
194
197
  !wildcardEntityCovers(USER_ENTITY, toSnakeCase(wanted.verb)));
195
198
  }
196
199
  /**
197
- * Feedback, the agent's held replies, the workspace's code and the agent's own tools always need
200
+ * Feedback, the agent's drafts, the workspace's code and the agent's own tools always need
198
201
  * named grants, including for pipeline workloads: nothing a pipeline runs approves a reply to a
199
202
  * person, reads the code it runs from or uses the agent's tools. The workspace's own pipeline code reads sign-in identities, for example to address a
200
203
  * member in a chat, but nothing it runs writes them: only the platform does.
@@ -206,12 +209,12 @@ function workloadWildcardCovers(required) {
206
209
  }
207
210
  return (!isUserAdministration(required) &&
208
211
  (wanted === undefined ||
209
- !["app_feedback", "app_feedback_file", "agent_review", WORKSPACE_SOURCE, AGENT_TOOL].includes(toSnakeCase(wanted.entity))));
212
+ !["app_feedback", "app_feedback_file", "agent_draft", WORKSPACE_SOURCE, AGENT_TOOL].includes(currentEntityName(toSnakeCase(wanted.entity)))));
210
213
  }
211
214
  /**
212
215
  * Whether the principal may do what `required` names: one of its permissions equals it, or covers it
213
- * through a wildcard. Workload wildcards exclude user administration, feedback, the agent's held
214
- * replies, the workspace's code, the agent's own tools and writes of sign-in identities.
216
+ * through a wildcard. Workload wildcards exclude user administration, feedback, the agent's
217
+ * drafts, the workspace's code, the agent's own tools and writes of sign-in identities.
215
218
  */
216
219
  export function isAllowed(required, principal) {
217
220
  return (
@@ -1,7 +1,7 @@
1
1
  import type { EntityManifest } from "./manifest.js";
2
2
  /**
3
3
  * The built-in entities whose records replicate to the warehouse as the workspace's own stored
4
- * entities do: the chats the agent is in, the replies held for review and the feedback members send
4
+ * entities do: the chats the agent is in, the agent's drafts and the feedback members send
5
5
  * the builders, which metrics and warehouse models join to the workspace's data. The user and their
6
6
  * sign-in identities stay in the ontology: no metric route passes through the user
7
7
  * (`metric-routes.ts`), and an identity is how a person signs in, nothing a query counts.
@@ -1,12 +1,15 @@
1
1
  /**
2
2
  * The built-in entities whose records replicate to the warehouse as the workspace's own stored
3
- * entities do: the chats the agent is in, the replies held for review and the feedback members send
3
+ * entities do: the chats the agent is in, the agent's drafts and the feedback members send
4
4
  * the builders, which metrics and warehouse models join to the workspace's data. The user and their
5
5
  * sign-in identities stay in the ontology: no metric route passes through the user
6
6
  * (`metric-routes.ts`), and an identity is how a person signs in, nothing a query counts.
7
7
  */
8
8
  export const REPLICATED_BUILT_IN_ENTITIES = new Set([
9
9
  "agent_chat",
10
+ "agent_draft",
11
+ // @legacy ontology.agent-review-entity-name
12
+ // The drafts' earlier name, in manifests an earlier SDK built, which the platform still reads.
10
13
  "agent_review",
11
14
  "app_feedback",
12
15
  ]);
@@ -22,10 +22,32 @@ export declare const REPLICATION_TABLE_SCHEMA: readonly [...{
22
22
  * ranking. The struct separates business field names from the private ranking/deletion columns.
23
23
  * Events captured before a field was added lack its key and read as its fill.
24
24
  */
25
- export declare function replicationViews(project: string, target: ReplicationTarget): {
25
+ export declare function replicationViews(project: string, target: ReplicationTarget): ({
26
26
  readonly name: string;
27
27
  readonly query: string;
28
- }[];
28
+ } | {
29
+ readonly name: string;
30
+ readonly query: string;
31
+ })[];
32
+ /**
33
+ * The views, and for each entity the manifest declares `renamedFrom` on, its own and its
34
+ * relationships' views again under the earlier name, so that warehouse queries written for that
35
+ * name keep working while the entity declares it. A view of that name already in the set wins:
36
+ * while an epoch of the earlier manifest still serves, its view is the real one.
37
+ */
38
+ export declare function withEarlierNameViews<V extends {
39
+ readonly name: string;
40
+ readonly query: string;
41
+ }>(views: ReadonlyArray<V>, entities: ReadonlyArray<{
42
+ readonly name: string;
43
+ readonly renamedFrom?: string | undefined;
44
+ readonly relationships: ReadonlyArray<{
45
+ readonly name: string;
46
+ }>;
47
+ }>): Array<V | {
48
+ readonly name: string;
49
+ readonly query: string;
50
+ }>;
29
51
  /**
30
52
  * A single BigQuery statement observes one selector snapshot across every entity/edge join.
31
53
  * Each branch has a literal generation predicate for clustered pruning. Unsupported new columns
@@ -118,7 +118,35 @@ export function replicationViews(project, target) {
118
118
  if (new Set(result.map((v) => v.name.toLowerCase())).size !== result.length) {
119
119
  throw new Error("Replicated entity and relationship view names collide.");
120
120
  }
121
- return result;
121
+ // @legacy ontology.replication-earlier-entity-views
122
+ return withEarlierNameViews(result, target.manifest.entities);
123
+ }
124
+ // @legacy ontology.replication-earlier-entity-views
125
+ /**
126
+ * The views, and for each entity the manifest declares `renamedFrom` on, its own and its
127
+ * relationships' views again under the earlier name, so that warehouse queries written for that
128
+ * name keep working while the entity declares it. A view of that name already in the set wins:
129
+ * while an epoch of the earlier manifest still serves, its view is the real one.
130
+ */
131
+ export function withEarlierNameViews(views, entities) {
132
+ const names = new Set(views.map((view) => view.name.toLowerCase()));
133
+ const aliases = [];
134
+ for (const entity of entities) {
135
+ const earlier = entity.renamedFrom;
136
+ if (earlier === undefined || earlier === entity.name) {
137
+ continue;
138
+ }
139
+ for (const suffix of ["", ...entity.relationships.map(({ name }) => `_${name}`)]) {
140
+ const view = views.find((candidate) => candidate.name === `${entity.name}${suffix}`);
141
+ const alias = `${earlier}${suffix}`;
142
+ if (view === undefined || names.has(alias.toLowerCase())) {
143
+ continue;
144
+ }
145
+ names.add(alias.toLowerCase());
146
+ aliases.push({ name: alias, query: view.query });
147
+ }
148
+ }
149
+ return [...views, ...aliases];
122
150
  }
123
151
  /**
124
152
  * A single BigQuery statement observes one selector snapshot across every entity/edge join.
@@ -130,6 +158,8 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
130
158
  const scopes = new Map();
131
159
  // A placeholder is typed like the column it stands in for; an untyped NULL reads as INT64.
132
160
  const types = new Map();
161
+ // The types an earlier name's view takes from a renamed entity's branches, behind its own.
162
+ const aliasTypes = new Map();
133
163
  for (const epoch of epochs) {
134
164
  for (const route of epoch.routes) {
135
165
  const entity = route.target.manifest.entities.find((item) => item.id === route.entityId);
@@ -145,12 +175,7 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
145
175
  ?.fields.includes(field.name))
146
176
  : [];
147
177
  const columns = route.kind === "entity" ? fields.map((field) => field.name) : ["source_id", "target_id"];
148
- for (const field of fields) {
149
- // Use the latest incarnation's type; older property identities remain policy-gated.
150
- types.set(`${name}.${field.name}`, columnType(field.storage));
151
- }
152
- const entries = scopes.get(name) ?? [];
153
- entries.push({
178
+ const branch = {
154
179
  epoch: epoch.id,
155
180
  query: view.query,
156
181
  columns,
@@ -158,13 +183,42 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
158
183
  kind: route.kind,
159
184
  generation: route.target.generation,
160
185
  target: route.target,
161
- });
162
- scopes.set(name, entries);
186
+ };
187
+ // @legacy ontology.replication-earlier-entity-views
188
+ // A renamed entity's branches also serve its view under the earlier name, beside an earlier
189
+ // epoch's own branches of that view, so the view reads across the selector's switch.
190
+ const earlier = entity.renamedFrom !== undefined && entity.renamedFrom !== entity.name
191
+ ? `${entity.renamedFrom}${name.slice(entity.name.length)}`
192
+ : undefined;
193
+ for (const [scope, alias] of [
194
+ [name, false],
195
+ ...(earlier === undefined ? [] : [[earlier, true]]),
196
+ ]) {
197
+ for (const field of fields) {
198
+ // Use the latest incarnation's type; older property identities remain policy-gated.
199
+ (alias ? aliasTypes : types).set(`${scope}.${field.name}`, columnType(field.storage));
200
+ }
201
+ const entries = scopes.get(scope) ?? [];
202
+ entries.push(alias ? { ...branch, alias: true } : branch);
203
+ scopes.set(scope, entries);
204
+ }
205
+ }
206
+ }
207
+ // @legacy ontology.replication-earlier-entity-views
208
+ // An earlier name that is another entity's or relationship's view in some epoch stays that view's.
209
+ for (const [name, branches] of scopes) {
210
+ const routes = new Set(branches
211
+ .filter((branch) => branch.alias === true)
212
+ .map((branch) => `${branch.entityId}:${branch.kind}`));
213
+ if (routes.size > 0 &&
214
+ branches.some((branch) => branch.alias !== true && !routes.has(`${branch.entityId}:${branch.kind}`))) {
215
+ scopes.set(name, branches.filter((branch) => branch.alias !== true));
163
216
  }
164
217
  }
165
- return [...scopes].map(([name, branches]) => {
218
+ const views = [...scopes].map(([name, branches]) => {
166
219
  const columns = [...new Set(branches.flatMap((branch) => branch.columns))];
167
- const empty = (column) => `CAST(NULL AS ${types.get(`${name}.${column}`) ?? "STRING"})`;
220
+ const renamedScope = branches.some((branch) => branch.alias === true);
221
+ const empty = (column) => `CAST(NULL AS ${types.get(`${name}.${column}`) ?? aliasTypes.get(`${name}.${column}`) ?? "STRING"})`;
168
222
  const missing = `NOT EXISTS(SELECT 1 FROM \`${project}.ontology_internal.serving_selector\` selector
169
223
  JOIN \`${project}.ontology_internal.epoch_routes\` route ON route.epoch=selector.epoch
170
224
  WHERE selector.singleton AND (${branches
@@ -193,7 +247,12 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
193
247
  .map((branch) => `SELECT ${columns
194
248
  .map((column) => branch.columns.includes(column) && permitted(branch, column)
195
249
  ? `data.${identifier(column)}`
196
- : `IF(EXISTS(SELECT 1 FROM \`${project}.ontology_internal.serving_selector\`
250
+ : // @legacy ontology.replication-earlier-entity-views
251
+ // A view under an earlier name reads a column one incarnation of the
252
+ // entity lacks, such as a property's earlier name, as null.
253
+ renamedScope && !branch.columns.includes(column)
254
+ ? `${empty(column)} AS ${identifier(column)}`
255
+ : `IF(EXISTS(SELECT 1 FROM \`${project}.ontology_internal.serving_selector\`
197
256
  WHERE singleton AND epoch=${literal(branch.epoch)}),
198
257
  ERROR('ontology_coverage_unavailable'), ${empty(column)}) AS ${identifier(column)}`)
199
258
  .join(", ")}
@@ -208,4 +267,5 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
208
267
  WHERE IF(${missing}, ERROR('ontology_unavailable'), FALSE)`,
209
268
  };
210
269
  });
270
+ return views;
211
271
  }
@@ -7,7 +7,7 @@ import { type AgentResolution, type AgentStore, type ResolveIdentityInput, type
7
7
  * `now()`; a pairing is consumed with one `UPDATE … WHERE "consumed_at" IS NULL`, so two concurrent
8
8
  * approvals cannot both succeed. Ids that are not UUIDs find nothing rather than fail the cast.
9
9
  * Chats the agent sees are `agent_chat` records, written as the platform with history. Holding a
10
- * reply is admitted, bounded and starts the workflows whose triggers on `agent_review` fire for it as
10
+ * reply is admitted, bounded and starts the workflows whose triggers on `agent_draft` fire for it as
11
11
  * an action is and does, with the `ActionTransactions` and `WorkflowStarter` it runs with.
12
12
  */
13
13
  export declare function sqlAgentStore(ontology: Ontology, sql: SqlClient.SqlClient): AgentStore;