@segmentstream/ontology 0.142.0 → 0.144.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 CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.144.0
4
+
5
+ The built-in entities `agent_chat`, `agent_review` and `app_feedback` replicate to the warehouse
6
+ as a workspace's own stored entities do: metric queries reach them as stored entities, such as a
7
+ visit metric grouped by its chat's `provider` through `rel.one(AgentChat, { on: { chat: "key" } })`,
8
+ and warehouse models can join them. The built-in `user` and `user_identity` stay in the ontology.
9
+
10
+ A migration applies what its release replicates even when the ontology did not change: when the
11
+ source's projection lacks an entity the release replicates, the migration installs its capture and
12
+ a scoped repair copies that entity's records alone. Existing workspaces start replicating these
13
+ entities at their next deployment.
14
+
15
+ ## 0.143.0
16
+
17
+ Roles can grant the agent's web search: `agent_tool:web_search`. It gives a caller the agent's
18
+ `segmentstream__web_search`, which the agent has while its `web.search` is on, and like every agent
19
+ tool permission only a role that names it, or `agent_tool:*`, grants it. A website's guests may hold
20
+ it in `guest.permissions`, beside `agent_tool:web`; each search is billed to the workspace.
21
+
3
22
  ## 0.142.0
4
23
 
5
24
  Every `agent_chat` record has a unique `key`, `<provider>:<externalId>`, such as
package/dist/analyze.js CHANGED
@@ -1965,14 +1965,14 @@ function checkGuestPermissions(input) {
1965
1965
  report("invalid-sign-in", path, `Guests cannot hold ${String(permission)}; name the entities they may use.`);
1966
1966
  continue;
1967
1967
  }
1968
- // A guest may read the agent's listed websites, never run its sandbox or drive its browser:
1969
- // the sandbox reads the records and files of whoever it acts for, the browser acts with the
1970
- // agent's own sign-ins, and anyone opening the website is a guest.
1968
+ // A guest may read the agent's listed websites and search the web, never run its sandbox or
1969
+ // drive its browser: the sandbox reads the records and files of whoever it acts for, the
1970
+ // browser acts with the agent's own sign-ins, and anyone opening the website is a guest.
1971
1971
  if (parsed.entity === AGENT_TOOL &&
1972
1972
  (MEMBER_AGENT_TOOLS.has(parsed.verb) || parsed.verb === WILDCARD)) {
1973
1973
  report("invalid-sign-in", path, `Guests cannot hold ${String(permission)}: the agent's sandbox and browser are never a ` +
1974
- `guest's. Name ${AGENT_TOOL}:web alone for the agent to read the websites web.domains ` +
1975
- "lists for them.");
1974
+ `guest's. Name ${AGENT_TOOL}:web for the agent to read the websites web.domains lists ` +
1975
+ `for them, and ${AGENT_TOOL}:web_search for it to search the web for them.`);
1976
1976
  continue;
1977
1977
  }
1978
1978
  const entity = entities.find((resolved) => resolved.name === parsed.entity);
@@ -2931,7 +2931,7 @@ function knownVerbs(entities, toolsets = []) {
2931
2931
  verbs.set(AGENT_TOOL, new Set(AGENT_TOOL_VERBS));
2932
2932
  return verbs;
2933
2933
  }
2934
- /** `agent_tool:sandbox, agent_tool:web and agent_tool:browser`, for messages. */
2934
+ /** `agent_tool:sandbox, agent_tool:web, agent_tool:browser and agent_tool:web_search`, for messages. */
2935
2935
  function agentToolPermissions() {
2936
2936
  return listed(AGENT_TOOL_VERBS.map((tool) => `${AGENT_TOOL}:${tool}`));
2937
2937
  }
@@ -1,3 +1,4 @@
1
+ import { replicatesRecords } from "./replicated-entities.js";
1
2
  import { User } from "./user.js";
2
3
  /** A route follows at most this many to-one relationships. */
3
4
  export const MAXIMUM_HOPS = 2;
@@ -114,9 +115,9 @@ class RouteCatalog {
114
115
  modelMetric(name) {
115
116
  return this.metrics.get(name) ?? refuse(unknownModelMetricMessage(name));
116
117
  }
117
- /** Declared properties, and the `id` of a stored entity, which its replication view carries. */
118
+ /** Declared properties, and the `id` of a replicated entity, which its replication view carries. */
118
119
  hasProperty(entity, property) {
119
- const stored = entity.source === undefined && !entity.builtIn;
120
+ const stored = replicatesRecords(entity);
120
121
  return (entity.properties.some((candidate) => candidate.name === property) ||
121
122
  (stored && property === "id"));
122
123
  }
@@ -9,6 +9,7 @@ import { isNoopMigration, NO_RETAINED_STORAGE, RetainedStorage, retainedStorageJ
9
9
  import { planMigration } from "./plan.js";
10
10
  import { REPLICATION_DDL, REPLICATION_SCHEMA_VERSION, replicationEntityDdl, } from "../replication/schema.js";
11
11
  import { lockReplicationLifecycle, lockReplicationSchema } from "../replication/transaction.js";
12
+ import { replicatesRecords } from "../replicated-entities.js";
12
13
  import { prepareReplicationBaseline } from "../replication/baseline.js";
13
14
  import { randomUUID } from "node:crypto";
14
15
  import { recordWorkflowCatalog } from "../runtime/workflow-admission.js";
@@ -206,6 +207,14 @@ const runStep = (sql, step, stepIndex) => Effect.forEach(step.sql, (statement) =
206
207
  const reconcileWorkflows = (plan, sql, workspaceId) => workspaceId === undefined
207
208
  ? Effect.void
208
209
  : reconcileWorkflowUsers(principalsOntology(plan.target), { workspaceId }).pipe(Effect.provideService(SqlClient.SqlClient, sql));
210
+ /**
211
+ * Whether a source's recorded projection holds every entity this release replicates. One recorded
212
+ * before the release replicated an entity, such as a built-in entity, is not current: the
213
+ * migration installs its capture and copies it, though the ontology did not change.
214
+ */
215
+ function replicatesAll(manifest, recorded) {
216
+ return resolveReplicationSelection(manifest).every((item) => recorded !== null && Object.hasOwn(recorded, item.entityId));
217
+ }
209
218
  /**
210
219
  * Applies a plan. Non-transactional steps run first, each statement on its own; they only add enum
211
220
  * values and build indexes concurrently, and are safe to run again after a failure. Every other step
@@ -246,7 +255,8 @@ export const applyMigration = (plan, options) => Effect.gen(function* () {
246
255
  const targetPresent = presence.targets && isNoopMigration(plan)
247
256
  ? "EXISTS (SELECT 1 FROM _ontology_replication_targets WHERE generation = active_generation)"
248
257
  : "false";
249
- const [state] = yield* sql.unsafe(`SELECT version, enabled, ${targetPresent} AS target_present FROM _ontology_replication WHERE id`);
258
+ const [state] = yield* sql.unsafe(`SELECT version, enabled, ${targetPresent} AS target_present, selection
259
+ FROM _ontology_replication WHERE id`);
250
260
  if (state?.enabled &&
251
261
  state.version >= 6 &&
252
262
  (yield* requestShadowSupersession(sql, plan.target.hash))) {
@@ -263,7 +273,7 @@ export const applyMigration = (plan, options) => Effect.gen(function* () {
263
273
  }
264
274
  if (isNoopMigration(plan) &&
265
275
  state?.version === REPLICATION_SCHEMA_VERSION &&
266
- (!state.enabled || state.target_present)) {
276
+ (!state.enabled || (state.target_present && replicatesAll(plan.target, state.selection)))) {
267
277
  if (options.workspaceId !== undefined || syncsIdentities(plan)) {
268
278
  // Under the migration lock, and only while the catalog still records this target.
269
279
  yield* sql.withTransaction(Effect.gen(function* () {
@@ -340,11 +350,9 @@ export const applyMigration = (plan, options) => Effect.gen(function* () {
340
350
  ],
341
351
  });
342
352
  }
353
+ // A runtime-only upgrade adopts the projection this release replicates as well, such as
354
+ // the built-in entities it replicates; only a projection that changed is scanned again.
343
355
  active = target;
344
- // A runtime-only upgrade retains an existing explicit projection.
345
- if (target?.manifest.hash === plan.target.hash) {
346
- refresh = target.selection;
347
- }
348
356
  // Both writer protocols are fenced. Capture and schema changes commit on one stream.
349
357
  yield* sql.unsafe("UPDATE _ontology_replication SET enabled = false WHERE id");
350
358
  }
@@ -363,8 +371,7 @@ export const applyMigration = (plan, options) => Effect.gen(function* () {
363
371
  }
364
372
  if (plan.steps.some((step) => step.kind === "dropEntities")) {
365
373
  for (const entity of previousManifest?.entities ?? []) {
366
- if (!entity.builtIn &&
367
- !entity.source &&
374
+ if (replicatesRecords(entity) &&
368
375
  !plan.target.entities.some((item) => item.id === entity.id)) {
369
376
  yield* retainReplicationHistory(sql, historyTableName(entity.name));
370
377
  }
@@ -47,10 +47,11 @@ export declare const AGENT_TOOL = "agent_tool";
47
47
  * sandbox, copying a file the caller may read into it, and calling an ontology tool with arguments
48
48
  * from a file in it. `web`: fetching pages from the domains the agent's configuration lists.
49
49
  * `browser`: driving the agent's own cloud browser, signed in as the agent, with scripts that run
50
- * in its sandbox, so the agent also needs the sandbox for it. The agent's configuration says which
51
- * of them the agent has; the roles say which caller gets them.
50
+ * in its sandbox, so the agent also needs the sandbox for it. `web_search`: searching the web, each
51
+ * search billed to the workspace. The agent's configuration says which of them the agent has; the
52
+ * roles say which caller gets them.
52
53
  */
53
- export declare const AGENT_TOOL_VERBS: readonly ["sandbox", "web", "browser"];
54
+ export declare const AGENT_TOOL_VERBS: readonly ["sandbox", "web", "browser", "web_search"];
54
55
  export type AgentToolVerb = (typeof AGENT_TOOL_VERBS)[number];
55
56
  /**
56
57
  * The agent tools no guest may hold, whoever opens the website: the sandbox reads the records and
@@ -54,10 +54,11 @@ export const AGENT_TOOL = "agent_tool";
54
54
  * sandbox, copying a file the caller may read into it, and calling an ontology tool with arguments
55
55
  * from a file in it. `web`: fetching pages from the domains the agent's configuration lists.
56
56
  * `browser`: driving the agent's own cloud browser, signed in as the agent, with scripts that run
57
- * in its sandbox, so the agent also needs the sandbox for it. The agent's configuration says which
58
- * of them the agent has; the roles say which caller gets them.
57
+ * in its sandbox, so the agent also needs the sandbox for it. `web_search`: searching the web, each
58
+ * search billed to the workspace. The agent's configuration says which of them the agent has; the
59
+ * roles say which caller gets them.
59
60
  */
60
- export const AGENT_TOOL_VERBS = ["sandbox", "web", "browser"];
61
+ export const AGENT_TOOL_VERBS = ["sandbox", "web", "browser", "web_search"];
61
62
  /**
62
63
  * The agent tools no guest may hold, whoever opens the website: the sandbox reads the records and
63
64
  * files of whoever the agent acts for, and the browser acts with the agent's own sign-ins.
@@ -0,0 +1,14 @@
1
+ import type { EntityManifest } from "./manifest.js";
2
+ /**
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
5
+ * the builders, which metrics and warehouse models join to the workspace's data. The user and their
6
+ * sign-in identities stay in the ontology: no metric route passes through the user
7
+ * (`metric-routes.ts`), and an identity is how a person signs in, nothing a query counts.
8
+ */
9
+ export declare const REPLICATED_BUILT_IN_ENTITIES: ReadonlySet<string>;
10
+ /**
11
+ * Whether an entity's records replicate: every stored entity the workspace declares, and the
12
+ * built-in entities above. A warehouse-backed entity is the warehouse's already.
13
+ */
14
+ export declare function replicatesRecords(entity: Pick<EntityManifest, "name" | "builtIn" | "source">): boolean;
@@ -0,0 +1,20 @@
1
+ /**
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
4
+ * the builders, which metrics and warehouse models join to the workspace's data. The user and their
5
+ * sign-in identities stay in the ontology: no metric route passes through the user
6
+ * (`metric-routes.ts`), and an identity is how a person signs in, nothing a query counts.
7
+ */
8
+ export const REPLICATED_BUILT_IN_ENTITIES = new Set([
9
+ "agent_chat",
10
+ "agent_review",
11
+ "app_feedback",
12
+ ]);
13
+ /**
14
+ * Whether an entity's records replicate: every stored entity the workspace declares, and the
15
+ * built-in entities above. A warehouse-backed entity is the warehouse's already.
16
+ */
17
+ export function replicatesRecords(entity) {
18
+ return (entity.source === undefined &&
19
+ (!entity.builtIn || REPLICATED_BUILT_IN_ENTITIES.has(entity.name)));
20
+ }
@@ -6,6 +6,7 @@ import { REPLICATION_RECORD_BYTES, validateReplicationSelection, } from "./model
6
6
  import { REPLICATION_TARGET_FILLS, REPLICATION_TARGET_METADATA, snapshotExpression, } from "./schema.js";
7
7
  import { lockReplicationLifecycle, shareReplicationLifecycle } from "./transaction.js";
8
8
  import { TARGET_MANIFEST_HASH, storedTargets } from "./stored-manifests.js";
9
+ import { replicatesRecords } from "../replicated-entities.js";
9
10
  import { initialReplicationLineage } from "./lineage.js";
10
11
  import { scheduleReplicationScans, replicationScanScopes, advanceReplicationScan, } from "./scans.js";
11
12
  /** An initial scan cannot be created with the requested state or record projection. */
@@ -44,7 +45,7 @@ export const prepareReplicationBaseline = (sql, input) => sql.withTransaction(Ef
44
45
  });
45
46
  // Drain raw/pre-upgrade writers only at enablement; online pages never lock business tables.
46
47
  const tables = input.manifest.entities
47
- .filter((e) => e.source === undefined && !e.builtIn)
48
+ .filter(replicatesRecords)
48
49
  .flatMap((e) => [
49
50
  tableName(e.name),
50
51
  ...e.relationships
@@ -4,6 +4,7 @@ import { joinColumnNames, joinTableName, quoteIdentifier as qi, quoteLiteral as
4
4
  import { REPLICATION_RECORD_BYTES, validateReplicationSelection, } from "./model.js";
5
5
  import { snapshotExpression } from "./schema.js";
6
6
  import { createReplicationBaseline as createCurrentBaseline } from "./baseline.js";
7
+ import { replicatesRecords } from "../replicated-entities.js";
7
8
  import { lockReplicationLifecycle } from "./transaction.js";
8
9
  /** An initial snapshot cannot be created with the requested state or record projection. */
9
10
  export class ReplicationBaselineFailed extends Schema.TaggedError()("ReplicationBaselineFailed", { message: Schema.String, cause: Schema.optionalKey(Schema.Unknown) }) {
@@ -35,7 +36,7 @@ export const createReplicationBaseline = (sql, input) => sql.withTransaction(Eff
35
36
  });
36
37
  // Drain older SDK transactions that started before the compatibility fence was enabled.
37
38
  const tables = manifest.entities
38
- .filter((e) => e.source === undefined && !e.builtIn)
39
+ .filter(replicatesRecords)
39
40
  .flatMap((e) => [
40
41
  tableName(e.name),
41
42
  ...e.relationships
@@ -12,7 +12,10 @@ export interface ReplicationSelection {
12
12
  export type ReplicationFills = Readonly<Record<string, Readonly<Record<string, unknown>>>>;
13
13
  /** System fields and relationship keys always replicate. Only declared properties can opt out. */
14
14
  export declare function replicatedFields(entity: EntityManifest): import("../query.js").QueryField[];
15
- /** Every stored business entity and its relationships to other stored entities replicate. */
15
+ /**
16
+ * Every stored entity the workspace declares, the built-in entities that replicate, and their
17
+ * relationships to one another replicate.
18
+ */
16
19
  export declare function resolveReplicationSelection(manifest: OntologyManifest): ReadonlyArray<ReplicationSelection>;
17
20
  export interface ReplicationEvent {
18
21
  readonly event_id: string;
@@ -1,13 +1,17 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { queryFields } from "../query.js";
3
+ import { replicatesRecords } from "../replicated-entities.js";
3
4
  /** System fields and relationship keys always replicate. Only declared properties can opt out. */
4
5
  export function replicatedFields(entity) {
5
6
  const excluded = new Set(entity.properties.filter((p) => p.excludeFromReplication).map((p) => p.name));
6
7
  return queryFields(entity).filter((field) => !excluded.has(field.name));
7
8
  }
8
- /** Every stored business entity and its relationships to other stored entities replicate. */
9
+ /**
10
+ * Every stored entity the workspace declares, the built-in entities that replicate, and their
11
+ * relationships to one another replicate.
12
+ */
9
13
  export function resolveReplicationSelection(manifest) {
10
- const entities = manifest.entities.filter((entity) => !entity.builtIn && !entity.source);
14
+ const entities = manifest.entities.filter(replicatesRecords);
11
15
  const selection = entities.map((entity) => ({
12
16
  entityId: entity.id,
13
17
  fields: replicatedFields(entity).map((field) => field.name),
@@ -37,7 +41,7 @@ export function validateReplicationSelection(manifest, selection) {
37
41
  };
38
42
  for (const item of selection) {
39
43
  const entity = manifest.entities.find((e) => e.id === item.entityId);
40
- if (!entity || entity.builtIn || entity.source !== undefined) {
44
+ if (!entity || !replicatesRecords(entity)) {
41
45
  throw new Error(`Only stored business entities can be replicated: ${item.entityId}.`);
42
46
  }
43
47
  addView(entity.name);
@@ -50,7 +54,7 @@ export function validateReplicationSelection(manifest, selection) {
50
54
  const target = manifest.entities.find((e) => e.name === relationship?.to);
51
55
  if (relationship?.cardinality !== "many" ||
52
56
  !target ||
53
- target.builtIn ||
57
+ !replicatesRecords(target) ||
54
58
  !selection.some((s) => s.entityId === target.id)) {
55
59
  throw new Error(`Replication of ${entity.name}.${name} requires both business entities.`);
56
60
  }
@@ -1,5 +1,6 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { columnName, historyTableName, quoteIdentifier as qi, quoteLiteral as ql, relationshipColumnName, SYSTEM_COLUMNS, tableName, joinTableName, } from "../migrate/storage.js";
3
+ import { replicatesRecords } from "../replicated-entities.js";
3
4
  import { replicatedFields, REPLICATION_RECORD_BYTES } from "./model.js";
4
5
  import { SHADOW_REPLICATION_DDL, SHADOW_CONTROL_JOBS_DDL, SHADOW_SOURCE_REPLACEMENTS_DDL, } from "./shadow-schema.js";
5
6
  export const REPLICATION_SCHEMA_VERSION = 6;
@@ -138,9 +139,7 @@ export function snapshotExpression(entity, alias) {
138
139
  }
139
140
  /** History is the durable source. Triggers also reject old writers while capture is enabled. */
140
141
  export function replicationEntityDdl(manifest, previous) {
141
- return manifest.entities
142
- .filter((e) => e.source === undefined && !e.builtIn)
143
- .flatMap((entity) => {
142
+ return manifest.entities.filter(replicatesRecords).flatMap((entity) => {
144
143
  const old = previous?.entities.find((item) => item.id === entity.id);
145
144
  const compatible = {
146
145
  ...entity,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.142.0",
3
+ "version": "0.144.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",