@opengeni/db 0.36.0 → 1.0.1

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 (51) hide show
  1. package/dist/canonical-human-identities.js +3 -3
  2. package/dist/{chunk-P3RE4D2B.js → chunk-4SR4HKKA.js} +2 -2
  3. package/dist/{chunk-5KTIEDX7.js → chunk-6ZYKCRFO.js} +124 -16
  4. package/dist/chunk-6ZYKCRFO.js.map +1 -0
  5. package/dist/{chunk-CQZ4QSP5.js → chunk-CUPM3FGB.js} +3 -3
  6. package/dist/{chunk-TAUTWOS5.js → chunk-LTG3S6ZY.js} +2 -2
  7. package/dist/{chunk-VKLB2ZGI.js → chunk-NXAFLCQX.js} +81 -2
  8. package/dist/chunk-NXAFLCQX.js.map +1 -0
  9. package/dist/{chunk-F32YT7MP.js → chunk-WD5ROSLL.js} +2 -2
  10. package/dist/{chunk-JC5QPLUM.js → chunk-ZWREYEWA.js} +48 -3
  11. package/dist/chunk-ZWREYEWA.js.map +1 -0
  12. package/dist/editable-artifact-durable-export.d.ts +10 -0
  13. package/dist/editable-artifact-durable-export.js +18 -3
  14. package/dist/editable-artifact-durable-export.js.map +1 -1
  15. package/dist/editable-artifacts.js +3 -3
  16. package/dist/index.d.ts +69 -8
  17. package/dist/index.js +869 -75
  18. package/dist/index.js.map +1 -1
  19. package/dist/knowledge-source-sync-schema.d.ts +730 -0
  20. package/dist/knowledge-source-sync.d.ts +60 -0
  21. package/dist/persistence-errors.d.ts +8 -0
  22. package/dist/provision-roles.js +1 -1
  23. package/dist/runtime-posture.d.ts +3 -3
  24. package/dist/schema.d.ts +21 -4
  25. package/dist/schema.js +5 -1
  26. package/dist/session-queue-commands.d.ts +1 -1
  27. package/dist/session-realtime-ledger.d.ts +1 -0
  28. package/dist/session-tenancy.js +3 -3
  29. package/dist/video-generation.js +3 -3
  30. package/drizzle/0238_recover_unclaimed_session_turns.sql +307 -0
  31. package/drizzle/0240_model_context_user_messages.sql +204 -0
  32. package/drizzle/0243_google_drive_object_acl_authority.sql +554 -0
  33. package/package.json +5 -5
  34. package/src/editable-artifact-durable-export.ts +27 -0
  35. package/src/index.ts +680 -30
  36. package/src/knowledge-source-sync-schema.ts +83 -0
  37. package/src/knowledge-source-sync.ts +530 -0
  38. package/src/persistence-errors.ts +60 -1
  39. package/src/provision-roles.ts +36 -0
  40. package/src/runtime-posture.ts +49 -1
  41. package/src/schema.ts +30 -7
  42. package/src/session-queue-commands.ts +13 -14
  43. package/src/session-realtime-context.ts +16 -0
  44. package/src/session-realtime-ledger.ts +53 -1
  45. package/dist/chunk-5KTIEDX7.js.map +0 -1
  46. package/dist/chunk-JC5QPLUM.js.map +0 -1
  47. package/dist/chunk-VKLB2ZGI.js.map +0 -1
  48. /package/dist/{chunk-P3RE4D2B.js.map → chunk-4SR4HKKA.js.map} +0 -0
  49. /package/dist/{chunk-CQZ4QSP5.js.map → chunk-CUPM3FGB.js.map} +0 -0
  50. /package/dist/{chunk-TAUTWOS5.js.map → chunk-LTG3S6ZY.js.map} +0 -0
  51. /package/dist/{chunk-F32YT7MP.js.map → chunk-WD5ROSLL.js.map} +0 -0
@@ -5,6 +5,7 @@ import {
5
5
  integer,
6
6
  jsonb,
7
7
  pgTable,
8
+ primaryKey,
8
9
  text,
9
10
  timestamp,
10
11
  uniqueIndex,
@@ -173,6 +174,7 @@ export const knowledgeSourceSyncIndexObligations = pgTable(
173
174
  objectLifecycleGeneration: bigint("object_lifecycle_generation", { mode: "number" }).notNull(),
174
175
  objectVersionGeneration: bigint("object_version_generation", { mode: "number" }).notNull(),
175
176
  citationLocator: jsonb("citation_locator").$type<Record<string, unknown>>().notNull(),
177
+ googleDriveAclEvidenceId: uuid("google_drive_acl_evidence_id"),
176
178
  aclEligibility: text("acl_eligibility").notNull().default("pending"),
177
179
  status: text("status").notNull().default("pending"),
178
180
  failureCode: text("failure_code"),
@@ -190,3 +192,84 @@ export const knowledgeSourceSyncIndexObligations = pgTable(
190
192
  ),
191
193
  }),
192
194
  );
195
+
196
+ export const googleDriveObjectAclEvidence = pgTable(
197
+ "google_drive_object_acl_evidence",
198
+ {
199
+ id: uuid("id").primaryKey().defaultRandom(),
200
+ accountId: uuid("account_id").notNull(),
201
+ workspaceId: uuid("workspace_id").notNull(),
202
+ sourceId: uuid("source_id").notNull(),
203
+ knowledgeSourceObjectId: uuid("knowledge_source_object_id").notNull(),
204
+ knowledgeDocumentVersionId: uuid("knowledge_document_version_id").notNull(),
205
+ documentId: uuid("document_id").notNull(),
206
+ fileId: uuid("file_id").notNull(),
207
+ indexObligationId: uuid("index_obligation_id").notNull(),
208
+ connectionId: uuid("connection_id").notNull(),
209
+ connectionVersion: bigint("connection_version", { mode: "number" }).notNull(),
210
+ sourcePrincipalHash: text("source_principal_hash").notNull(),
211
+ sourceSyncGeneration: bigint("source_sync_generation", { mode: "number" }).notNull(),
212
+ sourceConfigGeneration: bigint("source_config_generation", { mode: "number" }).notNull(),
213
+ sourceLifecycleGeneration: bigint("source_lifecycle_generation", { mode: "number" }).notNull(),
214
+ objectLifecycleGeneration: bigint("object_lifecycle_generation", {
215
+ mode: "number",
216
+ }).notNull(),
217
+ objectVersionGeneration: bigint("object_version_generation", { mode: "number" }).notNull(),
218
+ providerRevision: text("provider_revision"),
219
+ driveId: text("drive_id"),
220
+ aclRevision: text("acl_revision").notNull(),
221
+ aclHash: text("acl_hash").notNull(),
222
+ eligibility: text("eligibility").notNull(),
223
+ observedAt: timestamp("observed_at", { withTimezone: true }).notNull(),
224
+ expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(),
225
+ citationLocator: jsonb("citation_locator").$type<Record<string, unknown>>().notNull(),
226
+ operationId: text("operation_id").notNull(),
227
+ inputHash: text("input_hash").notNull(),
228
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
229
+ },
230
+ (table) => ({
231
+ obligationOperation: uniqueIndex("google_drive_object_acl_evidence_obligation_uq").on(
232
+ table.indexObligationId,
233
+ table.operationId,
234
+ ),
235
+ version: index("google_drive_object_acl_evidence_version_idx").on(
236
+ table.knowledgeDocumentVersionId,
237
+ table.expiresAt,
238
+ table.createdAt,
239
+ ),
240
+ file: index("google_drive_object_acl_evidence_file_idx").on(
241
+ table.accountId,
242
+ table.fileId,
243
+ table.expiresAt,
244
+ ),
245
+ }),
246
+ );
247
+
248
+ export const googleDriveObjectAclPrincipals = pgTable(
249
+ "google_drive_object_acl_principals",
250
+ {
251
+ evidenceId: uuid("evidence_id").notNull(),
252
+ ordinal: integer("ordinal").notNull(),
253
+ accountId: uuid("account_id").notNull(),
254
+ workspaceId: uuid("workspace_id").notNull(),
255
+ principalType: text("principal_type").notNull(),
256
+ permissionIdHash: text("permission_id_hash"),
257
+ emailHash: text("email_hash"),
258
+ domainHash: text("domain_hash"),
259
+ role: text("role").notNull(),
260
+ inherited: boolean("inherited").notNull().default(false),
261
+ allowFileDiscovery: boolean("allow_file_discovery"),
262
+ expirationTime: timestamp("expiration_time", { withTimezone: true }),
263
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
264
+ },
265
+ (table) => ({
266
+ identity: primaryKey({ columns: [table.evidenceId, table.ordinal] }),
267
+ match: index("google_drive_object_acl_principals_match_idx").on(
268
+ table.evidenceId,
269
+ table.principalType,
270
+ table.permissionIdHash,
271
+ table.emailHash,
272
+ table.domainHash,
273
+ ),
274
+ }),
275
+ );
@@ -4,6 +4,7 @@ import {
4
4
  KnowledgeSourceSyncRunSummary,
5
5
  type ScheduledTask,
6
6
  } from "@opengeni/contracts";
7
+ import { createHash } from "node:crypto";
7
8
  import { and, eq, inArray, sql } from "drizzle-orm";
8
9
  import type { Database } from "./database";
9
10
  import { setSubjectRlsContext, withRlsContext } from "./database";
@@ -43,6 +44,40 @@ export type KnowledgeSourceSyncObservationFence = {
43
44
  observations: KnowledgeSourceSyncObservationFloor[];
44
45
  };
45
46
 
47
+ export type GoogleDriveObjectAclPrincipal = {
48
+ type: "user" | "group" | "domain" | "anyone";
49
+ permissionId?: string | null;
50
+ emailAddress?: string | null;
51
+ domain?: string | null;
52
+ role: "owner" | "organizer" | "fileOrganizer" | "writer" | "commenter" | "reader";
53
+ inherited?: boolean;
54
+ allowFileDiscovery?: boolean | null;
55
+ expirationTime?: string | null;
56
+ };
57
+
58
+ export type GoogleDriveObjectAclEvidenceInput = {
59
+ accountId: string;
60
+ workspaceId: string;
61
+ obligationId: string;
62
+ connectionId: string;
63
+ connectionVersion: number;
64
+ sourceGooglePermissionId: string;
65
+ sourceSyncGeneration: number;
66
+ sourceConfigGeneration: number;
67
+ sourceLifecycleGeneration: number;
68
+ objectLifecycleGeneration: number;
69
+ objectVersionGeneration: number;
70
+ providerRevision: string | null;
71
+ driveId: string | null;
72
+ aclRevision: string;
73
+ eligibility: "eligible" | "denied";
74
+ observedAt: string;
75
+ expiresAt: string;
76
+ citationLocator: Record<string, unknown>;
77
+ operationId: string;
78
+ principals: GoogleDriveObjectAclPrincipal[];
79
+ };
80
+
46
81
  export type KnowledgeSourceSyncObjectObservationResult = KnowledgeSourceSyncObservationFloor & {
47
82
  disposition: "accepted" | "replayed" | "stale" | "conflict";
48
83
  currentVersion: {
@@ -1614,6 +1649,501 @@ export async function recordKnowledgeSourceSyncAclEvidence(
1614
1649
  );
1615
1650
  }
1616
1651
 
1652
+ /**
1653
+ * Persist one append-only Google Drive object ACL observation and atomically
1654
+ * make it the current authorization pointer for the exact index obligation.
1655
+ * Raw provider principals never enter storage: only domain-separated hashes
1656
+ * are retained. Denial deletes chunks in the same transaction so stale vector
1657
+ * or keyword rows cannot survive permission loss.
1658
+ */
1659
+ export async function recordGoogleDriveObjectAclEvidence(
1660
+ db: Database,
1661
+ input: GoogleDriveObjectAclEvidenceInput,
1662
+ ): Promise<{ evidenceId: string; aclHash: string }> {
1663
+ const observedAt = exactDate(input.observedAt, "Google Drive ACL observedAt");
1664
+ const expiresAt = exactDate(input.expiresAt, "Google Drive ACL expiresAt");
1665
+ if (expiresAt.getTime() <= observedAt.getTime()) {
1666
+ throw new Error("Google Drive ACL evidence must expire after it was observed");
1667
+ }
1668
+ if (input.principals.length > 1_000) {
1669
+ throw new Error("Google Drive ACL evidence exceeds 1000 principals");
1670
+ }
1671
+ const principals = input.principals.map(normalizeGoogleDriveAclPrincipal);
1672
+ const canonicalAcl = principals
1673
+ .map((principal) => JSON.stringify(principal))
1674
+ .sort((left, right) => left.localeCompare(right));
1675
+ const aclHash = sha256Hex(JSON.stringify(canonicalAcl));
1676
+ const sourcePrincipalHash = principalHash("permission", input.sourceGooglePermissionId);
1677
+ const inputHash = sha256Hex(
1678
+ JSON.stringify({
1679
+ version: 1,
1680
+ obligationId: input.obligationId,
1681
+ connectionId: input.connectionId,
1682
+ connectionVersion: input.connectionVersion,
1683
+ sourcePrincipalHash,
1684
+ sourceSyncGeneration: input.sourceSyncGeneration,
1685
+ sourceConfigGeneration: input.sourceConfigGeneration,
1686
+ sourceLifecycleGeneration: input.sourceLifecycleGeneration,
1687
+ objectLifecycleGeneration: input.objectLifecycleGeneration,
1688
+ objectVersionGeneration: input.objectVersionGeneration,
1689
+ providerRevision: input.providerRevision,
1690
+ driveId: input.driveId,
1691
+ aclRevision: input.aclRevision,
1692
+ aclHash,
1693
+ eligibility: input.eligibility,
1694
+ observedAt: observedAt.toISOString(),
1695
+ expiresAt: expiresAt.toISOString(),
1696
+ citationLocator: input.citationLocator,
1697
+ }),
1698
+ );
1699
+
1700
+ return await withRlsContext(
1701
+ db,
1702
+ { accountId: input.accountId, workspaceId: input.workspaceId },
1703
+ async (scopedDb) =>
1704
+ await scopedDb.transaction(async (tx) => {
1705
+ const [observedObligation] = await tx
1706
+ .select()
1707
+ .from(schema.knowledgeSourceSyncIndexObligations)
1708
+ .where(eq(schema.knowledgeSourceSyncIndexObligations.id, input.obligationId))
1709
+ .limit(1);
1710
+ if (!observedObligation) {
1711
+ throw new Error("Google Drive ACL evidence obligation was not found");
1712
+ }
1713
+ await setSubjectRlsContext(tx, observedObligation.initiatingSubjectId);
1714
+ const [state] = await tx
1715
+ .select()
1716
+ .from(schema.knowledgeSourceSyncStates)
1717
+ .where(eq(schema.knowledgeSourceSyncStates.sourceId, observedObligation.sourceId))
1718
+ .for("update")
1719
+ .limit(1);
1720
+ if (!state) throw new Error("Google Drive sync authorization is no longer active");
1721
+ await tx.execute(sql`
1722
+ SELECT knowledge_source_sync_lock_authority(
1723
+ ${input.accountId}::uuid,
1724
+ ${observedObligation.sourceId}::uuid,
1725
+ ${observedObligation.knowledgeSourceObjectId}::uuid
1726
+ )
1727
+ `);
1728
+ const [[source], [object], [version], [obligation], [connection]] = await Promise.all([
1729
+ tx
1730
+ .select()
1731
+ .from(schema.knowledgeSources)
1732
+ .where(eq(schema.knowledgeSources.id, observedObligation.sourceId))
1733
+ .limit(1),
1734
+ tx
1735
+ .select()
1736
+ .from(schema.knowledgeSourceObjects)
1737
+ .where(eq(schema.knowledgeSourceObjects.id, observedObligation.knowledgeSourceObjectId))
1738
+ .limit(1),
1739
+ tx
1740
+ .select()
1741
+ .from(schema.knowledgeDocumentVersions)
1742
+ .where(
1743
+ eq(
1744
+ schema.knowledgeDocumentVersions.id,
1745
+ observedObligation.knowledgeDocumentVersionId,
1746
+ ),
1747
+ )
1748
+ .limit(1),
1749
+ tx
1750
+ .select()
1751
+ .from(schema.knowledgeSourceSyncIndexObligations)
1752
+ .where(eq(schema.knowledgeSourceSyncIndexObligations.id, input.obligationId))
1753
+ .for("update")
1754
+ .limit(1),
1755
+ tx
1756
+ .select()
1757
+ .from(schema.connections)
1758
+ .where(eq(schema.connections.id, input.connectionId))
1759
+ .limit(1),
1760
+ ]);
1761
+ const [provider] = source
1762
+ ? await tx
1763
+ .select()
1764
+ .from(schema.knowledgeProviders)
1765
+ .where(eq(schema.knowledgeProviders.id, source.providerId))
1766
+ .limit(1)
1767
+ : [];
1768
+ const [document] = obligation
1769
+ ? await tx
1770
+ .select()
1771
+ .from(schema.documents)
1772
+ .where(eq(schema.documents.id, obligation.documentId))
1773
+ .for("update")
1774
+ .limit(1)
1775
+ : [];
1776
+ const metadata = connection?.metadata ?? {};
1777
+ const connectionPermissionId = cleanBoundedString(metadata.googlePermissionId, 256);
1778
+ const connectionLifecycle =
1779
+ metadata.lifecycle && typeof metadata.lifecycle === "object"
1780
+ ? cleanBoundedString((metadata.lifecycle as Record<string, unknown>).state, 64)
1781
+ : null;
1782
+ const hasDriveReadScope =
1783
+ Array.isArray(connection?.grantedScopes) &&
1784
+ connection.grantedScopes.some(
1785
+ (scope) =>
1786
+ scope === "https://www.googleapis.com/auth/drive" ||
1787
+ scope === "https://www.googleapis.com/auth/drive.readonly",
1788
+ );
1789
+ if (
1790
+ !obligation ||
1791
+ !source ||
1792
+ !object ||
1793
+ !version ||
1794
+ !provider ||
1795
+ !document ||
1796
+ !connection ||
1797
+ provider.providerKey !== "google-drive" ||
1798
+ provider.lifecycleState !== "active" ||
1799
+ state.connectionId !== input.connectionId ||
1800
+ state.connectionProviderDomain !== "googleapis.com" ||
1801
+ state.connectionKind !== "oauth2" ||
1802
+ state.connectionOwnerSubjectId !== connection.subjectId ||
1803
+ state.reconnectRequired ||
1804
+ connection.accountId !== input.accountId ||
1805
+ connection.subjectId !== observedObligation.initiatingSubjectId ||
1806
+ connection.providerDomain !== "googleapis.com" ||
1807
+ connection.kind !== "oauth2" ||
1808
+ connection.status !== "active" ||
1809
+ connection.version !== input.connectionVersion ||
1810
+ connection.version < state.connectionVersion ||
1811
+ metadata.accessMode !== "readonly" ||
1812
+ (connectionLifecycle !== null && connectionLifecycle !== "active") ||
1813
+ !hasDriveReadScope ||
1814
+ !connectionPermissionId ||
1815
+ principalHash("permission", connectionPermissionId) !== sourcePrincipalHash ||
1816
+ input.sourceSyncGeneration !== obligation.sourceSyncGeneration ||
1817
+ state.sourceSyncGeneration < obligation.sourceSyncGeneration ||
1818
+ state.sourceConfigGeneration !== input.sourceConfigGeneration ||
1819
+ state.sourceConfigGeneration !== obligation.sourceConfigGeneration ||
1820
+ state.sourceLifecycleGeneration !== input.sourceLifecycleGeneration ||
1821
+ state.sourceLifecycleGeneration !== obligation.sourceLifecycleGeneration ||
1822
+ source.lifecycleState !== "active" ||
1823
+ source.syncGeneration < obligation.sourceSyncGeneration ||
1824
+ source.lifecycleGeneration !== obligation.sourceLifecycleGeneration ||
1825
+ source.currentAclGeneration !== version.aclGeneration ||
1826
+ object.sourceId !== obligation.sourceId ||
1827
+ object.externalObjectId !== obligation.externalObjectId ||
1828
+ object.lifecycleState !== "active" ||
1829
+ object.lifecycleGeneration !== obligation.objectLifecycleGeneration ||
1830
+ object.versionGeneration !== obligation.objectVersionGeneration ||
1831
+ object.currentVersionId !== obligation.knowledgeDocumentVersionId ||
1832
+ version.sourceId !== obligation.sourceId ||
1833
+ version.objectId !== obligation.knowledgeSourceObjectId ||
1834
+ version.versionGeneration !== obligation.objectVersionGeneration ||
1835
+ version.documentId !== obligation.documentId ||
1836
+ version.fileId !== document.fileId ||
1837
+ document.accountId !== input.accountId ||
1838
+ document.workspaceId !== input.workspaceId ||
1839
+ document.sourceExternalId !== obligation.externalObjectId ||
1840
+ !document.knowledgeSourceIdentity ||
1841
+ obligation.sourceLifecycleGeneration !== input.sourceLifecycleGeneration ||
1842
+ obligation.objectLifecycleGeneration !== input.objectLifecycleGeneration ||
1843
+ obligation.objectVersionGeneration !== input.objectVersionGeneration
1844
+ ) {
1845
+ throw new Error("Google Drive ACL evidence authority changed");
1846
+ }
1847
+ if (input.eligibility === "eligible" && obligation.status !== "indexed") {
1848
+ throw new Error("Google Drive ACL eligibility requires a completed index obligation");
1849
+ }
1850
+
1851
+ const evidenceValues = {
1852
+ accountId: input.accountId,
1853
+ workspaceId: input.workspaceId,
1854
+ sourceId: obligation.sourceId,
1855
+ knowledgeSourceObjectId: obligation.knowledgeSourceObjectId,
1856
+ knowledgeDocumentVersionId: obligation.knowledgeDocumentVersionId,
1857
+ documentId: obligation.documentId,
1858
+ fileId: document.fileId,
1859
+ indexObligationId: obligation.id,
1860
+ connectionId: input.connectionId,
1861
+ connectionVersion: input.connectionVersion,
1862
+ sourcePrincipalHash,
1863
+ sourceSyncGeneration: input.sourceSyncGeneration,
1864
+ sourceConfigGeneration: input.sourceConfigGeneration,
1865
+ sourceLifecycleGeneration: input.sourceLifecycleGeneration,
1866
+ objectLifecycleGeneration: input.objectLifecycleGeneration,
1867
+ objectVersionGeneration: input.objectVersionGeneration,
1868
+ providerRevision: input.providerRevision,
1869
+ driveId: cleanBoundedString(input.driveId, 1024),
1870
+ aclRevision: exactBoundedString(input.aclRevision, 1024, "Google Drive ACL revision"),
1871
+ aclHash,
1872
+ eligibility: input.eligibility,
1873
+ observedAt,
1874
+ expiresAt,
1875
+ citationLocator: input.citationLocator,
1876
+ operationId: exactBoundedString(input.operationId, 256, "Google Drive ACL operation id"),
1877
+ inputHash,
1878
+ };
1879
+ const [inserted] = await tx
1880
+ .insert(schema.googleDriveObjectAclEvidence)
1881
+ .values(evidenceValues)
1882
+ .onConflictDoNothing({
1883
+ target: [
1884
+ schema.googleDriveObjectAclEvidence.indexObligationId,
1885
+ schema.googleDriveObjectAclEvidence.operationId,
1886
+ ],
1887
+ })
1888
+ .returning();
1889
+ const [evidence] = inserted
1890
+ ? [inserted]
1891
+ : await tx
1892
+ .select()
1893
+ .from(schema.googleDriveObjectAclEvidence)
1894
+ .where(
1895
+ and(
1896
+ eq(schema.googleDriveObjectAclEvidence.indexObligationId, obligation.id),
1897
+ eq(schema.googleDriveObjectAclEvidence.operationId, evidenceValues.operationId),
1898
+ ),
1899
+ )
1900
+ .limit(1);
1901
+ if (!evidence || evidence.inputHash !== inputHash) {
1902
+ throw new Error("Google Drive ACL evidence operation conflicted");
1903
+ }
1904
+ if (inserted && principals.length > 0) {
1905
+ await tx.insert(schema.googleDriveObjectAclPrincipals).values(
1906
+ principals.map((principal, ordinal) => ({
1907
+ evidenceId: evidence.id,
1908
+ ordinal,
1909
+ accountId: input.accountId,
1910
+ workspaceId: input.workspaceId,
1911
+ ...principal,
1912
+ })),
1913
+ );
1914
+ }
1915
+ const now = new Date();
1916
+ await tx
1917
+ .update(schema.knowledgeSourceSyncIndexObligations)
1918
+ .set({
1919
+ googleDriveAclEvidenceId: evidence.id,
1920
+ aclEligibility: input.eligibility,
1921
+ updatedAt: now,
1922
+ })
1923
+ .where(eq(schema.knowledgeSourceSyncIndexObligations.id, obligation.id));
1924
+ await tx
1925
+ .update(schema.knowledgeSourceSyncItemOutcomes)
1926
+ .set({
1927
+ aclEligibility: input.eligibility,
1928
+ aclEvidence: {
1929
+ version: 1,
1930
+ provider: "google_drive",
1931
+ evidenceId: evidence.id,
1932
+ aclRevision: evidence.aclRevision,
1933
+ observedAt: evidence.observedAt.toISOString(),
1934
+ expiresAt: evidence.expiresAt.toISOString(),
1935
+ },
1936
+ })
1937
+ .where(eq(schema.knowledgeSourceSyncItemOutcomes.indexObligationId, obligation.id));
1938
+ const [updatedDocument] = await tx
1939
+ .update(schema.documents)
1940
+ .set({
1941
+ agentAccess: input.eligibility === "eligible",
1942
+ ...(input.eligibility === "denied" ? { chunkCount: 0 } : {}),
1943
+ updatedAt: now,
1944
+ })
1945
+ .where(eq(schema.documents.id, obligation.documentId))
1946
+ .returning({ id: schema.documents.id });
1947
+ if (!updatedDocument) {
1948
+ throw new Error("Google Drive ACL evidence document authority changed");
1949
+ }
1950
+ if (input.eligibility === "denied") {
1951
+ await tx
1952
+ .delete(schema.documentChunks)
1953
+ .where(eq(schema.documentChunks.documentId, obligation.documentId));
1954
+ }
1955
+ return { evidenceId: evidence.id, aclHash };
1956
+ }),
1957
+ );
1958
+ }
1959
+
1960
+ /**
1961
+ * Clear the current Drive authorization pointer before provider I/O without
1962
+ * destroying an otherwise valid index. All read surfaces immediately deny
1963
+ * because agent_access is false and the current evidence pointer is null. A
1964
+ * successful refresh can restore access without re-embedding; a final denied
1965
+ * observation uses recordGoogleDriveObjectAclEvidence and deletes chunks.
1966
+ */
1967
+ export async function beginGoogleDriveObjectAclRefresh(
1968
+ db: Database,
1969
+ input: {
1970
+ accountId: string;
1971
+ workspaceId: string;
1972
+ obligationId: string;
1973
+ sourceSyncGeneration: number;
1974
+ sourceConfigGeneration: number;
1975
+ sourceLifecycleGeneration: number;
1976
+ objectLifecycleGeneration: number;
1977
+ objectVersionGeneration: number;
1978
+ },
1979
+ ): Promise<void> {
1980
+ await withRlsContext(
1981
+ db,
1982
+ { accountId: input.accountId, workspaceId: input.workspaceId },
1983
+ async (scopedDb) =>
1984
+ await scopedDb.transaction(async (tx) => {
1985
+ const [observed] = await tx
1986
+ .select()
1987
+ .from(schema.knowledgeSourceSyncIndexObligations)
1988
+ .where(eq(schema.knowledgeSourceSyncIndexObligations.id, input.obligationId))
1989
+ .limit(1);
1990
+ if (!observed) throw new Error("Google Drive ACL refresh obligation was not found");
1991
+ await setSubjectRlsContext(tx, observed.initiatingSubjectId);
1992
+ const [state] = await tx
1993
+ .select()
1994
+ .from(schema.knowledgeSourceSyncStates)
1995
+ .where(eq(schema.knowledgeSourceSyncStates.sourceId, observed.sourceId))
1996
+ .for("update")
1997
+ .limit(1);
1998
+ if (!state) throw new Error("Google Drive sync authorization is no longer active");
1999
+ await tx.execute(sql`
2000
+ SELECT knowledge_source_sync_lock_authority(
2001
+ ${input.accountId}::uuid,
2002
+ ${observed.sourceId}::uuid,
2003
+ ${observed.knowledgeSourceObjectId}::uuid
2004
+ )
2005
+ `);
2006
+ const [[source], [object], [version], [obligation]] = await Promise.all([
2007
+ tx
2008
+ .select()
2009
+ .from(schema.knowledgeSources)
2010
+ .where(eq(schema.knowledgeSources.id, observed.sourceId))
2011
+ .limit(1),
2012
+ tx
2013
+ .select()
2014
+ .from(schema.knowledgeSourceObjects)
2015
+ .where(eq(schema.knowledgeSourceObjects.id, observed.knowledgeSourceObjectId))
2016
+ .limit(1),
2017
+ tx
2018
+ .select()
2019
+ .from(schema.knowledgeDocumentVersions)
2020
+ .where(eq(schema.knowledgeDocumentVersions.id, observed.knowledgeDocumentVersionId))
2021
+ .limit(1),
2022
+ tx
2023
+ .select()
2024
+ .from(schema.knowledgeSourceSyncIndexObligations)
2025
+ .where(eq(schema.knowledgeSourceSyncIndexObligations.id, input.obligationId))
2026
+ .for("update")
2027
+ .limit(1),
2028
+ ]);
2029
+ const [document] = obligation
2030
+ ? await tx
2031
+ .select()
2032
+ .from(schema.documents)
2033
+ .where(eq(schema.documents.id, obligation.documentId))
2034
+ .for("update")
2035
+ .limit(1)
2036
+ : [];
2037
+ if (
2038
+ !source ||
2039
+ !object ||
2040
+ !version ||
2041
+ !obligation ||
2042
+ !document ||
2043
+ state.reconnectRequired ||
2044
+ state.sourceSyncGeneration < input.sourceSyncGeneration ||
2045
+ state.sourceConfigGeneration !== input.sourceConfigGeneration ||
2046
+ state.sourceLifecycleGeneration !== input.sourceLifecycleGeneration ||
2047
+ source.lifecycleState !== "active" ||
2048
+ source.syncGeneration < obligation.sourceSyncGeneration ||
2049
+ source.lifecycleGeneration !== input.sourceLifecycleGeneration ||
2050
+ source.currentAclGeneration !== version.aclGeneration ||
2051
+ object.lifecycleState !== "active" ||
2052
+ object.lifecycleGeneration !== input.objectLifecycleGeneration ||
2053
+ object.versionGeneration !== input.objectVersionGeneration ||
2054
+ object.currentVersionId !== version.id ||
2055
+ version.documentId !== document.id ||
2056
+ version.fileId !== document.fileId ||
2057
+ obligation.sourceSyncGeneration !== input.sourceSyncGeneration ||
2058
+ obligation.sourceConfigGeneration !== input.sourceConfigGeneration ||
2059
+ obligation.sourceLifecycleGeneration !== input.sourceLifecycleGeneration ||
2060
+ obligation.objectLifecycleGeneration !== input.objectLifecycleGeneration ||
2061
+ obligation.objectVersionGeneration !== input.objectVersionGeneration ||
2062
+ obligation.status !== "indexed"
2063
+ ) {
2064
+ throw new Error("Google Drive ACL refresh authority changed");
2065
+ }
2066
+ const now = new Date();
2067
+ await tx
2068
+ .update(schema.knowledgeSourceSyncIndexObligations)
2069
+ .set({
2070
+ googleDriveAclEvidenceId: null,
2071
+ aclEligibility: "denied",
2072
+ updatedAt: now,
2073
+ })
2074
+ .where(eq(schema.knowledgeSourceSyncIndexObligations.id, obligation.id));
2075
+ await tx
2076
+ .update(schema.knowledgeSourceSyncItemOutcomes)
2077
+ .set({
2078
+ aclEligibility: "denied",
2079
+ aclEvidence: { version: 1, provider: "google_drive", state: "refresh_pending" },
2080
+ })
2081
+ .where(eq(schema.knowledgeSourceSyncItemOutcomes.indexObligationId, obligation.id));
2082
+ await tx
2083
+ .update(schema.documents)
2084
+ .set({ agentAccess: false, updatedAt: now })
2085
+ .where(eq(schema.documents.id, obligation.documentId));
2086
+ }),
2087
+ );
2088
+ }
2089
+
2090
+ function normalizeGoogleDriveAclPrincipal(principal: GoogleDriveObjectAclPrincipal) {
2091
+ const permissionId = cleanBoundedString(principal.permissionId, 1024);
2092
+ const emailAddress = cleanBoundedString(principal.emailAddress, 320)?.toLowerCase() ?? null;
2093
+ const domain = cleanBoundedString(principal.domain, 320)?.toLowerCase() ?? null;
2094
+ if (principal.type === "user" || principal.type === "group") {
2095
+ if (!permissionId && !emailAddress) {
2096
+ throw new Error(`Google Drive ${principal.type} principal is missing identity`);
2097
+ }
2098
+ } else if (principal.type === "domain") {
2099
+ if (!domain) throw new Error("Google Drive domain principal is missing its domain");
2100
+ } else if (principal.type !== "anyone") {
2101
+ throw new Error("Google Drive ACL principal type is invalid");
2102
+ }
2103
+ const expirationTime = principal.expirationTime
2104
+ ? exactDate(principal.expirationTime, "Google Drive ACL principal expiration")
2105
+ : null;
2106
+ return {
2107
+ principalType: principal.type,
2108
+ permissionIdHash: permissionId ? principalHash("permission", permissionId) : null,
2109
+ emailHash: emailAddress ? principalHash("email", emailAddress) : null,
2110
+ domainHash: domain ? principalHash("domain", domain) : null,
2111
+ role: principal.role,
2112
+ inherited: principal.inherited === true,
2113
+ allowFileDiscovery:
2114
+ principal.allowFileDiscovery === undefined ? null : principal.allowFileDiscovery,
2115
+ expirationTime,
2116
+ };
2117
+ }
2118
+
2119
+ function principalHash(kind: "permission" | "email" | "domain", value: string): string {
2120
+ return sha256Hex(`${kind}:${value.trim().toLowerCase()}`);
2121
+ }
2122
+
2123
+ function sha256Hex(value: string): string {
2124
+ return createHash("sha256").update(value).digest("hex");
2125
+ }
2126
+
2127
+ function exactDate(value: string, label: string): Date {
2128
+ const parsed = new Date(value);
2129
+ if (!value || !Number.isFinite(parsed.getTime()) || parsed.toISOString() !== value) {
2130
+ throw new Error(`${label} must be an exact UTC timestamp`);
2131
+ }
2132
+ return parsed;
2133
+ }
2134
+
2135
+ function exactBoundedString(value: unknown, max: number, label: string): string {
2136
+ const normalized = cleanBoundedString(value, max);
2137
+ if (!normalized || normalized !== value) throw new Error(`${label} is invalid`);
2138
+ return normalized;
2139
+ }
2140
+
2141
+ function cleanBoundedString(value: unknown, max: number): string | null {
2142
+ if (typeof value !== "string") return null;
2143
+ const normalized = value.trim();
2144
+ return normalized && normalized.length <= max ? normalized : null;
2145
+ }
2146
+
1617
2147
  /** Bounded retention hook. Callers choose policy and cadence; pending index or
1618
2148
  * ACL obligations are never deleted by age alone. */
1619
2149
  export async function pruneKnowledgeSourceSyncEvidence(