@opengeni/db 3.5.2-canary.0 → 3.7.3-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dist/canonical-human-identities.js +3 -3
  2. package/dist/{chunk-PERPE2UY.js → chunk-4NAR7FBA.js} +122 -6
  3. package/dist/chunk-4NAR7FBA.js.map +1 -0
  4. package/dist/{chunk-ORKVXHL2.js → chunk-5NNCGFQY.js} +2 -2
  5. package/dist/{chunk-TRCYF26N.js → chunk-DZCYW2WT.js} +2 -2
  6. package/dist/{chunk-X6T2P3L3.js → chunk-F6PBS6WE.js} +5 -7
  7. package/dist/chunk-F6PBS6WE.js.map +1 -0
  8. package/dist/{chunk-6NYKC5DD.js → chunk-O56DM5GZ.js} +5062 -4661
  9. package/dist/chunk-O56DM5GZ.js.map +1 -0
  10. package/dist/{chunk-FJHEWYV5.js → chunk-S3TDDCS2.js} +3 -3
  11. package/dist/{chunk-Y44FKTYU.js → chunk-SZAWDXQC.js} +3 -3
  12. package/dist/{chunk-U7NKCX5E.js → chunk-U4WPPEBH.js} +2 -2
  13. package/dist/{chunk-NRGUX3M5.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 +404 -20
  21. package/dist/index.js +5433 -2309
  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 +7 -6
  28. package/dist/schema.d.ts +732 -10
  29. package/dist/schema.js +19 -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/dist/work-claims-schema.d.ts +975 -0
  37. package/dist/work-claims.d.ts +83 -0
  38. package/drizzle/0365_permission_scoped_work_claims.sql +1306 -0
  39. package/drizzle/0366_session_discovery_title_search_index.sql +5 -0
  40. package/drizzle/0367_session_discovery_goal_search_index.sql +5 -0
  41. package/drizzle/0368_session_discovery_claim_search_index.sql +6 -0
  42. package/drizzle/0369_local_human_personal_authority.sql +350 -0
  43. package/drizzle/0370_workspace_member_management_scope.sql +98 -0
  44. package/drizzle/0371_workspace_member_candidate_inventory.sql +128 -0
  45. package/drizzle/0372_ordered_variable_set_runtime_authority.sql +287 -0
  46. package/drizzle/0373_sandbox_shared_preparation.sql +59 -0
  47. package/drizzle/0374_session_event_cursors.sql +315 -0
  48. package/drizzle/0375_session_recovery_observability.sql +73 -0
  49. package/drizzle/0376_organization_workspace_inventory.sql +88 -0
  50. package/drizzle/0377_scoped_rig_health_projection.sql +80 -0
  51. package/drizzle/0378_scoped_rig_health_audit_timestamp.sql +77 -0
  52. package/drizzle/0379_session_event_raw_lane_activation.sql +518 -0
  53. package/drizzle/0380_autonomous_company_profile_agent_policy.sql +829 -0
  54. package/drizzle/0381_organization_codex_subscription_inheritance.sql +720 -0
  55. package/drizzle/0382_organization_api_key_provenance.sql +91 -0
  56. package/drizzle/0383_codex_cooldown_reconciliation.sql +51 -0
  57. package/drizzle/0384_codex_cooldown_revision_guard_privileges.sql +6 -0
  58. package/drizzle/0385_connected_machine_legacy_tilde_workdirs.sql +25 -0
  59. package/drizzle/0386_local_organization_administration.sql +157 -0
  60. package/package.json +6 -6
  61. package/src/codex-token-resolver.ts +26 -1
  62. package/src/company-brain-governed-writes.ts +30 -1
  63. package/src/company-profile-agent-admin.ts +68 -1
  64. package/src/company-profile-agent-policy.ts +126 -0
  65. package/src/company-profile-schema.ts +115 -1
  66. package/src/database.ts +3 -5
  67. package/src/index.ts +4677 -786
  68. package/src/managed-human-provisioning.ts +3 -3
  69. package/src/memory-domain.ts +9 -4
  70. package/src/organization-membership-lifecycle.ts +0 -1
  71. package/src/provision-roles.ts +101 -4
  72. package/src/runtime-posture.ts +34 -0
  73. package/src/schema.ts +162 -8
  74. package/src/session-control.ts +177 -59
  75. package/src/session-queue-commands.ts +146 -40
  76. package/src/session-tool-call-settlement.ts +34 -7
  77. package/src/user-resource-authority.ts +56 -0
  78. package/src/work-claims-schema.ts +148 -0
  79. package/src/work-claims.ts +200 -0
  80. package/dist/chunk-6NYKC5DD.js.map +0 -1
  81. package/dist/chunk-PERPE2UY.js.map +0 -1
  82. package/dist/chunk-X6T2P3L3.js.map +0 -1
  83. /package/dist/{chunk-ORKVXHL2.js.map → chunk-5NNCGFQY.js.map} +0 -0
  84. /package/dist/{chunk-TRCYF26N.js.map → chunk-DZCYW2WT.js.map} +0 -0
  85. /package/dist/{chunk-FJHEWYV5.js.map → chunk-S3TDDCS2.js.map} +0 -0
  86. /package/dist/{chunk-Y44FKTYU.js.map → chunk-SZAWDXQC.js.map} +0 -0
  87. /package/dist/{chunk-U7NKCX5E.js.map → chunk-U4WPPEBH.js.map} +0 -0
  88. /package/dist/{chunk-NRGUX3M5.js.map → chunk-US4IS6IV.js.map} +0 -0
@@ -0,0 +1,148 @@
1
+ import { sql } from "drizzle-orm";
2
+ import {
3
+ customType,
4
+ index,
5
+ integer,
6
+ pgTable,
7
+ primaryKey,
8
+ text,
9
+ timestamp,
10
+ uniqueIndex,
11
+ uuid,
12
+ } from "drizzle-orm/pg-core";
13
+
14
+ const xid8 = customType<{ data: string; driverData: string }>({
15
+ dataType: () => "xid8",
16
+ });
17
+
18
+ // Exact lifecycle-only mutation, immutable revisions, FORCE RLS, and runtime
19
+ // grants are owned by the work-claim migration. The application role receives
20
+ // SELECT on the compact head plus EXECUTE on exact-attempt mutation routines,
21
+ // never direct DML authority.
22
+ export const sessionWorkClaims = pgTable(
23
+ "session_work_claims",
24
+ {
25
+ id: uuid("id").primaryKey().defaultRandom(),
26
+ accountId: uuid("account_id").notNull(),
27
+ workspaceId: uuid("workspace_id").notNull(),
28
+ sessionId: uuid("session_id").notNull(),
29
+ rootSessionId: uuid("root_session_id").notNull(),
30
+ subjectNamespace: text("subject_namespace").notNull(),
31
+ subjectType: text("subject_type").notNull(),
32
+ canonicalKey: text("canonical_key").notNull(),
33
+ subjectDigest: text("subject_digest").notNull(),
34
+ displayLabel: text("display_label"),
35
+ role: text("role").notNull(),
36
+ state: text("state").notNull().default("active"),
37
+ revision: integer("revision").notNull().default(1),
38
+ provenance: text("provenance").notNull(),
39
+ versionKind: text("version_kind"),
40
+ versionValue: text("version_value"),
41
+ observedAt: timestamp("observed_at", { withTimezone: true }).notNull(),
42
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
43
+ updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
44
+ settledAt: timestamp("settled_at", { withTimezone: true }),
45
+ },
46
+ (table) => ({
47
+ workspaceIdentity: uniqueIndex("session_work_claims_workspace_id_uq").on(
48
+ table.workspaceId,
49
+ table.id,
50
+ ),
51
+ activeIdentity: uniqueIndex("session_work_claims_active_identity_uq")
52
+ .on(
53
+ table.workspaceId,
54
+ table.sessionId,
55
+ table.subjectNamespace,
56
+ table.subjectType,
57
+ table.subjectDigest,
58
+ table.role,
59
+ )
60
+ .where(sql`${table.state} = 'active'`),
61
+ sessionState: index("session_work_claims_session_state_idx").on(
62
+ table.workspaceId,
63
+ table.sessionId,
64
+ table.state,
65
+ table.updatedAt,
66
+ table.id,
67
+ ),
68
+ subjectState: index("session_work_claims_subject_state_idx").on(
69
+ table.workspaceId,
70
+ table.subjectNamespace,
71
+ table.subjectType,
72
+ table.subjectDigest,
73
+ table.state,
74
+ table.updatedAt,
75
+ table.id,
76
+ ),
77
+ }),
78
+ );
79
+
80
+ export const sessionWorkClaimRevisions = pgTable(
81
+ "session_work_claim_revisions",
82
+ {
83
+ id: uuid("id").primaryKey().defaultRandom(),
84
+ accountId: uuid("account_id").notNull(),
85
+ workspaceId: uuid("workspace_id").notNull(),
86
+ claimId: uuid("claim_id").notNull(),
87
+ sessionId: uuid("session_id").notNull(),
88
+ rootSessionId: uuid("root_session_id").notNull(),
89
+ operationId: uuid("operation_id").notNull(),
90
+ inputHash: text("input_hash").notNull(),
91
+ mutationKind: text("mutation_kind").notNull(),
92
+ priorRevision: integer("prior_revision"),
93
+ resultingRevision: integer("resulting_revision").notNull(),
94
+ subjectNamespace: text("subject_namespace").notNull(),
95
+ subjectType: text("subject_type").notNull(),
96
+ canonicalKey: text("canonical_key").notNull(),
97
+ subjectDigest: text("subject_digest").notNull(),
98
+ displayLabel: text("display_label"),
99
+ role: text("role").notNull(),
100
+ state: text("state").notNull(),
101
+ provenance: text("provenance").notNull(),
102
+ versionKind: text("version_kind"),
103
+ versionValue: text("version_value"),
104
+ observedAt: timestamp("observed_at", { withTimezone: true }).notNull(),
105
+ claimCreatedAt: timestamp("claim_created_at", { withTimezone: true }).notNull(),
106
+ claimUpdatedAt: timestamp("claim_updated_at", { withTimezone: true }).notNull(),
107
+ settledAt: timestamp("settled_at", { withTimezone: true }),
108
+ actorKind: text("actor_kind").notNull(),
109
+ actorSubjectId: text("actor_subject_id").notNull(),
110
+ actorSessionId: uuid("actor_session_id"),
111
+ actorTurnId: uuid("actor_turn_id"),
112
+ actorAttemptId: uuid("actor_attempt_id"),
113
+ actorExecutionGeneration: integer("actor_execution_generation"),
114
+ reason: text("reason"),
115
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
116
+ },
117
+ (table) => ({
118
+ operation: uniqueIndex("session_work_claim_revisions_workspace_operation_uq").on(
119
+ table.workspaceId,
120
+ table.operationId,
121
+ ),
122
+ claimRevision: uniqueIndex("session_work_claim_revisions_claim_revision_uq").on(
123
+ table.workspaceId,
124
+ table.claimId,
125
+ table.resultingRevision,
126
+ ),
127
+ sessionTimeline: index("session_work_claim_revisions_session_timeline_idx").on(
128
+ table.workspaceId,
129
+ table.sessionId,
130
+ table.createdAt,
131
+ table.id,
132
+ ),
133
+ }),
134
+ );
135
+
136
+ export const sessionWorkClaimWriteCapabilities = pgTable(
137
+ "session_work_claim_write_capabilities",
138
+ {
139
+ backendPid: integer("backend_pid").notNull(),
140
+ transactionId: xid8("transaction_id").notNull(),
141
+ capabilityId: uuid("capability_id").notNull(),
142
+ },
143
+ (table) => ({
144
+ identity: primaryKey({
145
+ columns: [table.backendPid, table.transactionId, table.capabilityId],
146
+ }),
147
+ }),
148
+ );
@@ -0,0 +1,200 @@
1
+ import {
2
+ WorkClaim,
3
+ WorkClaimCanonicalKey,
4
+ WorkClaimDisplayLabel,
5
+ WorkClaimMutationKind,
6
+ WorkClaimMutationResult,
7
+ WorkClaimNamespace,
8
+ WorkClaimReleaseReason,
9
+ WorkClaimRole,
10
+ WorkClaimSubjectType,
11
+ WorkClaimVersionKind,
12
+ WorkClaimVersionValue,
13
+ normalizeWorkClaimCanonicalKey,
14
+ normalizeWorkClaimDisplayLabel,
15
+ normalizeWorkClaimNamespace,
16
+ type WorkClaimRole as WorkClaimRoleType,
17
+ type WorkClaimSubjectType as WorkClaimSubjectTypeType,
18
+ type WorkClaimVersionKind as WorkClaimVersionKindType,
19
+ } from "@opengeni/contracts";
20
+ import { sql } from "drizzle-orm";
21
+ import type { Database } from "./database";
22
+ import { rawRows, withSessionActivityRlsContext } from "./database";
23
+
24
+ export type WorkClaimAttemptClaims = {
25
+ accountId: string;
26
+ workspaceId: string;
27
+ sessionId: string;
28
+ turnId: string;
29
+ attemptId: string;
30
+ executionGeneration: number;
31
+ };
32
+
33
+ export type UpsertWorkClaimInput = WorkClaimAttemptClaims & {
34
+ operationId: string;
35
+ expectedRevision: number;
36
+ subjectNamespace: string;
37
+ subjectType: WorkClaimSubjectTypeType;
38
+ canonicalKey: string;
39
+ displayLabel?: string | null;
40
+ role: WorkClaimRoleType;
41
+ version?: {
42
+ kind: WorkClaimVersionKindType;
43
+ value: string;
44
+ } | null;
45
+ };
46
+
47
+ export type ReleaseWorkClaimInput = WorkClaimAttemptClaims & {
48
+ operationId: string;
49
+ claimId: string;
50
+ expectedRevision: number;
51
+ reason: string;
52
+ };
53
+
54
+ type WorkClaimMutationRow = {
55
+ claim_id: string;
56
+ session_id: string;
57
+ root_session_id: string;
58
+ subject_namespace: string;
59
+ subject_type: string;
60
+ canonical_key: string;
61
+ display_label: string | null;
62
+ role: string;
63
+ state: string;
64
+ revision: number;
65
+ provenance: string;
66
+ version_kind: string | null;
67
+ version_value: string | null;
68
+ observed_at: Date | string;
69
+ created_at: Date | string;
70
+ updated_at: Date | string;
71
+ settled_at: Date | string | null;
72
+ mutation_kind: string;
73
+ replayed: boolean;
74
+ };
75
+
76
+ function iso(value: Date | string): string {
77
+ return (value instanceof Date ? value : new Date(value)).toISOString();
78
+ }
79
+
80
+ function assertAttemptClaims(claims: WorkClaimAttemptClaims): void {
81
+ if (!Number.isSafeInteger(claims.executionGeneration) || claims.executionGeneration < 1) {
82
+ throw new Error("Work claims require a positive exact execution generation");
83
+ }
84
+ }
85
+
86
+ function mutationResultFromRow(row: WorkClaimMutationRow) {
87
+ const version =
88
+ row.version_kind === null || row.version_value === null
89
+ ? null
90
+ : {
91
+ kind: WorkClaimVersionKind.parse(row.version_kind),
92
+ value: WorkClaimVersionValue.parse(row.version_value),
93
+ };
94
+ return WorkClaimMutationResult.parse({
95
+ claim: WorkClaim.parse({
96
+ id: row.claim_id,
97
+ sessionId: row.session_id,
98
+ rootSessionId: row.root_session_id,
99
+ subject: {
100
+ namespace: row.subject_namespace,
101
+ type: row.subject_type,
102
+ canonicalKey: row.canonical_key,
103
+ displayLabel: row.display_label,
104
+ },
105
+ role: row.role,
106
+ state: row.state,
107
+ revision: Number(row.revision),
108
+ provenance: row.provenance,
109
+ version,
110
+ observedAt: iso(row.observed_at),
111
+ createdAt: iso(row.created_at),
112
+ updatedAt: iso(row.updated_at),
113
+ settledAt: row.settled_at === null ? null : iso(row.settled_at),
114
+ }),
115
+ mutation: WorkClaimMutationKind.parse(row.mutation_kind),
116
+ replayed: row.replayed,
117
+ });
118
+ }
119
+
120
+ export async function upsertWorkClaim(db: Database, input: UpsertWorkClaimInput) {
121
+ assertAttemptClaims(input);
122
+ if (!Number.isSafeInteger(input.expectedRevision) || input.expectedRevision < 0) {
123
+ throw new Error("Work claim expectedRevision must be a non-negative safe integer");
124
+ }
125
+ const subjectNamespace = WorkClaimNamespace.parse(
126
+ normalizeWorkClaimNamespace(input.subjectNamespace),
127
+ );
128
+ const subjectType = WorkClaimSubjectType.parse(input.subjectType);
129
+ const canonicalKey = WorkClaimCanonicalKey.parse(
130
+ normalizeWorkClaimCanonicalKey(input.canonicalKey),
131
+ );
132
+ const role = WorkClaimRole.parse(input.role);
133
+ const displayLabel =
134
+ input.displayLabel === undefined || input.displayLabel === null
135
+ ? null
136
+ : WorkClaimDisplayLabel.parse(normalizeWorkClaimDisplayLabel(input.displayLabel));
137
+ const version = input.version
138
+ ? {
139
+ kind: WorkClaimVersionKind.parse(input.version.kind),
140
+ value: WorkClaimVersionValue.parse(normalizeWorkClaimCanonicalKey(input.version.value)),
141
+ }
142
+ : null;
143
+ const rows = await withSessionActivityRlsContext(
144
+ db,
145
+ { accountId: input.accountId, workspaceId: input.workspaceId },
146
+ async (tx) =>
147
+ await rawRows<WorkClaimMutationRow>(
148
+ tx,
149
+ sql`SELECT * FROM upsert_session_work_claim_for_attempt(
150
+ ${input.accountId}::uuid,
151
+ ${input.workspaceId}::uuid,
152
+ ${input.sessionId}::uuid,
153
+ ${input.turnId}::uuid,
154
+ ${input.attemptId}::uuid,
155
+ ${input.executionGeneration}::integer,
156
+ ${input.operationId}::uuid,
157
+ ${input.expectedRevision}::integer,
158
+ ${subjectNamespace}::text,
159
+ ${subjectType}::text,
160
+ ${canonicalKey}::text,
161
+ ${displayLabel}::text,
162
+ ${role}::text,
163
+ ${version?.kind ?? null}::text,
164
+ ${version?.value ?? null}::text
165
+ )`,
166
+ ),
167
+ );
168
+ if (rows.length !== 1) throw new Error("Work claim upsert returned no durable receipt");
169
+ return mutationResultFromRow(rows[0]!);
170
+ }
171
+
172
+ export async function releaseWorkClaim(db: Database, input: ReleaseWorkClaimInput) {
173
+ assertAttemptClaims(input);
174
+ if (!Number.isSafeInteger(input.expectedRevision) || input.expectedRevision < 1) {
175
+ throw new Error("Work claim release requires a positive expectedRevision");
176
+ }
177
+ const reason = WorkClaimReleaseReason.parse(input.reason);
178
+ const rows = await withSessionActivityRlsContext(
179
+ db,
180
+ { accountId: input.accountId, workspaceId: input.workspaceId },
181
+ async (tx) =>
182
+ await rawRows<WorkClaimMutationRow>(
183
+ tx,
184
+ sql`SELECT * FROM release_session_work_claim_for_attempt(
185
+ ${input.accountId}::uuid,
186
+ ${input.workspaceId}::uuid,
187
+ ${input.sessionId}::uuid,
188
+ ${input.turnId}::uuid,
189
+ ${input.attemptId}::uuid,
190
+ ${input.executionGeneration}::integer,
191
+ ${input.operationId}::uuid,
192
+ ${input.claimId}::uuid,
193
+ ${input.expectedRevision}::integer,
194
+ ${reason}::text
195
+ )`,
196
+ ),
197
+ );
198
+ if (rows.length !== 1) throw new Error("Work claim release returned no durable receipt");
199
+ return mutationResultFromRow(rows[0]!);
200
+ }