@segmentstream-cloud/ontology 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -53,6 +53,7 @@ export { makeOntologyRuntime } from "./runtime/sql-runtime.js";
53
53
  export type { SqlOntologyRuntime, WriteScope } from "./runtime/sql-runtime.js";
54
54
  export { BASE_PATH, deriveApi } from "./service/api.js";
55
55
  export type { DeriveApiOptions, OntologyApi } from "./service/api.js";
56
+ export { WorkloadExecution } from "./runtime/workload.js";
56
57
  export { CurrentPrincipal } from "./service/authentication.js";
57
58
  export { deployedConnections, LocalConnectionContextError, localConnections, OIDC_TOKEN_HEADER, parseLocalConnectionContext, readLocalConnectionContext, RUNTIME_CONTEXT_FD, } from "./service/connections.js";
58
59
  export type { ConnectionAudit, ConnectionTransportFactory, LocalConnection, LocalConnectionContext, } from "./service/connections.js";
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ export { backendLayer, postgresLayer, sqlServiceRuntime } from "./runtime/servic
31
31
  export { makeOntologyRuntime } from "./runtime/sql-runtime.js";
32
32
  // The HTTP service derived from the ontology.
33
33
  export { BASE_PATH, deriveApi } from "./service/api.js";
34
+ export { WorkloadExecution } from "./runtime/workload.js";
34
35
  export { CurrentPrincipal } from "./service/authentication.js";
35
36
  export { deployedConnections, LocalConnectionContextError, localConnections, OIDC_TOKEN_HEADER, parseLocalConnectionContext, readLocalConnectionContext, RUNTIME_CONTEXT_FD, } from "./service/connections.js";
36
37
  export { configFromEnv, AGENT_HOST_VARIABLE, ENVIRONMENT_VARIABLES, NEON_AUTH_ENVIRONMENT_VARIABLES, OntologyServiceConfigError, RUNTIME_IDENTITY_ENVIRONMENT_VARIABLES, } from "./service/config.js";
@@ -449,6 +449,7 @@ export declare const OntologyManifestWire: Schema.encodeKeys<Schema.Struct<{
449
449
  }>, {
450
450
  readonly builtIn: "built_in";
451
451
  }>>;
452
+ readonly runtimeCapabilities: Schema.optionalKey<Schema.$Array<Schema.String>>;
452
453
  readonly version: Schema.Int;
453
454
  /** `sha256:` and the hex digest of the canonical JSON of the manifest without `hash`. */
454
455
  readonly hash: Schema.String;
@@ -619,6 +620,7 @@ export declare const OntologyManifest: Schema.Union<readonly [Schema.encodeKeys<
619
620
  }>, {
620
621
  readonly builtIn: "built_in";
621
622
  }>>;
623
+ readonly runtimeCapabilities: Schema.optionalKey<Schema.$Array<Schema.String>>;
622
624
  readonly version: Schema.Int;
623
625
  /** `sha256:` and the hex digest of the canonical JSON of the manifest without `hash`. */
624
626
  readonly hash: Schema.String;
@@ -653,6 +655,7 @@ export declare const OntologyManifest: Schema.Union<readonly [Schema.encodeKeys<
653
655
  }>, {
654
656
  readonly signIn: "sign_in";
655
657
  }>, Schema.Struct<{
658
+ readonly runtimeCapabilities: Schema.optionalKey<Schema.$Array<Schema.String>>;
656
659
  readonly version: Schema.Int;
657
660
  /** `sha256:` and the hex digest of the canonical JSON of the manifest without `hash`. */
658
661
  readonly hash: Schema.String;
@@ -929,6 +932,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
929
932
  readonly key: string;
930
933
  readonly description: string;
931
934
  }[] | undefined;
935
+ readonly runtimeCapabilities?: readonly string[] | undefined;
932
936
  readonly signIn?: {
933
937
  readonly policy: string;
934
938
  } | undefined;
@@ -1047,6 +1051,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
1047
1051
  readonly key: string;
1048
1052
  readonly description: string;
1049
1053
  }[] | undefined;
1054
+ readonly runtimeCapabilities?: readonly string[] | undefined;
1050
1055
  readonly signIn?: {
1051
1056
  readonly policy: string;
1052
1057
  } | undefined;
package/dist/manifest.js CHANGED
@@ -213,6 +213,7 @@ export const SignInManifest = Schema.Struct({
213
213
  * `onDelete`) are validated because the migration planner must map every value to storage.
214
214
  */
215
215
  const OntologyManifestModel = Schema.Struct({
216
+ runtimeCapabilities: Schema.optionalKey(Schema.Array(Schema.String)),
216
217
  version: Schema.Int,
217
218
  /** `sha256:` and the hex digest of the canonical JSON of the manifest without `hash`. */
218
219
  hash: Schema.String,
@@ -462,6 +463,7 @@ export function buildManifest(input) {
462
463
  .map((candidate) => candidate.name));
463
464
  const body = {
464
465
  version: MANIFEST_VERSION,
466
+ runtimeCapabilities: ["pipeline-workload-tokens-v1"],
465
467
  entities: input.entities.map((candidate) => entityManifest({ resolved: candidate, warehouseBacked })),
466
468
  roles: input.roles.map((candidate) => ({
467
469
  name: candidate.name,
@@ -7,7 +7,7 @@ import { REPLICATION_DDL, REPLICATION_SCHEMA_VERSION, replicationEntityDdl, } fr
7
7
  import { replicationMigrationRefusals } from "../replication/migrate.js";
8
8
  import { ATTACHMENTS_TABLE } from "../storage/tables.js";
9
9
  import { resolveReplicationSelection, } from "../replication/model.js";
10
- import { AGENT_TABLES, API_KEYS_TABLE, CATALOG_TABLE, OAUTH_TABLES, USER_ROLES_TABLE, quoteIdentifier, quoteLiteral, } from "./storage.js";
10
+ import { ACTION_ATTRIBUTION_TABLE, AGENT_TABLES, API_KEYS_TABLE, CATALOG_TABLE, OAUTH_TABLES, USER_ROLES_TABLE, quoteIdentifier, quoteLiteral, } from "./storage.js";
11
11
  const CATALOG = quoteIdentifier(CATALOG_TABLE);
12
12
  /** Serializes migrations of one database; transaction-scoped, so it ends with the transaction. */
13
13
  const MIGRATION_LOCK = "segmentstream.ontology.migration";
@@ -180,6 +180,7 @@ export const applyMigration = (plan, options) => Effect.gen(function* () {
180
180
  */
181
181
  /** The runtime's tables, beside the catalog and the entity tables. */
182
182
  export const RUNTIME_TABLES = [
183
+ ACTION_ATTRIBUTION_TABLE,
183
184
  USER_ROLES_TABLE,
184
185
  API_KEYS_TABLE,
185
186
  ...OAUTH_TABLES,
@@ -152,6 +152,7 @@ export declare const MigrationPlan: Schema.Struct<{
152
152
  }>, {
153
153
  readonly builtIn: "built_in";
154
154
  }>>;
155
+ readonly runtimeCapabilities: Schema.optionalKey<Schema.$Array<Schema.String>>;
155
156
  readonly version: Schema.Int;
156
157
  readonly hash: Schema.String;
157
158
  readonly signIn: Schema.optionalKey<Schema.Struct<{
@@ -174,6 +175,7 @@ export declare const MigrationPlan: Schema.Struct<{
174
175
  }>, {
175
176
  readonly signIn: "sign_in";
176
177
  }>, Schema.Struct<{
178
+ readonly runtimeCapabilities: Schema.optionalKey<Schema.$Array<Schema.String>>;
177
179
  readonly version: Schema.Int;
178
180
  readonly hash: Schema.String;
179
181
  readonly entities: Schema.$Array<Schema.Struct<{
@@ -1,7 +1,7 @@
1
1
  import { isValueMatched, } from "../manifest.js";
2
2
  import { ATTACHMENTS_TABLE, ATTACHMENT_TABLES_DDL, BLOB_TABLES_DDL } from "../storage/tables.js";
3
3
  import { SNAKE_CASE_NAME, isSystemProperty, toSnakeCase } from "../names.js";
4
- import { AGENT_TABLES, AGENT_TABLES_DDL, CATALOG_TABLE_DDL, MAX_IDENTIFIER_LENGTH, OAUTH_TABLES, OAUTH_TABLES_DDL, USER_TABLE, addConstraintStatement, columnDefinition, constraintDefinition, createEnumStatement, createIndexStatement, createTableStatement, defaultExpression, entityStorage, enumTypeName, fieldColumnName, joinTable, propertyCheck, blobForeignKey, propertyColumn, propertyEnum, quoteIdentifier, quoteLiteral, relationshipColumn, RUNTIME_TABLES_DDL, relationshipForeignKey, SYSTEM_COLUMNS, tableName, } from "./storage.js";
4
+ import { ACTION_ATTRIBUTION_TABLE, ACTION_ATTRIBUTION_DDL, AGENT_TABLES, AGENT_TABLES_DDL, CATALOG_TABLE_DDL, MAX_IDENTIFIER_LENGTH, OAUTH_TABLES, OAUTH_TABLES_DDL, USER_TABLE, addConstraintStatement, columnDefinition, constraintDefinition, createEnumStatement, createIndexStatement, createTableStatement, defaultExpression, entityStorage, enumTypeName, fieldColumnName, joinTable, propertyCheck, blobForeignKey, propertyColumn, propertyEnum, quoteIdentifier, quoteLiteral, relationshipColumn, RUNTIME_TABLES_DDL, relationshipForeignKey, SYSTEM_COLUMNS, tableName, } from "./storage.js";
5
5
  const GLOBAL_KINDS = new Set(["table", "type", "index"]);
6
6
  function step(input) {
7
7
  return {
@@ -77,6 +77,14 @@ export function planMigration(input) {
77
77
  else {
78
78
  // A database an earlier SDK migrated lacks the tables added since; each set arrives on its own.
79
79
  const lacks = (tables) => tables.some((table) => input.existingTables?.has(table) === false);
80
+ if (lacks([ACTION_ATTRIBUTION_TABLE])) {
81
+ draft.runtimeTables.push(step({
82
+ kind: "createCatalog",
83
+ classification: "additive",
84
+ description: "Create the runtime's verified pipeline action attribution table.",
85
+ sql: ACTION_ATTRIBUTION_DDL,
86
+ }));
87
+ }
80
88
  if (lacks(OAUTH_TABLES)) {
81
89
  draft.runtimeTables.push(step({
82
90
  kind: "createCatalog",
@@ -180,6 +180,9 @@ export declare const AGENT_TABLES_DDL: ReadonlyArray<string>;
180
180
  * The runtime's own tables beside the catalog. They are created with it, after the entity tables
181
181
  * because they refer to the built-in user table, and every statement is idempotent.
182
182
  */
183
+ export declare const ACTION_ATTRIBUTION_TABLE = "_ontology_action_attribution";
184
+ /** Runtime audit metadata deliberately has no foreign key: deleted records retain their history. */
185
+ export declare const ACTION_ATTRIBUTION_DDL: ReadonlyArray<string>;
183
186
  export declare const RUNTIME_TABLES_DDL: ReadonlyArray<string>;
184
187
  export declare function columnDefinition(column: ColumnStorage): string;
185
188
  export declare function constraintDefinition(constraint: {
@@ -575,7 +575,17 @@ export const AGENT_TABLES_DDL = [
575
575
  * The runtime's own tables beside the catalog. They are created with it, after the entity tables
576
576
  * because they refer to the built-in user table, and every statement is idempotent.
577
577
  */
578
+ export const ACTION_ATTRIBUTION_TABLE = "_ontology_action_attribution";
579
+ /** Runtime audit metadata deliberately has no foreign key: deleted records retain their history. */
580
+ export const ACTION_ATTRIBUTION_DDL = [
581
+ `CREATE TABLE IF NOT EXISTS "${ACTION_ATTRIBUTION_TABLE}" (
582
+ "action_id" uuid PRIMARY KEY,
583
+ "execution" jsonb NOT NULL,
584
+ "at" timestamptz NOT NULL DEFAULT now()
585
+ )`,
586
+ ];
578
587
  export const RUNTIME_TABLES_DDL = [
588
+ ...ACTION_ATTRIBUTION_DDL,
579
589
  [
580
590
  `CREATE TABLE IF NOT EXISTS ${quoteIdentifier(USER_ROLES_TABLE)} (`,
581
591
  ` "user_id" uuid NOT NULL,`,
@@ -33,6 +33,7 @@ export declare function permissionOf(entity: string, verb: string): string;
33
33
  export declare function grants(granted: string, required: string): boolean;
34
34
  /** Anything that holds permissions: a principal, or the reader of a catalog. */
35
35
  export interface PermissionHolder {
36
+ readonly execution?: import("./runtime/workload.js").WorkloadExecution;
36
37
  readonly permissions: ReadonlyArray<string>;
37
38
  }
38
39
  /**
@@ -40,6 +41,8 @@ export interface PermissionHolder {
40
41
  * through a wildcard.
41
42
  */
42
43
  export declare function isAllowed(required: string, principal: PermissionHolder): boolean;
44
+ /** Expand workload authority for permission-only catalog and documentation readers. */
45
+ export declare function effectivePermissions(principal: PermissionHolder): ReadonlyArray<string>;
43
46
  /** The first of the required permissions the principal lacks, or `undefined` when it holds all. */
44
47
  export declare function missingPermission(required: ReadonlyArray<string>, principal: PermissionHolder): string | undefined;
45
48
  /**
@@ -52,7 +52,14 @@ export function grants(granted, required) {
52
52
  * through a wildcard.
53
53
  */
54
54
  export function isAllowed(required, principal) {
55
- return principal.permissions.some((granted) => grants(granted, required));
55
+ return ((principal.execution !== undefined && principal.permissions.includes("*:*")) ||
56
+ principal.permissions.some((granted) => grants(granted, required)));
57
+ }
58
+ /** Expand workload authority for permission-only catalog and documentation readers. */
59
+ export function effectivePermissions(principal) {
60
+ return principal.execution !== undefined && principal.permissions.includes("*:*")
61
+ ? [...principal.permissions, ...Array.from(EXPLICIT_GRANT_ENTITIES, (entity) => `${entity}:*`)]
62
+ : principal.permissions;
56
63
  }
57
64
  /** The first of the required permissions the principal lacks, or `undefined` when it holds all. */
58
65
  export function missingPermission(required, principal) {
@@ -1,4 +1,5 @@
1
1
  import { Schema } from "effect";
2
+ import { WorkloadExecution } from "./workload.js";
2
3
  import type { StoredRecord } from "../runtime.js";
3
4
  import type { EntityModel } from "./model.js";
4
5
  import type { Params } from "./statement.js";
@@ -31,6 +32,26 @@ export declare const Edit: Schema.Struct<{
31
32
  export type Edit = typeof Edit.Type;
32
33
  /** A row of `<entity>_history`. */
33
34
  export declare const HistoryEntry: Schema.Struct<{
35
+ readonly execution: Schema.optionalKey<Schema.Struct<{
36
+ readonly issuer: Schema.String;
37
+ readonly audience: Schema.String;
38
+ readonly organizationId: Schema.String;
39
+ readonly workspaceId: Schema.String;
40
+ readonly subject: Schema.String;
41
+ readonly deploymentId: Schema.String;
42
+ readonly ontologyDeploymentId: Schema.String;
43
+ readonly servingOntologyDeploymentId: Schema.String;
44
+ readonly ontologyOrigin: Schema.String;
45
+ readonly environment: Schema.Literal<"production">;
46
+ readonly jobName: Schema.String;
47
+ readonly runId: Schema.String;
48
+ readonly executionId: Schema.NullOr<Schema.String>;
49
+ readonly attemptId: Schema.String;
50
+ readonly generation: Schema.Number;
51
+ readonly tokenId: Schema.String;
52
+ readonly issuedAt: Schema.Number;
53
+ readonly expiresAt: Schema.Number;
54
+ }>>;
34
55
  readonly id: Schema.String;
35
56
  readonly recordId: Schema.String;
36
57
  /** The qualified action, for example `lead.changeStatus`. */
@@ -54,6 +75,7 @@ export interface HistoryWrite {
54
75
  readonly edit: Edit;
55
76
  }
56
77
  export interface HistoryScope {
78
+ readonly execution?: WorkloadExecution;
57
79
  readonly action: string;
58
80
  readonly actionId: string;
59
81
  readonly principalId: string | null;
@@ -1,5 +1,6 @@
1
1
  import { Schema } from "effect";
2
2
  import { canonicalJson } from "../manifest.js";
3
+ import { WorkloadExecution } from "./workload.js";
3
4
  import { quoteIdentifier } from "../migrate/storage.js";
4
5
  import { uuidv7 } from "./ids.js";
5
6
  export const EditKind = Schema.Literals(["create", "update", "delete", "link", "unlink"]);
@@ -21,6 +22,7 @@ export const Edit = Schema.Struct({
21
22
  });
22
23
  /** A row of `<entity>_history`. */
23
24
  export const HistoryEntry = Schema.Struct({
25
+ execution: Schema.optionalKey(WorkloadExecution),
24
26
  id: Schema.String,
25
27
  recordId: Schema.String,
26
28
  /** The qualified action, for example `lead.changeStatus`. */
@@ -141,7 +141,7 @@ export function runAction(ontology, request) {
141
141
  if (consumesUpload) {
142
142
  const saved = yield* sql
143
143
  .withTransaction(Effect.gen(function* () {
144
- yield* authorizeBlobOperation(ontology, sql, request.principal.id, found.permissions);
144
+ yield* authorizeBlobOperation(ontology, sql, request.principal.id, found.permissions, request.principal.execution);
145
145
  const [saved] = yield* sql.unsafe(`SELECT fingerprint, outcome FROM "_blob_action_results"
146
146
  WHERE principal_id = $1::uuid AND key = $2`, [request.principal.id, key]);
147
147
  if (saved && saved.fingerprint !== fingerprint) {
@@ -168,6 +168,7 @@ export function runAction(ontology, request) {
168
168
  action: qualified,
169
169
  actionId,
170
170
  principalId: request.principal.id,
171
+ ...(request.principal.execution ? { execution: request.principal.execution } : {}),
171
172
  ...(key ? { blobActionKey: key } : {}),
172
173
  ...(request.branchId ? { branchId: request.branchId } : {}),
173
174
  ...(request.options?.createIntent ? { createIntent: request.options.createIntent } : {}),
@@ -177,8 +178,11 @@ export function runAction(ontology, request) {
177
178
  const handler = Effect.suspend(() => handle({ ...context, prepared }));
178
179
  let replay;
179
180
  const transactionalHandler = Effect.gen(function* () {
181
+ if (request.principal.execution !== undefined) {
182
+ yield* authorizeBlobOperation(ontology, sql, request.principal.id, found.permissions, request.principal.execution);
183
+ }
180
184
  if (consumesUpload) {
181
- yield* authorizeBlobOperation(ontology, sql, request.principal.id, found.permissions);
185
+ yield* authorizeBlobOperation(ontology, sql, request.principal.id, found.permissions, request.principal.execution);
182
186
  yield* sql.unsafe(`INSERT INTO "_blob_action_results" (principal_id, key, fingerprint)
183
187
  VALUES ($1::uuid, $2, $3) ON CONFLICT DO NOTHING`, [request.principal.id, key, fingerprint]);
184
188
  const [saved] = yield* sql.unsafe(`SELECT fingerprint, outcome FROM "_blob_action_results" WHERE principal_id = $1::uuid AND key = $2
@@ -49,6 +49,8 @@ export declare const SIGN_IN_RECORD_INTERVAL_MS: number;
49
49
  * {@link SIGN_IN_RECORD_INTERVAL_MS}, so history keeps it. Inactive users are rejected.
50
50
  */
51
51
  export declare function resolvePrincipalByIdentity(ontology: Ontology, claims: IdentityClaims): Effect.Effect<Principal, PrincipalRejected, SqlClient.SqlClient>;
52
+ /** Resolve the workspace's stable keyless pipeline service, without customer roles or sign-in. */
53
+ export declare function resolvePipelineWorker(ontology: Ontology, execution: import("./workload.js").WorkloadExecution): Effect.Effect<Principal, PrincipalRejected, SqlClient.SqlClient>;
52
54
  /**
53
55
  * The principal of an API key, with the permissions its roles have in the ontology. Unknown and
54
56
  * revoked keys and inactive users are rejected.
@@ -207,6 +207,61 @@ export function resolvePrincipalByIdentity(ontology, claims) {
207
207
  return yield* activePrincipal(ontology, sql, user);
208
208
  }).pipe(Effect.catchIf(isSqlError, (error) => Effect.die(error)));
209
209
  }
210
+ /** Resolve the workspace's stable keyless pipeline service, without customer roles or sign-in. */
211
+ export function resolvePipelineWorker(ontology, execution) {
212
+ return Effect.gen(function* () {
213
+ const sql = yield* SqlClient.SqlClient;
214
+ const provider = "segmentstream-ontology-workload";
215
+ const subject = `pipeline-workers:${execution.workspaceId}`;
216
+ const runtime = platformRuntime(ontology, sql, "user.resolve_pipeline_worker");
217
+ const principal = (user) => user.kind !== "service" || user.active !== true
218
+ ? Effect.fail(new PrincipalRejected({
219
+ reason: "inactive_user",
220
+ message: "The pipeline worker principal is inactive or has an invalid kind.",
221
+ }))
222
+ : Effect.succeed({
223
+ id: user.id,
224
+ kind: "service",
225
+ roles: [],
226
+ permissions: ["*:*"],
227
+ execution,
228
+ });
229
+ const find = (forUpdate) => runtime.find({
230
+ entity: "user",
231
+ options: {
232
+ where: { identity_provider: provider, identity_subject: subject },
233
+ limit: 1,
234
+ forUpdate,
235
+ },
236
+ });
237
+ const [known] = yield* find(false);
238
+ // Returning requests only read the principal; writes revalidate it in their transaction.
239
+ if (known !== undefined) {
240
+ return yield* principal(known);
241
+ }
242
+ return yield* sql
243
+ .withTransaction(Effect.gen(function* () {
244
+ // The replication lock precedes every business lock, including principal initialization.
245
+ yield* beginReplicationWrite(sql);
246
+ yield* sql.unsafe("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", [
247
+ provider + ":" + subject,
248
+ ]);
249
+ const [existing] = yield* find(true);
250
+ const user = existing ??
251
+ (yield* runtime.create({
252
+ entity: "user",
253
+ values: {
254
+ name: "Pipeline workers",
255
+ kind: "service",
256
+ identity_provider: provider,
257
+ identity_subject: subject,
258
+ },
259
+ }));
260
+ return yield* principal(user);
261
+ }))
262
+ .pipe(Effect.catchTag("Conflict", (error) => Effect.die(error)), Effect.catchTag("NotFound", (error) => Effect.die(error)));
263
+ }).pipe(Effect.catchIf(isSqlError, (error) => Effect.die(error)));
264
+ }
210
265
  /**
211
266
  * The principal of an API key, with the permissions its roles have in the ontology. Unknown and
212
267
  * revoked keys and inactive users are rejected.
@@ -238,17 +238,25 @@ export function readHistory(context, entity, input) {
238
238
  const at = params.add(cursor[0], "timestamptz");
239
239
  conditions.push(`("h"."at" < ${at} OR ("h"."at" = ${at} AND "h"."id" < ${params.add(cursor[1], "uuid")}))`);
240
240
  }
241
+ const presenceParams = new Params();
242
+ const [presence] = yield* context.run("SELECT to_regclass('_ontology_action_attribution') IS NOT NULL AS present", presenceParams);
243
+ const attribution = presence?.present === true;
241
244
  const rows = yield* context.run(`SELECT "h"."id"::text AS "id", "h"."record_id"::text AS "recordId", "h"."action" AS "action", ` +
242
245
  `"h"."action_id"::text AS "actionId", "h"."kind" AS "kind", ` +
243
246
  `"h"."principal_id"::text AS "principalId", ` +
244
247
  `to_char("h"."at" AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS "at", ` +
245
248
  `"h"."changes"::text AS "changes" ` +
249
+ (attribution ? `, "a"."execution" AS "execution" ` : "") +
246
250
  `FROM ${quoteIdentifier(entity.historyTable)} AS "h" ` +
251
+ (attribution
252
+ ? `LEFT JOIN "_ontology_action_attribution" AS "a" ON "a"."action_id" = "h"."action_id" `
253
+ : "") +
247
254
  `WHERE ${conditions.join(" AND ")} ` +
248
255
  `ORDER BY "h"."at" DESC, "h"."id" DESC ` +
249
256
  `LIMIT ${params.add(limit + 1, "bigint")}`, params);
250
257
  const items = rows.slice(0, limit).map((row) => Schema.decodeUnknownSync(HistoryEntry)({
251
- ...row,
258
+ ...Object.fromEntries(Object.entries(row).filter(([key, value]) => key !== "execution" || value != null)),
259
+ ...(row.execution == null ? {} : { execution: row.execution }),
252
260
  at: canonicalInstant(String(row.at)),
253
261
  changes: JSON.parse(String(row.changes)),
254
262
  }));
@@ -11,7 +11,7 @@ import { sqlAgentStore } from "./agents.js";
11
11
  import { generatedActions } from "./generated-actions.js";
12
12
  import { sqlOAuthStore } from "./oauth.js";
13
13
  import { runAction } from "./pipeline.js";
14
- import { checkSignIn, resolvePrincipalByApiKey, resolvePrincipalByIdentity } from "./principals.js";
14
+ import { checkSignIn, resolvePrincipalByApiKey, resolvePrincipalByIdentity, resolvePipelineWorker, } from "./principals.js";
15
15
  import { postgresTls } from "./postgres-tls.js";
16
16
  import { makeOntologyRuntime } from "./sql-runtime.js";
17
17
  /** Connections one service instance holds to the database's pooler. */
@@ -61,6 +61,7 @@ export function backendLayer(ontology, blobStorage) {
61
61
  })).pipe(Effect.catchTag("PrincipalRejected", (rejection) => rejection.reason === "not_invited"
62
62
  ? rejectedAs("sign_in")(rejection).pipe(Effect.andThen(Effect.fail(new NotInvited({ message: rejection.message }))))
63
63
  : rejectedAs("sign_in")(rejection))),
64
+ resolvePipelineWorker: (execution) => withSql(resolvePipelineWorker(ontology, execution)).pipe(Effect.catchTag("PrincipalRejected", rejectedAs("pipeline_worker"))),
64
65
  checkSignIn: (email) => withSql(checkSignIn(ontology, email)),
65
66
  resolveApiKey: (key) => withSql(resolvePrincipalByApiKey(ontology, Redacted.value(key))).pipe(Effect.catchTag("PrincipalRejected", rejectedAs("api_key"))),
66
67
  appliedHash: readCatalog({ sql }).pipe(Effect.map((manifest) => manifest?.hash), Effect.orDie),
@@ -0,0 +1,23 @@
1
+ import { Schema } from "effect";
2
+ /** Verified platform claims retained with each write; never supplied by request headers. */
3
+ export declare const WorkloadExecution: Schema.Struct<{
4
+ readonly issuer: Schema.String;
5
+ readonly audience: Schema.String;
6
+ readonly organizationId: Schema.String;
7
+ readonly workspaceId: Schema.String;
8
+ readonly subject: Schema.String;
9
+ readonly deploymentId: Schema.String;
10
+ readonly ontologyDeploymentId: Schema.String;
11
+ readonly servingOntologyDeploymentId: Schema.String;
12
+ readonly ontologyOrigin: Schema.String;
13
+ readonly environment: Schema.Literal<"production">;
14
+ readonly jobName: Schema.String;
15
+ readonly runId: Schema.String;
16
+ readonly executionId: Schema.NullOr<Schema.String>;
17
+ readonly attemptId: Schema.String;
18
+ readonly generation: Schema.Number;
19
+ readonly tokenId: Schema.String;
20
+ readonly issuedAt: Schema.Number;
21
+ readonly expiresAt: Schema.Number;
22
+ }>;
23
+ export type WorkloadExecution = typeof WorkloadExecution.Type;
@@ -0,0 +1,22 @@
1
+ import { Schema } from "effect";
2
+ /** Verified platform claims retained with each write; never supplied by request headers. */
3
+ export const WorkloadExecution = Schema.Struct({
4
+ issuer: Schema.String,
5
+ audience: Schema.String,
6
+ organizationId: Schema.String,
7
+ workspaceId: Schema.String,
8
+ subject: Schema.String,
9
+ deploymentId: Schema.String,
10
+ ontologyDeploymentId: Schema.String,
11
+ servingOntologyDeploymentId: Schema.String,
12
+ ontologyOrigin: Schema.String,
13
+ environment: Schema.Literal("production"),
14
+ jobName: Schema.String,
15
+ runId: Schema.String,
16
+ executionId: Schema.NullOr(Schema.String),
17
+ attemptId: Schema.String,
18
+ generation: Schema.Number,
19
+ tokenId: Schema.String,
20
+ issuedAt: Schema.Number,
21
+ expiresAt: Schema.Number,
22
+ });
@@ -1,7 +1,7 @@
1
1
  import { Effect } from "effect";
2
2
  import { resolveBlobWrites } from "../storage/writes.js";
3
3
  import { Conflict } from "../errors.js";
4
- import { quoteIdentifier } from "../migrate/storage.js";
4
+ import { ACTION_ATTRIBUTION_TABLE, quoteIdentifier } from "../migrate/storage.js";
5
5
  import { InvalidValues } from "./errors.js";
6
6
  import { uuidv7 } from "./ids.js";
7
7
  import { diffRecords, insertHistory } from "./history.js";
@@ -13,6 +13,12 @@ import { columnOf, Params } from "./statement.js";
13
13
  function recordEdit(context, entity, edit) {
14
14
  return Effect.gen(function* () {
15
15
  const params = new Params();
16
+ if (context.scope.execution !== undefined) {
17
+ const attribution = new Params();
18
+ yield* context.run(`INSERT INTO "${ACTION_ATTRIBUTION_TABLE}" (action_id, execution) VALUES (` +
19
+ `${attribution.add(context.scope.actionId, "uuid")}, ` +
20
+ `${attribution.add(JSON.stringify(context.scope.execution), "jsonb")}) ON CONFLICT DO NOTHING`, attribution);
21
+ }
16
22
  yield* context.run(insertHistory(context.scope, { entity, edit }, params), params);
17
23
  context.edits.push(edit);
18
24
  });
package/dist/runtime.d.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import { Context, Effect } from "effect";
2
2
  import type { Conflict, NotFound } from "./errors.js";
3
3
  import type { Where } from "./query.js";
4
+ import type { WorkloadExecution } from "./runtime/workload.js";
4
5
  /** Who runs an action: an app member or a service principal, both `user` records. */
5
6
  export interface Principal {
7
+ /** Present only for a verified managed pipeline worker. */
8
+ readonly execution?: WorkloadExecution;
6
9
  /** The id of the principal's `user` record. */
7
10
  readonly id: string;
8
11
  readonly kind: "member" | "service";
@@ -2,6 +2,7 @@ import { Context, Effect, Layer, Redacted } from "effect";
2
2
  import { HttpServerRequest, type HttpServerResponse } from "effect/unstable/http";
3
3
  import { HttpApiMiddleware, HttpApiSecurity } from "effect/unstable/httpapi";
4
4
  import { type JWTVerifyGetKey } from "jose";
5
+ import type { WorkloadExecution } from "../runtime/workload.js";
5
6
  import type { Principal } from "../runtime.js";
6
7
  import type { NeonAuthEnvironment } from "./config.js";
7
8
  import { RequestId } from "./errors.js";
@@ -32,6 +33,10 @@ declare const Authentication_base: HttpApiMiddleware.ServiceClass<Authentication
32
33
  export declare class Authentication extends Authentication_base {
33
34
  }
34
35
  export interface TokenVerifierConfig {
36
+ /** Production workload activation and the deployment actually serving the request. */
37
+ readonly workloadTarget?: {
38
+ readonly deploymentId: string;
39
+ };
35
40
  /** The control plane: the issuer of app tokens and the host of their JWKS. */
36
41
  readonly segmentstreamUrl: string;
37
42
  readonly applicationId: string;
@@ -39,6 +44,9 @@ export interface TokenVerifierConfig {
39
44
  readonly organizationId: string;
40
45
  }
41
46
  export type TokenVerification = {
47
+ readonly status: "workload";
48
+ readonly execution: WorkloadExecution;
49
+ } | {
42
50
  readonly status: "valid";
43
51
  readonly identity: SignInIdentity;
44
52
  } | {
@@ -56,6 +64,9 @@ export declare function neonAuthKeySet(jwksUrl: string): JWTVerifyGetKey;
56
64
  * `SEGMENTSTREAM_URL` for this application (`aud: app:<application id>`), organization and workspace.
57
65
  */
58
66
  export declare function makeTokenVerifier(config: TokenVerifierConfig, keySet: JWTVerifyGetKey): TokenVerifier;
67
+ export declare const WORKLOAD_TOKEN_TYPE = "segmentstream-ontology-workload+jwt";
68
+ /** A separate verifier: a workload token can never resolve as a human or act as an agent. */
69
+ export declare function makeWorkloadTokenVerifier(config: TokenVerifierConfig, keySet: JWTVerifyGetKey): TokenVerifier;
59
70
  /**
60
71
  * Neon Auth's issuer is the origin of its Auth URL: `https://ep-….neon.tech/ontology/auth` issues
61
72
  * tokens with `iss: https://ep-….neon.tech`.
@@ -1,7 +1,7 @@
1
1
  import { Cause, Context, Effect, Layer, Redacted } from "effect";
2
2
  import { HttpServerRequest } from "effect/unstable/http";
3
3
  import { HttpApiMiddleware, HttpApiSecurity, OpenApi } from "effect/unstable/httpapi";
4
- import { createRemoteJWKSet, decodeJwt, errors, jwtVerify, } from "jose";
4
+ import { createRemoteJWKSet, decodeJwt, decodeProtectedHeader, errors, jwtVerify, } from "jose";
5
5
  import { API_KEY_PREFIX } from "../runtime/principals.js";
6
6
  import { RequestId, crossOriginSession, forbidden, internalError, notInvited, toServerResponse, unauthorized, unavailable, } from "./errors.js";
7
7
  import { ACCESS_TOKEN_PREFIX, principalOfAccessToken } from "./oauth/protocol.js";
@@ -76,6 +76,7 @@ export function makeTokenVerifier(config, keySet) {
76
76
  try {
77
77
  const verified = await jwtVerify(token, keySet, {
78
78
  algorithms: ["ES256"],
79
+ typ: "JWT",
79
80
  issuer,
80
81
  audience,
81
82
  clockTolerance: CLOCK_TOLERANCE_SECONDS,
@@ -90,6 +91,96 @@ export function makeTokenVerifier(config, keySet) {
90
91
  return identity === undefined ? { status: "invalid" } : { status: "valid", identity };
91
92
  };
92
93
  }
94
+ export const WORKLOAD_TOKEN_TYPE = "segmentstream-ontology-workload+jwt";
95
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
96
+ /** A separate verifier: a workload token can never resolve as a human or act as an agent. */
97
+ export function makeWorkloadTokenVerifier(config, keySet) {
98
+ return async (token) => {
99
+ if (config.workloadTarget === undefined) {
100
+ return { status: "invalid" };
101
+ }
102
+ let payload;
103
+ try {
104
+ const verified = await jwtVerify(token, keySet, {
105
+ algorithms: ["ES256"],
106
+ typ: WORKLOAD_TOKEN_TYPE,
107
+ issuer: new URL(config.segmentstreamUrl).origin,
108
+ audience: `ontology:${config.applicationId}`,
109
+ clockTolerance: CLOCK_TOLERANCE_SECONDS,
110
+ requiredClaims: [
111
+ "sub",
112
+ "exp",
113
+ "iat",
114
+ "jti",
115
+ "org",
116
+ "wsp",
117
+ "deploymentId",
118
+ "ontologyDeploymentId",
119
+ "ontologyOrigin",
120
+ "environment",
121
+ "jobName",
122
+ "runId",
123
+ "executionId",
124
+ "attemptId",
125
+ "generation",
126
+ ],
127
+ });
128
+ payload = verified.payload;
129
+ }
130
+ catch (error) {
131
+ return keySetUnavailable(error) ? { status: "unavailable" } : { status: "invalid" };
132
+ }
133
+ const strings = ["jti", "jobName"];
134
+ const ids = ["deploymentId", "ontologyDeploymentId", "runId", "attemptId"];
135
+ if (payload.org !== config.organizationId ||
136
+ payload.wsp !== config.workspaceId ||
137
+ payload.sub !== `pipeline-workers:${config.workspaceId}` ||
138
+ payload.environment !== "production" ||
139
+ payload.aud !== `ontology:${config.applicationId}` ||
140
+ strings.some((name) => typeof payload[name] !== "string" || payload[name].trim() === "") ||
141
+ ids.some((name) => typeof payload[name] !== "string" || !UUID.test(payload[name])) ||
142
+ (payload.executionId !== null &&
143
+ (typeof payload.executionId !== "string" || !UUID.test(payload.executionId))) ||
144
+ !Number.isSafeInteger(payload.generation) ||
145
+ payload.generation < 1 ||
146
+ !Number.isSafeInteger(payload.iat) ||
147
+ !Number.isSafeInteger(payload.exp) ||
148
+ payload.iat > Math.floor(Date.now() / 1000) + CLOCK_TOLERANCE_SECONDS ||
149
+ payload.exp <= payload.iat ||
150
+ payload.exp - payload.iat > 300 ||
151
+ typeof payload.ontologyOrigin !== "string" ||
152
+ !URL.canParse(payload.ontologyOrigin)) {
153
+ return { status: "invalid" };
154
+ }
155
+ const origin = new URL(payload.ontologyOrigin);
156
+ if (origin.protocol !== "https:" || origin.origin !== payload.ontologyOrigin) {
157
+ return { status: "invalid" };
158
+ }
159
+ return {
160
+ status: "workload",
161
+ execution: {
162
+ issuer: payload.iss,
163
+ audience: payload.aud,
164
+ organizationId: config.organizationId,
165
+ workspaceId: config.workspaceId,
166
+ subject: payload.sub,
167
+ deploymentId: payload.deploymentId,
168
+ ontologyDeploymentId: payload.ontologyDeploymentId,
169
+ servingOntologyDeploymentId: config.workloadTarget.deploymentId,
170
+ ontologyOrigin: payload.ontologyOrigin,
171
+ environment: "production",
172
+ jobName: payload.jobName,
173
+ runId: payload.runId,
174
+ executionId: payload.executionId,
175
+ attemptId: payload.attemptId,
176
+ generation: payload.generation,
177
+ tokenId: payload.jti,
178
+ issuedAt: payload.iat,
179
+ expiresAt: payload.exp,
180
+ },
181
+ };
182
+ };
183
+ }
93
184
  /**
94
185
  * Neon Auth's issuer is the origin of its Auth URL: `https://ep-….neon.tech/ontology/auth` issues
95
186
  * tokens with `iss: https://ep-….neon.tech`.
@@ -166,11 +257,21 @@ export function verifyByIssuer(verifiers) {
166
257
  * once it has Neon Auth. Both are accepted while workspaces move to Neon Auth.
167
258
  */
168
259
  export function tokenVerifier(config) {
260
+ const platformKeys = remoteKeySet(config.segmentstreamUrl);
261
+ const human = makeTokenVerifier(config, platformKeys);
262
+ const workload = makeWorkloadTokenVerifier(config, platformKeys);
263
+ const platform = (token) => {
264
+ try {
265
+ return decodeProtectedHeader(token).typ === WORKLOAD_TOKEN_TYPE
266
+ ? workload(token)
267
+ : human(token);
268
+ }
269
+ catch {
270
+ return Promise.resolve({ status: "invalid" });
271
+ }
272
+ };
169
273
  const verifiers = new Map([
170
- [
171
- new URL(config.segmentstreamUrl).origin,
172
- makeTokenVerifier(config, remoteKeySet(config.segmentstreamUrl)),
173
- ],
274
+ [new URL(config.segmentstreamUrl).origin, platform],
174
275
  ]);
175
276
  if (config.neonAuth !== undefined) {
176
277
  verifiers.set(neonAuthIssuer(config.neonAuth.baseUrl), makeNeonAuthTokenVerifier(config.neonAuth, neonAuthKeySet(config.neonAuth.jwksUrl)));
@@ -337,6 +438,14 @@ export function authenticate(credential, verifyToken, options = {}) {
337
438
  }
338
439
  }
339
440
  const verification = yield* Effect.promise(() => verifyToken(token));
441
+ if (verification.status === "workload") {
442
+ const principal = backend.resolvePipelineWorker === undefined
443
+ ? undefined
444
+ : yield* backend.resolvePipelineWorker(verification.execution);
445
+ return principal === undefined
446
+ ? { status: "rejected" }
447
+ : { status: "authenticated", principal };
448
+ }
340
449
  if (verification.status !== "valid") {
341
450
  return verification.status === "invalid"
342
451
  ? { status: "rejected" }
@@ -66,6 +66,10 @@ export interface OntologyEnvironment {
66
66
  * `segmentstreamUrl` for `workspaceId`; previews and local development have none.
67
67
  */
68
68
  readonly appCredential?: Redacted.Redacted<string>;
69
+ /** Production deployment identity; absent in local, preview and earlier deployments. */
70
+ readonly workloadTarget?: {
71
+ readonly deploymentId: string;
72
+ };
69
73
  /** Present once the workspace has Neon Auth: members then sign in with its tokens. */
70
74
  readonly neonAuth?: NeonAuthEnvironment;
71
75
  /**
@@ -134,6 +134,13 @@ export function configFromEnv(environment = process.env) {
134
134
  message: `The ontology service cannot start: ${list} ${verb} not set.`,
135
135
  });
136
136
  }
137
+ const deploymentId = read("SEGMENTSTREAM_ONTOLOGY_DEPLOYMENT_ID");
138
+ if (deploymentId !== "" &&
139
+ !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(deploymentId)) {
140
+ throw new OntologyServiceConfigError({
141
+ message: "SEGMENTSTREAM_ONTOLOGY_DEPLOYMENT_ID must be a UUID.",
142
+ });
143
+ }
137
144
  const neonAuth = neonAuthFromEnv(read);
138
145
  const runtimeIdentity = runtimeIdentityFromEnv(read);
139
146
  const localConnections = localConnectionsFromEnv(environment);
@@ -163,6 +170,10 @@ export function configFromEnv(environment = process.env) {
163
170
  ...(read(APP_CREDENTIAL_VARIABLE)
164
171
  ? { appCredential: Redacted.make(read(APP_CREDENTIAL_VARIABLE)) }
165
172
  : {}),
173
+ ...(environment.VERCEL_ENV === "production" &&
174
+ read("SEGMENTSTREAM_ONTOLOGY_DEPLOYMENT_ID") !== ""
175
+ ? { workloadTarget: { deploymentId: read("SEGMENTSTREAM_ONTOLOGY_DEPLOYMENT_ID") } }
176
+ : {}),
166
177
  ...(neonAuth === undefined ? {} : { neonAuth }),
167
178
  ...(runtimeIdentity === undefined ? {} : { runtimeIdentity }),
168
179
  ...(runtimeIdentity === undefined || read(AGENT_HOST_VARIABLE) === ""
@@ -1,3 +1,4 @@
1
+ import { effectivePermissions } from "../permissions.js";
1
2
  import { HttpServerResponse } from "effect/unstable/http";
2
3
  import { Effect, Layer, Schema } from "effect";
3
4
  import { HttpApiBuilder, OpenApi } from "effect/unstable/httpapi";
@@ -145,7 +146,7 @@ function serviceHandlers(built, operationsOf, aggregate) {
145
146
  return undefined;
146
147
  }
147
148
  const principal = yield* CurrentPrincipal;
148
- return principal.permissions;
149
+ return effectivePermissions(principal);
149
150
  });
150
151
  return handlers
151
152
  .handle("catalog", (request) => Effect.map(permissionsFor(request.query.as), (permissions) => catalogFor(manifest, permissions)))
@@ -1,3 +1,4 @@
1
+ import { effectivePermissions } from "../../permissions.js";
1
2
  import { Cause, Data, Effect, Layer, Option, Schema, SchemaTransformation } from "effect";
2
3
  import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
3
4
  import { HttpApiBuilder, HttpApiSecurity } from "effect/unstable/httpapi";
@@ -175,7 +176,7 @@ function runTool({ built, operations, aggregate, conversations }, tool, args, re
175
176
  case "describe": {
176
177
  const principal = yield* CurrentPrincipal;
177
178
  return {
178
- text: describeCatalog(built.plan.ontology.manifest, principal.permissions),
179
+ text: describeCatalog(built.plan.ontology.manifest, effectivePermissions(principal)),
179
180
  };
180
181
  }
181
182
  case "aggregate": {
@@ -285,7 +286,7 @@ function readResource({ built }, uri) {
285
286
  {
286
287
  uri,
287
288
  mimeType: "application/json",
288
- text: JSON.stringify(encodeManifest(catalogFor(manifest, principal.permissions))),
289
+ text: JSON.stringify(encodeManifest(catalogFor(manifest, effectivePermissions(principal)))),
289
290
  },
290
291
  ];
291
292
  case DESCRIBE_URI:
@@ -293,7 +294,7 @@ function readResource({ built }, uri) {
293
294
  {
294
295
  uri,
295
296
  mimeType: "text/plain",
296
- text: describeCatalog(manifest, principal.permissions),
297
+ text: describeCatalog(manifest, effectivePermissions(principal)),
297
298
  },
298
299
  ];
299
300
  default:
@@ -354,7 +355,7 @@ function dispatch(server, message, request) {
354
355
  title: "Ontology",
355
356
  version: server.built.plan.ontology.manifest.hash,
356
357
  },
357
- instructions: instructions(server, principal.permissions),
358
+ instructions: instructions(server, effectivePermissions(principal)),
358
359
  });
359
360
  case "ping":
360
361
  return rpcResult(id, {});
@@ -74,19 +74,39 @@ export declare function makeOperations(backend: Backend, plan: ApiPlan, hooks: R
74
74
  readonly after?: string;
75
75
  readonly limit?: number;
76
76
  }) => Effect.Effect<QueryResult<{
77
+ readonly at: string;
77
78
  readonly id: string;
78
- readonly recordId: string;
79
- readonly action: string;
80
- readonly actionId: string;
81
79
  readonly kind: "create" | "update" | "delete" | "link" | "unlink";
82
- readonly principalId: string | null;
83
- readonly at: string;
80
+ readonly action: string;
84
81
  readonly changes: {
85
82
  readonly [x: string]: {
86
83
  readonly before: import("effect/Schema").Json;
87
84
  readonly after: import("effect/Schema").Json;
88
85
  };
89
86
  };
87
+ readonly principalId: string | null;
88
+ readonly recordId: string;
89
+ readonly actionId: string;
90
+ readonly execution?: {
91
+ readonly issuer: string;
92
+ readonly audience: string;
93
+ readonly organizationId: string;
94
+ readonly workspaceId: string;
95
+ readonly subject: string;
96
+ readonly deploymentId: string;
97
+ readonly ontologyDeploymentId: string;
98
+ readonly servingOntologyDeploymentId: string;
99
+ readonly ontologyOrigin: string;
100
+ readonly environment: "production";
101
+ readonly jobName: string;
102
+ readonly runId: string;
103
+ readonly executionId: string | null;
104
+ readonly attemptId: string;
105
+ readonly generation: number;
106
+ readonly tokenId: string;
107
+ readonly issuedAt: number;
108
+ readonly expiresAt: number;
109
+ } | undefined;
90
110
  }>, PermissionDenied | import("../errors.js").NotFound | import("../index.js").InvalidQuery, CurrentPrincipal>;
91
111
  /** The records a relationship reaches from one record, as a page of the target entity. */
92
112
  traverse: (planned: PlannedEntity, traversal: Traversal, id: string, query: Query) => Effect.Effect<QueryResult<Readonly<Record<string, unknown>>>, PermissionDenied | import("../errors.js").NotFound | import("../index.js").InvalidQuery, CurrentPrincipal>;
@@ -274,6 +274,7 @@ declare const OntologyBackend_base: Context.ServiceClass<OntologyBackend, "@segm
274
274
  * Finds, links or creates the member's user. `undefined` when the user may not sign in, and
275
275
  * {@link NotInvited} when the sign-in policy turns away a person nobody invited.
276
276
  */
277
+ readonly resolvePipelineWorker?: (execution: import("../runtime/workload.js").WorkloadExecution) => Effect.Effect<Principal | undefined>;
277
278
  readonly resolvePrincipal: (identity: SignInIdentity) => Effect.Effect<Principal | undefined, NotInvited>;
278
279
  /**
279
280
  * Whether a person may sign in with this address, and how, read before the app emails them.
@@ -40,6 +40,26 @@ export declare const Validation: Schema.Struct<{
40
40
  }>>;
41
41
  }>;
42
42
  export declare const HistoryEntry: Schema.decodeTo<Schema.Struct<{
43
+ readonly execution: Schema.optionalKey<Schema.Struct<{
44
+ readonly issuer: Schema.String;
45
+ readonly audience: Schema.String;
46
+ readonly organizationId: Schema.String;
47
+ readonly workspaceId: Schema.String;
48
+ readonly subject: Schema.String;
49
+ readonly deploymentId: Schema.String;
50
+ readonly ontologyDeploymentId: Schema.String;
51
+ readonly servingOntologyDeploymentId: Schema.String;
52
+ readonly ontologyOrigin: Schema.String;
53
+ readonly environment: Schema.Literal<"production">;
54
+ readonly jobName: Schema.String;
55
+ readonly runId: Schema.String;
56
+ readonly executionId: Schema.NullOr<Schema.String>;
57
+ readonly attemptId: Schema.String;
58
+ readonly generation: Schema.Number;
59
+ readonly tokenId: Schema.String;
60
+ readonly issuedAt: Schema.Number;
61
+ readonly expiresAt: Schema.Number;
62
+ }>>;
43
63
  readonly id: Schema.String;
44
64
  readonly recordId: Schema.String;
45
65
  readonly action: Schema.String;
@@ -52,6 +72,26 @@ export declare const HistoryEntry: Schema.decodeTo<Schema.Struct<{
52
72
  readonly after: Schema.Codec<Schema.Json, Schema.Json, never, never>;
53
73
  }>>;
54
74
  }>, Schema.Struct<{
75
+ readonly execution: Schema.toEncoded<Schema.optionalKey<Schema.Struct<{
76
+ readonly issuer: Schema.String;
77
+ readonly audience: Schema.String;
78
+ readonly organizationId: Schema.String;
79
+ readonly workspaceId: Schema.String;
80
+ readonly subject: Schema.String;
81
+ readonly deploymentId: Schema.String;
82
+ readonly ontologyDeploymentId: Schema.String;
83
+ readonly servingOntologyDeploymentId: Schema.String;
84
+ readonly ontologyOrigin: Schema.String;
85
+ readonly environment: Schema.Literal<"production">;
86
+ readonly jobName: Schema.String;
87
+ readonly runId: Schema.String;
88
+ readonly executionId: Schema.NullOr<Schema.String>;
89
+ readonly attemptId: Schema.String;
90
+ readonly generation: Schema.Number;
91
+ readonly tokenId: Schema.String;
92
+ readonly issuedAt: Schema.Number;
93
+ readonly expiresAt: Schema.Number;
94
+ }>>>;
55
95
  readonly id: Schema.toEncoded<Schema.String>;
56
96
  readonly record_id: Schema.toEncoded<Schema.String>;
57
97
  readonly action: Schema.toEncoded<Schema.String>;
@@ -66,6 +106,26 @@ export declare const HistoryEntry: Schema.decodeTo<Schema.Struct<{
66
106
  }>, never, never>;
67
107
  export declare const HistoryPage: Schema.decodeTo<Schema.Struct<{
68
108
  readonly items: Schema.$Array<Schema.decodeTo<Schema.Struct<{
109
+ readonly execution: Schema.optionalKey<Schema.Struct<{
110
+ readonly issuer: Schema.String;
111
+ readonly audience: Schema.String;
112
+ readonly organizationId: Schema.String;
113
+ readonly workspaceId: Schema.String;
114
+ readonly subject: Schema.String;
115
+ readonly deploymentId: Schema.String;
116
+ readonly ontologyDeploymentId: Schema.String;
117
+ readonly servingOntologyDeploymentId: Schema.String;
118
+ readonly ontologyOrigin: Schema.String;
119
+ readonly environment: Schema.Literal<"production">;
120
+ readonly jobName: Schema.String;
121
+ readonly runId: Schema.String;
122
+ readonly executionId: Schema.NullOr<Schema.String>;
123
+ readonly attemptId: Schema.String;
124
+ readonly generation: Schema.Number;
125
+ readonly tokenId: Schema.String;
126
+ readonly issuedAt: Schema.Number;
127
+ readonly expiresAt: Schema.Number;
128
+ }>>;
69
129
  readonly id: Schema.String;
70
130
  readonly recordId: Schema.String;
71
131
  readonly action: Schema.String;
@@ -78,6 +138,26 @@ export declare const HistoryPage: Schema.decodeTo<Schema.Struct<{
78
138
  readonly after: Schema.Codec<Schema.Json, Schema.Json, never, never>;
79
139
  }>>;
80
140
  }>, Schema.Struct<{
141
+ readonly execution: Schema.toEncoded<Schema.optionalKey<Schema.Struct<{
142
+ readonly issuer: Schema.String;
143
+ readonly audience: Schema.String;
144
+ readonly organizationId: Schema.String;
145
+ readonly workspaceId: Schema.String;
146
+ readonly subject: Schema.String;
147
+ readonly deploymentId: Schema.String;
148
+ readonly ontologyDeploymentId: Schema.String;
149
+ readonly servingOntologyDeploymentId: Schema.String;
150
+ readonly ontologyOrigin: Schema.String;
151
+ readonly environment: Schema.Literal<"production">;
152
+ readonly jobName: Schema.String;
153
+ readonly runId: Schema.String;
154
+ readonly executionId: Schema.NullOr<Schema.String>;
155
+ readonly attemptId: Schema.String;
156
+ readonly generation: Schema.Number;
157
+ readonly tokenId: Schema.String;
158
+ readonly issuedAt: Schema.Number;
159
+ readonly expiresAt: Schema.Number;
160
+ }>>>;
81
161
  readonly id: Schema.toEncoded<Schema.String>;
82
162
  readonly record_id: Schema.toEncoded<Schema.String>;
83
163
  readonly action: Schema.toEncoded<Schema.String>;
@@ -93,6 +173,26 @@ export declare const HistoryPage: Schema.decodeTo<Schema.Struct<{
93
173
  readonly nextCursor: Schema.NullOr<Schema.String>;
94
174
  }>, Schema.Struct<{
95
175
  readonly items: Schema.toEncoded<Schema.$Array<Schema.decodeTo<Schema.Struct<{
176
+ readonly execution: Schema.optionalKey<Schema.Struct<{
177
+ readonly issuer: Schema.String;
178
+ readonly audience: Schema.String;
179
+ readonly organizationId: Schema.String;
180
+ readonly workspaceId: Schema.String;
181
+ readonly subject: Schema.String;
182
+ readonly deploymentId: Schema.String;
183
+ readonly ontologyDeploymentId: Schema.String;
184
+ readonly servingOntologyDeploymentId: Schema.String;
185
+ readonly ontologyOrigin: Schema.String;
186
+ readonly environment: Schema.Literal<"production">;
187
+ readonly jobName: Schema.String;
188
+ readonly runId: Schema.String;
189
+ readonly executionId: Schema.NullOr<Schema.String>;
190
+ readonly attemptId: Schema.String;
191
+ readonly generation: Schema.Number;
192
+ readonly tokenId: Schema.String;
193
+ readonly issuedAt: Schema.Number;
194
+ readonly expiresAt: Schema.Number;
195
+ }>>;
96
196
  readonly id: Schema.String;
97
197
  readonly recordId: Schema.String;
98
198
  readonly action: Schema.String;
@@ -105,6 +205,26 @@ export declare const HistoryPage: Schema.decodeTo<Schema.Struct<{
105
205
  readonly after: Schema.Codec<Schema.Json, Schema.Json, never, never>;
106
206
  }>>;
107
207
  }>, Schema.Struct<{
208
+ readonly execution: Schema.toEncoded<Schema.optionalKey<Schema.Struct<{
209
+ readonly issuer: Schema.String;
210
+ readonly audience: Schema.String;
211
+ readonly organizationId: Schema.String;
212
+ readonly workspaceId: Schema.String;
213
+ readonly subject: Schema.String;
214
+ readonly deploymentId: Schema.String;
215
+ readonly ontologyDeploymentId: Schema.String;
216
+ readonly servingOntologyDeploymentId: Schema.String;
217
+ readonly ontologyOrigin: Schema.String;
218
+ readonly environment: Schema.Literal<"production">;
219
+ readonly jobName: Schema.String;
220
+ readonly runId: Schema.String;
221
+ readonly executionId: Schema.NullOr<Schema.String>;
222
+ readonly attemptId: Schema.String;
223
+ readonly generation: Schema.Number;
224
+ readonly tokenId: Schema.String;
225
+ readonly issuedAt: Schema.Number;
226
+ readonly expiresAt: Schema.Number;
227
+ }>>>;
108
228
  readonly id: Schema.toEncoded<Schema.String>;
109
229
  readonly record_id: Schema.toEncoded<Schema.String>;
110
230
  readonly action: Schema.toEncoded<Schema.String>;
@@ -259,6 +379,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
259
379
  }>, {
260
380
  readonly builtIn: "built_in";
261
381
  }>>;
382
+ readonly runtimeCapabilities: Schema.optionalKey<Schema.$Array<Schema.String>>;
262
383
  readonly version: Schema.Int;
263
384
  readonly hash: Schema.String;
264
385
  readonly signIn: Schema.optionalKey<Schema.Struct<{
@@ -388,6 +509,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
388
509
  }>, {
389
510
  readonly builtIn: "built_in";
390
511
  }>>>;
512
+ readonly runtimeCapabilities: Schema.toEncoded<Schema.optionalKey<Schema.$Array<Schema.String>>>;
391
513
  readonly version: Schema.toEncoded<Schema.Int>;
392
514
  readonly hash: Schema.toEncoded<Schema.String>;
393
515
  readonly sign_in: Schema.toEncoded<Schema.optionalKey<Schema.Struct<{
@@ -128,7 +128,7 @@ export function attachmentRuntime(ontology, sql, provider) {
128
128
  ]);
129
129
  }
130
130
  // The member must still be active; a deactivated member's agent reads nothing.
131
- yield* authorizeBlobOperation(ontology, sql, caller.principal.id, []);
131
+ yield* authorizeBlobOperation(ontology, sql, caller.principal.id, [], caller.principal.execution);
132
132
  return row;
133
133
  });
134
134
  /**
@@ -154,7 +154,7 @@ export function attachmentRuntime(ontology, sql, provider) {
154
154
  });
155
155
  }
156
156
  const reserved = yield* sql.withTransaction(Effect.gen(function* () {
157
- yield* authorizeBlobOperation(ontology, sql, caller.principal.id, []);
157
+ yield* authorizeBlobOperation(ontology, sql, caller.principal.id, [], caller.principal.execution);
158
158
  yield* sql.unsafe(`SELECT pg_advisory_xact_lock(818724)`);
159
159
  const [existing] = yield* sql.unsafe(`SELECT id::text FROM "${ATTACHMENTS_TABLE}"
160
160
  WHERE owner_id = $1::uuid AND conversation = $2 AND source_ref = $3`, [caller.principal.id, caller.conversation, input.sourceRef]);
@@ -272,7 +272,7 @@ export function attachmentRuntime(ontology, sql, provider) {
272
272
  });
273
273
  /** The caller's attachments in this chat and session that are still readable, newest first. */
274
274
  const list = (caller) => sql.withTransaction(Effect.gen(function* () {
275
- yield* authorizeBlobOperation(ontology, sql, caller.principal.id, []);
275
+ yield* authorizeBlobOperation(ontology, sql, caller.principal.id, [], caller.principal.execution);
276
276
  const rows = yield* sql.unsafe(`${SELECT} WHERE a.owner_id = $1::uuid AND a.conversation = $2 AND a.branch_id = $3
277
277
  AND a.state IN ('ready', 'promoted') AND a.expires_at > now()
278
278
  AND (a.session_id IS NULL OR a.session_id = $4)
@@ -365,7 +365,7 @@ export function attachmentRuntime(ontology, sql, provider) {
365
365
  `${row.content_type}, ${row.size} bytes.`,
366
366
  });
367
367
  }
368
- yield* authorizeBlobOperation(ontology, sql, caller.principal.id, found.permissions);
368
+ yield* authorizeBlobOperation(ontology, sql, caller.principal.id, found.permissions, caller.principal.execution);
369
369
  const [earlier] = yield* sql.unsafe(`SELECT id::text, promotion FROM "${ATTACHMENTS_TABLE}"
370
370
  WHERE sha256 = $1 AND branch_id = $2 AND state = 'promoted' AND id <> $3::uuid
371
371
  AND promotion->>'action' = $4 AND promotion->>'deduplicated_from' IS NULL
@@ -5,4 +5,4 @@ import { PermissionDenied } from "../errors.js";
5
5
  /** Re-read user and roles in the same transaction that authorizes a storage operation. */
6
6
  export declare function authorizeBlobOperation(ontology: Ontology, sql: {
7
7
  readonly unsafe: (text: string, args: ReadonlyArray<unknown>) => Effect.Effect<ReadonlyArray<Readonly<Record<string, unknown>>>, SqlError>;
8
- }, principalId: string, required: ReadonlyArray<string>): Effect.Effect<undefined, PermissionDenied | SqlError, never>;
8
+ }, principalId: string, required: ReadonlyArray<string>, execution?: import("../runtime/workload.js").WorkloadExecution): Effect.Effect<undefined, PermissionDenied | SqlError, never>;
@@ -2,7 +2,7 @@ import { Effect } from "effect";
2
2
  import { missingPermission, permissionsOf } from "../permissions.js";
3
3
  import { PermissionDenied } from "../errors.js";
4
4
  /** Re-read user and roles in the same transaction that authorizes a storage operation. */
5
- export function authorizeBlobOperation(ontology, sql, principalId, required) {
5
+ export function authorizeBlobOperation(ontology, sql, principalId, required, execution) {
6
6
  return Effect.gen(function* () {
7
7
  const [catalog] = yield* sql.unsafe(`SELECT hash FROM "_ontology" WHERE id = true FOR SHARE`, []);
8
8
  if (catalog?.hash !== ontology.manifest.hash) {
@@ -10,17 +10,22 @@ export function authorizeBlobOperation(ontology, sql, principalId, required) {
10
10
  message: "The deployed ontology no longer matches the database catalog.",
11
11
  });
12
12
  }
13
- const users = yield* sql.unsafe(`SELECT kind::text, active FROM "user" WHERE id = $1::uuid FOR SHARE`, [principalId]);
13
+ const users = yield* sql.unsafe(`SELECT kind::text, active, identity_provider, identity_subject FROM "user" WHERE id = $1::uuid FOR SHARE`, [principalId]);
14
14
  const user = users[0];
15
15
  const roles = yield* sql.unsafe(`SELECT role FROM "_user_roles" WHERE user_id = $1::uuid FOR SHARE`, [principalId]);
16
16
  const held = roles.map((row) => String(row.role));
17
17
  if (user?.kind === "member") {
18
18
  held.push("member");
19
19
  }
20
+ const worker = execution !== undefined &&
21
+ user?.kind === "service" &&
22
+ user.identity_provider === "segmentstream-ontology-workload" &&
23
+ user.identity_subject === `pipeline-workers:${execution.workspaceId}`;
20
24
  const missing = missingPermission(required, {
21
- permissions: permissionsOf(ontology.roles, held),
25
+ ...(worker ? { execution } : {}),
26
+ permissions: worker ? ["*:*"] : permissionsOf(ontology.roles, held),
22
27
  });
23
- if (!user?.active || missing) {
28
+ if (!user?.active || missing || (execution !== undefined && !worker)) {
24
29
  return yield* new PermissionDenied({
25
30
  message: "The member cannot use this file operation.",
26
31
  ...(missing ? { permission: missing } : {}),
@@ -98,7 +98,7 @@ export function blobRuntime(ontology, sql, provider) {
98
98
  if (!found || row.manifest_hash !== ontology.manifest.hash) {
99
99
  return yield* conflict();
100
100
  }
101
- yield* authorizeBlobOperation(ontology, sql, principal.id, found.permissions);
101
+ yield* authorizeBlobOperation(ontology, sql, principal.id, found.permissions, principal.execution);
102
102
  return row;
103
103
  });
104
104
  const begin = (input, principal) => Effect.gen(function* () {
@@ -129,7 +129,7 @@ export function blobRuntime(ontology, sql, provider) {
129
129
  const claim = randomUUID();
130
130
  const key = `revisions/${blobId}`;
131
131
  const reservation = yield* sql.withTransaction(Effect.gen(function* () {
132
- yield* authorizeBlobOperation(ontology, sql, principal.id, found.permissions);
132
+ yield* authorizeBlobOperation(ontology, sql, principal.id, found.permissions, principal.execution);
133
133
  if (input.recordId) {
134
134
  yield* makeOntologyRuntime(ontology, sql).get({
135
135
  entity: input.entity,
@@ -310,7 +310,7 @@ export function blobRuntime(ontology, sql, provider) {
310
310
  });
311
311
  }
312
312
  const blob = yield* sql.withTransaction(Effect.gen(function* () {
313
- yield* authorizeBlobOperation(ontology, sql, principal.id, [`${entityName}:read`]);
313
+ yield* authorizeBlobOperation(ontology, sql, principal.id, [`${entityName}:read`], principal.execution);
314
314
  let revisionId = input.revisionId;
315
315
  if (!revisionId) {
316
316
  const record = yield* makeOntologyRuntime(ontology, sql).get({
@@ -39,7 +39,7 @@ export function resolveBlobWrites(context, entity, recordId, values, creating) {
39
39
  }
40
40
  return context.run(statement, params);
41
41
  },
42
- }, context.scope.principalId, authored ? [authored.permission] : generatedActionPermissions(actionEntity, actionName)).pipe(Effect.catchTag("PermissionDenied", () => Effect.fail(reject())));
42
+ }, context.scope.principalId, authored ? [authored.permission] : generatedActionPermissions(actionEntity, actionName), context.scope.execution).pipe(Effect.catchTag("PermissionDenied", () => Effect.fail(reject())));
43
43
  const params = new Params();
44
44
  const rows = yield* context.run(`SELECT u.*, b.id::text AS blob_id, b.filename, b.content_type, b.size::text AS size,
45
45
  to_char(b.created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') AS created_at
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream-cloud/ontology",
3
- "version": "0.23.0",
3
+ "version": "0.24.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",