@opengeni/db 3.6.0 → 3.7.4

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 (80) hide show
  1. package/dist/canonical-human-identities.js +3 -3
  2. package/dist/{chunk-744DFC65.js → chunk-4NAR7FBA.js} +92 -6
  3. package/dist/chunk-4NAR7FBA.js.map +1 -0
  4. package/dist/{chunk-IW6O6G7R.js → chunk-5NNCGFQY.js} +2 -2
  5. package/dist/{chunk-G3732M3T.js → chunk-DZCYW2WT.js} +2 -2
  6. package/dist/{chunk-WW7AJ5IC.js → chunk-F6PBS6WE.js} +5 -7
  7. package/dist/chunk-F6PBS6WE.js.map +1 -0
  8. package/dist/{chunk-UJ2EW3QI.js → chunk-O56DM5GZ.js} +340 -85
  9. package/dist/chunk-O56DM5GZ.js.map +1 -0
  10. package/dist/{chunk-CDMMSUWJ.js → chunk-S3TDDCS2.js} +3 -3
  11. package/dist/{chunk-Q5YUGQVK.js → chunk-SZAWDXQC.js} +3 -3
  12. package/dist/{chunk-UGDVSQ6U.js → chunk-U4WPPEBH.js} +2 -2
  13. package/dist/{chunk-2NT476HD.js → chunk-US4IS6IV.js} +2 -2
  14. package/dist/codex-token-resolver.d.ts +12 -1
  15. package/dist/company-profile-agent-admin.d.ts +4 -2
  16. package/dist/company-profile-agent-policy.d.ts +20 -0
  17. package/dist/company-profile-schema.d.ts +573 -0
  18. package/dist/editable-artifact-durable-export.js +2 -2
  19. package/dist/editable-artifacts.js +3 -3
  20. package/dist/index.d.ts +382 -18
  21. package/dist/index.js +4513 -2065
  22. package/dist/index.js.map +1 -1
  23. package/dist/managed-auth-session-sets.js +3 -3
  24. package/dist/managed-human-provisioning.d.ts +3 -3
  25. package/dist/memory-domain.d.ts +5 -3
  26. package/dist/provision-roles.js +1 -1
  27. package/dist/runtime-posture.d.ts +5 -5
  28. package/dist/schema.d.ts +725 -4
  29. package/dist/schema.js +13 -1
  30. package/dist/session-background-commands.js +3 -3
  31. package/dist/session-control.d.ts +40 -5
  32. package/dist/session-queue-commands.d.ts +2 -0
  33. package/dist/session-tenancy.js +3 -3
  34. package/dist/user-resource-authority.d.ts +8 -0
  35. package/dist/video-generation.js +3 -3
  36. package/drizzle/0369_local_human_personal_authority.sql +350 -0
  37. package/drizzle/0370_workspace_member_management_scope.sql +98 -0
  38. package/drizzle/0371_workspace_member_candidate_inventory.sql +128 -0
  39. package/drizzle/0372_ordered_variable_set_runtime_authority.sql +287 -0
  40. package/drizzle/0373_sandbox_shared_preparation.sql +59 -0
  41. package/drizzle/0374_session_event_cursors.sql +315 -0
  42. package/drizzle/0375_session_recovery_observability.sql +73 -0
  43. package/drizzle/0376_organization_workspace_inventory.sql +88 -0
  44. package/drizzle/0377_scoped_rig_health_projection.sql +80 -0
  45. package/drizzle/0378_scoped_rig_health_audit_timestamp.sql +77 -0
  46. package/drizzle/0379_session_event_raw_lane_activation.sql +518 -0
  47. package/drizzle/0380_autonomous_company_profile_agent_policy.sql +829 -0
  48. package/drizzle/0381_organization_codex_subscription_inheritance.sql +720 -0
  49. package/drizzle/0382_organization_api_key_provenance.sql +91 -0
  50. package/drizzle/0383_codex_cooldown_reconciliation.sql +51 -0
  51. package/drizzle/0384_codex_cooldown_revision_guard_privileges.sql +6 -0
  52. package/drizzle/0385_connected_machine_legacy_tilde_workdirs.sql +25 -0
  53. package/drizzle/0386_local_organization_administration.sql +157 -0
  54. package/package.json +5 -5
  55. package/src/codex-token-resolver.ts +26 -1
  56. package/src/company-brain-governed-writes.ts +30 -1
  57. package/src/company-profile-agent-admin.ts +68 -1
  58. package/src/company-profile-agent-policy.ts +126 -0
  59. package/src/company-profile-schema.ts +115 -1
  60. package/src/database.ts +3 -5
  61. package/src/index.ts +3626 -456
  62. package/src/managed-human-provisioning.ts +3 -3
  63. package/src/memory-domain.ts +9 -4
  64. package/src/organization-membership-lifecycle.ts +0 -1
  65. package/src/provision-roles.ts +83 -4
  66. package/src/runtime-posture.ts +22 -0
  67. package/src/schema.ts +161 -8
  68. package/src/session-control.ts +177 -59
  69. package/src/session-queue-commands.ts +146 -40
  70. package/src/session-tool-call-settlement.ts +34 -7
  71. package/src/user-resource-authority.ts +56 -0
  72. package/dist/chunk-744DFC65.js.map +0 -1
  73. package/dist/chunk-UJ2EW3QI.js.map +0 -1
  74. package/dist/chunk-WW7AJ5IC.js.map +0 -1
  75. /package/dist/{chunk-IW6O6G7R.js.map → chunk-5NNCGFQY.js.map} +0 -0
  76. /package/dist/{chunk-G3732M3T.js.map → chunk-DZCYW2WT.js.map} +0 -0
  77. /package/dist/{chunk-CDMMSUWJ.js.map → chunk-S3TDDCS2.js.map} +0 -0
  78. /package/dist/{chunk-Q5YUGQVK.js.map → chunk-SZAWDXQC.js.map} +0 -0
  79. /package/dist/{chunk-UGDVSQ6U.js.map → chunk-U4WPPEBH.js.map} +0 -0
  80. /package/dist/{chunk-2NT476HD.js.map → chunk-US4IS6IV.js.map} +0 -0
@@ -0,0 +1,126 @@
1
+ import {
2
+ CompanyProfileAgentPolicy,
3
+ CompanyProfileAgentPolicyMode,
4
+ type CompanyProfileAgentPolicy as CompanyProfileAgentPolicyType,
5
+ type CompanyProfileAgentPolicyMode as CompanyProfileAgentPolicyModeType,
6
+ } from "@opengeni/contracts";
7
+ import { sql } from "drizzle-orm";
8
+ import { rawRows, withRlsContext, withSessionRlsActorContext, type Database } from "./database";
9
+ import { nestedPostgresSqlState } from "./persistence-errors";
10
+
11
+ export class CompanyProfileAgentPolicyError extends Error {
12
+ readonly name = "CompanyProfileAgentPolicyError";
13
+
14
+ constructor(
15
+ readonly code: "authority_unavailable" | "policy_conflict" | "operation_reused" | "invalid",
16
+ message: string,
17
+ options?: ErrorOptions,
18
+ ) {
19
+ super(message, options);
20
+ }
21
+ }
22
+
23
+ function translate(error: unknown): never {
24
+ const state = nestedPostgresSqlState(error);
25
+ if (state === "42501") {
26
+ throw new CompanyProfileAgentPolicyError(
27
+ "authority_unavailable",
28
+ "An active organization owner is required to manage company-profile agent autonomy.",
29
+ { cause: error },
30
+ );
31
+ }
32
+ if (state === "40001") {
33
+ throw new CompanyProfileAgentPolicyError(
34
+ "policy_conflict",
35
+ "The company-profile agent policy changed in another request. Reload and try again.",
36
+ { cause: error },
37
+ );
38
+ }
39
+ if (state === "23505" || state === "P1851") {
40
+ throw new CompanyProfileAgentPolicyError(
41
+ "operation_reused",
42
+ "The company-profile agent policy operation id was already used for different input.",
43
+ { cause: error },
44
+ );
45
+ }
46
+ if (state === "22023" || state === "23514" || state === "55000") {
47
+ throw new CompanyProfileAgentPolicyError(
48
+ "invalid",
49
+ "The company-profile agent policy request is invalid.",
50
+ { cause: error },
51
+ );
52
+ }
53
+ throw error;
54
+ }
55
+
56
+ async function withHumanPolicyContext<T>(
57
+ db: Database,
58
+ input: { accountId: string; workspaceId: string; actorSubjectId: string },
59
+ fn: (scoped: Database) => Promise<T>,
60
+ ): Promise<T> {
61
+ return await withSessionRlsActorContext({ subjectId: input.actorSubjectId }, async () =>
62
+ withRlsContext(
63
+ db,
64
+ { accountId: input.accountId, workspaceId: input.workspaceId },
65
+ async (scoped) => {
66
+ await scoped.execute(
67
+ sql`select set_config('opengeni.principal_kind', 'human_session', true)`,
68
+ );
69
+ return await fn(scoped);
70
+ },
71
+ ),
72
+ );
73
+ }
74
+
75
+ export async function getCompanyProfileAgentPolicy(
76
+ db: Database,
77
+ input: { accountId: string; workspaceId: string; actorSubjectId: string },
78
+ ): Promise<CompanyProfileAgentPolicyType> {
79
+ try {
80
+ return await withHumanPolicyContext(db, input, async (scoped) => {
81
+ const [row] = await rawRows<{ result: unknown }>(
82
+ scoped,
83
+ sql`select get_company_profile_agent_policy(
84
+ ${input.accountId}::uuid,
85
+ ${input.workspaceId}::uuid,
86
+ ${input.actorSubjectId}
87
+ ) as result`,
88
+ );
89
+ return CompanyProfileAgentPolicy.parse(row?.result);
90
+ });
91
+ } catch (error) {
92
+ translate(error);
93
+ }
94
+ }
95
+
96
+ export async function updateCompanyProfileAgentPolicy(
97
+ db: Database,
98
+ input: {
99
+ accountId: string;
100
+ workspaceId: string;
101
+ actorSubjectId: string;
102
+ mode: CompanyProfileAgentPolicyModeType;
103
+ expectedVersion: number;
104
+ operationId: string;
105
+ },
106
+ ): Promise<CompanyProfileAgentPolicyType> {
107
+ const mode = CompanyProfileAgentPolicyMode.parse(input.mode);
108
+ try {
109
+ return await withHumanPolicyContext(db, input, async (scoped) => {
110
+ const [row] = await rawRows<{ result: unknown }>(
111
+ scoped,
112
+ sql`select update_company_profile_agent_policy(
113
+ ${input.accountId}::uuid,
114
+ ${input.workspaceId}::uuid,
115
+ ${input.actorSubjectId},
116
+ ${mode},
117
+ ${input.expectedVersion}::bigint,
118
+ ${input.operationId}::uuid
119
+ ) as result`,
120
+ );
121
+ return CompanyProfileAgentPolicy.parse(row?.result);
122
+ });
123
+ } catch (error) {
124
+ translate(error);
125
+ }
126
+ }
@@ -1,10 +1,12 @@
1
1
  import { sql } from "drizzle-orm";
2
2
  import type {
3
3
  CompanyProfileAgentHumanInputPrompt,
4
+ CompanyProfileAgentPolicyMode,
4
5
  CompanyProfileSnapshotEntry,
5
6
  } from "@opengeni/contracts";
6
7
  import {
7
8
  bigint,
9
+ boolean,
8
10
  check,
9
11
  index,
10
12
  integer,
@@ -111,6 +113,57 @@ export const companyProfileActivationEvents = pgTable(
111
113
  }),
112
114
  );
113
115
 
116
+ export const organizationCompanyProfileAgentPolicies = pgTable(
117
+ "organization_company_profile_agent_policies",
118
+ {
119
+ accountId: uuid("account_id").primaryKey(),
120
+ mode: text("mode").$type<CompanyProfileAgentPolicyMode>().notNull().default("suggest"),
121
+ version: bigint("version", { mode: "number" }).notNull().default(0),
122
+ updatedByMembershipId: uuid("updated_by_membership_id"),
123
+ updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
124
+ },
125
+ (table) => ({
126
+ mode: check(
127
+ "organization_company_profile_agent_policies_mode_check",
128
+ sql`${table.mode} in ('off', 'suggest', 'automatic')`,
129
+ ),
130
+ version: check(
131
+ "organization_company_profile_agent_policies_version_check",
132
+ sql`${table.version} >= 0`,
133
+ ),
134
+ }),
135
+ );
136
+
137
+ export const organizationCompanyProfileAgentPolicyEvents = pgTable(
138
+ "organization_company_profile_agent_policy_events",
139
+ {
140
+ id: uuid("id").primaryKey(),
141
+ accountId: uuid("account_id").notNull(),
142
+ actorMembershipId: uuid("actor_membership_id").notNull(),
143
+ requestedMode: text("requested_mode").$type<CompanyProfileAgentPolicyMode>().notNull(),
144
+ expectedVersion: bigint("expected_version", { mode: "number" }).notNull(),
145
+ resultMode: text("result_mode").$type<CompanyProfileAgentPolicyMode>().notNull(),
146
+ resultVersion: bigint("result_version", { mode: "number" }).notNull(),
147
+ resultUpdatedAt: timestamp("result_updated_at", { withTimezone: true }).notNull(),
148
+ changed: boolean("changed").notNull(),
149
+ },
150
+ (table) => ({
151
+ accountTimeline: index("organization_company_profile_agent_policy_events_account_idx").on(
152
+ table.accountId,
153
+ table.resultVersion,
154
+ ),
155
+ modes: check(
156
+ "organization_company_profile_agent_policy_events_mode_check",
157
+ sql`${table.requestedMode} in ('off', 'suggest', 'automatic')
158
+ and ${table.resultMode} in ('off', 'suggest', 'automatic')`,
159
+ ),
160
+ versions: check(
161
+ "organization_company_profile_agent_policy_events_version_check",
162
+ sql`${table.expectedVersion} >= 0 and ${table.resultVersion} > 0`,
163
+ ),
164
+ }),
165
+ );
166
+
114
167
  export const companyProfileSnapshots = pgTable(
115
168
  "company_profile_snapshots",
116
169
  {
@@ -164,6 +217,8 @@ export const companyProfileAgentProposalReceipts = pgTable(
164
217
  revisionId: uuid("revision_id").notNull(),
165
218
  expectedCurrentRevisionId: uuid("expected_current_revision_id"),
166
219
  expectedActivationVersion: bigint("expected_activation_version", { mode: "number" }).notNull(),
220
+ policyMode: text("policy_mode").$type<CompanyProfileAgentPolicyMode>().notNull(),
221
+ policyVersion: bigint("policy_version", { mode: "number" }).notNull(),
167
222
  reason: text("reason").notNull(),
168
223
  humanInput: jsonb("human_input").$type<CompanyProfileAgentHumanInputPrompt>().notNull(),
169
224
  createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
@@ -193,7 +248,9 @@ export const companyProfileAgentProposalReceipts = pgTable(
193
248
  ),
194
249
  generation: check(
195
250
  "company_profile_agent_proposals_generation_check",
196
- sql`${table.executionGeneration} > 0 and ${table.expectedActivationVersion} >= 0`,
251
+ sql`${table.executionGeneration} > 0
252
+ and ${table.expectedActivationVersion} >= 0
253
+ and ${table.policyVersion} >= 0`,
197
254
  ),
198
255
  subject: check(
199
256
  "company_profile_agent_proposals_subject_check",
@@ -204,6 +261,10 @@ export const companyProfileAgentProposalReceipts = pgTable(
204
261
  sql`char_length(btrim(${table.reason})) between 1 and 4096
205
262
  and octet_length(convert_to(btrim(${table.reason}), 'UTF8')) <= 16384`,
206
263
  ),
264
+ policyMode: check(
265
+ "company_profile_agent_proposals_policy_mode_check",
266
+ sql`${table.policyMode} in ('suggest', 'automatic')`,
267
+ ),
207
268
  humanInputShape: check(
208
269
  "company_profile_agent_proposals_human_input_check",
209
270
  sql`jsonb_typeof(${table.humanInput}) = 'object'
@@ -261,3 +322,56 @@ export const companyProfileAgentConfirmationReceipts = pgTable(
261
322
  ),
262
323
  }),
263
324
  );
325
+
326
+ export const companyProfileAgentAutomaticActivationReceipts = pgTable(
327
+ "company_profile_agent_automatic_activation_receipts",
328
+ {
329
+ id: uuid("id").primaryKey().defaultRandom(),
330
+ operationId: uuid("operation_id").notNull(),
331
+ inputHash: text("input_hash").notNull(),
332
+ accountId: uuid("account_id").notNull(),
333
+ workspaceId: uuid("workspace_id").notNull(),
334
+ sessionId: uuid("session_id").notNull(),
335
+ turnId: uuid("turn_id").notNull(),
336
+ activationAttemptId: uuid("activation_attempt_id").notNull(),
337
+ executionGeneration: integer("execution_generation").notNull(),
338
+ proposalReceiptId: uuid("proposal_receipt_id").notNull(),
339
+ proposalRevisionId: uuid("proposal_revision_id").notNull(),
340
+ initiatingHumanSubjectId: text("initiating_human_subject_id").notNull(),
341
+ initiatingMembershipId: uuid("initiating_membership_id").notNull(),
342
+ policyVersion: bigint("policy_version", { mode: "number" }).notNull(),
343
+ activationEventId: uuid("activation_event_id").notNull(),
344
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
345
+ },
346
+ (table) => ({
347
+ accountOperation: uniqueIndex(
348
+ "company_profile_agent_automatic_activations_account_operation_uq",
349
+ ).on(table.accountId, table.operationId),
350
+ proposal: uniqueIndex("company_profile_agent_automatic_activations_proposal_uq").on(
351
+ table.accountId,
352
+ table.proposalReceiptId,
353
+ ),
354
+ activationEvent: uniqueIndex("company_profile_agent_automatic_activations_event_uq").on(
355
+ table.accountId,
356
+ table.activationEventId,
357
+ ),
358
+ turn: index("company_profile_agent_automatic_activations_turn_idx").on(
359
+ table.workspaceId,
360
+ table.sessionId,
361
+ table.turnId,
362
+ table.executionGeneration,
363
+ ),
364
+ hash: check(
365
+ "company_profile_agent_automatic_activations_input_hash_check",
366
+ sql`${table.inputHash} ~ '^[0-9a-f]{64}$'`,
367
+ ),
368
+ generation: check(
369
+ "company_profile_agent_automatic_activations_generation_check",
370
+ sql`${table.executionGeneration} > 0 and ${table.policyVersion} >= 0`,
371
+ ),
372
+ subject: check(
373
+ "company_profile_agent_automatic_activations_subject_check",
374
+ sql`octet_length(btrim(${table.initiatingHumanSubjectId})) between 1 and 1024`,
375
+ ),
376
+ }),
377
+ );
package/src/database.ts CHANGED
@@ -374,16 +374,14 @@ export async function setRlsContext(db: Database, context: RlsContext): Promise<
374
374
  if (typeof context.accountId !== "string" || context.accountId.trim() === "") {
375
375
  throw new Error("setRlsContext: a non-empty accountId is required to establish an RLS context");
376
376
  }
377
- await db.execute(sql`select set_config('opengeni.account_id', ${context.accountId}, true)`);
378
- await db.execute(
379
- sql`select set_config('opengeni.workspace_id', ${context.workspaceId ?? ""}, true)`,
380
- );
381
377
  // Transaction-local writer identity covers supported injected/embedded
382
378
  // database handles whose connection-level application_name is host-owned.
383
379
  // Standalone createDb connections also carry version receipts in their exact
384
380
  // application_name, while old OpenGeni binaries set neither current receipt.
385
- await db.execute(sql`select set_config('opengeni.lossless_content_writer', '1', true)`);
386
381
  await db.execute(sql`select
382
+ set_config('opengeni.account_id', ${context.accountId}, true),
383
+ set_config('opengeni.workspace_id', ${context.workspaceId ?? ""}, true),
384
+ set_config('opengeni.lossless_content_writer', '1', true),
387
385
  set_config('opengeni.sandbox_recovery_protocol_v2', '1', true),
388
386
  set_config('opengeni.pending_tool_event_output_v1', '1', true),
389
387
  set_config('opengeni.session_variable_set_attachments_v1', '1', true)`);