sf-plugin-data 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +618 -240
- package/docs/artifact-format.md +528 -633
- package/docs/capabilities.md +1160 -168
- package/docs/git-workflow.md +135 -12
- package/docs/handover.md +7826 -12
- package/lib/api.d.ts +13 -0
- package/lib/api.js +10 -0
- package/lib/artifacts/code-fields.d.ts +49 -0
- package/lib/artifacts/code-fields.js +142 -0
- package/lib/artifacts/data-graph-document.d.ts +29 -0
- package/lib/artifacts/data-graph-document.js +52 -0
- package/lib/artifacts/data-space-field.d.ts +65 -0
- package/lib/artifacts/data-space-field.js +98 -0
- package/lib/artifacts/data-stream-document.d.ts +264 -0
- package/lib/artifacts/data-stream-document.js +680 -0
- package/lib/artifacts/data-transform-document.d.ts +152 -0
- package/lib/artifacts/data-transform-document.js +560 -0
- package/lib/artifacts/dependency-selection.d.ts +51 -0
- package/lib/artifacts/dependency-selection.js +154 -0
- package/lib/artifacts/dlo-document.d.ts +93 -0
- package/lib/artifacts/dlo-document.js +253 -0
- package/lib/artifacts/enum-case.d.ts +39 -0
- package/lib/artifacts/enum-case.js +67 -0
- package/lib/artifacts/git-deploy-selection.d.ts +68 -0
- package/lib/artifacts/git-deploy-selection.js +474 -0
- package/lib/artifacts/identity-resolution-document.d.ts +133 -0
- package/lib/artifacts/identity-resolution-document.js +625 -0
- package/lib/artifacts/mapping-document.d.ts +1 -0
- package/lib/artifacts/mapping-document.js +1 -0
- package/lib/artifacts/native-calculated-insight-document.d.ts +142 -0
- package/lib/artifacts/native-calculated-insight-document.js +321 -0
- package/lib/artifacts/native-data-graph-document.d.ts +124 -0
- package/lib/artifacts/native-data-graph-document.js +780 -0
- package/lib/artifacts/native-dmo-document.d.ts +151 -0
- package/lib/artifacts/native-dmo-document.js +313 -0
- package/lib/artifacts/native-mapping-document.d.ts +67 -0
- package/lib/artifacts/native-mapping-document.js +187 -0
- package/lib/artifacts/native-relationship-document.d.ts +76 -0
- package/lib/artifacts/native-relationship-document.js +232 -0
- package/lib/artifacts/path-selector.d.ts +55 -0
- package/lib/artifacts/path-selector.js +167 -0
- package/lib/artifacts/read-deploy-input.d.ts +222 -6
- package/lib/artifacts/read-deploy-input.js +1213 -53
- package/lib/artifacts/segment-document.d.ts +183 -0
- package/lib/artifacts/segment-document.js +570 -0
- package/lib/artifacts/verbatim-document.d.ts +203 -0
- package/lib/artifacts/verbatim-document.js +339 -0
- package/lib/artifacts/write-data-graph.d.ts +17 -0
- package/lib/artifacts/write-data-graph.js +162 -0
- package/lib/artifacts/write-data-stream.d.ts +11 -0
- package/lib/artifacts/write-data-stream.js +175 -0
- package/lib/artifacts/write-data-transform.d.ts +11 -0
- package/lib/artifacts/write-data-transform.js +180 -0
- package/lib/artifacts/write-dlo.d.ts +11 -0
- package/lib/artifacts/write-dlo.js +158 -0
- package/lib/artifacts/write-identity-resolution.d.ts +18 -0
- package/lib/artifacts/write-identity-resolution.js +165 -0
- package/lib/artifacts/write-mapping-definition.d.ts +17 -0
- package/lib/artifacts/write-mapping-definition.js +127 -0
- package/lib/artifacts/write-member-export.d.ts +88 -0
- package/lib/artifacts/write-member-export.js +199 -0
- package/lib/artifacts/write-native-calculated-insight.d.ts +21 -0
- package/lib/artifacts/write-native-calculated-insight.js +176 -0
- package/lib/artifacts/write-native-dmo.d.ts +11 -0
- package/lib/artifacts/write-native-dmo.js +158 -0
- package/lib/artifacts/write-native-mapping.d.ts +11 -0
- package/lib/artifacts/write-native-mapping.js +157 -0
- package/lib/artifacts/write-native-relationship.d.ts +11 -0
- package/lib/artifacts/write-native-relationship.js +157 -0
- package/lib/artifacts/write-segment.d.ts +26 -0
- package/lib/artifacts/write-segment.js +212 -0
- package/lib/commands/data360/delete.d.ts +280 -0
- package/lib/commands/data360/delete.js +2347 -0
- package/lib/commands/data360/deploy.d.ts +235 -16
- package/lib/commands/data360/deploy.js +2339 -102
- package/lib/commands/data360/list/dataspace.d.ts +53 -0
- package/lib/commands/data360/list/dataspace.js +109 -0
- package/lib/commands/data360/publish.d.ts +93 -0
- package/lib/commands/data360/publish.js +298 -0
- package/lib/commands/data360/retrieve.d.ts +283 -14
- package/lib/commands/data360/retrieve.js +1945 -95
- package/lib/commands/data360/segment/members/export.d.ts +101 -0
- package/lib/commands/data360/segment/members/export.js +320 -0
- package/lib/errors/codes/calculated-insight.d.ts +2 -0
- package/lib/errors/codes/calculated-insight.js +25 -0
- package/lib/errors/codes/data-graph.d.ts +2 -0
- package/lib/errors/codes/data-graph.js +22 -0
- package/lib/errors/codes/data-stream.d.ts +2 -0
- package/lib/errors/codes/data-stream.js +70 -0
- package/lib/errors/codes/data-transform.d.ts +2 -0
- package/lib/errors/codes/data-transform.js +35 -0
- package/lib/errors/codes/dependency.d.ts +2 -0
- package/lib/errors/codes/dependency.js +11 -0
- package/lib/errors/codes/dlo.d.ts +2 -0
- package/lib/errors/codes/dlo.js +31 -0
- package/lib/errors/codes/dmo.d.ts +2 -0
- package/lib/errors/codes/dmo.js +28 -0
- package/lib/errors/codes/general.d.ts +2 -0
- package/lib/errors/codes/general.js +58 -0
- package/lib/errors/codes/identity-resolution.d.ts +2 -0
- package/lib/errors/codes/identity-resolution.js +24 -0
- package/lib/errors/codes/mapping.d.ts +2 -0
- package/lib/errors/codes/mapping.js +42 -0
- package/lib/errors/codes/relationship.d.ts +2 -0
- package/lib/errors/codes/relationship.js +21 -0
- package/lib/errors/codes/segment.d.ts +2 -0
- package/lib/errors/codes/segment.js +49 -0
- package/lib/errors/registry.d.ts +60 -0
- package/lib/errors/registry.js +123 -0
- package/lib/exit-code.d.ts +10 -0
- package/lib/exit-code.js +24 -0
- package/lib/plugin.d.ts +1 -0
- package/lib/plugin.js +3 -1
- package/lib/services/acquire-retrieve-selection.d.ts +43 -0
- package/lib/services/acquire-retrieve-selection.js +145 -0
- package/lib/services/bind-identity-resolution-dependencies.d.ts +53 -0
- package/lib/services/bind-identity-resolution-dependencies.js +238 -0
- package/lib/services/bind-segment-dependencies.d.ts +49 -0
- package/lib/services/bind-segment-dependencies.js +115 -0
- package/lib/services/bounded-reads.d.ts +17 -0
- package/lib/services/bounded-reads.js +42 -0
- package/lib/services/calculated-insight-sql-checks.d.ts +33 -0
- package/lib/services/calculated-insight-sql-checks.js +193 -0
- package/lib/services/command-dlo.d.ts +48 -0
- package/lib/services/command-dlo.js +103 -0
- package/lib/services/command-native-data-graph.d.ts +54 -0
- package/lib/services/command-native-data-graph.js +485 -0
- package/lib/services/command-native-data-stream.d.ts +104 -0
- package/lib/services/command-native-data-stream.js +257 -0
- package/lib/services/command-native-dmo.d.ts +124 -0
- package/lib/services/command-native-dmo.js +338 -0
- package/lib/services/command-native-mapping.d.ts +65 -0
- package/lib/services/command-native-mapping.js +129 -0
- package/lib/services/command-native-relationship.d.ts +46 -0
- package/lib/services/command-native-relationship.js +103 -0
- package/lib/services/cross-dataspace-deploy.d.ts +111 -0
- package/lib/services/cross-dataspace-deploy.js +493 -0
- package/lib/services/cross-dataspace-rename.d.ts +74 -0
- package/lib/services/cross-dataspace-rename.js +1314 -0
- package/lib/services/cross-dataspace-sql.d.ts +109 -0
- package/lib/services/cross-dataspace-sql.js +1301 -0
- package/lib/services/cross-space-context.d.ts +23 -0
- package/lib/services/cross-space-context.js +38 -0
- package/lib/services/crud-plan.d.ts +137 -0
- package/lib/services/crud-plan.js +408 -0
- package/lib/services/data-space-check.d.ts +92 -0
- package/lib/services/data-space-check.js +169 -0
- package/lib/services/data-space-prefix.d.ts +77 -0
- package/lib/services/data-space-prefix.js +127 -0
- package/lib/services/data-transform-space.d.ts +31 -0
- package/lib/services/data-transform-space.js +79 -0
- package/lib/services/delete-calculated-insight.d.ts +44 -0
- package/lib/services/delete-calculated-insight.js +101 -0
- package/lib/services/delete-data-transform.d.ts +52 -0
- package/lib/services/delete-data-transform.js +222 -0
- package/lib/services/delete-dmo-field.d.ts +211 -0
- package/lib/services/delete-dmo-field.js +472 -0
- package/lib/services/delete-identity-resolution.d.ts +43 -0
- package/lib/services/delete-identity-resolution.js +295 -0
- package/lib/services/delete-segment.d.ts +42 -0
- package/lib/services/delete-segment.js +248 -0
- package/lib/services/deploy-result.d.ts +54 -0
- package/lib/services/deploy-result.js +60 -0
- package/lib/services/escaped-copy.d.ts +23 -0
- package/lib/services/escaped-copy.js +88 -0
- package/lib/services/execute-calculated-insight-deploy.d.ts +42 -0
- package/lib/services/execute-calculated-insight-deploy.js +96 -0
- package/lib/services/execute-data-transform-deploy.d.ts +56 -0
- package/lib/services/execute-data-transform-deploy.js +168 -0
- package/lib/services/execute-native-identity-resolution-deploy.d.ts +48 -0
- package/lib/services/execute-native-identity-resolution-deploy.js +360 -0
- package/lib/services/execute-segment-deploy.d.ts +45 -0
- package/lib/services/execute-segment-deploy.js +393 -0
- package/lib/services/list-data-spaces.d.ts +17 -0
- package/lib/services/list-data-spaces.js +24 -0
- package/lib/services/local-file-removal.d.ts +36 -0
- package/lib/services/local-file-removal.js +103 -0
- package/lib/services/native-calculated-insight.d.ts +87 -0
- package/lib/services/native-calculated-insight.js +164 -0
- package/lib/services/native-crm-data-stream.d.ts +91 -0
- package/lib/services/native-crm-data-stream.js +272 -0
- package/lib/services/native-data-graph-context.d.ts +12 -0
- package/lib/services/native-data-graph-context.js +139 -0
- package/lib/services/native-data-graph-crd.d.ts +261 -0
- package/lib/services/native-data-graph-crd.js +866 -0
- package/lib/services/native-data-stream.d.ts +329 -0
- package/lib/services/native-data-stream.js +1830 -0
- package/lib/services/native-dlo.d.ts +163 -0
- package/lib/services/native-dlo.js +402 -0
- package/lib/services/native-dmo.d.ts +191 -0
- package/lib/services/native-dmo.js +614 -0
- package/lib/services/native-mapping.d.ts +255 -0
- package/lib/services/native-mapping.js +1226 -0
- package/lib/services/native-relationship.d.ts +132 -0
- package/lib/services/native-relationship.js +512 -0
- package/lib/services/operation-diagnostic.d.ts +83 -0
- package/lib/services/operation-diagnostic.js +272 -0
- package/lib/services/preflight-calculated-insight-deploy.d.ts +58 -0
- package/lib/services/preflight-calculated-insight-deploy.js +95 -0
- package/lib/services/preflight-data-transform-deploy.d.ts +33 -0
- package/lib/services/preflight-data-transform-deploy.js +104 -0
- package/lib/services/preflight-identity-resolution-deploy.d.ts +107 -0
- package/lib/services/preflight-identity-resolution-deploy.js +281 -0
- package/lib/services/preflight-segment-deploy.d.ts +31 -0
- package/lib/services/preflight-segment-deploy.js +109 -0
- package/lib/services/publish-calculated-insight.d.ts +40 -0
- package/lib/services/publish-calculated-insight.js +95 -0
- package/lib/services/publish-segment.d.ts +65 -0
- package/lib/services/publish-segment.js +194 -0
- package/lib/services/readback-pace.d.ts +16 -0
- package/lib/services/readback-pace.js +23 -0
- package/lib/services/retrieve-data-transform.d.ts +26 -0
- package/lib/services/retrieve-data-transform.js +65 -0
- package/lib/services/retrieve-identity-resolution.d.ts +40 -0
- package/lib/services/retrieve-identity-resolution.js +87 -0
- package/lib/services/retrieve-inventory.d.ts +32 -0
- package/lib/services/retrieve-inventory.js +151 -0
- package/lib/services/retrieve-segment.d.ts +120 -0
- package/lib/services/retrieve-segment.js +502 -0
- package/lib/services/run-retrieve.d.ts +24 -0
- package/lib/services/run-retrieve.js +51 -0
- package/lib/services/segment-lookup.d.ts +46 -0
- package/lib/services/segment-lookup.js +108 -0
- package/lib/services/segment-members.d.ts +144 -0
- package/lib/services/segment-members.js +402 -0
- package/lib/services/select-native-retrieve.d.ts +60 -0
- package/lib/services/select-native-retrieve.js +226 -0
- package/lib/services/settle-calculated-insight.d.ts +22 -0
- package/lib/services/settle-calculated-insight.js +53 -0
- package/lib/services/sql-tokens.d.ts +58 -0
- package/lib/services/sql-tokens.js +147 -0
- package/lib/transport/api-version.d.ts +36 -0
- package/lib/transport/api-version.js +89 -0
- package/lib/transport/bound-ssot.d.ts +27 -0
- package/lib/transport/bound-ssot.js +116 -0
- package/lib/transport/calculated-insight-rest.d.ts +28 -0
- package/lib/transport/calculated-insight-rest.js +62 -0
- package/lib/transport/connect-client.d.ts +94 -32
- package/lib/transport/connect-client.js +318 -178
- package/lib/transport/connect-diagnostic.d.ts +89 -0
- package/lib/transport/connect-diagnostic.js +331 -0
- package/lib/transport/data-graph-rest.d.ts +27 -0
- package/lib/transport/data-graph-rest.js +76 -0
- package/lib/transport/data-stream-rest.d.ts +62 -0
- package/lib/transport/data-stream-rest.js +348 -0
- package/lib/transport/data-transform-rest.d.ts +25 -0
- package/lib/transport/data-transform-rest.js +100 -0
- package/lib/transport/dlo-native-rest.d.ts +22 -0
- package/lib/transport/dlo-native-rest.js +89 -0
- package/lib/transport/dlo-rest.d.ts +23 -0
- package/lib/transport/dlo-rest.js +135 -0
- package/lib/transport/dmo-native-rest.d.ts +18 -0
- package/lib/transport/dmo-native-rest.js +55 -0
- package/lib/transport/dmo-rest.d.ts +30 -0
- package/lib/transport/dmo-rest.js +163 -0
- package/lib/transport/mapping-rest.d.ts +63 -0
- package/lib/transport/mapping-rest.js +341 -0
- package/lib/transport/project-metadata.d.ts +36 -0
- package/lib/transport/project-metadata.js +118 -0
- package/lib/transport/relationship-rest.d.ts +43 -0
- package/lib/transport/relationship-rest.js +201 -0
- package/package.json +29 -6
- package/lib/artifacts/dmo-document.d.ts +0 -181
- package/lib/artifacts/dmo-document.js +0 -848
- package/lib/artifacts/dmo-update-delta.d.ts +0 -28
- package/lib/artifacts/dmo-update-delta.js +0 -86
- package/lib/artifacts/write-other-dmo.d.ts +0 -17
- package/lib/artifacts/write-other-dmo.js +0 -140
- package/lib/services/execute-dmo-create.d.ts +0 -24
- package/lib/services/execute-dmo-create.js +0 -118
- package/lib/services/execute-dmo-deploy.d.ts +0 -33
- package/lib/services/execute-dmo-deploy.js +0 -197
- package/lib/services/preflight-dmo-deploy.d.ts +0 -69
- package/lib/services/preflight-dmo-deploy.js +0 -161
- package/lib/services/retrieve-other-dmo.d.ts +0 -46
- package/lib/services/retrieve-other-dmo.js +0 -140
|
@@ -0,0 +1,866 @@
|
|
|
1
|
+
import { getConnectDiagnostic, retainConnectDiagnostic } from '../transport/connect-diagnostic.js';
|
|
2
|
+
import { refuseEscapedCopy } from './escaped-copy.js';
|
|
3
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
6
|
+
import { DataGraphError, graphRecord, graphRequire, graphText, } from '../artifacts/data-graph-document.js';
|
|
7
|
+
import { nativeGraphFingerprint, nativeDataGraphDocumentFromResponse, nativeDataGraphSpace, nativeDataGraphSpaceMismatch, buildNativeDataGraphUpdateRequest, observeNativeDataGraph, validateNativeDataGraphDocument, } from '../artifacts/native-data-graph-document.js';
|
|
8
|
+
import { isDataSpaceName } from '../artifacts/data-space-field.js';
|
|
9
|
+
import { explainInput } from './operation-diagnostic.js';
|
|
10
|
+
/**
|
|
11
|
+
* Canonicalize validated keyed root inventories without discarding field properties.
|
|
12
|
+
* @param {Map<string, Record<string, unknown>>} values Unique field records indexed by name.
|
|
13
|
+
* @returns {Record<string, unknown>[]} Name-ordered records.
|
|
14
|
+
*/
|
|
15
|
+
function orderedRootFields(values) {
|
|
16
|
+
return ([...values.entries()]
|
|
17
|
+
// eslint-disable-next-line unicorn/no-array-sort -- ES2022 target lacks toSorted; entries is newly allocated.
|
|
18
|
+
.sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
|
|
19
|
+
.map(([, field]) => field));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Independently bind the graph's enabled primary object in the run's data space. The root is
|
|
23
|
+
* the document's `primaryObjectName` (standard or unified); its fields are not restricted. A root
|
|
24
|
+
* the read names in another data space is DATA_GRAPH_DEPENDENCY_OTHER_DATASPACE.
|
|
25
|
+
* @param {NativeGraphContext} context Scope-checked readers.
|
|
26
|
+
* @param {string} rootName The document's primary object.
|
|
27
|
+
* @returns {Promise<{id: string; creationType: string; fingerprint: string}>} Detached root binding.
|
|
28
|
+
*/
|
|
29
|
+
export async function bindNativeGraphRoot(context, rootName) {
|
|
30
|
+
context.check();
|
|
31
|
+
graphText(rootName);
|
|
32
|
+
const acquired = structuredClone(await context.root(rootName));
|
|
33
|
+
context.check();
|
|
34
|
+
const detail = graphRecord(acquired.detail);
|
|
35
|
+
graphText(detail.creationType);
|
|
36
|
+
graphRequire(detail.name === rootName && detail.isEnabled === true && isDataSpaceName(context.space));
|
|
37
|
+
// The root read names the run's data space; a root of another one is not this graph's.
|
|
38
|
+
if (detail.dataSpaceName !== context.space)
|
|
39
|
+
throw explainInput(new DataGraphError('DATA_GRAPH_DEPENDENCY_OTHER_DATASPACE'), `The graph root ${rootName} is in the data space ${isDataSpaceName(detail.dataSpaceName) ? detail.dataSpaceName : '(none named)'}, not in the graph's data space ${context.space}. Name the DMO of the graph's data space (a standard DMO copy outside default is <prefix>_<Name>); nothing was written.`);
|
|
40
|
+
graphText(detail.id);
|
|
41
|
+
graphRequire(Array.isArray(detail.fields));
|
|
42
|
+
const index = (values, key) => {
|
|
43
|
+
const map = new Map();
|
|
44
|
+
for (const value of values) {
|
|
45
|
+
const field = graphRecord(value);
|
|
46
|
+
graphText(field[key]);
|
|
47
|
+
graphRequire(!map.has(field[key]));
|
|
48
|
+
map.set(field[key], field);
|
|
49
|
+
}
|
|
50
|
+
return map;
|
|
51
|
+
};
|
|
52
|
+
// The field inventory is a keyed set, not an ordered projection. Preserve every property.
|
|
53
|
+
detail.fields = orderedRootFields(index(detail.fields, 'name'));
|
|
54
|
+
return {
|
|
55
|
+
id: detail.id,
|
|
56
|
+
creationType: detail.creationType,
|
|
57
|
+
fingerprint: nativeGraphFingerprint(acquired),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Detect a ready item whose server-generated names are not populated yet.
|
|
62
|
+
* @param {unknown} body Untrusted item body.
|
|
63
|
+
* @returns {boolean} True while generation is visibly incomplete.
|
|
64
|
+
*/
|
|
65
|
+
export function nativeGraphStillGenerating(body) {
|
|
66
|
+
if (!body || typeof body !== 'object' || Array.isArray(body))
|
|
67
|
+
return false;
|
|
68
|
+
const raw = body;
|
|
69
|
+
if (raw.status !== 'ready')
|
|
70
|
+
return false;
|
|
71
|
+
const source = raw.sourceObject;
|
|
72
|
+
return [
|
|
73
|
+
raw.idDmoName,
|
|
74
|
+
raw.valuesDmoName,
|
|
75
|
+
raw.dayZeroMappingDevName,
|
|
76
|
+
raw.dayZeroTransformDevName,
|
|
77
|
+
source?.fragmentDMOName,
|
|
78
|
+
].some((value) => typeof value !== 'string' || !value.trim());
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Read the exact graph metadata build status after the item itself reads ready.
|
|
82
|
+
* @param {NativeGraphContext} context Deadline-guarded readers.
|
|
83
|
+
* @param {string} name Exact graph name.
|
|
84
|
+
* @returns {Promise<string>} Metadata status, for example Inprogress or Ready.
|
|
85
|
+
*/
|
|
86
|
+
async function nativeGraphMetadataStatus(context, name) {
|
|
87
|
+
const metadata = await context.metadata(name);
|
|
88
|
+
context.check();
|
|
89
|
+
if (metadata.status !== 200)
|
|
90
|
+
throw retainConnectDiagnostic(new Error('Native Graph metadata unavailable'), getConnectDiagnostic(metadata));
|
|
91
|
+
const entries = graphRecord(metadata.body).dataGraphMetadata;
|
|
92
|
+
graphRequire(Array.isArray(entries));
|
|
93
|
+
const matches = entries
|
|
94
|
+
.map((entry) => graphRecord(entry))
|
|
95
|
+
.filter((entry) => entry.developerName === name);
|
|
96
|
+
// During the build the entry was recorded live to vanish briefly; that is still building.
|
|
97
|
+
if (matches.length === 0)
|
|
98
|
+
return 'Missing';
|
|
99
|
+
graphRequire(matches.length === 1 && matches[0].dataspaceName === context.space);
|
|
100
|
+
graphText(matches[0].status);
|
|
101
|
+
return matches[0].status;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Take the node types the API assigned on create. A recorded API68 create sent `standard` for a
|
|
105
|
+
* unified root and the echo read `derived` (2026-11-12); later reads keep the assigned type, so
|
|
106
|
+
* the created graph is compared with the assigned types, matched by node path. A `derived`
|
|
107
|
+
* (unified DMO) root also takes its assigned node devName (node labels are always the target's).
|
|
108
|
+
* @param {NativeDataGraphDocument} document Exact native intent.
|
|
109
|
+
* @param {unknown} echoed Untrusted echoed root node.
|
|
110
|
+
* @returns {NativeDataGraphDocument} Detached intent carrying the assigned node types.
|
|
111
|
+
*/
|
|
112
|
+
function nativeGraphAssignedTypes(document, echoed) {
|
|
113
|
+
const result = validateNativeDataGraphDocument(document);
|
|
114
|
+
const visit = (node, found) => {
|
|
115
|
+
const echo = graphRecord(found);
|
|
116
|
+
graphText(echo.type);
|
|
117
|
+
node.type = echo.type;
|
|
118
|
+
const children = echo.relatedObjects;
|
|
119
|
+
graphRequire(Array.isArray(children));
|
|
120
|
+
for (const child of node.relatedObjects)
|
|
121
|
+
visit(child, children.find((entry) => graphRecord(entry).jsonPath === child.jsonPath));
|
|
122
|
+
};
|
|
123
|
+
const top = graphRecord(result.definition.sourceObject);
|
|
124
|
+
visit(top, echoed);
|
|
125
|
+
// A unified DMO root (assigned `derived`) gets Salesforce's own node devName on create
|
|
126
|
+
// (recorded live at API68, 2026-11-13); every other authored setting must still hold.
|
|
127
|
+
const echo = graphRecord(echoed);
|
|
128
|
+
if (echo.type === 'derived' && Object.hasOwn(echo, 'devName')) {
|
|
129
|
+
graphText(echo.devName);
|
|
130
|
+
top.devName = echo.devName;
|
|
131
|
+
}
|
|
132
|
+
return validateNativeDataGraphDocument(result);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Take the identity of a create echo. A complete echo is fully observed; an echo that reads
|
|
136
|
+
* ready before its generated names exist (still building) yields only its raw identity.
|
|
137
|
+
* @param {unknown} response Untrusted create response.
|
|
138
|
+
* @param {NativeDataGraphDocument} authored Exact native intent.
|
|
139
|
+
* @returns {object} Identity fields the settled graph must match, and the intent to settle with.
|
|
140
|
+
*/
|
|
141
|
+
function nativeGraphCreateEcho(response, authored) {
|
|
142
|
+
const raw = graphRecord(response);
|
|
143
|
+
const document = nativeGraphAssignedTypes(authored, raw.sourceObject);
|
|
144
|
+
if (!nativeGraphStillGenerating(response)) {
|
|
145
|
+
const returned = observeNativeDataGraph(response, document);
|
|
146
|
+
return {
|
|
147
|
+
id: returned.id,
|
|
148
|
+
createdDate: returned.createdDate,
|
|
149
|
+
source: graphRecord(returned.target.sourceObject),
|
|
150
|
+
document,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
graphText(raw.id);
|
|
154
|
+
graphText(raw.createdDate);
|
|
155
|
+
graphRequire(raw.name === document.definition.name && Number.isFinite(Date.parse(raw.createdDate)));
|
|
156
|
+
return {
|
|
157
|
+
id: raw.id,
|
|
158
|
+
createdDate: new Date(raw.createdDate).toISOString(),
|
|
159
|
+
source: graphRecord(raw.sourceObject),
|
|
160
|
+
document,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Prove that no graph of this name exists. HTTP400 alone is never absence evidence: only the
|
|
165
|
+
* item route's exact not-found answer, the metadata route's 404 METADATA_ERROR_ENTITY_NOTFOUND,
|
|
166
|
+
* a complete inventory without the name and an empty SOQL read prove it. Metadata still present
|
|
167
|
+
* (or the name still listed or queried) is pending; anything else is unknown.
|
|
168
|
+
* @param {NativeGraphContext} context Independent exact readers, not mutation responses.
|
|
169
|
+
* @param {string} name Exact graph name.
|
|
170
|
+
* @param {NativeGraphRead} [item] The item read already made for this name; read when omitted.
|
|
171
|
+
* @returns {Promise<object>} Absent with its proof, pending, or unknown with any retained diagnostic.
|
|
172
|
+
*/
|
|
173
|
+
export async function proveNativeDataGraphAbsent(context, name, item) {
|
|
174
|
+
let diagnostic;
|
|
175
|
+
try {
|
|
176
|
+
graphText(name);
|
|
177
|
+
context.check();
|
|
178
|
+
const read = item ?? (await context.get(name));
|
|
179
|
+
diagnostic = getConnectDiagnostic(read);
|
|
180
|
+
context.check();
|
|
181
|
+
graphRequire(read.status === 400 &&
|
|
182
|
+
isDeepStrictEqual(read.body, [
|
|
183
|
+
{ errorCode: 'INTERNAL_ERROR', message: `Data Graph not found with name ${name}` },
|
|
184
|
+
]));
|
|
185
|
+
const exact = await context.metadata(name);
|
|
186
|
+
diagnostic = getConnectDiagnostic(exact);
|
|
187
|
+
context.check();
|
|
188
|
+
if (exact.status === 200)
|
|
189
|
+
return { state: 'pending', deleting: true };
|
|
190
|
+
graphRequire(exact.status === 404 && Array.isArray(exact.body) && exact.body.length === 1);
|
|
191
|
+
const error = graphRecord(exact.body[0]);
|
|
192
|
+
graphRequire(error.errorCode === 'INTERNAL_ERROR' && typeof error.message === 'string');
|
|
193
|
+
const nested = graphRecord(JSON.parse(error.message));
|
|
194
|
+
graphRequire(nested.error === '404 NOT_FOUND' &&
|
|
195
|
+
nested.internalErrorCode === 'METADATA_ERROR_ENTITY_NOTFOUND' &&
|
|
196
|
+
typeof nested.message === 'string' &&
|
|
197
|
+
nested.message.startsWith(`NOT_FOUND: DaoEntity with daoId = ${name} and tenantId = `));
|
|
198
|
+
const inventory = await context.inventory();
|
|
199
|
+
diagnostic = getConnectDiagnostic(inventory);
|
|
200
|
+
context.check();
|
|
201
|
+
const entries = graphRecord(inventory.body).dataGraphMetadata;
|
|
202
|
+
graphRequire(inventory.status === 200 && Array.isArray(entries));
|
|
203
|
+
const names = new Set();
|
|
204
|
+
for (const value of entries) {
|
|
205
|
+
const entry = graphRecord(value);
|
|
206
|
+
graphText(entry.developerName);
|
|
207
|
+
graphRequire(entry.dataspaceName === context.space && !names.has(entry.developerName));
|
|
208
|
+
names.add(entry.developerName);
|
|
209
|
+
}
|
|
210
|
+
const query = await context.query(name);
|
|
211
|
+
diagnostic = getConnectDiagnostic(query);
|
|
212
|
+
context.check();
|
|
213
|
+
const rows = graphRecord(query.body);
|
|
214
|
+
graphRequire(query.status === 200 &&
|
|
215
|
+
rows.done === true &&
|
|
216
|
+
Array.isArray(rows.records) &&
|
|
217
|
+
rows.totalSize === rows.records.length &&
|
|
218
|
+
rows.records.length <= 1);
|
|
219
|
+
if (names.has(name) || rows.records.length > 0)
|
|
220
|
+
return { state: 'pending', deleting: true };
|
|
221
|
+
return { state: 'absent', proof: 'item+metadata+inventory+soql' };
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
return retainConnectDiagnostic({ state: 'unknown' }, getConnectDiagnostic(error) ?? diagnostic);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Acquire exact native state. HTTP400 alone is never absence evidence; stale metadata is pending.
|
|
229
|
+
* @param {NativeGraphContext} context Independent exact readers, not mutation responses.
|
|
230
|
+
* @param {NativeDataGraphDocument} document Native intent.
|
|
231
|
+
* @param {boolean} [baseline] Acquire target independent of desired equality.
|
|
232
|
+
* @param {DocumentMode} [mode] Retrieve mode takes the graph in any status, as read.
|
|
233
|
+
* @returns {Promise<NativeGraphAcquisition>} Correlated state or conservative uncertainty.
|
|
234
|
+
*/
|
|
235
|
+
export async function acquireNativeDataGraph(context, document, baseline = false, mode = 'deploy') {
|
|
236
|
+
let diagnostic;
|
|
237
|
+
try {
|
|
238
|
+
const expected = validateNativeDataGraphDocument(document, mode);
|
|
239
|
+
const name = expected.definition.name;
|
|
240
|
+
context.check();
|
|
241
|
+
const item = await context.get(name);
|
|
242
|
+
diagnostic = getConnectDiagnostic(item);
|
|
243
|
+
context.check();
|
|
244
|
+
if (item.status === 200) {
|
|
245
|
+
const found = nativeDataGraphSpace(item.body);
|
|
246
|
+
if (found !== expected.c__dataSpace)
|
|
247
|
+
throw nativeDataGraphSpaceMismatch(name, found, expected.c__dataSpace, 'org');
|
|
248
|
+
// A just-created graph can read ready ~30s before its generated DMO, mapping and
|
|
249
|
+
// transform names exist (recorded live at API68); it is still being built, not unknown.
|
|
250
|
+
// Retrieve takes the graph as it is, generated names or not.
|
|
251
|
+
if (mode === 'deploy' && nativeGraphStillGenerating(item.body))
|
|
252
|
+
return { state: 'pending' };
|
|
253
|
+
const admitted = baseline ? nativeDataGraphDocumentFromResponse(item.body, mode) : expected;
|
|
254
|
+
graphRequire(admitted.definition.name === name);
|
|
255
|
+
return { state: 'present', observation: observeNativeDataGraph(item.body, admitted, mode) };
|
|
256
|
+
}
|
|
257
|
+
return await proveNativeDataGraphAbsent(context, name, item);
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
// A graph of another data space is refused, never an unknown state.
|
|
261
|
+
if (error instanceof DataGraphError && error.code === 'DATA_GRAPH_DATASPACE_MISMATCH')
|
|
262
|
+
throw error;
|
|
263
|
+
return retainConnectDiagnostic({ state: 'unknown' }, getConnectDiagnostic(error) ?? diagnostic);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Settle using bounded GET-only reads; no write is retried on any outcome.
|
|
268
|
+
* @param {NativeGraphContext} context Readers sharing one operation deadline.
|
|
269
|
+
* @param {NativeDataGraphDocument} document Exact native intent.
|
|
270
|
+
* @param {'ready' | 'absent'} goal Required independent state.
|
|
271
|
+
* @param {Function} pause Injectable read-only polling delay.
|
|
272
|
+
* @returns {Promise<NativeGraphAcquisition>} Settled evidence or unknown.
|
|
273
|
+
*/
|
|
274
|
+
export async function settleNativeDataGraph(context, document, goal, pause = delay) {
|
|
275
|
+
let expired = false;
|
|
276
|
+
let timer;
|
|
277
|
+
const check = () => {
|
|
278
|
+
graphRequire(!expired);
|
|
279
|
+
context.check();
|
|
280
|
+
};
|
|
281
|
+
const work = async () => {
|
|
282
|
+
graphRequire(goal === 'ready' || goal === 'absent');
|
|
283
|
+
for (let poll = 0; poll < 12; poll++) {
|
|
284
|
+
check();
|
|
285
|
+
const result = await acquireNativeDataGraph({ ...context, check }, document);
|
|
286
|
+
check();
|
|
287
|
+
if (result.state === 'unknown')
|
|
288
|
+
return result;
|
|
289
|
+
if (goal === 'absent' && result.state === 'absent')
|
|
290
|
+
return result;
|
|
291
|
+
if (result.state === 'present' && result.observation.status === 'ready' && goal === 'ready')
|
|
292
|
+
return result;
|
|
293
|
+
if (goal === 'ready' && result.state === 'absent')
|
|
294
|
+
return { state: 'unknown' };
|
|
295
|
+
if (poll < 11)
|
|
296
|
+
await pause(1000);
|
|
297
|
+
}
|
|
298
|
+
return { state: 'unknown' };
|
|
299
|
+
};
|
|
300
|
+
try {
|
|
301
|
+
return await Promise.race([
|
|
302
|
+
work(),
|
|
303
|
+
new Promise((resolve) => {
|
|
304
|
+
timer = setTimeout(() => resolve({ state: 'unknown' }), 120_000);
|
|
305
|
+
}),
|
|
306
|
+
]);
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
return retainConnectDiagnostic({ state: 'unknown' }, getConnectDiagnostic(error));
|
|
310
|
+
}
|
|
311
|
+
finally {
|
|
312
|
+
expired = true;
|
|
313
|
+
clearTimeout(timer);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Wait for a created graph to finish building, bounded only by the invocation deadline.
|
|
318
|
+
* While the item reads creating, still lacks its generated names, or its metadata still shows
|
|
319
|
+
* it in progress, the graph is being built: keep reading with the documented pacing (20-second
|
|
320
|
+
* waits, never past the deadline) instead of reporting an unknown result early. GET-only; the
|
|
321
|
+
* create is never replayed.
|
|
322
|
+
* @param {NativeGraphContext} context Deadline-guarded readers.
|
|
323
|
+
* @param {NativeDataGraphDocument} document Exact native intent.
|
|
324
|
+
* @param {object} timing Invocation clock, deadline, delay and progress sink.
|
|
325
|
+
* @param {Function} timing.now Invocation clock.
|
|
326
|
+
* @param {number} timing.started Invocation start time.
|
|
327
|
+
* @param {number} timing.end Invocation deadline (--wait-timeout).
|
|
328
|
+
* @param {Function} timing.pause Read-only delay.
|
|
329
|
+
* @param {Function} timing.progress Stderr-only progress sink.
|
|
330
|
+
* @returns {Promise<NativeGraphAcquisition>} Ready evidence or unknown.
|
|
331
|
+
*/
|
|
332
|
+
export async function settleNativeDataGraphCreation(context, document, timing) {
|
|
333
|
+
try {
|
|
334
|
+
for (;;) {
|
|
335
|
+
context.check();
|
|
336
|
+
const result = await acquireNativeDataGraph(context, document);
|
|
337
|
+
if (result.state === 'unknown')
|
|
338
|
+
return result;
|
|
339
|
+
// Right after a create, absence is not a building state: report it as unknown.
|
|
340
|
+
if (result.state === 'absent')
|
|
341
|
+
return { state: 'unknown' };
|
|
342
|
+
let stage = result.state === 'present' ? result.observation.status : 'build in progress';
|
|
343
|
+
// Deleting (or any non-build status) is terminal for a create: stop at once.
|
|
344
|
+
if (result.state === 'present' &&
|
|
345
|
+
![
|
|
346
|
+
'creating',
|
|
347
|
+
'processing',
|
|
348
|
+
'editing',
|
|
349
|
+
'editing_no_version_update',
|
|
350
|
+
'editing_post_sync',
|
|
351
|
+
'ready',
|
|
352
|
+
].includes(result.observation.status))
|
|
353
|
+
return { state: 'unknown' };
|
|
354
|
+
if (result.state === 'present' && result.observation.status === 'ready') {
|
|
355
|
+
// The item can read ready while its metadata is still Inprogress (recorded ~3.5 min)
|
|
356
|
+
// or briefly absent; any other metadata status (e.g. Failed) is terminal.
|
|
357
|
+
const metadata = await nativeGraphMetadataStatus(context, document.definition.name);
|
|
358
|
+
if (metadata === 'Ready')
|
|
359
|
+
return result;
|
|
360
|
+
if (metadata !== 'Inprogress' && metadata !== 'Missing')
|
|
361
|
+
return { state: 'unknown' };
|
|
362
|
+
stage = `ready, metadata ${metadata}`;
|
|
363
|
+
}
|
|
364
|
+
const remaining = timing.end - timing.now();
|
|
365
|
+
if (remaining <= 0)
|
|
366
|
+
return { state: 'unknown' };
|
|
367
|
+
const wait = Math.min(20_000, remaining);
|
|
368
|
+
timing.progress(`Data Graph ${stage}; elapsed ${Math.floor((timing.now() - timing.started) / 1000)}s; next wait ${wait / 1000}s.`);
|
|
369
|
+
await timing.pause(wait);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
catch (error) {
|
|
373
|
+
return retainConnectDiagnostic({ state: 'unknown' }, getConnectDiagnostic(error));
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* A Graph write Salesforce refused with HTTP429/503 until the wait budget ran out. The request
|
|
378
|
+
* was not processed; the retained diagnostic is the last refusal.
|
|
379
|
+
*/
|
|
380
|
+
export class NativeGraphWriteRefused extends Error {
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Salesforce answered the update PUT with an API 4xx: the PUT was refused and nothing was
|
|
384
|
+
* written (live, API 68: removals, root and related-object swaps are refused with 400).
|
|
385
|
+
*/
|
|
386
|
+
export class NativeGraphWriteRejected extends Error {
|
|
387
|
+
}
|
|
388
|
+
/** An update that changes the graph was requested without --allow-processing; nothing was sent. */
|
|
389
|
+
export class NativeGraphProcessingRequired extends Error {
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Send one Graph write; HTTP429/503 means Salesforce did not process it, so pause (Retry-After
|
|
393
|
+
* delta-seconds, else 20 s) and send the identical request again within the invocation deadline.
|
|
394
|
+
* Any other failure is rethrown unchanged.
|
|
395
|
+
* @template T
|
|
396
|
+
* @param {Function} write Sends the identical request.
|
|
397
|
+
* @param {string} operation Name used in progress messages.
|
|
398
|
+
* @param {object} timing Invocation clock, deadline, delay and stderr progress.
|
|
399
|
+
* @param {Function} timing.now Invocation clock.
|
|
400
|
+
* @param {number} timing.end Invocation deadline.
|
|
401
|
+
* @param {Function} timing.pause Delay seam.
|
|
402
|
+
* @param {Function} timing.progress Stderr-only progress sink.
|
|
403
|
+
* @returns {Promise<T>} The accepted write's response.
|
|
404
|
+
* @throws {NativeGraphWriteRefused} When the next wait would reach the deadline.
|
|
405
|
+
*/
|
|
406
|
+
export async function sendNativeGraphWrite(write, operation, timing) {
|
|
407
|
+
for (;;) {
|
|
408
|
+
try {
|
|
409
|
+
return await write();
|
|
410
|
+
}
|
|
411
|
+
catch (error) {
|
|
412
|
+
const diagnostic = getConnectDiagnostic(error);
|
|
413
|
+
if (diagnostic?.kind !== 'rate-limit' && diagnostic?.kind !== 'service-unavailable')
|
|
414
|
+
throw error;
|
|
415
|
+
const header = diagnostic.retryAfter;
|
|
416
|
+
// At least 1 s: Retry-After 0 must not turn into a tight resend loop.
|
|
417
|
+
const seconds = header && /^\d+$/u.test(header) ? Math.max(1, Number(header)) : 20;
|
|
418
|
+
if (timing.now() + seconds * 1000 >= timing.end)
|
|
419
|
+
throw retainConnectDiagnostic(new NativeGraphWriteRefused('Native Graph write refused'), diagnostic);
|
|
420
|
+
timing.progress(`Data Graph ${operation} rate-limited (HTTP ${diagnostic.statusCode}); retrying in ${seconds}s.`);
|
|
421
|
+
await timing.pause(seconds * 1000);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Resolve the authoritative record of the run's data space once per invocation.
|
|
427
|
+
* @param {NativeGraphContext} context Bound target readers.
|
|
428
|
+
* @returns {Promise<string>} Exact active default record identity.
|
|
429
|
+
*/
|
|
430
|
+
export async function bindNativeGraphDataSpace(context) {
|
|
431
|
+
context.check();
|
|
432
|
+
graphRequire(typeof context.dataSpace === 'function');
|
|
433
|
+
const read = await context.dataSpace();
|
|
434
|
+
context.check();
|
|
435
|
+
try {
|
|
436
|
+
const body = graphRecord(read.body);
|
|
437
|
+
graphRequire(read.status === 200 &&
|
|
438
|
+
body.done === true &&
|
|
439
|
+
body.totalSize === 1 &&
|
|
440
|
+
Array.isArray(body.records) &&
|
|
441
|
+
body.records.length === 1);
|
|
442
|
+
const row = graphRecord(body.records[0]);
|
|
443
|
+
graphText(row.Id);
|
|
444
|
+
graphRequire(row.DataSpaceApiName === context.space && row.Status === 'ACTIVE' && row.IsDeleted === false);
|
|
445
|
+
return row.Id;
|
|
446
|
+
}
|
|
447
|
+
catch (error) {
|
|
448
|
+
if (error instanceof Error)
|
|
449
|
+
retainConnectDiagnostic(error, getConnectDiagnostic(read));
|
|
450
|
+
throw error;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Read one fresh item/metadata/SOQL tuple without unrelated reads or write replay.
|
|
455
|
+
* @param {NativeGraphContext} context Bound target readers.
|
|
456
|
+
* @param {NativeDataGraphDocument} document Exact API name.
|
|
457
|
+
* @param {string} dataSpaceId Invocation-resolved data space record identity.
|
|
458
|
+
* @returns {Promise<NativeGraphReadiness>} Detached independent tuple.
|
|
459
|
+
*/
|
|
460
|
+
export async function readNativeGraphReadiness(context, document, dataSpaceId) {
|
|
461
|
+
const name = document.definition.name;
|
|
462
|
+
context.check();
|
|
463
|
+
const item = await context.get(name);
|
|
464
|
+
context.check();
|
|
465
|
+
if (item.status !== 200)
|
|
466
|
+
throw retainConnectDiagnostic(new Error('Native Graph item unavailable'), getConnectDiagnostic(item));
|
|
467
|
+
const actual = nativeDataGraphDocumentFromResponse(item.body);
|
|
468
|
+
graphRequire(actual.definition.name === name);
|
|
469
|
+
const observation = observeNativeDataGraph(item.body, actual);
|
|
470
|
+
const metadata = await context.metadata(name);
|
|
471
|
+
context.check();
|
|
472
|
+
if (metadata.status !== 200)
|
|
473
|
+
throw retainConnectDiagnostic(new Error('Native Graph metadata unavailable'), getConnectDiagnostic(metadata));
|
|
474
|
+
const entries = graphRecord(metadata.body).dataGraphMetadata;
|
|
475
|
+
graphRequire(Array.isArray(entries) && entries.length === 1);
|
|
476
|
+
const entry = graphRecord(entries[0]);
|
|
477
|
+
graphRequire(entry.developerName === name &&
|
|
478
|
+
entry.dataspaceName === context.space &&
|
|
479
|
+
entry.primaryObjectName === actual.definition.primaryObjectName);
|
|
480
|
+
graphText(entry.status);
|
|
481
|
+
const query = await context.query(name);
|
|
482
|
+
context.check();
|
|
483
|
+
if (query.status !== 200)
|
|
484
|
+
throw retainConnectDiagnostic(new Error('Native Graph query unavailable'), getConnectDiagnostic(query));
|
|
485
|
+
const rows = graphRecord(query.body);
|
|
486
|
+
graphRequire(query.status === 200 &&
|
|
487
|
+
rows.done === true &&
|
|
488
|
+
rows.totalSize === 1 &&
|
|
489
|
+
Array.isArray(rows.records) &&
|
|
490
|
+
rows.records.length === 1);
|
|
491
|
+
const row = graphRecord(rows.records[0]);
|
|
492
|
+
graphRequire(row.Id === observation.id &&
|
|
493
|
+
row.DataGraphApiName === name &&
|
|
494
|
+
row.Version === observation.target.version &&
|
|
495
|
+
row.DataSpaceId === dataSpaceId);
|
|
496
|
+
graphText(row.Status);
|
|
497
|
+
graphText(row.LastRunStatus);
|
|
498
|
+
return {
|
|
499
|
+
observation,
|
|
500
|
+
ready: observation.status === 'ready' &&
|
|
501
|
+
entry.status === 'Ready' &&
|
|
502
|
+
row.Status === 'READY' &&
|
|
503
|
+
row.LastRunStatus === 'ACTIVE',
|
|
504
|
+
status: `${observation.status}/${entry.status}/${row.Status}/${row.LastRunStatus}`,
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Apply one authored change to the bound target (see buildNativeDataGraphUpdateRequest), then
|
|
509
|
+
* reconcile solely with paced GETs. An equal definition is unchanged with zero writes.
|
|
510
|
+
* @param {NativeGraphContext} context Invocation/deadline guarded readers.
|
|
511
|
+
* @param {DataGraphClient} client Single-attempt guarded writer.
|
|
512
|
+
* @param {NativeDataGraphDocument} document Authored delta.
|
|
513
|
+
* @param {NativeDataGraphObservation} baseline Initial independently acquired target.
|
|
514
|
+
* @param {object} options Shared invocation controls.
|
|
515
|
+
* @param {boolean} options.preview Zero-write comparison.
|
|
516
|
+
* @param {boolean} options.confirmed Processing acknowledgement.
|
|
517
|
+
* @param {Function} options.verify Revalidate bytes/flags/root before final tuple.
|
|
518
|
+
* @param {Function} options.now Invocation clock.
|
|
519
|
+
* @param {Function} options.pause Injectable read-only delay.
|
|
520
|
+
* @param {Function} options.progress Stderr-only progress sink.
|
|
521
|
+
* @param {number} options.started Invocation start time.
|
|
522
|
+
* @param {number} options.end Invocation deadline.
|
|
523
|
+
* @returns {Promise<'update'|'updated'|'unchanged'>} Independent result, never response echo.
|
|
524
|
+
* @throws {NativeGraphWriteRefused} When HTTP429/503 refused every PUT until the deadline.
|
|
525
|
+
* @throws {NativeGraphWriteRejected} When Salesforce answered the PUT with an API 4xx.
|
|
526
|
+
* @throws {DataGraphError} DATA_GRAPH_JOIN_CHANGE before any write; DATA_GRAPH_CHANGE_NOT_APPLIED
|
|
527
|
+
* when the settled graph kept a join the PUT changed.
|
|
528
|
+
*/
|
|
529
|
+
export async function updateNativeDataGraph(context, client, document, baseline, options) {
|
|
530
|
+
const dataSpaceId = await bindNativeGraphDataSpace(context);
|
|
531
|
+
refuseEscapedCopy(document.definition, nativeDataGraphDocumentFromResponse(baseline.target).definition, (code) => new DataGraphError(code));
|
|
532
|
+
const desired = buildNativeDataGraphUpdateRequest(baseline.target, document);
|
|
533
|
+
const same = isDeepStrictEqual(desired, nativeDataGraphDocumentFromResponse(baseline.target).definition);
|
|
534
|
+
if (!same && options.preview)
|
|
535
|
+
return 'update';
|
|
536
|
+
if (!same && !options.confirmed)
|
|
537
|
+
throw new NativeGraphProcessingRequired();
|
|
538
|
+
if (!same)
|
|
539
|
+
graphRequire(typeof client.update === 'function');
|
|
540
|
+
options.progress('Data Graph processing can take seconds to many minutes, up to 30 minutes depending on objects, fields, data volume and service load.');
|
|
541
|
+
let lastDiagnostic;
|
|
542
|
+
/**
|
|
543
|
+
* Wait before every retry under the same invocation deadline.
|
|
544
|
+
* @param {string} stage Lifecycle stage/status.
|
|
545
|
+
*/
|
|
546
|
+
const pause = async (stage) => {
|
|
547
|
+
context.check();
|
|
548
|
+
const header = lastDiagnostic?.retryAfter;
|
|
549
|
+
const retry = header && /^\d+$/u.test(header)
|
|
550
|
+
? Number(header) * 1000
|
|
551
|
+
: header
|
|
552
|
+
? Date.parse(header) - options.now()
|
|
553
|
+
: 0;
|
|
554
|
+
const wait = Math.max(20_000, Number.isFinite(retry) ? retry : 0);
|
|
555
|
+
options.progress(`Data Graph ${stage}; elapsed ${Math.floor((options.now() - options.started) / 1000)}s; next wait ${Math.min(wait, options.end - options.now()) / 1000}s.`);
|
|
556
|
+
await options.pause(Math.min(wait, options.end - options.now()));
|
|
557
|
+
context.check();
|
|
558
|
+
};
|
|
559
|
+
/**
|
|
560
|
+
* Retry only explicit safe GET throttling/service failures; unknown shapes stop.
|
|
561
|
+
* @param {boolean} [beforeWrite] Revalidate local/root inputs before each fresh prewrite attempt.
|
|
562
|
+
* @returns {Promise<NativeGraphReadiness>} Fresh readiness tuple.
|
|
563
|
+
*/
|
|
564
|
+
const read = async (beforeWrite = false) => {
|
|
565
|
+
for (;;) {
|
|
566
|
+
if (beforeWrite)
|
|
567
|
+
await options.verify();
|
|
568
|
+
context.check();
|
|
569
|
+
try {
|
|
570
|
+
return await readNativeGraphReadiness(context, document, dataSpaceId);
|
|
571
|
+
}
|
|
572
|
+
catch (error) {
|
|
573
|
+
const diagnostic = getConnectDiagnostic(error);
|
|
574
|
+
if (!diagnostic || ![429, 503].includes(diagnostic.statusCode))
|
|
575
|
+
throw error;
|
|
576
|
+
lastDiagnostic = diagnostic;
|
|
577
|
+
try {
|
|
578
|
+
await pause(`GET HTTP${diagnostic.statusCode}`);
|
|
579
|
+
}
|
|
580
|
+
catch {
|
|
581
|
+
throw retainConnectDiagnostic(new Error('Native Graph deadline'), diagnostic);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
for (;;) {
|
|
587
|
+
context.check();
|
|
588
|
+
const fresh = await read(true);
|
|
589
|
+
graphRequire(fresh.observation.definitionFingerprint === baseline.definitionFingerprint);
|
|
590
|
+
if (fresh.ready) {
|
|
591
|
+
if (same)
|
|
592
|
+
return 'unchanged';
|
|
593
|
+
const payload = buildNativeDataGraphUpdateRequest(fresh.observation.target, document);
|
|
594
|
+
graphRequire(isDeepStrictEqual(payload, desired));
|
|
595
|
+
context.check();
|
|
596
|
+
const update = client.update;
|
|
597
|
+
graphRequire(typeof update === 'function');
|
|
598
|
+
// HTTP429/503 was not processed: resend the identical PUT within the deadline. Any other
|
|
599
|
+
// rejected/uncertain PUT is never replayed or reported confirmed from a later echo.
|
|
600
|
+
try {
|
|
601
|
+
await sendNativeGraphWrite(async () => update(document.definition.name, payload), 'update', options);
|
|
602
|
+
}
|
|
603
|
+
catch (error) {
|
|
604
|
+
const evidence = getConnectDiagnostic(error);
|
|
605
|
+
if (!(error instanceof NativeGraphWriteRefused) &&
|
|
606
|
+
evidence?.kind === 'api-error' &&
|
|
607
|
+
evidence.statusCode >= 400 &&
|
|
608
|
+
evidence.statusCode < 500)
|
|
609
|
+
throw retainConnectDiagnostic(new NativeGraphWriteRejected('Native Graph update rejected'), evidence);
|
|
610
|
+
throw error;
|
|
611
|
+
}
|
|
612
|
+
context.check();
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
await pause(`readiness ${fresh.status}`);
|
|
616
|
+
}
|
|
617
|
+
for (;;) {
|
|
618
|
+
await pause('settlement');
|
|
619
|
+
const settled = await read();
|
|
620
|
+
graphRequire(settled.observation.id === baseline.id &&
|
|
621
|
+
settled.observation.createdDate === baseline.createdDate);
|
|
622
|
+
if (settled.ready) {
|
|
623
|
+
const expected = { ...document, definition: desired };
|
|
624
|
+
try {
|
|
625
|
+
observeNativeDataGraph({ ...settled.observation.target, status: 'ready' }, expected);
|
|
626
|
+
}
|
|
627
|
+
catch (error) {
|
|
628
|
+
// Salesforce answered 200 but kept the old join: never report that as updated.
|
|
629
|
+
if (error instanceof DataGraphError && error.code === 'DATA_GRAPH_JOIN_CHANGE')
|
|
630
|
+
throw new DataGraphError('DATA_GRAPH_CHANGE_NOT_APPLIED');
|
|
631
|
+
throw error;
|
|
632
|
+
}
|
|
633
|
+
const actual = nativeDataGraphDocumentFromResponse(settled.observation.target).definition;
|
|
634
|
+
for (const key of Object.keys(desired).filter((key) => key !== 'sourceObject'))
|
|
635
|
+
graphRequire(isDeepStrictEqual(actual[key], desired[key]));
|
|
636
|
+
/**
|
|
637
|
+
* Match every retained binding; only new projections/nodes may gain generated names. Node
|
|
638
|
+
* labels are Salesforce's (the object's label), also on a node the PUT added.
|
|
639
|
+
* @param {Record<string, unknown>} wanted Complete dispatched node.
|
|
640
|
+
* @param {Record<string, unknown>} found Independent settled node.
|
|
641
|
+
*/
|
|
642
|
+
function preserved(wanted, found) {
|
|
643
|
+
for (const key of Object.keys(wanted).filter((key) => !['fields', 'relatedObjects', 'path', 'label'].includes(key)))
|
|
644
|
+
graphRequire(isDeepStrictEqual(wanted[key], found[key]));
|
|
645
|
+
const wantedPaths = wanted.path;
|
|
646
|
+
const foundPaths = found.path;
|
|
647
|
+
graphRequire(wantedPaths.length === foundPaths.length &&
|
|
648
|
+
// Salesforce may add fields (a key qualifier on a related object added by PUT).
|
|
649
|
+
wanted.fields.length <= found.fields.length &&
|
|
650
|
+
wanted.relatedObjects.length ===
|
|
651
|
+
found.relatedObjects.length);
|
|
652
|
+
for (const [index, path] of wantedPaths.entries())
|
|
653
|
+
for (const key of Object.keys(path))
|
|
654
|
+
graphRequire(isDeepStrictEqual(path[key], foundPaths[index][key]));
|
|
655
|
+
for (const field of wanted.fields) {
|
|
656
|
+
const match = found.fields.find((candidate) => candidate.sourceFieldName === field.sourceFieldName);
|
|
657
|
+
graphRequire(match);
|
|
658
|
+
for (const key of Object.keys(field))
|
|
659
|
+
graphRequire(isDeepStrictEqual(field[key], match[key]));
|
|
660
|
+
}
|
|
661
|
+
for (const child of wanted.relatedObjects) {
|
|
662
|
+
const match = found.relatedObjects.find((candidate) => candidate.jsonPath === child.jsonPath);
|
|
663
|
+
graphRequire(match);
|
|
664
|
+
preserved(child, match);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
preserved(graphRecord(desired.sourceObject), graphRecord(actual.sourceObject));
|
|
668
|
+
context.check();
|
|
669
|
+
return 'updated';
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Create one run-local CRD owner; receipts cannot be imported to adopt preexisting graphs.
|
|
675
|
+
* Each operation is single-use, mutually exclusive, deadline-bound and journaled before dispatch.
|
|
676
|
+
* The coordinator supplies trusted scoped readers and an exclusive durable ledger guard.
|
|
677
|
+
* @param {unknown} value Distinct native authored document.
|
|
678
|
+
* @param {NativeGraphContext} context Exact API68/data space/org read context.
|
|
679
|
+
* @param {DataGraphClient} client Single-attempt native transport.
|
|
680
|
+
* @param {object} guards Explicit intent, ledger and independent dependency safety.
|
|
681
|
+
* @param {Function} guards.record Persist predispatch intent/receipt; reject duplicate run ownership.
|
|
682
|
+
* @param {Function} guards.safeToDelete Independently verify no external usage; fail closed.
|
|
683
|
+
* @param {Function} [guards.now] Injectable monotonic wall clock.
|
|
684
|
+
* @param {Function} [guards.pause] Injectable polling delay.
|
|
685
|
+
* @param {object} [guards.creation] Invocation budget; create settlement waits while building.
|
|
686
|
+
* @param {number} guards.creation.started Invocation start time.
|
|
687
|
+
* @param {number} guards.creation.end Invocation deadline (--wait-timeout).
|
|
688
|
+
* @param {Function} guards.creation.pause Paced read-only delay.
|
|
689
|
+
* @param {Function} guards.creation.progress Stderr-only progress sink.
|
|
690
|
+
* @returns {{create: Function; delete: Function}} Internal lifecycle, no UPDATE or name-only delete.
|
|
691
|
+
*/
|
|
692
|
+
export function nativeDataGraphLifecycle(value, context, client, guards) {
|
|
693
|
+
const document = validateNativeDataGraphDocument(value);
|
|
694
|
+
const orgId = context.orgId;
|
|
695
|
+
graphRequire(/^00D[A-Za-z0-9]{12}(?:[A-Za-z0-9]{3})?$/u.test(orgId));
|
|
696
|
+
const name = document.definition.name;
|
|
697
|
+
const rootName = document.definition.primaryObjectName;
|
|
698
|
+
const bodySHA256 = createHash('sha256').update(JSON.stringify(document.definition)).digest('hex');
|
|
699
|
+
let busy = false;
|
|
700
|
+
let createUsed = false;
|
|
701
|
+
let deleteUsed = false;
|
|
702
|
+
let receipt;
|
|
703
|
+
let binding;
|
|
704
|
+
// After a create, the intent carries the node types the API assigned (see the echo).
|
|
705
|
+
let intent = document;
|
|
706
|
+
// This is intentionally run-local only. A later public integration needs its own persisted adoption policy.
|
|
707
|
+
const run = async (operation, confirmed) => {
|
|
708
|
+
if (busy ||
|
|
709
|
+
confirmed !== true ||
|
|
710
|
+
(operation === 'create' ? createUsed : deleteUsed || !receipt))
|
|
711
|
+
return { outcome: 'blocked', mutation: 'not-attempted' };
|
|
712
|
+
busy = true;
|
|
713
|
+
let attempted = false;
|
|
714
|
+
let diagnostic;
|
|
715
|
+
let expired = false;
|
|
716
|
+
let createCheck;
|
|
717
|
+
const now = guards.now ?? Date.now;
|
|
718
|
+
let previous = now();
|
|
719
|
+
const creation = operation === 'create' ? guards.creation : undefined;
|
|
720
|
+
const end = creation ? creation.end : previous + 120_000;
|
|
721
|
+
let timer;
|
|
722
|
+
const check = () => {
|
|
723
|
+
context.check();
|
|
724
|
+
const current = now();
|
|
725
|
+
graphRequire(!expired &&
|
|
726
|
+
Number.isFinite(previous) &&
|
|
727
|
+
Number.isFinite(current) &&
|
|
728
|
+
current >= previous &&
|
|
729
|
+
current < end &&
|
|
730
|
+
context.orgId === orgId);
|
|
731
|
+
previous = current;
|
|
732
|
+
};
|
|
733
|
+
const readers = { ...context, check };
|
|
734
|
+
const acquire = async () => {
|
|
735
|
+
const result = await acquireNativeDataGraph(readers, intent);
|
|
736
|
+
diagnostic = getConnectDiagnostic(result) ?? diagnostic;
|
|
737
|
+
return result;
|
|
738
|
+
};
|
|
739
|
+
const work = async () => {
|
|
740
|
+
check();
|
|
741
|
+
const root = await bindNativeGraphRoot(readers, rootName);
|
|
742
|
+
const before = await acquire();
|
|
743
|
+
check();
|
|
744
|
+
if (operation === 'create') {
|
|
745
|
+
graphRequire(before.state === 'absent');
|
|
746
|
+
const freshRoot = await bindNativeGraphRoot(readers, rootName);
|
|
747
|
+
graphRequire(isDeepStrictEqual(root, freshRoot));
|
|
748
|
+
await guards.record('create-attempt', { orgId, name, bodySHA256, rootId: root.id });
|
|
749
|
+
check();
|
|
750
|
+
createUsed = true;
|
|
751
|
+
attempted = true;
|
|
752
|
+
// HTTP429/503 was not processed: the identical POST is sent again within the deadline.
|
|
753
|
+
// Any other thrown/ambiguous write is reconciled read-only, never replayed or adopted.
|
|
754
|
+
let response;
|
|
755
|
+
createCheck = 'request';
|
|
756
|
+
try {
|
|
757
|
+
response = await sendNativeGraphWrite(async () => {
|
|
758
|
+
check();
|
|
759
|
+
return client.create(document.definition);
|
|
760
|
+
}, 'create', {
|
|
761
|
+
now,
|
|
762
|
+
end,
|
|
763
|
+
pause: creation?.pause ?? guards.pause ?? delay,
|
|
764
|
+
progress: creation?.progress ?? (() => { }),
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
catch (error) {
|
|
768
|
+
// Refused, not processed: no create happened, so no failed confirmation check either.
|
|
769
|
+
if (error instanceof NativeGraphWriteRefused)
|
|
770
|
+
return retainConnectDiagnostic({ outcome: 'unknown', mutation: 'not-attempted' }, getConnectDiagnostic(error));
|
|
771
|
+
const settled = await settleNativeDataGraph(readers, document, 'ready', guards.pause);
|
|
772
|
+
return retainConnectDiagnostic({ outcome: 'unknown', mutation: 'attempted', failedCheck: 'request' }, getConnectDiagnostic(error) ?? getConnectDiagnostic(settled));
|
|
773
|
+
}
|
|
774
|
+
createCheck = 'response';
|
|
775
|
+
check();
|
|
776
|
+
const returned = nativeGraphCreateEcho(response, document);
|
|
777
|
+
createCheck = 'settlement';
|
|
778
|
+
const settled = creation
|
|
779
|
+
? await settleNativeDataGraphCreation(readers, returned.document, { ...creation, now })
|
|
780
|
+
: await settleNativeDataGraph(readers, returned.document, 'ready', guards.pause);
|
|
781
|
+
diagnostic = getConnectDiagnostic(settled) ?? diagnostic;
|
|
782
|
+
graphRequire(settled.state === 'present');
|
|
783
|
+
createCheck = 'identity';
|
|
784
|
+
graphRequire(settled.state === 'present' &&
|
|
785
|
+
settled.observation.id === returned.id &&
|
|
786
|
+
settled.observation.createdDate === returned.createdDate &&
|
|
787
|
+
graphRecord(settled.observation.target.sourceObject).id === returned.source.id &&
|
|
788
|
+
graphRecord(settled.observation.target.sourceObject).devName ===
|
|
789
|
+
returned.source.devName &&
|
|
790
|
+
isDeepStrictEqual(graphRecord(settled.observation.target.sourceObject).fields, returned.source.fields) &&
|
|
791
|
+
isDeepStrictEqual(root, await bindNativeGraphRoot(readers, rootName)));
|
|
792
|
+
const observation = settled.observation;
|
|
793
|
+
const owned = {
|
|
794
|
+
orgId,
|
|
795
|
+
dataSpace: context.space,
|
|
796
|
+
apiName: name,
|
|
797
|
+
id: observation.id,
|
|
798
|
+
createdDate: observation.createdDate,
|
|
799
|
+
bodySHA256,
|
|
800
|
+
rootId: root.id,
|
|
801
|
+
definitionFingerprint: observation.definitionFingerprint,
|
|
802
|
+
};
|
|
803
|
+
await guards.record('created', structuredClone(owned));
|
|
804
|
+
check();
|
|
805
|
+
binding = root;
|
|
806
|
+
intent = returned.document;
|
|
807
|
+
receipt = owned;
|
|
808
|
+
return { outcome: 'created', mutation: 'attempted', receipt: structuredClone(owned) };
|
|
809
|
+
}
|
|
810
|
+
const owned = receipt;
|
|
811
|
+
graphRequire(isDeepStrictEqual(root, binding) &&
|
|
812
|
+
before.state === 'present' &&
|
|
813
|
+
before.observation.status === 'ready' &&
|
|
814
|
+
before.observation.id === owned.id &&
|
|
815
|
+
before.observation.createdDate === owned.createdDate &&
|
|
816
|
+
before.observation.definitionFingerprint === owned.definitionFingerprint &&
|
|
817
|
+
owned.bodySHA256 === bodySHA256);
|
|
818
|
+
graphRequire((await guards.safeToDelete(structuredClone(owned))) === true);
|
|
819
|
+
const fresh = await acquire();
|
|
820
|
+
graphRequire(isDeepStrictEqual(before, fresh) &&
|
|
821
|
+
isDeepStrictEqual(root, await bindNativeGraphRoot(readers, rootName)));
|
|
822
|
+
await guards.record('delete-attempt', structuredClone(owned));
|
|
823
|
+
check();
|
|
824
|
+
deleteUsed = true;
|
|
825
|
+
attempted = true;
|
|
826
|
+
try {
|
|
827
|
+
await client.delete(name);
|
|
828
|
+
}
|
|
829
|
+
catch (error) {
|
|
830
|
+
diagnostic = getConnectDiagnostic(error) ?? diagnostic;
|
|
831
|
+
/* Reconcile ambiguous acknowledgement with independent GETs only. */
|
|
832
|
+
}
|
|
833
|
+
check();
|
|
834
|
+
const settled = await settleNativeDataGraph(readers, intent, 'absent', guards.pause);
|
|
835
|
+
diagnostic = diagnostic ?? getConnectDiagnostic(settled);
|
|
836
|
+
graphRequire(settled.state === 'absent');
|
|
837
|
+
return { outcome: 'deleted', mutation: 'attempted', receipt: structuredClone(owned) };
|
|
838
|
+
};
|
|
839
|
+
try {
|
|
840
|
+
return await Promise.race([
|
|
841
|
+
work(),
|
|
842
|
+
new Promise((_resolve, reject) => {
|
|
843
|
+
timer = setTimeout(() => reject(new Error('Native Graph deadline')),
|
|
844
|
+
// Node overflows longer delays to 1ms; the guarded check still enforces the end.
|
|
845
|
+
Math.min(Math.max(end - now(), 0), 2_147_483_647));
|
|
846
|
+
}),
|
|
847
|
+
]);
|
|
848
|
+
}
|
|
849
|
+
catch (error) {
|
|
850
|
+
return retainConnectDiagnostic({
|
|
851
|
+
outcome: attempted ? 'unknown' : 'blocked',
|
|
852
|
+
mutation: attempted ? 'attempted' : 'not-attempted',
|
|
853
|
+
...(attempted && createCheck ? { failedCheck: createCheck } : {}),
|
|
854
|
+
}, getConnectDiagnostic(error) ?? diagnostic);
|
|
855
|
+
}
|
|
856
|
+
finally {
|
|
857
|
+
expired = true;
|
|
858
|
+
clearTimeout(timer);
|
|
859
|
+
busy = false;
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
return {
|
|
863
|
+
create: (confirmed) => run('create', confirmed),
|
|
864
|
+
delete: (confirmed) => run('delete', confirmed),
|
|
865
|
+
};
|
|
866
|
+
}
|