@opengeni/worker-bundle 0.20.0 → 0.20.3
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/activities/agent-turn.d.ts +25 -2
- package/dist/activities/google-drive-publication.d.ts +53 -0
- package/dist/activities/image-generation-references.d.ts +2 -0
- package/dist/activities/knowledge-source-sync-driver.d.ts +21 -0
- package/dist/activities/knowledge-source-sync.d.ts +29 -1
- package/dist/activities/run-input.d.ts +20 -2
- package/dist/activities/session-state.d.ts +5 -3
- package/dist/activities/types.d.ts +27 -0
- package/dist/{activities-control-XURE4VKN.js → activities-control-BJSDRSIP.js} +369 -20
- package/dist/activities-control-BJSDRSIP.js.map +1 -0
- package/dist/activities-control.d.ts +2 -2
- package/dist/{activities-turn-6EBXGGLS.js → activities-turn-J5YO7XXX.js} +2281 -1074
- package/dist/activities-turn-J5YO7XXX.js.map +1 -0
- package/dist/activities.d.ts +3 -3
- package/dist/{chunk-TYNPCG4U.js → chunk-N5TJIFCE.js} +69 -1
- package/dist/chunk-N5TJIFCE.js.map +1 -0
- package/dist/{chunk-5GRJIP5H.js → chunk-P6Z2SLAA.js} +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/observability-metrics.d.ts +37 -0
- package/dist/workflow-bundle.js +174 -4
- package/dist/workflows/session.d.ts +17 -1
- package/package.json +13 -13
- package/src/activities/agent-turn.ts +1801 -1175
- package/src/activities/documents.ts +6 -4
- package/src/activities/google-drive-publication.ts +840 -0
- package/src/activities/image-generation-references.ts +11 -2
- package/src/activities/knowledge-source-sync-driver.ts +23 -0
- package/src/activities/knowledge-source-sync.ts +384 -9
- package/src/activities/run-input.ts +175 -51
- package/src/activities/session-state.ts +69 -5
- package/src/activities/types.ts +27 -0
- package/src/activities.ts +2 -0
- package/src/observability-metrics.ts +184 -3
- package/src/workflows/session.ts +199 -8
- package/dist/activities-control-XURE4VKN.js.map +0 -1
- package/dist/activities-turn-6EBXGGLS.js.map +0 -1
- package/dist/chunk-TYNPCG4U.js.map +0 -1
- /package/dist/{chunk-5GRJIP5H.js.map → chunk-P6Z2SLAA.js.map} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ImageGenerationReference, type FileAsset } from "@opengeni/contracts";
|
|
2
|
-
import { getGeneratedImageArtifact,
|
|
2
|
+
import { getGeneratedImageArtifact, requireFileForSubject, type Database } from "@opengeni/db";
|
|
3
3
|
import type { ObjectStorage } from "@opengeni/storage";
|
|
4
4
|
import { validateGeneratedImage, type GeneratedImageMediaType } from "./generated-images";
|
|
5
5
|
|
|
@@ -23,7 +23,9 @@ export type SandboxImageReferenceReader = (path: string, maxBytes: number) => Pr
|
|
|
23
23
|
export async function resolveImageGenerationReferences(input: {
|
|
24
24
|
db: Database;
|
|
25
25
|
objectStorage: ObjectStorage;
|
|
26
|
+
accountId: string;
|
|
26
27
|
workspaceId: string;
|
|
28
|
+
subjectId: string | null;
|
|
27
29
|
references: readonly ImageGenerationReference[];
|
|
28
30
|
readSandboxFile?: SandboxImageReferenceReader;
|
|
29
31
|
}): Promise<ResolvedImageGenerationReference[]> {
|
|
@@ -57,7 +59,9 @@ async function referenceBytes(
|
|
|
57
59
|
input: {
|
|
58
60
|
db: Database;
|
|
59
61
|
objectStorage: ObjectStorage;
|
|
62
|
+
accountId: string;
|
|
60
63
|
workspaceId: string;
|
|
64
|
+
subjectId: string | null;
|
|
61
65
|
readSandboxFile?: SandboxImageReferenceReader;
|
|
62
66
|
},
|
|
63
67
|
reference: ImageGenerationReference,
|
|
@@ -73,7 +77,12 @@ async function referenceBytes(
|
|
|
73
77
|
|
|
74
78
|
const file =
|
|
75
79
|
reference.kind === "file"
|
|
76
|
-
? await
|
|
80
|
+
? await requireFileForSubject(input.db, {
|
|
81
|
+
accountId: input.accountId,
|
|
82
|
+
workspaceId: input.workspaceId,
|
|
83
|
+
subjectId: input.subjectId,
|
|
84
|
+
fileId: reference.fileId,
|
|
85
|
+
})
|
|
77
86
|
: await artifactFile(input.db, input.workspaceId, reference.artifactId);
|
|
78
87
|
if (file.status !== "ready" || !file.sha256) {
|
|
79
88
|
throw new Error("Image reference file is not durably ready");
|
|
@@ -11,6 +11,28 @@ export type KnowledgeSourceSyncDriverInventory<Entry, StopReason extends string>
|
|
|
11
11
|
hardLimitReached: boolean;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
+
export type KnowledgeSourceSyncAclPrincipal = {
|
|
15
|
+
type: "user" | "group" | "domain" | "anyone";
|
|
16
|
+
permissionId?: string | null;
|
|
17
|
+
emailAddress?: string | null;
|
|
18
|
+
domain?: string | null;
|
|
19
|
+
role: "owner" | "organizer" | "fileOrganizer" | "writer" | "commenter" | "reader";
|
|
20
|
+
inherited?: boolean;
|
|
21
|
+
allowFileDiscovery?: boolean | null;
|
|
22
|
+
expirationTime?: string | null;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type KnowledgeSourceSyncAclEvidence = {
|
|
26
|
+
eligibility: "eligible" | "denied";
|
|
27
|
+
providerRevision: string | null;
|
|
28
|
+
driveId: string | null;
|
|
29
|
+
aclRevision: string;
|
|
30
|
+
observedAt: string;
|
|
31
|
+
expiresAt: string;
|
|
32
|
+
providerRequests: number;
|
|
33
|
+
principals: KnowledgeSourceSyncAclPrincipal[];
|
|
34
|
+
};
|
|
35
|
+
|
|
14
36
|
/** Provider port for deterministic knowledge ingestion. Implementations own
|
|
15
37
|
* provider JSON and byte transfer; the shared activity owns authority,
|
|
16
38
|
* persistence, indexing obligations, telemetry, and checkpoint settlement. */
|
|
@@ -24,4 +46,5 @@ export type KnowledgeSourceSyncDriver<Entry, StopReason extends string> = {
|
|
|
24
46
|
) => Promise<KnowledgeSourceSyncDriverInventory<Entry, StopReason>>;
|
|
25
47
|
fetchContent: (entry: Entry, maxBytes: number) => Promise<Uint8Array>;
|
|
26
48
|
citationLocator: (entry: Entry) => Record<string, unknown>;
|
|
49
|
+
readAcl?: (entry: Entry, maxProviderRequests: number) => Promise<KnowledgeSourceSyncAclEvidence>;
|
|
27
50
|
};
|
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
appendKnowledgeDocumentVersion,
|
|
40
40
|
appendKnowledgeSourceAclVersion,
|
|
41
41
|
beginKnowledgeSyncRun,
|
|
42
|
+
beginGoogleDriveObjectAclRefresh,
|
|
42
43
|
buildConnectionTokenResolver,
|
|
43
44
|
checkpointKnowledgeSourceSync,
|
|
44
45
|
claimKnowledgeSourceSyncLease,
|
|
@@ -60,6 +61,7 @@ import {
|
|
|
60
61
|
KnowledgeSourceSyncObservationFenceError,
|
|
61
62
|
reconcileKnowledgeSourceSyncCompleteScan,
|
|
62
63
|
reconcileKnowledgeSourceSyncLiveGeneration,
|
|
64
|
+
recordGoogleDriveObjectAclEvidence,
|
|
63
65
|
recordKnowledgeSourceSyncItemOutcomes,
|
|
64
66
|
recordKnowledgeSourceSyncObjectObservations,
|
|
65
67
|
recordUsageEvent,
|
|
@@ -83,7 +85,10 @@ import type {
|
|
|
83
85
|
RunKnowledgeSourceSyncBatchInput,
|
|
84
86
|
RunKnowledgeSourceSyncBatchResult,
|
|
85
87
|
} from "./types";
|
|
86
|
-
import type {
|
|
88
|
+
import type {
|
|
89
|
+
KnowledgeSourceSyncAclEvidence,
|
|
90
|
+
KnowledgeSourceSyncDriver,
|
|
91
|
+
} from "./knowledge-source-sync-driver";
|
|
87
92
|
import {
|
|
88
93
|
advanceGoogleDriveChangesCursor,
|
|
89
94
|
buildGoogleDriveChangesCursor,
|
|
@@ -104,12 +109,31 @@ const DRIVE_JSON_MAX_BYTES = 2 * 1024 * 1024;
|
|
|
104
109
|
const GOOGLE_DRIVE_FULL_RECONCILIATION_INTERVAL_MS = 24 * 60 * 60 * 1_000;
|
|
105
110
|
const GOOGLE_DRIVE_SYNC_INVOCATION_SLICE_MS = 30_000;
|
|
106
111
|
const GOOGLE_DRIVE_EXECUTION_CHECKPOINT_MAX_BYTES = 2 * 1024 * 1024;
|
|
112
|
+
const GOOGLE_DRIVE_ACL_FRESHNESS_MS = 26 * 60 * 60 * 1_000;
|
|
113
|
+
const GOOGLE_DRIVE_ACL_MAX_PRINCIPALS = 1_000;
|
|
107
114
|
const ATLASSIAN_API_BASE = "https://api.atlassian.com";
|
|
108
115
|
const ATLASSIAN_JSON_MAX_BYTES = 4 * 1024 * 1024;
|
|
109
116
|
|
|
110
117
|
type KnowledgeSyncEntry = GoogleDriveInventoryEntry | AtlassianInventoryEntry;
|
|
111
118
|
type KnowledgeSyncStopReason = GoogleDriveInventoryStopReason | AtlassianInventoryStopReason;
|
|
112
119
|
|
|
120
|
+
type GoogleDrivePermission = {
|
|
121
|
+
id: string;
|
|
122
|
+
type: "user" | "group" | "domain" | "anyone";
|
|
123
|
+
role: "owner" | "organizer" | "fileOrganizer" | "writer" | "commenter" | "reader";
|
|
124
|
+
emailAddress: string | null;
|
|
125
|
+
domain: string | null;
|
|
126
|
+
allowFileDiscovery: boolean | null;
|
|
127
|
+
expirationTime: string | null;
|
|
128
|
+
inherited: boolean;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
type GoogleDrivePermissionsPage = {
|
|
132
|
+
permissions: GoogleDrivePermission[];
|
|
133
|
+
nextPageToken: string | null;
|
|
134
|
+
denied: boolean;
|
|
135
|
+
};
|
|
136
|
+
|
|
113
137
|
export function knowledgeSyncObservationPolicy(kind: "google_drive" | "atlassian") {
|
|
114
138
|
return kind === "google_drive"
|
|
115
139
|
? { revisionOrdering: "canonical_decimal" as const, filterWithDriveDurability: true }
|
|
@@ -437,7 +461,26 @@ export function createKnowledgeSourceSyncActivities(
|
|
|
437
461
|
if (obligation.documentId !== details.version.documentId) {
|
|
438
462
|
throw new SyncFailure("authority_changed", false);
|
|
439
463
|
}
|
|
440
|
-
if (obligation.status === "indexed") return obligation;
|
|
464
|
+
if (obligation.status === "indexed" && !details.indexRequired) return obligation;
|
|
465
|
+
if (obligation.status === "indexed" && details.indexRequired) {
|
|
466
|
+
summary.phase = "index";
|
|
467
|
+
try {
|
|
468
|
+
const indexedDocument = await documentActivities.indexDocument({
|
|
469
|
+
accountId: input.accountId,
|
|
470
|
+
workspaceId: input.workspaceId,
|
|
471
|
+
documentId: details.version.documentId,
|
|
472
|
+
authorityKind: action.destination.kind,
|
|
473
|
+
authorityWorkspaceId: action.destination.workspaceId,
|
|
474
|
+
authoritySubjectId: action.destination.subjectId,
|
|
475
|
+
});
|
|
476
|
+
if (indexedDocument.status !== "ready") {
|
|
477
|
+
throw new Error("document indexing did not complete");
|
|
478
|
+
}
|
|
479
|
+
} catch {
|
|
480
|
+
throw new SyncFailure("indexing_failed", true);
|
|
481
|
+
}
|
|
482
|
+
return obligation;
|
|
483
|
+
}
|
|
441
484
|
if (obligation.status === "failed") {
|
|
442
485
|
const retried = await retryKnowledgeSourceSyncIndexObligation(db, {
|
|
443
486
|
accountId: input.accountId,
|
|
@@ -491,6 +534,74 @@ export function createKnowledgeSourceSyncActivities(
|
|
|
491
534
|
return { ...obligation, status: "indexed" };
|
|
492
535
|
};
|
|
493
536
|
|
|
537
|
+
const recordEntryAcl = async (details: {
|
|
538
|
+
entry: KnowledgeSyncEntry;
|
|
539
|
+
object: { id: string; lifecycleGeneration: number };
|
|
540
|
+
version: { id: string; versionGeneration: number };
|
|
541
|
+
obligation: { id: string; sourceSyncGeneration: number };
|
|
542
|
+
}): Promise<{
|
|
543
|
+
aclEligibility: "pending" | "eligible" | "denied";
|
|
544
|
+
aclEvidence: Record<string, unknown> | null;
|
|
545
|
+
}> => {
|
|
546
|
+
if (!driver.readAcl || !provider.aclAuthority) {
|
|
547
|
+
return { aclEligibility: "pending", aclEvidence: null };
|
|
548
|
+
}
|
|
549
|
+
await beginGoogleDriveObjectAclRefresh(db, {
|
|
550
|
+
accountId: input.accountId,
|
|
551
|
+
workspaceId: input.workspaceId,
|
|
552
|
+
obligationId: details.obligation.id,
|
|
553
|
+
sourceSyncGeneration: details.obligation.sourceSyncGeneration,
|
|
554
|
+
sourceConfigGeneration: action.sourceConfigGeneration,
|
|
555
|
+
sourceLifecycleGeneration: action.sourceLifecycleGeneration,
|
|
556
|
+
objectLifecycleGeneration: details.object.lifecycleGeneration,
|
|
557
|
+
objectVersionGeneration: details.version.versionGeneration,
|
|
558
|
+
});
|
|
559
|
+
const remainingProviderRequests =
|
|
560
|
+
action.limits.maxProviderRequests - summary.providerRequests;
|
|
561
|
+
if (remainingProviderRequests < 1) {
|
|
562
|
+
throw new SyncFailure("resource_limit", false);
|
|
563
|
+
}
|
|
564
|
+
const evidence = await driver.readAcl(details.entry, remainingProviderRequests);
|
|
565
|
+
summary.providerRequests += evidence.providerRequests;
|
|
566
|
+
if (summary.providerRequests > action.limits.maxProviderRequests) {
|
|
567
|
+
throw new SyncFailure("resource_limit", false);
|
|
568
|
+
}
|
|
569
|
+
const recorded = await recordGoogleDriveObjectAclEvidence(db, {
|
|
570
|
+
accountId: input.accountId,
|
|
571
|
+
workspaceId: input.workspaceId,
|
|
572
|
+
obligationId: details.obligation.id,
|
|
573
|
+
connectionId: provider.aclAuthority.connectionId,
|
|
574
|
+
connectionVersion: provider.aclAuthority.connectionVersion,
|
|
575
|
+
sourceGooglePermissionId: provider.aclAuthority.sourceGooglePermissionId,
|
|
576
|
+
sourceSyncGeneration: details.obligation.sourceSyncGeneration,
|
|
577
|
+
sourceConfigGeneration: action.sourceConfigGeneration,
|
|
578
|
+
sourceLifecycleGeneration: action.sourceLifecycleGeneration,
|
|
579
|
+
objectLifecycleGeneration: details.object.lifecycleGeneration,
|
|
580
|
+
objectVersionGeneration: details.version.versionGeneration,
|
|
581
|
+
providerRevision: evidence.providerRevision,
|
|
582
|
+
driveId: evidence.driveId,
|
|
583
|
+
aclRevision: evidence.aclRevision,
|
|
584
|
+
eligibility: evidence.eligibility,
|
|
585
|
+
observedAt: evidence.observedAt,
|
|
586
|
+
expiresAt: evidence.expiresAt,
|
|
587
|
+
citationLocator: driver.citationLocator(details.entry),
|
|
588
|
+
operationId: `drive-acl:${details.obligation.id}:${evidence.aclRevision}`,
|
|
589
|
+
principals: evidence.principals,
|
|
590
|
+
});
|
|
591
|
+
return {
|
|
592
|
+
aclEligibility: evidence.eligibility,
|
|
593
|
+
aclEvidence: {
|
|
594
|
+
version: 1,
|
|
595
|
+
provider: "google_drive",
|
|
596
|
+
evidenceId: recorded.evidenceId,
|
|
597
|
+
aclRevision: evidence.aclRevision,
|
|
598
|
+
aclHash: recorded.aclHash,
|
|
599
|
+
observedAt: evidence.observedAt,
|
|
600
|
+
expiresAt: evidence.expiresAt,
|
|
601
|
+
},
|
|
602
|
+
};
|
|
603
|
+
};
|
|
604
|
+
|
|
494
605
|
const outcomes = [];
|
|
495
606
|
for (const entry of entriesToProcess) {
|
|
496
607
|
heartbeat({ sourceId: input.sourceId, externalObjectId: entry.externalObjectId });
|
|
@@ -609,9 +720,15 @@ export function createKnowledgeSourceSyncActivities(
|
|
|
609
720
|
entry,
|
|
610
721
|
object,
|
|
611
722
|
version: currentVersion,
|
|
612
|
-
indexRequired:
|
|
723
|
+
indexRequired: (currentObservation?.documentChunkCount ?? 0) === 0,
|
|
613
724
|
obligation: currentObligation,
|
|
614
725
|
});
|
|
726
|
+
const objectAcl = await recordEntryAcl({
|
|
727
|
+
entry,
|
|
728
|
+
object,
|
|
729
|
+
version: currentVersion,
|
|
730
|
+
obligation,
|
|
731
|
+
});
|
|
615
732
|
summary.unchanged += 1;
|
|
616
733
|
outcomes.push({
|
|
617
734
|
externalObjectId: entry.externalObjectId,
|
|
@@ -619,7 +736,8 @@ export function createKnowledgeSourceSyncActivities(
|
|
|
619
736
|
contentSha256: currentVersion.contentSha256,
|
|
620
737
|
providerRevision: entry.externalVersionId,
|
|
621
738
|
metadataHash,
|
|
622
|
-
aclEligibility:
|
|
739
|
+
aclEligibility: objectAcl.aclEligibility,
|
|
740
|
+
aclEvidence: objectAcl.aclEvidence,
|
|
623
741
|
indexObligationId: obligation.id,
|
|
624
742
|
});
|
|
625
743
|
continue;
|
|
@@ -706,9 +824,10 @@ export function createKnowledgeSourceSyncActivities(
|
|
|
706
824
|
version,
|
|
707
825
|
indexRequired: (currentObservation?.documentChunkCount ?? 0) === 0,
|
|
708
826
|
});
|
|
827
|
+
const objectAcl = await recordEntryAcl({ entry, object, version, obligation });
|
|
709
828
|
summary.unchanged += 1;
|
|
710
829
|
summary.indexed += 1;
|
|
711
|
-
summary.aclPending += 1;
|
|
830
|
+
if (objectAcl.aclEligibility === "pending") summary.aclPending += 1;
|
|
712
831
|
outcomes.push({
|
|
713
832
|
externalObjectId: entry.externalObjectId,
|
|
714
833
|
outcome: "unchanged" as const,
|
|
@@ -716,7 +835,8 @@ export function createKnowledgeSourceSyncActivities(
|
|
|
716
835
|
sizeBytes: bytes.byteLength,
|
|
717
836
|
providerRevision: entry.externalVersionId,
|
|
718
837
|
metadataHash,
|
|
719
|
-
aclEligibility:
|
|
838
|
+
aclEligibility: objectAcl.aclEligibility,
|
|
839
|
+
aclEvidence: objectAcl.aclEvidence,
|
|
720
840
|
indexObligationId: obligation.id,
|
|
721
841
|
});
|
|
722
842
|
continue;
|
|
@@ -806,9 +926,10 @@ export function createKnowledgeSourceSyncActivities(
|
|
|
806
926
|
version,
|
|
807
927
|
indexRequired: true,
|
|
808
928
|
});
|
|
929
|
+
const objectAcl = await recordEntryAcl({ entry, object, version, obligation });
|
|
809
930
|
summary.indexed += 1;
|
|
810
931
|
summary.imported += 1;
|
|
811
|
-
summary.aclPending += 1;
|
|
932
|
+
if (objectAcl.aclEligibility === "pending") summary.aclPending += 1;
|
|
812
933
|
summary.bytes += bytes.byteLength;
|
|
813
934
|
outcomes.push({
|
|
814
935
|
externalObjectId: entry.externalObjectId,
|
|
@@ -817,7 +938,8 @@ export function createKnowledgeSourceSyncActivities(
|
|
|
817
938
|
sizeBytes: bytes.byteLength,
|
|
818
939
|
providerRevision: entry.externalVersionId,
|
|
819
940
|
metadataHash,
|
|
820
|
-
aclEligibility:
|
|
941
|
+
aclEligibility: objectAcl.aclEligibility,
|
|
942
|
+
aclEvidence: objectAcl.aclEvidence,
|
|
821
943
|
indexObligationId: obligation.id,
|
|
822
944
|
});
|
|
823
945
|
} catch (error) {
|
|
@@ -1201,6 +1323,11 @@ export type GoogleDriveSyncProviderPort = {
|
|
|
1201
1323
|
pageToken: string | null;
|
|
1202
1324
|
pageSize: number;
|
|
1203
1325
|
}) => Promise<GoogleDriveInventoryPage>;
|
|
1326
|
+
listPermissions?: (input: {
|
|
1327
|
+
fileId: string;
|
|
1328
|
+
pageToken: string | null;
|
|
1329
|
+
pageSize: number;
|
|
1330
|
+
}) => Promise<GoogleDrivePermissionsPage>;
|
|
1204
1331
|
};
|
|
1205
1332
|
|
|
1206
1333
|
async function resolveKnowledgeSyncProvider(input: {
|
|
@@ -1217,6 +1344,11 @@ async function resolveKnowledgeSyncProvider(input: {
|
|
|
1217
1344
|
GoogleDriveSelectedSource["destination"] | AtlassianSelectedSource["destination"]
|
|
1218
1345
|
>;
|
|
1219
1346
|
driver: KnowledgeSourceSyncDriver<KnowledgeSyncEntry, KnowledgeSyncStopReason>;
|
|
1347
|
+
aclAuthority: {
|
|
1348
|
+
connectionId: string;
|
|
1349
|
+
connectionVersion: number;
|
|
1350
|
+
sourceGooglePermissionId: string;
|
|
1351
|
+
} | null;
|
|
1220
1352
|
}> {
|
|
1221
1353
|
const { action } = input;
|
|
1222
1354
|
const connection = await getConnectionMetadata(
|
|
@@ -1341,6 +1473,11 @@ async function resolveKnowledgeSyncProvider(input: {
|
|
|
1341
1473
|
return {
|
|
1342
1474
|
kind: "google_drive",
|
|
1343
1475
|
selectedDestination: source.destination!,
|
|
1476
|
+
aclAuthority: {
|
|
1477
|
+
connectionId: resolvedConnection.id,
|
|
1478
|
+
connectionVersion: resolvedConnection.version,
|
|
1479
|
+
sourceGooglePermissionId: metadata.googlePermissionId,
|
|
1480
|
+
},
|
|
1344
1481
|
driver: googleDriveSyncDriver({
|
|
1345
1482
|
actionProviderCoordinationKey: action.providerCoordinationKey,
|
|
1346
1483
|
metadata,
|
|
@@ -1377,6 +1514,7 @@ async function resolveKnowledgeSyncProvider(input: {
|
|
|
1377
1514
|
return {
|
|
1378
1515
|
kind: "atlassian",
|
|
1379
1516
|
selectedDestination: source.destination!,
|
|
1517
|
+
aclAuthority: null,
|
|
1380
1518
|
driver: atlassianSyncDriver({
|
|
1381
1519
|
actionProviderCoordinationKey: action.providerCoordinationKey,
|
|
1382
1520
|
metadata,
|
|
@@ -1495,6 +1633,7 @@ export function googleDriveSyncDriver(input: {
|
|
|
1495
1633
|
await listDriveChanges(pageToken, driveId, pageSize, input.authorization),
|
|
1496
1634
|
getFile: async (fileId) => await getDriveFile(fileId, input.authorization),
|
|
1497
1635
|
listChildren: async (request) => await listDriveChildren(request, input.authorization),
|
|
1636
|
+
listPermissions: async (request) => await listDrivePermissions(request, input.authorization),
|
|
1498
1637
|
};
|
|
1499
1638
|
const clock = provider.now ?? Date.now;
|
|
1500
1639
|
const budgetLimits = {
|
|
@@ -1848,11 +1987,25 @@ export function googleDriveSyncDriver(input: {
|
|
|
1848
1987
|
},
|
|
1849
1988
|
fetchContent: async (entry, maxBytes) =>
|
|
1850
1989
|
await fetchDriveBytes(entry, input.authorization, maxBytes),
|
|
1990
|
+
readAcl: async (entry, maxProviderRequests) => {
|
|
1991
|
+
if (!provider.listPermissions) {
|
|
1992
|
+
throw new SyncFailure("provider_rejected", false);
|
|
1993
|
+
}
|
|
1994
|
+
return await collectGoogleDriveAclEvidence({
|
|
1995
|
+
entry,
|
|
1996
|
+
maxProviderRequests,
|
|
1997
|
+
listPermissions: provider.listPermissions,
|
|
1998
|
+
getFile: provider.getFile,
|
|
1999
|
+
now: clock,
|
|
2000
|
+
});
|
|
2001
|
+
},
|
|
1851
2002
|
citationLocator: (entry) => ({
|
|
1852
|
-
version:
|
|
2003
|
+
version: 2,
|
|
1853
2004
|
providerKey: "google-drive",
|
|
1854
2005
|
providerCoordinationKey: input.actionProviderCoordinationKey,
|
|
1855
2006
|
externalObjectId: entry.externalObjectId,
|
|
2007
|
+
providerRevision: entry.externalVersionId,
|
|
2008
|
+
driveId: entry.driveId,
|
|
1856
2009
|
sourceUri: entry.sourceUri,
|
|
1857
2010
|
}),
|
|
1858
2011
|
};
|
|
@@ -2268,6 +2421,228 @@ async function getDriveFile(
|
|
|
2268
2421
|
);
|
|
2269
2422
|
}
|
|
2270
2423
|
|
|
2424
|
+
async function listDrivePermissions(
|
|
2425
|
+
input: { fileId: string; pageToken: string | null; pageSize: number },
|
|
2426
|
+
authorization: string | undefined,
|
|
2427
|
+
): Promise<GoogleDrivePermissionsPage> {
|
|
2428
|
+
if (!authorization) throw new SyncFailure("connection_reconnect_required", true, true);
|
|
2429
|
+
const url = new URL(`${DRIVE_API_BASE}/files/${encodeURIComponent(input.fileId)}/permissions`);
|
|
2430
|
+
url.searchParams.set("supportsAllDrives", "true");
|
|
2431
|
+
url.searchParams.set("pageSize", String(input.pageSize));
|
|
2432
|
+
url.searchParams.set(
|
|
2433
|
+
"fields",
|
|
2434
|
+
"nextPageToken,permissions(id,type,emailAddress,domain,role,allowFileDiscovery,expirationTime,deleted,permissionDetails(inherited))",
|
|
2435
|
+
);
|
|
2436
|
+
if (input.pageToken) url.searchParams.set("pageToken", input.pageToken);
|
|
2437
|
+
const response = await fetch(url, {
|
|
2438
|
+
headers: { authorization, accept: "application/json" },
|
|
2439
|
+
redirect: "error",
|
|
2440
|
+
signal: AbortSignal.timeout(15_000),
|
|
2441
|
+
});
|
|
2442
|
+
if (response.status === 403 || response.status === 404) {
|
|
2443
|
+
return { permissions: [], nextPageToken: null, denied: true };
|
|
2444
|
+
}
|
|
2445
|
+
if (!response.ok) throwGoogleDriveResponse(response);
|
|
2446
|
+
const payload = (await readResponseJsonBounded(
|
|
2447
|
+
response,
|
|
2448
|
+
DRIVE_JSON_MAX_BYTES,
|
|
2449
|
+
"Google Drive permission list",
|
|
2450
|
+
)) as Record<string, unknown>;
|
|
2451
|
+
const rawPermissions = Array.isArray(payload.permissions) ? payload.permissions : null;
|
|
2452
|
+
if (!rawPermissions) throw new SyncFailure("provider_payload_invalid", false);
|
|
2453
|
+
return {
|
|
2454
|
+
permissions: rawPermissions.map(parseDrivePermission),
|
|
2455
|
+
nextPageToken: optionalString(payload.nextPageToken),
|
|
2456
|
+
denied: false,
|
|
2457
|
+
};
|
|
2458
|
+
}
|
|
2459
|
+
|
|
2460
|
+
export async function collectGoogleDriveAclEvidence(input: {
|
|
2461
|
+
entry: Pick<GoogleDriveInventoryEntry, "externalObjectId" | "externalVersionId" | "driveId">;
|
|
2462
|
+
maxProviderRequests: number;
|
|
2463
|
+
listPermissions: NonNullable<GoogleDriveSyncProviderPort["listPermissions"]>;
|
|
2464
|
+
getFile?: GoogleDriveSyncProviderPort["getFile"];
|
|
2465
|
+
now?: () => number;
|
|
2466
|
+
}): Promise<KnowledgeSourceSyncAclEvidence> {
|
|
2467
|
+
if (!Number.isSafeInteger(input.maxProviderRequests) || input.maxProviderRequests < 1) {
|
|
2468
|
+
throw new SyncFailure("resource_limit", false);
|
|
2469
|
+
}
|
|
2470
|
+
const now = input.now ?? Date.now;
|
|
2471
|
+
const observedAt = new Date(now());
|
|
2472
|
+
if (!Number.isFinite(observedAt.getTime())) {
|
|
2473
|
+
throw new SyncFailure("provider_payload_invalid", false);
|
|
2474
|
+
}
|
|
2475
|
+
const permissions: GoogleDrivePermission[] = [];
|
|
2476
|
+
const seenTokens = new Set<string>();
|
|
2477
|
+
let pageToken: string | null = null;
|
|
2478
|
+
let providerRequests = 0;
|
|
2479
|
+
let permissionListDenied = false;
|
|
2480
|
+
do {
|
|
2481
|
+
if (providerRequests >= input.maxProviderRequests) {
|
|
2482
|
+
throw new SyncFailure("resource_limit", false);
|
|
2483
|
+
}
|
|
2484
|
+
providerRequests += 1;
|
|
2485
|
+
const page = await input.listPermissions({
|
|
2486
|
+
fileId: input.entry.externalObjectId,
|
|
2487
|
+
pageToken,
|
|
2488
|
+
pageSize: Math.min(100, GOOGLE_DRIVE_ACL_MAX_PRINCIPALS - permissions.length),
|
|
2489
|
+
});
|
|
2490
|
+
if (page.denied) {
|
|
2491
|
+
permissionListDenied = true;
|
|
2492
|
+
break;
|
|
2493
|
+
}
|
|
2494
|
+
permissions.push(...page.permissions);
|
|
2495
|
+
if (permissions.length > GOOGLE_DRIVE_ACL_MAX_PRINCIPALS) {
|
|
2496
|
+
throw new SyncFailure("resource_limit", false);
|
|
2497
|
+
}
|
|
2498
|
+
pageToken = page.nextPageToken;
|
|
2499
|
+
if (pageToken) {
|
|
2500
|
+
if (pageToken.length > 4096 || seenTokens.has(pageToken)) {
|
|
2501
|
+
throw new SyncFailure("provider_payload_invalid", false);
|
|
2502
|
+
}
|
|
2503
|
+
seenTokens.add(pageToken);
|
|
2504
|
+
}
|
|
2505
|
+
} while (pageToken);
|
|
2506
|
+
|
|
2507
|
+
if (permissionListDenied) {
|
|
2508
|
+
if (!input.getFile || providerRequests >= input.maxProviderRequests) {
|
|
2509
|
+
return googleDriveDeniedAclEvidence(input.entry, observedAt, providerRequests);
|
|
2510
|
+
}
|
|
2511
|
+
providerRequests += 1;
|
|
2512
|
+
const current = await input.getFile(input.entry.externalObjectId);
|
|
2513
|
+
if (!current || current.trashed) {
|
|
2514
|
+
return googleDriveDeniedAclEvidence(input.entry, observedAt, providerRequests);
|
|
2515
|
+
}
|
|
2516
|
+
const aclRevision = createHash("sha256")
|
|
2517
|
+
.update(
|
|
2518
|
+
JSON.stringify({
|
|
2519
|
+
version: 1,
|
|
2520
|
+
mode: "source_owner_only",
|
|
2521
|
+
externalObjectId: input.entry.externalObjectId,
|
|
2522
|
+
providerRevision: input.entry.externalVersionId,
|
|
2523
|
+
}),
|
|
2524
|
+
)
|
|
2525
|
+
.digest("hex");
|
|
2526
|
+
return {
|
|
2527
|
+
eligibility: "eligible",
|
|
2528
|
+
providerRevision: input.entry.externalVersionId,
|
|
2529
|
+
driveId: input.entry.driveId,
|
|
2530
|
+
aclRevision,
|
|
2531
|
+
observedAt: observedAt.toISOString(),
|
|
2532
|
+
expiresAt: new Date(observedAt.getTime() + GOOGLE_DRIVE_ACL_FRESHNESS_MS).toISOString(),
|
|
2533
|
+
providerRequests,
|
|
2534
|
+
principals: [],
|
|
2535
|
+
};
|
|
2536
|
+
}
|
|
2537
|
+
|
|
2538
|
+
const canonicalPermissions = permissions
|
|
2539
|
+
.map((permission) => ({
|
|
2540
|
+
id: permission.id,
|
|
2541
|
+
type: permission.type,
|
|
2542
|
+
role: permission.role,
|
|
2543
|
+
emailAddress: permission.emailAddress,
|
|
2544
|
+
domain: permission.domain,
|
|
2545
|
+
allowFileDiscovery: permission.allowFileDiscovery,
|
|
2546
|
+
expirationTime: permission.expirationTime,
|
|
2547
|
+
inherited: permission.inherited,
|
|
2548
|
+
}))
|
|
2549
|
+
.sort((left, right) => JSON.stringify(left).localeCompare(JSON.stringify(right)));
|
|
2550
|
+
const aclRevision = createHash("sha256")
|
|
2551
|
+
.update(
|
|
2552
|
+
JSON.stringify({
|
|
2553
|
+
version: 1,
|
|
2554
|
+
externalObjectId: input.entry.externalObjectId,
|
|
2555
|
+
providerRevision: input.entry.externalVersionId,
|
|
2556
|
+
permissions: canonicalPermissions,
|
|
2557
|
+
}),
|
|
2558
|
+
)
|
|
2559
|
+
.digest("hex");
|
|
2560
|
+
return {
|
|
2561
|
+
eligibility: "eligible",
|
|
2562
|
+
providerRevision: input.entry.externalVersionId,
|
|
2563
|
+
driveId: input.entry.driveId,
|
|
2564
|
+
aclRevision,
|
|
2565
|
+
observedAt: observedAt.toISOString(),
|
|
2566
|
+
expiresAt: new Date(observedAt.getTime() + GOOGLE_DRIVE_ACL_FRESHNESS_MS).toISOString(),
|
|
2567
|
+
providerRequests,
|
|
2568
|
+
principals: canonicalPermissions.map((permission) => ({
|
|
2569
|
+
type: permission.type,
|
|
2570
|
+
permissionId: permission.id,
|
|
2571
|
+
emailAddress: permission.emailAddress,
|
|
2572
|
+
domain: permission.domain,
|
|
2573
|
+
role: permission.role,
|
|
2574
|
+
inherited: permission.inherited,
|
|
2575
|
+
allowFileDiscovery: permission.allowFileDiscovery,
|
|
2576
|
+
expirationTime: permission.expirationTime,
|
|
2577
|
+
})),
|
|
2578
|
+
};
|
|
2579
|
+
}
|
|
2580
|
+
|
|
2581
|
+
function googleDriveDeniedAclEvidence(
|
|
2582
|
+
entry: Pick<GoogleDriveInventoryEntry, "externalObjectId" | "externalVersionId" | "driveId">,
|
|
2583
|
+
observedAt: Date,
|
|
2584
|
+
providerRequests: number,
|
|
2585
|
+
) {
|
|
2586
|
+
return {
|
|
2587
|
+
eligibility: "denied" as const,
|
|
2588
|
+
providerRevision: entry.externalVersionId,
|
|
2589
|
+
driveId: entry.driveId,
|
|
2590
|
+
aclRevision: createHash("sha256")
|
|
2591
|
+
.update(
|
|
2592
|
+
JSON.stringify({
|
|
2593
|
+
version: 1,
|
|
2594
|
+
mode: "provider_denied",
|
|
2595
|
+
externalObjectId: entry.externalObjectId,
|
|
2596
|
+
providerRevision: entry.externalVersionId,
|
|
2597
|
+
}),
|
|
2598
|
+
)
|
|
2599
|
+
.digest("hex"),
|
|
2600
|
+
observedAt: observedAt.toISOString(),
|
|
2601
|
+
expiresAt: new Date(observedAt.getTime() + GOOGLE_DRIVE_ACL_FRESHNESS_MS).toISOString(),
|
|
2602
|
+
providerRequests,
|
|
2603
|
+
principals: [],
|
|
2604
|
+
};
|
|
2605
|
+
}
|
|
2606
|
+
|
|
2607
|
+
function parseDrivePermission(value: unknown): GoogleDrivePermission {
|
|
2608
|
+
const row = objectRecord(value);
|
|
2609
|
+
if (row.deleted === true) throw new SyncFailure("provider_payload_invalid", false);
|
|
2610
|
+
const type = requiredString(row.type, "permission.type");
|
|
2611
|
+
if (type !== "user" && type !== "group" && type !== "domain" && type !== "anyone") {
|
|
2612
|
+
throw new SyncFailure("provider_payload_invalid", false);
|
|
2613
|
+
}
|
|
2614
|
+
const role = requiredString(row.role, "permission.role");
|
|
2615
|
+
if (
|
|
2616
|
+
role !== "owner" &&
|
|
2617
|
+
role !== "organizer" &&
|
|
2618
|
+
role !== "fileOrganizer" &&
|
|
2619
|
+
role !== "writer" &&
|
|
2620
|
+
role !== "commenter" &&
|
|
2621
|
+
role !== "reader"
|
|
2622
|
+
) {
|
|
2623
|
+
throw new SyncFailure("provider_payload_invalid", false);
|
|
2624
|
+
}
|
|
2625
|
+
const permissionDetails = Array.isArray(row.permissionDetails) ? row.permissionDetails : [];
|
|
2626
|
+
return {
|
|
2627
|
+
id: requiredString(row.id, "permission.id"),
|
|
2628
|
+
type,
|
|
2629
|
+
role,
|
|
2630
|
+
emailAddress: optionalString(row.emailAddress),
|
|
2631
|
+
domain: optionalString(row.domain),
|
|
2632
|
+
allowFileDiscovery: typeof row.allowFileDiscovery === "boolean" ? row.allowFileDiscovery : null,
|
|
2633
|
+
expirationTime: optionalExactProviderTimestamp(row.expirationTime),
|
|
2634
|
+
inherited: permissionDetails.some((detail) => objectRecord(detail).inherited === true),
|
|
2635
|
+
};
|
|
2636
|
+
}
|
|
2637
|
+
|
|
2638
|
+
function optionalExactProviderTimestamp(value: unknown): string | null {
|
|
2639
|
+
const timestamp = optionalString(value);
|
|
2640
|
+
if (!timestamp) return null;
|
|
2641
|
+
const parsed = new Date(timestamp);
|
|
2642
|
+
if (!Number.isFinite(parsed.getTime())) throw new SyncFailure("provider_payload_invalid", false);
|
|
2643
|
+
return parsed.toISOString();
|
|
2644
|
+
}
|
|
2645
|
+
|
|
2271
2646
|
function throwGoogleDriveResponse(response: Response): never {
|
|
2272
2647
|
if (response.status === 401 || response.status === 403) {
|
|
2273
2648
|
throw new SyncFailure("connection_reconnect_required", true, true);
|