farseer-cli 1.16.0 → 1.18.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/README.md +329 -1
- package/dist/commands/code.js +104 -0
- package/dist/commands/code.js.map +7 -0
- package/dist/commands/formula.js +16 -7
- package/dist/commands/formula.js.map +2 -2
- package/dist/commands/tool.js +120 -0
- package/dist/commands/tool.js.map +7 -0
- package/dist/index.js +21 -2
- package/dist/index.js.map +2 -2
- package/dist/mcp/tools/modelTools.js +65 -23
- package/dist/mcp/tools/modelTools.js.map +3 -3
- package/dist/mcp/tools/runTools.js +6 -2
- package/dist/mcp/tools/runTools.js.map +2 -2
- package/dist/mcp/utils/launchPin.js +1 -1
- package/dist/mcp/utils/launchPin.js.map +2 -2
- package/dist/pipeline/service.js +1 -1
- package/dist/pipeline/service.js.map +2 -2
- package/dist/services/codefirst/api.js +269 -0
- package/dist/services/codefirst/api.js.map +7 -0
- package/dist/services/codefirst/binaryDisk.js +67 -0
- package/dist/services/codefirst/binaryDisk.js.map +7 -0
- package/dist/services/codefirst/byteHashes.js +129 -0
- package/dist/services/codefirst/byteHashes.js.map +7 -0
- package/dist/services/codefirst/documents.js +563 -0
- package/dist/services/codefirst/documents.js.map +7 -0
- package/dist/services/codefirst/layoutBinaryProjection.js +974 -0
- package/dist/services/codefirst/layoutBinaryProjection.js.map +7 -0
- package/dist/services/codefirst/layoutCompose.js +593 -0
- package/dist/services/codefirst/layoutCompose.js.map +7 -0
- package/dist/services/codefirst/layoutCore.js +260 -0
- package/dist/services/codefirst/layoutCore.js.map +7 -0
- package/dist/services/codefirst/layoutPaths.js +492 -0
- package/dist/services/codefirst/layoutPaths.js.map +7 -0
- package/dist/services/codefirst/layoutProjection.js +995 -0
- package/dist/services/codefirst/layoutProjection.js.map +7 -0
- package/dist/services/codefirst/layoutReadback.js +2552 -0
- package/dist/services/codefirst/layoutReadback.js.map +7 -0
- package/dist/services/codefirst/layoutReadbackPlacement.js +1517 -0
- package/dist/services/codefirst/layoutReadbackPlacement.js.map +7 -0
- package/dist/services/codefirst/layoutReceipt.js +1592 -0
- package/dist/services/codefirst/layoutReceipt.js.map +7 -0
- package/dist/services/codefirst/layoutScan.js +882 -0
- package/dist/services/codefirst/layoutScan.js.map +7 -0
- package/dist/services/codefirst/output.js +179 -0
- package/dist/services/codefirst/output.js.map +7 -0
- package/dist/services/codefirst/types.js +17 -0
- package/dist/services/codefirst/types.js.map +7 -0
- package/dist/services/codefirst/workflow.js +581 -0
- package/dist/services/codefirst/workflow.js.map +7 -0
- package/dist/services/codefirst/workspace.js +310 -0
- package/dist/services/codefirst/workspace.js.map +7 -0
- package/dist/services/codefirst/workspaceLock.js +148 -0
- package/dist/services/codefirst/workspaceLock.js.map +7 -0
- package/dist/services/codefirst/workspaceRoot.js +147 -0
- package/dist/services/codefirst/workspaceRoot.js.map +7 -0
- package/dist/utils/formulaFilters.js +48 -0
- package/dist/utils/formulaFilters.js.map +7 -0
- package/dist/utils/logger.js +9 -3
- package/dist/utils/logger.js.map +2 -2
- package/docs/cli-guide.md +13 -0
- package/package.json +3 -2
|
@@ -0,0 +1,2552 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var layoutReadback_exports = {};
|
|
20
|
+
__export(layoutReadback_exports, {
|
|
21
|
+
bindLayoutReceiptSubmission: () => bindLayoutReceiptSubmission,
|
|
22
|
+
captureLayoutSubmissionComposition: () => captureLayoutSubmissionComposition,
|
|
23
|
+
decideCheckoutEligibility: () => decideCheckoutEligibility,
|
|
24
|
+
decideReceiptEligibility: () => decideReceiptEligibility,
|
|
25
|
+
planMaterialization: () => planMaterialization,
|
|
26
|
+
planReceiptReadback: () => planReceiptReadback,
|
|
27
|
+
planReceiptRendering: () => planReceiptRendering,
|
|
28
|
+
withoutContainerSuppliedFields: () => withoutContainerSuppliedFields
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(layoutReadback_exports);
|
|
31
|
+
var import_documents = require("./documents");
|
|
32
|
+
var import_layoutBinaryProjection = require("./layoutBinaryProjection");
|
|
33
|
+
var import_layoutCore = require("./layoutCore");
|
|
34
|
+
var import_layoutPaths = require("./layoutPaths");
|
|
35
|
+
var import_layoutProjection = require("./layoutProjection");
|
|
36
|
+
var import_layoutReadbackPlacement = require("./layoutReadbackPlacement");
|
|
37
|
+
var import_farseer_client = require("farseer-client");
|
|
38
|
+
function sameBinarySelector(left, right) {
|
|
39
|
+
return (0, import_layoutCore.same)(left, right);
|
|
40
|
+
}
|
|
41
|
+
function stableSourceKey(source) {
|
|
42
|
+
return source.type === "resource" && "id" in source.resource ? `${source.resource.kind}/${source.resource.id}` : void 0;
|
|
43
|
+
}
|
|
44
|
+
function resourceEvidence(composition) {
|
|
45
|
+
return {
|
|
46
|
+
documents: composition.documents,
|
|
47
|
+
sources: composition.sources,
|
|
48
|
+
groupedObservations: composition.grouped.observations,
|
|
49
|
+
groupedNavigation: composition.grouped.navigation,
|
|
50
|
+
rawObservations: composition.binary.rawObservations,
|
|
51
|
+
ownerFields: composition.binary.ownerFields,
|
|
52
|
+
attachmentObservations: composition.binary.attachmentObservations,
|
|
53
|
+
rawNavigation: composition.rawNavigation,
|
|
54
|
+
groupedSubmissions: composition.grouped.submissions,
|
|
55
|
+
binarySubmissions: composition.binary.submissions
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function mappingDocument(composition, source) {
|
|
59
|
+
return composition.documents[source.mapping.documentIndex];
|
|
60
|
+
}
|
|
61
|
+
function mappingValue(document, mapping) {
|
|
62
|
+
if (!document) return void 0;
|
|
63
|
+
if (mapping.selector.slot === "member-set") return document.members;
|
|
64
|
+
if (mapping.selector.slot === "share-entry") {
|
|
65
|
+
const entry = { ...document };
|
|
66
|
+
delete entry.kind;
|
|
67
|
+
delete entry.owner;
|
|
68
|
+
return entry;
|
|
69
|
+
}
|
|
70
|
+
return document;
|
|
71
|
+
}
|
|
72
|
+
function indexSnapshot(snapshot) {
|
|
73
|
+
return {
|
|
74
|
+
snapshot,
|
|
75
|
+
sources: (0, import_layoutCore.groupBySource)(snapshot.sources, (source) => (0, import_layoutCore.sourceIdentityKey)(source.mapping.source)),
|
|
76
|
+
groupedObservations: (0, import_layoutCore.groupBySource)(snapshot.groupedObservations, (item) => (0, import_layoutCore.sourceIdentityKey)(item.source)),
|
|
77
|
+
rawObservations: (0, import_layoutCore.groupBySource)(snapshot.rawObservations, (item) => (0, import_layoutCore.sourceIdentityKey)(item.source)),
|
|
78
|
+
attachmentObservations: (0, import_layoutCore.groupBySource)(
|
|
79
|
+
snapshot.attachmentObservations,
|
|
80
|
+
(item) => (0, import_layoutCore.sourceIdentityKey)(item.source)
|
|
81
|
+
),
|
|
82
|
+
ownerFields: (0, import_layoutCore.groupBySource)(snapshot.ownerFields, (item) => (0, import_layoutCore.sourceIdentityKey)(item.ownerSource)),
|
|
83
|
+
groupedNavigation: (0, import_layoutCore.groupBySource)(snapshot.groupedNavigation, (item) => (0, import_layoutCore.sourceIdentityKey)(item.source)),
|
|
84
|
+
rawNavigation: (0, import_layoutCore.groupBySource)(snapshot.rawNavigation, (item) => (0, import_layoutCore.sourceIdentityKey)(item.source)),
|
|
85
|
+
groupOwners: (0, import_layoutCore.groupBySource)(
|
|
86
|
+
snapshot.groupedObservations.filter(
|
|
87
|
+
(item) => item.source.type === "group" && item.selector.slot === "share-group-owner"
|
|
88
|
+
),
|
|
89
|
+
(item) => `${item.selector.file}\0${(0, import_layoutCore.ownerKey)(item.source.owner)}`
|
|
90
|
+
)
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function groupedObservation(index, mapping) {
|
|
94
|
+
const matches = (index.groupedObservations.get((0, import_layoutCore.sourceIdentityKey)(mapping.source)) ?? []).filter(
|
|
95
|
+
(candidate) => (0, import_layoutCore.sameSelector)(candidate.selector, mapping.selector)
|
|
96
|
+
);
|
|
97
|
+
return matches.length === 1 ? matches[0] : void 0;
|
|
98
|
+
}
|
|
99
|
+
function rawObservation(index, mapping) {
|
|
100
|
+
const matches = (index.rawObservations.get((0, import_layoutCore.sourceIdentityKey)(mapping.source)) ?? []).filter(
|
|
101
|
+
(candidate) => sameBinarySelector(candidate.selector, mapping.selector)
|
|
102
|
+
);
|
|
103
|
+
return matches.length === 1 ? matches[0] : void 0;
|
|
104
|
+
}
|
|
105
|
+
function contributorsForMapping(index, source) {
|
|
106
|
+
const composition = index.snapshot;
|
|
107
|
+
const document = mappingDocument(composition, source);
|
|
108
|
+
const contributors = [];
|
|
109
|
+
if (source.codec === "grouped") {
|
|
110
|
+
const observation = groupedObservation(index, source.mapping);
|
|
111
|
+
if (observation)
|
|
112
|
+
contributors.push({
|
|
113
|
+
kind: "grouped",
|
|
114
|
+
source: (0, import_layoutCore.clone)(observation.source),
|
|
115
|
+
selector: (0, import_layoutCore.clone)(observation.selector),
|
|
116
|
+
sourcePath: (0, import_layoutCore.clone)(observation.sourcePath),
|
|
117
|
+
state: observation.state,
|
|
118
|
+
...observation.currentHash ? { currentHash: observation.currentHash } : {},
|
|
119
|
+
...observation.derivedSelectorHash ? { derivedSelectorHash: observation.derivedSelectorHash } : {},
|
|
120
|
+
...mappingValue(document, source.mapping) !== void 0 ? { value: (0, import_layoutCore.clone)(mappingValue(document, source.mapping)) } : {}
|
|
121
|
+
});
|
|
122
|
+
if (source.mapping.selector.slot === "share-entry" && source.mapping.groupOwner) {
|
|
123
|
+
const owners = index.groupOwners.get(
|
|
124
|
+
`${source.mapping.selector.file}\0${(0, import_layoutCore.ownerKey)(source.mapping.groupOwner)}`
|
|
125
|
+
) ?? [];
|
|
126
|
+
if (owners.length === 1)
|
|
127
|
+
contributors.push({
|
|
128
|
+
kind: "share-group-owner",
|
|
129
|
+
source: (0, import_layoutCore.clone)(owners[0].source),
|
|
130
|
+
selector: (0, import_layoutCore.clone)(owners[0].selector),
|
|
131
|
+
sourcePath: (0, import_layoutCore.clone)(owners[0].sourcePath),
|
|
132
|
+
state: owners[0].state,
|
|
133
|
+
...owners[0].currentHash ? { currentHash: owners[0].currentHash } : {},
|
|
134
|
+
value: (0, import_layoutCore.clone)(source.mapping.groupOwner)
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
const observation = rawObservation(index, source.mapping);
|
|
139
|
+
if (observation)
|
|
140
|
+
contributors.push({
|
|
141
|
+
kind: "raw",
|
|
142
|
+
source: (0, import_layoutCore.clone)(observation.source),
|
|
143
|
+
selector: (0, import_layoutCore.clone)(source.mapping.selector),
|
|
144
|
+
sourcePath: (0, import_layoutCore.clone)(observation.sourcePath),
|
|
145
|
+
state: observation.state,
|
|
146
|
+
...observation.currentHash ? { currentHash: observation.currentHash } : {},
|
|
147
|
+
...observation.entryHash ? { entryHash: observation.entryHash } : {},
|
|
148
|
+
...observation.byteHash ? { byteHash: observation.byteHash } : {},
|
|
149
|
+
...observation.bytes ? { bytes: observation.bytes } : {},
|
|
150
|
+
detached: source.mapping.detached,
|
|
151
|
+
nameOverride: source.mapping.nameOverride,
|
|
152
|
+
...document ? { value: (0, import_layoutCore.clone)(document) } : {}
|
|
153
|
+
});
|
|
154
|
+
for (const navigation of index.rawNavigation.get((0, import_layoutCore.sourceIdentityKey)(source.mapping.source)) ?? [])
|
|
155
|
+
contributors.push({
|
|
156
|
+
kind: "raw-navigation",
|
|
157
|
+
source: (0, import_layoutCore.clone)(navigation.source),
|
|
158
|
+
selector: (0, import_layoutCore.clone)(navigation.selector),
|
|
159
|
+
sourcePath: (0, import_layoutCore.clone)(navigation.sourcePath),
|
|
160
|
+
state: navigation.state,
|
|
161
|
+
...navigation.currentHash ? { currentHash: navigation.currentHash } : {},
|
|
162
|
+
...navigation.groupFile ? { groupFile: navigation.groupFile } : {}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
for (const navigation of index.groupedNavigation.get((0, import_layoutCore.sourceIdentityKey)(source.mapping.source)) ?? [])
|
|
166
|
+
contributors.push({
|
|
167
|
+
kind: "grouped-navigation",
|
|
168
|
+
source: (0, import_layoutCore.clone)(navigation.source),
|
|
169
|
+
selector: (0, import_layoutCore.clone)(navigation.selector),
|
|
170
|
+
sourcePath: (0, import_layoutCore.clone)(navigation.sourcePath),
|
|
171
|
+
groupFile: navigation.groupFile,
|
|
172
|
+
owner: (0, import_layoutCore.clone)(navigation.owner)
|
|
173
|
+
});
|
|
174
|
+
for (const field of index.ownerFields.get((0, import_layoutCore.sourceIdentityKey)(source.mapping.source)) ?? [])
|
|
175
|
+
contributors.push({
|
|
176
|
+
kind: "owner-field",
|
|
177
|
+
source: (0, import_layoutCore.clone)(field.ownerSource),
|
|
178
|
+
ownerFile: field.ownerFile,
|
|
179
|
+
field: field.field,
|
|
180
|
+
status: field.status,
|
|
181
|
+
...field.value !== void 0 ? { value: (0, import_layoutCore.clone)(field.value) } : {},
|
|
182
|
+
...field.bytes ? { bytes: field.bytes } : {},
|
|
183
|
+
...field.currentHash ? { currentHash: field.currentHash } : {},
|
|
184
|
+
...field.state ? { state: field.state } : {}
|
|
185
|
+
});
|
|
186
|
+
for (const observation of (index.attachmentObservations.get((0, import_layoutCore.sourceIdentityKey)(source.mapping.source)) ?? []).filter(
|
|
187
|
+
(candidate) => candidate.selector.slot === "owner-attachment"
|
|
188
|
+
))
|
|
189
|
+
contributors.push({
|
|
190
|
+
kind: "attachment",
|
|
191
|
+
source: (0, import_layoutCore.clone)(observation.source),
|
|
192
|
+
selector: (0, import_layoutCore.clone)(observation.selector),
|
|
193
|
+
sourcePath: (0, import_layoutCore.clone)(observation.sourcePath),
|
|
194
|
+
state: observation.state,
|
|
195
|
+
...observation.currentHash ? { currentHash: observation.currentHash } : {},
|
|
196
|
+
...observation.byteHash ? { byteHash: observation.byteHash } : {},
|
|
197
|
+
...observation.bytes ? { bytes: observation.bytes } : {}
|
|
198
|
+
});
|
|
199
|
+
return contributors;
|
|
200
|
+
}
|
|
201
|
+
function currentResource(index, rawSources, identity, discriminator) {
|
|
202
|
+
const sourceKey = (0, import_layoutCore.sourceIdentityKey)(identity);
|
|
203
|
+
const all = index.sources.get(sourceKey) ?? [];
|
|
204
|
+
const matches = discriminator ? all.filter(
|
|
205
|
+
(candidate) => candidate.codec === discriminator.codec && (candidate.codec === "grouped" && discriminator.codec === "grouped" ? (0, import_layoutCore.sameSelector)(candidate.mapping.selector, discriminator.mapping.selector) : candidate.codec === "raw" && discriminator.codec === "raw" && sameBinarySelector(candidate.mapping.selector, discriminator.mapping.selector))
|
|
206
|
+
) : all;
|
|
207
|
+
if (matches.length > 1) throw new Error(`Composition emitted more than one source for ${sourceKey}`);
|
|
208
|
+
if (matches.length === 1) {
|
|
209
|
+
const source = matches[0];
|
|
210
|
+
const contributors = contributorsForMapping(index, source);
|
|
211
|
+
const primary = source.codec === "grouped" ? contributors.find((candidate) => candidate.kind === "grouped") : contributors.find((candidate) => candidate.kind === "raw");
|
|
212
|
+
if (!primary || primary.state === "present-unclassified" || primary.currentHash === void 0)
|
|
213
|
+
return { contributors, presence: "unsupported" };
|
|
214
|
+
if (primary.state === "absent") return { contributors, presence: "absent" };
|
|
215
|
+
let current;
|
|
216
|
+
if (source.codec === "grouped") current = { codec: "grouped", mapping: source.mapping };
|
|
217
|
+
else {
|
|
218
|
+
const raw = (rawSources.get(sourceKey) ?? []).filter(
|
|
219
|
+
(candidate) => sameBinarySelector(candidate.selector, source.mapping.selector)
|
|
220
|
+
);
|
|
221
|
+
if (raw.length === 1) current = { codec: "raw", mapping: raw[0] };
|
|
222
|
+
}
|
|
223
|
+
if (!current) return { contributors, presence: "unsupported" };
|
|
224
|
+
return {
|
|
225
|
+
document: mappingDocument(index.snapshot, source),
|
|
226
|
+
current,
|
|
227
|
+
contributors,
|
|
228
|
+
currentHash: primary.currentHash,
|
|
229
|
+
presence: "present"
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
const absent = [];
|
|
233
|
+
for (const observation of (index.groupedObservations.get(sourceKey) ?? []).filter(
|
|
234
|
+
(candidate) => candidate.state === "absent"
|
|
235
|
+
))
|
|
236
|
+
absent.push({
|
|
237
|
+
kind: "grouped",
|
|
238
|
+
source: (0, import_layoutCore.clone)(observation.source),
|
|
239
|
+
selector: (0, import_layoutCore.clone)(observation.selector),
|
|
240
|
+
sourcePath: (0, import_layoutCore.clone)(observation.sourcePath),
|
|
241
|
+
state: observation.state,
|
|
242
|
+
...observation.derivedSelectorHash ? { derivedSelectorHash: observation.derivedSelectorHash } : {}
|
|
243
|
+
});
|
|
244
|
+
for (const observation of (index.rawObservations.get(sourceKey) ?? []).filter(
|
|
245
|
+
(candidate) => candidate.state === "absent"
|
|
246
|
+
))
|
|
247
|
+
absent.push({
|
|
248
|
+
kind: "raw",
|
|
249
|
+
source: (0, import_layoutCore.clone)(observation.source),
|
|
250
|
+
selector: (0, import_layoutCore.clone)(observation.selector),
|
|
251
|
+
sourcePath: (0, import_layoutCore.clone)(observation.sourcePath),
|
|
252
|
+
state: observation.state,
|
|
253
|
+
...observation.bytes ? { bytes: observation.bytes } : {}
|
|
254
|
+
});
|
|
255
|
+
return { contributors: absent, presence: absent.length > 0 ? "absent" : "unsupported" };
|
|
256
|
+
}
|
|
257
|
+
function resourceFingerprint(contributors) {
|
|
258
|
+
const parts = contributors.map((contributor) => {
|
|
259
|
+
const value = (0, import_layoutCore.clone)(contributor);
|
|
260
|
+
if (contributor.kind === "grouped" && contributor.selector.slot === "share-entry") delete value.sourcePath;
|
|
261
|
+
return (0, import_layoutCore.canonicalFragmentHash)(value);
|
|
262
|
+
}).sort();
|
|
263
|
+
return (0, import_layoutCore.canonicalFragmentHash)(parts);
|
|
264
|
+
}
|
|
265
|
+
function sourceMatchesKey(source, key) {
|
|
266
|
+
const expected = (0, import_layoutCore.stableSource)(key);
|
|
267
|
+
return !!expected && (0, import_layoutCore.sourceIdentityKey)(source) === (0, import_layoutCore.sourceIdentityKey)(expected);
|
|
268
|
+
}
|
|
269
|
+
function submittedResources(submission, snapshotIndex) {
|
|
270
|
+
const diagnostics = [];
|
|
271
|
+
const resources = /* @__PURE__ */ new Map();
|
|
272
|
+
const { composition } = submission;
|
|
273
|
+
for (const source of composition.sources) {
|
|
274
|
+
const index = source.mapping.documentIndex;
|
|
275
|
+
const documentKey = submission.documentKeys[index];
|
|
276
|
+
const document = mappingDocument(composition, source);
|
|
277
|
+
if (!documentKey || !document) continue;
|
|
278
|
+
if ((0, import_layoutCore.stableSource)(documentKey) && !sourceMatchesKey(source.mapping.source, documentKey)) continue;
|
|
279
|
+
if (resources.has(documentKey)) {
|
|
280
|
+
diagnostics.push(
|
|
281
|
+
(0, import_layoutCore.diagnostic)("duplicate-submission-resource", documentKey, "Submission has duplicate resource sources")
|
|
282
|
+
);
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const contributors = contributorsForMapping(snapshotIndex, source);
|
|
286
|
+
if (contributors.length === 0)
|
|
287
|
+
diagnostics.push(
|
|
288
|
+
(0, import_layoutCore.diagnostic)("unsupported-submission-evidence", documentKey, "Submission has no resource contributor")
|
|
289
|
+
);
|
|
290
|
+
resources.set(documentKey, {
|
|
291
|
+
documentKey,
|
|
292
|
+
document: (0, import_layoutCore.clone)(document),
|
|
293
|
+
source: (0, import_layoutCore.clone)(source.mapping.source),
|
|
294
|
+
submissionSource: (0, import_layoutCore.clone)(source),
|
|
295
|
+
contributors
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return { resources, diagnostics };
|
|
299
|
+
}
|
|
300
|
+
function hasSavedTransientProof(composition, source) {
|
|
301
|
+
if (source.mapping.source.type !== "transient") return true;
|
|
302
|
+
const proofs = source.codec === "grouped" ? composition.groupedSubmissions : composition.binarySubmissions;
|
|
303
|
+
if (!proofs) return false;
|
|
304
|
+
const matches = proofs.filter(
|
|
305
|
+
(proof) => (0, import_layoutCore.sourceIdentityKey)(proof.source) === (0, import_layoutCore.sourceIdentityKey)(source.mapping.source) && (0, import_layoutCore.samePath)(proof.sourcePath, source.mapping.sourcePath) && (source.codec === "grouped" ? (0, import_layoutCore.sameSelector)(
|
|
306
|
+
proof.selector,
|
|
307
|
+
source.mapping.selector
|
|
308
|
+
) : sameBinarySelector(
|
|
309
|
+
proof.selector,
|
|
310
|
+
source.mapping.selector
|
|
311
|
+
))
|
|
312
|
+
);
|
|
313
|
+
return matches.length === 1;
|
|
314
|
+
}
|
|
315
|
+
function createSourceMatches(composition, submitted, operation) {
|
|
316
|
+
const source = submitted.source;
|
|
317
|
+
if (source.type === "transient") return hasSavedTransientProof(composition, submitted.submissionSource);
|
|
318
|
+
if (source.type !== "resource" || !("key" in source.resource)) return false;
|
|
319
|
+
return source.resource.kind === submitted.document.kind && operation.kind === submitted.document.kind;
|
|
320
|
+
}
|
|
321
|
+
function topLevelCreateHandleValid(submission, operation, submitted) {
|
|
322
|
+
if (!operation.handle) return false;
|
|
323
|
+
const location = submission.handles[operation.handle];
|
|
324
|
+
if (!location || !(0, import_layoutCore.samePath)(location.path, ["id"])) return false;
|
|
325
|
+
if (operation.kind === "dimension_members" && location.kind === "dimension_table") {
|
|
326
|
+
const ownerIndex = submission.documentKeys.indexOf(`dimension_table/${operation.handle}`);
|
|
327
|
+
if (ownerIndex < 0 || ownerIndex !== location.documentIndex) return false;
|
|
328
|
+
const owner = submission.composition.sources.filter(
|
|
329
|
+
(source) => source.mapping.documentIndex === ownerIndex && source.codec === "grouped"
|
|
330
|
+
);
|
|
331
|
+
const member = submitted.submissionSource;
|
|
332
|
+
return owner.length === 1 && owner[0].codec === "grouped" && member.codec === "grouped" && owner[0].mapping.selector.slot === "table-definition" && member.mapping.selector.slot === "member-set" && owner[0].mapping.selector.file === member.mapping.selector.file;
|
|
333
|
+
}
|
|
334
|
+
return location.kind === operation.kind && location.documentIndex === submitted.submissionSource.mapping.documentIndex;
|
|
335
|
+
}
|
|
336
|
+
function locationExists(document, handle) {
|
|
337
|
+
if (!Number.isInteger(handle.documentIndex) || handle.documentIndex < 0) return false;
|
|
338
|
+
let value = document;
|
|
339
|
+
for (const part of handle.path.slice(0, -1)) {
|
|
340
|
+
if (typeof part === "number") {
|
|
341
|
+
if (!Array.isArray(value) || part < 0 || part >= value.length) return false;
|
|
342
|
+
value = value[part];
|
|
343
|
+
} else {
|
|
344
|
+
if (!value || typeof value !== "object" || Array.isArray(value) || !(part in value)) return false;
|
|
345
|
+
value = value[part];
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return handle.path.length > 0 && value !== null && typeof value === "object" && !Array.isArray(value) && handle.path[handle.path.length - 1] === "id";
|
|
349
|
+
}
|
|
350
|
+
function validateAssignments(submission, receipt) {
|
|
351
|
+
const diagnostics = [];
|
|
352
|
+
const assignments = [];
|
|
353
|
+
const locations = /* @__PURE__ */ new Set();
|
|
354
|
+
for (const [handle, location] of Object.entries(submission.handles)) {
|
|
355
|
+
const document = submission.composition.documents[location.documentIndex];
|
|
356
|
+
const locationKey = (0, import_documents.canonicalText)([location.documentIndex, location.path]);
|
|
357
|
+
if (typeof location.kind !== "string" || !document || !Array.isArray(location.path) || !locationExists(document, location))
|
|
358
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("invalid-submission-handle", handle, "Submission handle location is invalid"));
|
|
359
|
+
else if (locations.has(locationKey))
|
|
360
|
+
diagnostics.push(
|
|
361
|
+
(0, import_layoutCore.diagnostic)("duplicate-submission-handle", handle, "Submission handle location is duplicated")
|
|
362
|
+
);
|
|
363
|
+
else locations.add(locationKey);
|
|
364
|
+
}
|
|
365
|
+
for (const [handle, assignment] of Object.entries(receipt.assignments)) {
|
|
366
|
+
const location = submission.handles[handle];
|
|
367
|
+
if (!location || location.kind !== assignment.kind || !import_layoutCore.RESOURCE_ID.test(assignment.id)) {
|
|
368
|
+
diagnostics.push(
|
|
369
|
+
(0, import_layoutCore.diagnostic)("invalid-receipt-assignment", handle, "Receipt assignment does not match its handle")
|
|
370
|
+
);
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
assignments.push({ handle, assignment: (0, import_layoutCore.clone)(assignment), location: (0, import_layoutCore.clone)(location) });
|
|
374
|
+
}
|
|
375
|
+
return { assignments, diagnostics };
|
|
376
|
+
}
|
|
377
|
+
function operationOutcomeKey(operation) {
|
|
378
|
+
if (operation.key) return operation.key;
|
|
379
|
+
return operation.handle ? `${operation.kind}/${operation.handle}` : void 0;
|
|
380
|
+
}
|
|
381
|
+
function operationMatchesHandle(operationKind, handleKind) {
|
|
382
|
+
return operationKind === handleKind || operationKind === "dimension_members" && handleKind === "dimension_table";
|
|
383
|
+
}
|
|
384
|
+
function currentContainer(resource) {
|
|
385
|
+
if (!resource.current) {
|
|
386
|
+
const contributor = resource.contributors[0];
|
|
387
|
+
if (!contributor) return ".";
|
|
388
|
+
if (contributor.kind === "grouped" || contributor.kind === "share-group-owner" || contributor.kind === "grouped-navigation")
|
|
389
|
+
return contributor.selector.file;
|
|
390
|
+
if (contributor.kind === "raw" || contributor.kind === "raw-navigation") return contributor.selector.sidecar;
|
|
391
|
+
return contributor.kind === "owner-field" ? contributor.ownerFile : contributor.selector.ownerFile;
|
|
392
|
+
}
|
|
393
|
+
return resource.current.codec === "grouped" ? resource.current.mapping.selector.file : resource.current.mapping.selector.sidecar;
|
|
394
|
+
}
|
|
395
|
+
function pathInside(path, directory) {
|
|
396
|
+
return path.startsWith(`${directory}/`);
|
|
397
|
+
}
|
|
398
|
+
function hasFolderDescendants(scan, mapping, current, currentFolder, detachedDocumentProofs, detachedFileProofs) {
|
|
399
|
+
const directory = mapping.selector.file.endsWith("/_folder.json") ? (0, import_layoutPaths.folderDirectory)(mapping.selector.file) : void 0;
|
|
400
|
+
const paths = [
|
|
401
|
+
...scan.artifacts.filter((artifact) => artifact.present).map((artifact) => artifact.path),
|
|
402
|
+
...scan.tracked.map((tracked) => tracked.path),
|
|
403
|
+
...scan.localOnlyFiles,
|
|
404
|
+
...scan.opaqueDirectories
|
|
405
|
+
];
|
|
406
|
+
if (directory && paths.some((path) => path !== mapping.selector.file && pathInside(path, directory))) return true;
|
|
407
|
+
const semanticPath = currentFolder?.kind === "folder" && Array.isArray(currentFolder.path) && currentFolder.path.every((part) => typeof part === "string") ? currentFolder.path : void 0;
|
|
408
|
+
if (semanticPath) {
|
|
409
|
+
const descendant = current.documents.some((document) => {
|
|
410
|
+
if (document === currentFolder) return false;
|
|
411
|
+
const path = document.kind === "folder" && Array.isArray(document.path) ? document.path : Array.isArray(document.folder) ? document.folder : void 0;
|
|
412
|
+
if (!path || path.some((part) => typeof part !== "string") || path.length < semanticPath.length)
|
|
413
|
+
return false;
|
|
414
|
+
if (!semanticPath.every((part, index) => path[index] === part)) return false;
|
|
415
|
+
return document.kind !== "folder" || path.length > semanticPath.length;
|
|
416
|
+
});
|
|
417
|
+
if (descendant) return true;
|
|
418
|
+
}
|
|
419
|
+
return hasDetachedDescendants(mapping, directory, semanticPath, detachedDocumentProofs, detachedFileProofs);
|
|
420
|
+
}
|
|
421
|
+
function hasDetachedDescendants(mapping, directory, semanticPath, detachedDocumentProofs, detachedFileProofs) {
|
|
422
|
+
const dependentSources = /* @__PURE__ */ new Set([(0, import_layoutCore.sourceIdentityKey)(mapping.source)]);
|
|
423
|
+
const proofs = [...Object.values(detachedDocumentProofs), ...Object.values(detachedFileProofs)];
|
|
424
|
+
let changed = true;
|
|
425
|
+
while (changed) {
|
|
426
|
+
changed = false;
|
|
427
|
+
for (const proof of proofs)
|
|
428
|
+
if (proof.reason === "representation-fallback" && proof.inheritedFrom && dependentSources.has(proof.inheritedFrom.sourceKey) && !dependentSources.has(proof.sourceKey)) {
|
|
429
|
+
dependentSources.add(proof.sourceKey);
|
|
430
|
+
changed = true;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (dependentSources.size > 1) return true;
|
|
434
|
+
let semanticDirectory;
|
|
435
|
+
if (semanticPath)
|
|
436
|
+
try {
|
|
437
|
+
semanticDirectory = `tree/${semanticPath.map(import_layoutPaths.encodeLayoutSegment).join("/")}`;
|
|
438
|
+
} catch {
|
|
439
|
+
semanticDirectory = void 0;
|
|
440
|
+
}
|
|
441
|
+
return proofs.some(
|
|
442
|
+
(proof) => proof.reason === "representation-fallback" && proof.sourceKey !== (0, import_layoutCore.sourceIdentityKey)(mapping.source) && proof.request !== void 0 && [directory, semanticDirectory].some(
|
|
443
|
+
(parent) => parent !== void 0 && (proof.request.directory === parent || pathInside(proof.request.directory, parent))
|
|
444
|
+
)
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
function folderMoveAllowed(current, mapping, currentFolder) {
|
|
448
|
+
const evidence = current.context.evidence;
|
|
449
|
+
return !hasFolderDescendants(
|
|
450
|
+
current.context.scan,
|
|
451
|
+
mapping,
|
|
452
|
+
current,
|
|
453
|
+
currentFolder,
|
|
454
|
+
evidence.detachedDocumentProofs,
|
|
455
|
+
evidence.detachedFileProofs
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
function placedAsAuthoritative(current, authoritative) {
|
|
459
|
+
return current !== void 0 && (0, import_layoutCore.same)(withoutContainerSuppliedFields(current), withoutContainerSuppliedFields(authoritative));
|
|
460
|
+
}
|
|
461
|
+
function folderRouteChanged(current, authoritative) {
|
|
462
|
+
return authoritative.kind === "folder" && (!current || !(0, import_layoutCore.same)(current.path, authoritative.path));
|
|
463
|
+
}
|
|
464
|
+
function tableSiblingKey(key) {
|
|
465
|
+
if (key.startsWith("dimension_table/")) return `dimension_members/${key.slice("dimension_table/".length)}`;
|
|
466
|
+
if (key.startsWith("dimension_members/")) return `dimension_table/${key.slice("dimension_members/".length)}`;
|
|
467
|
+
return void 0;
|
|
468
|
+
}
|
|
469
|
+
function knownNull(document) {
|
|
470
|
+
if (document.kind === "remote_job" && (document.icon === null || document.icon === void 0))
|
|
471
|
+
return { field: "icon" };
|
|
472
|
+
if (document.kind === "export_configuration" && document.template === null) return { field: "template" };
|
|
473
|
+
return void 0;
|
|
474
|
+
}
|
|
475
|
+
function coherentPreservedAttachment(current, source, ownerFile, field, authoritative) {
|
|
476
|
+
const matches = current.binary.ownerFields.filter(
|
|
477
|
+
(candidate) => (0, import_layoutCore.sourceIdentityKey)(candidate.ownerSource) === (0, import_layoutCore.sourceIdentityKey)(source) && candidate.ownerFile === ownerFile && candidate.field === field
|
|
478
|
+
);
|
|
479
|
+
if (matches.length !== 1) return void 0;
|
|
480
|
+
const projection = matches[0];
|
|
481
|
+
if (projection.status === "omitted") return authoritative === void 0 ? projection : void 0;
|
|
482
|
+
if (projection.status !== "value") return void 0;
|
|
483
|
+
if (projection.value === null) return authoritative === null ? projection : void 0;
|
|
484
|
+
return (0, import_layoutCore.same)(projection.value, authoritative) ? projection : void 0;
|
|
485
|
+
}
|
|
486
|
+
function withoutContainerSuppliedFields(document) {
|
|
487
|
+
if (document.kind === "dimension_members") return (0, import_layoutCore.without)(document, ["table"]);
|
|
488
|
+
if (document.kind === "folder") return (0, import_layoutCore.without)(document, ["name"]);
|
|
489
|
+
return (0, import_layoutCore.clone)(document);
|
|
490
|
+
}
|
|
491
|
+
function renderingContext(current) {
|
|
492
|
+
return (0, import_layoutCore.clone)({
|
|
493
|
+
authored: current.context.scan,
|
|
494
|
+
grouped: current.grouped,
|
|
495
|
+
binary: current.binary,
|
|
496
|
+
byteFacts: current.context.byteFacts
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
function captureLayoutSubmissionComposition(composition) {
|
|
500
|
+
return { composition: (0, import_layoutCore.clone)(resourceEvidence(composition)) };
|
|
501
|
+
}
|
|
502
|
+
function bindLayoutReceiptSubmission(input) {
|
|
503
|
+
if (!input.planId || input.plan.planId !== void 0 && input.plan.planId !== input.planId)
|
|
504
|
+
throw new Error("Submission plan identity is invalid");
|
|
505
|
+
if (input.plan.documentKeys.length !== input.captured.composition.documents.length)
|
|
506
|
+
throw new Error("Submission document keys do not align");
|
|
507
|
+
return (0, import_layoutCore.clone)({
|
|
508
|
+
planId: input.planId,
|
|
509
|
+
documentKeys: input.plan.documentKeys,
|
|
510
|
+
operations: input.plan.operations,
|
|
511
|
+
handles: input.plan.handles,
|
|
512
|
+
composition: input.captured.composition
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
function containerIsClean(current, file) {
|
|
516
|
+
return current.grouped.observations.every(
|
|
517
|
+
(observation) => observation.selector.file !== file || observation.state === "clean"
|
|
518
|
+
) && current.binary.rawObservations.every(
|
|
519
|
+
(observation) => observation.selector.slot !== "file-entry" || observation.selector.sidecar !== file || observation.state === "clean"
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
function pathRemovals(paths, descending = false) {
|
|
523
|
+
return [...paths].map(([path, keys]) => ({ path, documentKeys: [...keys].sort(import_layoutCore.compareText) })).sort((left, right) => descending ? (0, import_layoutCore.compareText)(right.path, left.path) : (0, import_layoutCore.compareText)(left.path, right.path));
|
|
524
|
+
}
|
|
525
|
+
function planRemovals(removals, current) {
|
|
526
|
+
const json = /* @__PURE__ */ new Map();
|
|
527
|
+
const bytes = /* @__PURE__ */ new Map();
|
|
528
|
+
const directories = /* @__PURE__ */ new Map();
|
|
529
|
+
const groupEntries = [];
|
|
530
|
+
const pointers = [];
|
|
531
|
+
const keys = /* @__PURE__ */ new Set();
|
|
532
|
+
const sources = /* @__PURE__ */ new Map();
|
|
533
|
+
const removedKeys = new Set(removals.map((removal) => removal.documentKey));
|
|
534
|
+
const claim = (paths, path, documentKey) => paths.set(path, (paths.get(path) ?? /* @__PURE__ */ new Set()).add(documentKey));
|
|
535
|
+
const memberFiles = /* @__PURE__ */ new Map();
|
|
536
|
+
const presentSidecars = new Set(
|
|
537
|
+
current.context.scan.artifacts.filter((artifact) => artifact.role === "raw-metadata" && artifact.present).map((artifact) => artifact.path)
|
|
538
|
+
);
|
|
539
|
+
const removable = (contributor) => contributorIsClean(contributor) || contributor.kind === "raw" && contributor.state === "absent";
|
|
540
|
+
for (const removal of removals) {
|
|
541
|
+
keys.add(removal.documentKey);
|
|
542
|
+
sources.set((0, import_layoutCore.sourceIdentityKey)(removal.source), (0, import_layoutCore.clone)(removal.source));
|
|
543
|
+
if (removal.force !== true && !removal.contributors.every(removable)) continue;
|
|
544
|
+
for (const contributor of removal.contributors) {
|
|
545
|
+
if (contributor.kind === "grouped") {
|
|
546
|
+
if (contributor.state === "absent") continue;
|
|
547
|
+
if (contributor.selector.slot === "share-entry")
|
|
548
|
+
groupEntries.push({
|
|
549
|
+
documentKey: removal.documentKey,
|
|
550
|
+
container: contributor.selector.file,
|
|
551
|
+
entry: (0, import_layoutCore.clone)(contributor.selector)
|
|
552
|
+
});
|
|
553
|
+
else if (contributor.selector.slot === "member-set")
|
|
554
|
+
claim(memberFiles, contributor.selector.file, removal.documentKey);
|
|
555
|
+
else {
|
|
556
|
+
claim(json, contributor.selector.file, removal.documentKey);
|
|
557
|
+
if (contributor.selector.file.endsWith("/_folder.json"))
|
|
558
|
+
claim(directories, (0, import_layoutPaths.folderDirectory)(contributor.selector.file), removal.documentKey);
|
|
559
|
+
}
|
|
560
|
+
} else if (contributor.kind === "raw") {
|
|
561
|
+
if (contributor.bytes && contributor.state !== "absent")
|
|
562
|
+
claim(bytes, contributor.bytes, removal.documentKey);
|
|
563
|
+
if (presentSidecars.has(contributor.selector.sidecar))
|
|
564
|
+
claim(json, contributor.selector.sidecar, removal.documentKey);
|
|
565
|
+
} else if (contributor.kind === "attachment" && contributor.bytes && contributor.state !== "absent")
|
|
566
|
+
claim(bytes, contributor.bytes, removal.documentKey);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
for (const [file, documentKeys] of memberFiles)
|
|
570
|
+
if (json.has(file)) for (const documentKey of documentKeys) claim(json, file, documentKey);
|
|
571
|
+
for (const file of new Set(groupEntries.map((entry) => entry.container))) {
|
|
572
|
+
const entries = current.grouped.observations.filter(
|
|
573
|
+
(observation) => observation.selector.file === file && observation.selector.slot === "share-entry"
|
|
574
|
+
);
|
|
575
|
+
const removed = groupEntries.filter((entry) => entry.container === file);
|
|
576
|
+
if (entries.length > removed.length || !containerIsClean(current, file)) continue;
|
|
577
|
+
for (const entry of removed) claim(json, file, entry.documentKey);
|
|
578
|
+
for (const navigation of current.grouped.navigation.filter((route) => route.groupFile === file))
|
|
579
|
+
if (!removedKeys.has(stableSourceKey(navigation.source) ?? "") && !json.has(navigation.selector.file) && containerIsClean(current, navigation.selector.file))
|
|
580
|
+
pointers.push({
|
|
581
|
+
documentKey: removed[0].documentKey,
|
|
582
|
+
owner: navigation.selector.file,
|
|
583
|
+
groupFile: file
|
|
584
|
+
});
|
|
585
|
+
for (const navigation of current.rawNavigation.filter((route) => route.groupFile === file))
|
|
586
|
+
if (!removedKeys.has(stableSourceKey(navigation.source) ?? "") && !json.has(navigation.selector.sidecar) && containerIsClean(current, navigation.selector.sidecar))
|
|
587
|
+
pointers.push({
|
|
588
|
+
documentKey: removed[0].documentKey,
|
|
589
|
+
owner: navigation.selector.sidecar,
|
|
590
|
+
groupFile: file
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
return {
|
|
594
|
+
json: pathRemovals(json),
|
|
595
|
+
bytes: pathRemovals(bytes),
|
|
596
|
+
directories: pathRemovals(directories, true),
|
|
597
|
+
groupEntries: groupEntries.filter((entry) => !json.has(entry.container)),
|
|
598
|
+
pointers,
|
|
599
|
+
keys: [...keys].sort(import_layoutCore.compareText),
|
|
600
|
+
sources: [...sources.values()]
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
function planReceiptReadback(input) {
|
|
604
|
+
const eligibility = decideReceiptEligibility(input);
|
|
605
|
+
if (eligibility.status === "blocked") return (0, import_layoutCore.blocked)(eligibility.diagnostics);
|
|
606
|
+
return planMaterialization({ eligibility, head: input.head, current: input.current });
|
|
607
|
+
}
|
|
608
|
+
function decideReceiptEligibility(input) {
|
|
609
|
+
let head;
|
|
610
|
+
try {
|
|
611
|
+
head = (0, import_documents.headDocuments)(input.head);
|
|
612
|
+
} catch (error) {
|
|
613
|
+
return (0, import_layoutCore.blocked)([(0, import_layoutCore.diagnostic)("invalid-selected-head", ".", error.message)]);
|
|
614
|
+
}
|
|
615
|
+
const submittedIndex = indexSnapshot(input.submission.composition);
|
|
616
|
+
const currentIndex = indexSnapshot(resourceEvidence(input.current));
|
|
617
|
+
const currentRawSources = (0, import_layoutCore.groupBySource)(
|
|
618
|
+
input.current.binary.rawSources,
|
|
619
|
+
(mapping) => (0, import_layoutCore.sourceIdentityKey)(mapping.source)
|
|
620
|
+
);
|
|
621
|
+
const snapshot = submittedResources(input.submission, submittedIndex);
|
|
622
|
+
const assignmentValidation = validateAssignments(input.submission, input.receipt);
|
|
623
|
+
const diagnostics = [...snapshot.diagnostics, ...assignmentValidation.diagnostics];
|
|
624
|
+
const warnings = [];
|
|
625
|
+
const receiptResults = [];
|
|
626
|
+
const decisions = [];
|
|
627
|
+
const eligible = [];
|
|
628
|
+
const consumedOutcomes = /* @__PURE__ */ new Set();
|
|
629
|
+
const resolvedKeys = /* @__PURE__ */ new Set();
|
|
630
|
+
const submittedByResolvedKey = /* @__PURE__ */ new Map();
|
|
631
|
+
const deletedKeys = new Set(input.receipt.baseline.deleted);
|
|
632
|
+
const removedKeys = /* @__PURE__ */ new Set();
|
|
633
|
+
const removeResource = (documentKey) => {
|
|
634
|
+
if (removedKeys.has(documentKey)) return;
|
|
635
|
+
const source = (0, import_layoutCore.stableSource)(documentKey);
|
|
636
|
+
if (!source) {
|
|
637
|
+
warnings.push(
|
|
638
|
+
(0, import_layoutCore.diagnostic)("unsupported-removal-key", documentKey, "Deleted key has no stable resource identity")
|
|
639
|
+
);
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
removedKeys.add(documentKey);
|
|
643
|
+
const resource = currentResource(currentIndex, currentRawSources, source);
|
|
644
|
+
decisions.push({
|
|
645
|
+
mode: "remove",
|
|
646
|
+
documentKey,
|
|
647
|
+
source: (0, import_layoutCore.clone)(source),
|
|
648
|
+
contributors: (0, import_layoutCore.clone)(resource.contributors),
|
|
649
|
+
...resource.current ? { current: (0, import_layoutCore.clone)(resource.current) } : {},
|
|
650
|
+
...resource.document ? { document: (0, import_layoutCore.clone)(resource.document) } : {}
|
|
651
|
+
});
|
|
652
|
+
};
|
|
653
|
+
for (const operation of input.submission.operations) {
|
|
654
|
+
const outcomeKey = operationOutcomeKey(operation);
|
|
655
|
+
if (!outcomeKey) {
|
|
656
|
+
diagnostics.push(
|
|
657
|
+
(0, import_layoutCore.diagnostic)("invalid-submission-operation", operation.name, "Operation has no receipt identity")
|
|
658
|
+
);
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
if (consumedOutcomes.has(outcomeKey)) {
|
|
662
|
+
diagnostics.push(
|
|
663
|
+
(0, import_layoutCore.diagnostic)("duplicate-submission-operation", outcomeKey, "Operation receipt identity is duplicated")
|
|
664
|
+
);
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
667
|
+
consumedOutcomes.add(outcomeKey);
|
|
668
|
+
if (operation.handle && (!input.submission.handles[operation.handle] || !operationMatchesHandle(operation.kind, input.submission.handles[operation.handle].kind)))
|
|
669
|
+
diagnostics.push(
|
|
670
|
+
(0, import_layoutCore.diagnostic)(
|
|
671
|
+
"invalid-submission-operation",
|
|
672
|
+
outcomeKey,
|
|
673
|
+
"Create operation does not match its stored handle"
|
|
674
|
+
)
|
|
675
|
+
);
|
|
676
|
+
const outcome = input.receipt.outcomes[outcomeKey];
|
|
677
|
+
const assignment = operation.handle ? input.receipt.assignments[operation.handle] : void 0;
|
|
678
|
+
if (assignment && !operationMatchesHandle(operation.kind, assignment.kind))
|
|
679
|
+
diagnostics.push(
|
|
680
|
+
(0, import_layoutCore.diagnostic)(
|
|
681
|
+
"invalid-receipt-assignment",
|
|
682
|
+
outcomeKey,
|
|
683
|
+
"Receipt assignment kind does not match the create operation"
|
|
684
|
+
)
|
|
685
|
+
);
|
|
686
|
+
if (operation.handle && outcome && (outcome.outcome === import_farseer_client.OperationOutcomeOutcomeEnum.Applied || outcome.outcome === import_farseer_client.OperationOutcomeOutcomeEnum.Unchanged) && !assignment)
|
|
687
|
+
diagnostics.push(
|
|
688
|
+
(0, import_layoutCore.diagnostic)("missing-receipt-assignment", outcomeKey, "Successful create outcome has no assignment")
|
|
689
|
+
);
|
|
690
|
+
const documentKey = operation.key ?? (assignment ? `${operation.kind}/${assignment.id}` : void 0);
|
|
691
|
+
if (documentKey && resolvedKeys.has(documentKey)) {
|
|
692
|
+
diagnostics.push(
|
|
693
|
+
(0, import_layoutCore.diagnostic)("duplicate-resolved-receipt-key", documentKey, "Receipt operations resolve to one resource")
|
|
694
|
+
);
|
|
695
|
+
continue;
|
|
696
|
+
}
|
|
697
|
+
if (documentKey) resolvedKeys.add(documentKey);
|
|
698
|
+
const receiptHash = documentKey ? input.receipt.baseline.set[documentKey] : void 0;
|
|
699
|
+
const headHash = documentKey ? input.head.manifest[documentKey] : void 0;
|
|
700
|
+
const base = {
|
|
701
|
+
outcomeKey,
|
|
702
|
+
...documentKey ? { documentKey } : {},
|
|
703
|
+
...operation.handle ? { handle: operation.handle } : {},
|
|
704
|
+
...assignment ? { assignment: (0, import_layoutCore.clone)(assignment) } : {},
|
|
705
|
+
...outcome ? { outcome: outcome.outcome } : {},
|
|
706
|
+
...receiptHash ? { receiptHash } : {},
|
|
707
|
+
...headHash ? { headHash } : {}
|
|
708
|
+
};
|
|
709
|
+
let classification;
|
|
710
|
+
if (!outcome) classification = "missing-outcome";
|
|
711
|
+
else if (outcome.outcome === import_farseer_client.OperationOutcomeOutcomeEnum.Failed) classification = "failed";
|
|
712
|
+
else if (outcome.outcome === import_farseer_client.OperationOutcomeOutcomeEnum.Blocked || outcome.outcome === import_farseer_client.OperationOutcomeOutcomeEnum.Skipped)
|
|
713
|
+
classification = "blocked";
|
|
714
|
+
else if (operation.action === import_farseer_client.PlanOperationActionEnum.Delete)
|
|
715
|
+
classification = documentKey && deletedKeys.has(documentKey) ? "eligible-removal" : "delete-deferred";
|
|
716
|
+
else if (!receiptHash) classification = "no-receipt-baseline";
|
|
717
|
+
else if (!headHash) classification = "missing-head-document";
|
|
718
|
+
else if (receiptHash !== headHash) classification = "head-hash-mismatch";
|
|
719
|
+
else classification = operation.handle ? "eligible-assigned-create" : "eligible-stable-set";
|
|
720
|
+
const result = { ...base, classification };
|
|
721
|
+
receiptResults.push(result);
|
|
722
|
+
if (operation.action === import_farseer_client.PlanOperationActionEnum.Delete) {
|
|
723
|
+
if (documentKey && classification === "eligible-removal") removeResource(documentKey);
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
if (!documentKey) continue;
|
|
727
|
+
const create = operation.handle !== void 0;
|
|
728
|
+
const submitted = snapshot.resources.get(create ? outcomeKey : documentKey);
|
|
729
|
+
if (create && submitted) submittedByResolvedKey.set(documentKey, submitted);
|
|
730
|
+
if (create && classification !== "eligible-assigned-create") continue;
|
|
731
|
+
if (!submitted) {
|
|
732
|
+
if (create) result.classification = "assigned-create-deferred";
|
|
733
|
+
else if (operation.action === import_farseer_client.PlanOperationActionEnum.Noop) continue;
|
|
734
|
+
else
|
|
735
|
+
diagnostics.push(
|
|
736
|
+
(0, import_layoutCore.diagnostic)(
|
|
737
|
+
"unsupported-submission-evidence",
|
|
738
|
+
documentKey,
|
|
739
|
+
"Stable operation has no retained resource snapshot"
|
|
740
|
+
)
|
|
741
|
+
);
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
if (create && !topLevelCreateHandleValid(input.submission, operation, submitted)) {
|
|
745
|
+
diagnostics.push(
|
|
746
|
+
(0, import_layoutCore.diagnostic)("invalid-submission-operation", outcomeKey, "Create operation handle is not top-level")
|
|
747
|
+
);
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
if (create && !createSourceMatches(input.submission.composition, submitted, operation)) {
|
|
751
|
+
result.classification = "assigned-create-deferred";
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
const current = currentResource(
|
|
755
|
+
currentIndex,
|
|
756
|
+
currentRawSources,
|
|
757
|
+
submitted.source,
|
|
758
|
+
create ? submitted.submissionSource : void 0
|
|
759
|
+
);
|
|
760
|
+
const preserve = (reason) => {
|
|
761
|
+
decisions.push({
|
|
762
|
+
mode: "preserve",
|
|
763
|
+
documentKey,
|
|
764
|
+
source: (0, import_layoutCore.clone)(submitted.source),
|
|
765
|
+
...current.document ? { document: (0, import_layoutCore.clone)(current.document) } : {},
|
|
766
|
+
contributors: (0, import_layoutCore.clone)(current.contributors),
|
|
767
|
+
reason
|
|
768
|
+
});
|
|
769
|
+
};
|
|
770
|
+
if (current.presence === "unsupported") {
|
|
771
|
+
if (create) result.classification = "assigned-create-deferred";
|
|
772
|
+
else preserve("unsupported-evidence");
|
|
773
|
+
continue;
|
|
774
|
+
}
|
|
775
|
+
if (current.presence === "absent") {
|
|
776
|
+
if (create) result.classification = "assigned-create-deferred";
|
|
777
|
+
else decisions.push({ mode: "keep-absent", documentKey, contributors: (0, import_layoutCore.clone)(current.contributors) });
|
|
778
|
+
continue;
|
|
779
|
+
}
|
|
780
|
+
if (classification !== "eligible-stable-set" && classification !== "eligible-assigned-create") {
|
|
781
|
+
preserve(
|
|
782
|
+
classification === "failed" || classification === "blocked" ? "unsuccessful-outcome" : classification === "missing-outcome" ? "missing-outcome" : "deferred-receipt"
|
|
783
|
+
);
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
if (resourceFingerprint(submitted.contributors) !== resourceFingerprint(current.contributors)) {
|
|
787
|
+
if (create) result.classification = "assigned-create-deferred";
|
|
788
|
+
else preserve("post-submission-change");
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
const authoritative = head.get(documentKey);
|
|
792
|
+
const hash = current.currentHash;
|
|
793
|
+
if (!authoritative || !current.current || !hash) {
|
|
794
|
+
preserve("unsupported-evidence");
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
if (authoritative.kind === "folder" && current.current.codec === "grouped" && folderRouteChanged(current.document, authoritative) && !folderMoveAllowed(input.current, current.current.mapping, current.document)) {
|
|
798
|
+
preserve("protected-descendant");
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
eligible.push({
|
|
802
|
+
result,
|
|
803
|
+
submitted,
|
|
804
|
+
current: { ...current, current: current.current, currentHash: hash, presence: "present" },
|
|
805
|
+
authoritative
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
for (const [outcomeKey, outcome] of Object.entries(input.receipt.outcomes)) {
|
|
809
|
+
if (consumedOutcomes.has(outcomeKey)) continue;
|
|
810
|
+
if (!outcomeKey.startsWith("share/")) {
|
|
811
|
+
warnings.push(
|
|
812
|
+
(0, import_layoutCore.diagnostic)("unmatched-receipt-outcome", outcomeKey, "Receipt outcome has no stored operation")
|
|
813
|
+
);
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
const receiptHash = input.receipt.baseline.set[outcomeKey];
|
|
817
|
+
const headHash = input.head.manifest[outcomeKey];
|
|
818
|
+
receiptResults.push({
|
|
819
|
+
outcomeKey,
|
|
820
|
+
documentKey: outcomeKey,
|
|
821
|
+
outcome: outcome.outcome,
|
|
822
|
+
...receiptHash ? { receiptHash } : {},
|
|
823
|
+
...headHash ? { headHash } : {},
|
|
824
|
+
classification: outcome.outcome === import_farseer_client.OperationOutcomeOutcomeEnum.Failed ? "failed" : outcome.outcome === import_farseer_client.OperationOutcomeOutcomeEnum.Blocked ? "blocked" : "generated-share-deferred"
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
for (const documentKey of [...deletedKeys].sort(import_layoutCore.compareText)) removeResource(documentKey);
|
|
828
|
+
const knownBaselineKeys = /* @__PURE__ */ new Set([
|
|
829
|
+
...receiptResults.flatMap((result) => result.documentKey ? [result.documentKey] : [])
|
|
830
|
+
]);
|
|
831
|
+
for (const key of Object.keys(input.receipt.baseline.set).sort(import_layoutCore.compareText)) {
|
|
832
|
+
if (knownBaselineKeys.has(key)) continue;
|
|
833
|
+
if (key.startsWith("share/")) {
|
|
834
|
+
knownBaselineKeys.add(key);
|
|
835
|
+
receiptResults.push({
|
|
836
|
+
outcomeKey: key,
|
|
837
|
+
documentKey: key,
|
|
838
|
+
receiptHash: input.receipt.baseline.set[key],
|
|
839
|
+
...input.head.manifest[key] ? { headHash: input.head.manifest[key] } : {},
|
|
840
|
+
classification: "generated-share-deferred"
|
|
841
|
+
});
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
warnings.push(
|
|
845
|
+
(0, import_layoutCore.diagnostic)("unmatched-receipt-baseline", key, "Receipt baseline has no operation or generated share")
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
if (diagnostics.length > 0) return (0, import_layoutCore.blocked)(diagnostics);
|
|
849
|
+
for (const result of receiptResults.filter((item) => item.classification === "generated-share-deferred")) {
|
|
850
|
+
const documentKey = result.documentKey;
|
|
851
|
+
const source = (0, import_layoutCore.stableSource)(documentKey);
|
|
852
|
+
if (!source) {
|
|
853
|
+
warnings.push(
|
|
854
|
+
(0, import_layoutCore.diagnostic)("unsupported-generated-share", documentKey, "Generated share key has no stable identity")
|
|
855
|
+
);
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
const resource = currentResource(currentIndex, currentRawSources, source);
|
|
859
|
+
const authoritative = head.get(documentKey);
|
|
860
|
+
const preserveGenerated = (reason) => {
|
|
861
|
+
decisions.push({
|
|
862
|
+
mode: "preserve",
|
|
863
|
+
documentKey,
|
|
864
|
+
source: (0, import_layoutCore.clone)(source),
|
|
865
|
+
...resource.document ? { document: (0, import_layoutCore.clone)(resource.document) } : {},
|
|
866
|
+
contributors: (0, import_layoutCore.clone)(resource.contributors),
|
|
867
|
+
reason
|
|
868
|
+
});
|
|
869
|
+
};
|
|
870
|
+
if (!authoritative || !result.receiptHash || result.receiptHash !== result.headHash) {
|
|
871
|
+
preserveGenerated("pull-needed");
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
if (resource.presence === "present") {
|
|
875
|
+
if (resource.contributors.every(contributorIsClean) && (0, import_layoutCore.same)(resource.document, authoritative))
|
|
876
|
+
decisions.push({ mode: "noop", documentKey });
|
|
877
|
+
else preserveGenerated("kept-local");
|
|
878
|
+
continue;
|
|
879
|
+
}
|
|
880
|
+
if (resource.presence === "absent" || ownerGroupDeleted(input.current, authoritative)) {
|
|
881
|
+
preserveGenerated("local-deletion-pending");
|
|
882
|
+
continue;
|
|
883
|
+
}
|
|
884
|
+
decisions.push({
|
|
885
|
+
mode: "set",
|
|
886
|
+
documentKey,
|
|
887
|
+
placement: "insert-authoritative",
|
|
888
|
+
document: (0, import_layoutCore.clone)(authoritative),
|
|
889
|
+
source: (0, import_layoutCore.clone)(source),
|
|
890
|
+
contributors: (0, import_layoutCore.clone)(resource.contributors)
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
if (diagnostics.length > 0) return (0, import_layoutCore.blocked)(diagnostics);
|
|
894
|
+
const suppressed = /* @__PURE__ */ new Set();
|
|
895
|
+
for (const candidate of eligible) {
|
|
896
|
+
const siblingKey = tableSiblingKey(candidate.result.documentKey);
|
|
897
|
+
if (!siblingKey) continue;
|
|
898
|
+
const decidedAbsent = decisions.find(
|
|
899
|
+
(decision) => decision.mode === "keep-absent" && decision.documentKey === siblingKey
|
|
900
|
+
);
|
|
901
|
+
const siblingSubmittedKey = tableSiblingKey(candidate.result.outcomeKey);
|
|
902
|
+
const submittedSibling = submittedByResolvedKey.get(siblingKey) ?? snapshot.resources.get(siblingKey) ?? (siblingSubmittedKey ? snapshot.resources.get(siblingSubmittedKey) : void 0);
|
|
903
|
+
const currentSibling = submittedSibling ? currentResource(
|
|
904
|
+
currentIndex,
|
|
905
|
+
currentRawSources,
|
|
906
|
+
submittedSibling.source,
|
|
907
|
+
submittedSibling.submissionSource
|
|
908
|
+
) : void 0;
|
|
909
|
+
if (!decidedAbsent && currentSibling?.presence !== "absent") {
|
|
910
|
+
if (!submittedSibling || currentSibling?.presence === "unsupported")
|
|
911
|
+
diagnostics.push(
|
|
912
|
+
(0, import_layoutCore.diagnostic)(
|
|
913
|
+
"unsupported-table-sibling",
|
|
914
|
+
siblingKey,
|
|
915
|
+
"Table sibling evidence is missing or ambiguous"
|
|
916
|
+
)
|
|
917
|
+
);
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
if (!decidedAbsent && currentSibling?.presence === "absent")
|
|
921
|
+
decisions.push({
|
|
922
|
+
mode: "keep-absent",
|
|
923
|
+
documentKey: siblingKey,
|
|
924
|
+
contributors: (0, import_layoutCore.clone)(currentSibling.contributors)
|
|
925
|
+
});
|
|
926
|
+
suppressed.add(candidate.result.documentKey);
|
|
927
|
+
decisions.push({
|
|
928
|
+
mode: "suppressed",
|
|
929
|
+
documentKey: candidate.result.documentKey,
|
|
930
|
+
container: currentContainer(candidate.current),
|
|
931
|
+
reason: "intentional-sibling-absence"
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
if (diagnostics.length > 0) return (0, import_layoutCore.blocked)(diagnostics);
|
|
935
|
+
for (const candidate of eligible) {
|
|
936
|
+
const documentKey = candidate.result.documentKey;
|
|
937
|
+
if (suppressed.has(documentKey)) continue;
|
|
938
|
+
decisions.push({
|
|
939
|
+
mode: "set",
|
|
940
|
+
documentKey,
|
|
941
|
+
placement: "replace-proven",
|
|
942
|
+
outcomeKey: candidate.result.outcomeKey,
|
|
943
|
+
document: (0, import_layoutCore.clone)(candidate.authoritative),
|
|
944
|
+
source: (0, import_layoutCore.clone)(candidate.submitted.source),
|
|
945
|
+
current: (0, import_layoutCore.clone)(candidate.current.current),
|
|
946
|
+
expectedCurrentHash: candidate.current.currentHash,
|
|
947
|
+
...candidate.current.document ? { currentDocument: (0, import_layoutCore.clone)(candidate.current.document) } : {},
|
|
948
|
+
contributors: (0, import_layoutCore.clone)(candidate.current.contributors)
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
const rewrittenIndexes = new Set(
|
|
952
|
+
eligible.filter((candidate) => !suppressed.has(candidate.result.documentKey)).map((candidate) => candidate.submitted.submissionSource.mapping.documentIndex)
|
|
953
|
+
);
|
|
954
|
+
const assignmentsByIndex = (0, import_layoutCore.groupBySource)(
|
|
955
|
+
assignmentValidation.assignments,
|
|
956
|
+
(item) => String(item.location.documentIndex)
|
|
957
|
+
);
|
|
958
|
+
const identityPatches = [];
|
|
959
|
+
for (const [index, list] of [...assignmentsByIndex].sort((left, right) => (0, import_layoutCore.compareText)(left[0], right[0]))) {
|
|
960
|
+
const documentIndex = Number(index);
|
|
961
|
+
if (rewrittenIndexes.has(documentIndex)) continue;
|
|
962
|
+
const document = input.submission.composition.documents[documentIndex];
|
|
963
|
+
const sources = input.submission.composition.sources.filter(
|
|
964
|
+
(candidate) => candidate.mapping.documentIndex === documentIndex && (candidate.codec === "raw" || candidate.mapping.selector.slot !== "member-set")
|
|
965
|
+
);
|
|
966
|
+
if (!document || sources.length !== 1) continue;
|
|
967
|
+
const source = sources[0];
|
|
968
|
+
const submittedKey = input.submission.documentKeys[documentIndex];
|
|
969
|
+
const resolved = receiptResults.filter((item) => item.outcomeKey === submittedKey && item.documentKey);
|
|
970
|
+
identityPatches.push({
|
|
971
|
+
documentKey: resolved.length === 1 ? resolved[0].documentKey : submittedKey,
|
|
972
|
+
source: (0, import_layoutCore.clone)(source.mapping.source),
|
|
973
|
+
...source.codec === "grouped" ? {
|
|
974
|
+
container: source.mapping.selector.file,
|
|
975
|
+
slot: source.mapping.selector.slot,
|
|
976
|
+
...source.mapping.selector.slot === "share-entry" ? { shareIdentity: (0, import_layoutCore.clone)(source.mapping.selector.identity) } : {}
|
|
977
|
+
} : { container: source.mapping.selector.sidecar, slot: "file-entry" },
|
|
978
|
+
submittedDocument: (0, import_layoutCore.clone)(document),
|
|
979
|
+
assignments: (0, import_layoutCore.clone)(list)
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
return {
|
|
983
|
+
status: "complete",
|
|
984
|
+
decisions,
|
|
985
|
+
receiptResults,
|
|
986
|
+
assignments: assignmentValidation.assignments,
|
|
987
|
+
headDocuments: head,
|
|
988
|
+
submittedSources: Object.fromEntries(
|
|
989
|
+
[...submittedByResolvedKey].map(([key, item]) => [key, (0, import_layoutCore.clone)(item.source)])
|
|
990
|
+
),
|
|
991
|
+
identityPatches,
|
|
992
|
+
submissionProofs: {
|
|
993
|
+
grouped: (0, import_layoutCore.clone)(input.submission.composition.groupedSubmissions ?? []),
|
|
994
|
+
binary: (0, import_layoutCore.clone)(input.submission.composition.binarySubmissions ?? [])
|
|
995
|
+
},
|
|
996
|
+
warnings
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
function planMaterialization(input) {
|
|
1000
|
+
const { eligibility } = input;
|
|
1001
|
+
const removals = eligibility.decisions.flatMap((decision) => decision.mode === "remove" ? [decision] : []);
|
|
1002
|
+
const deletes = planRemovals(removals, input.current);
|
|
1003
|
+
const setDecisions = eligibility.decisions.flatMap((decision) => decision.mode === "set" ? [decision] : []);
|
|
1004
|
+
const setByKey = new Map(setDecisions.map((decision) => [decision.documentKey, decision]));
|
|
1005
|
+
const demoted = /* @__PURE__ */ new Map();
|
|
1006
|
+
for (; ; ) {
|
|
1007
|
+
const result = planMaterializationAttempt(input, { setDecisions, setByKey, deletes, demoted });
|
|
1008
|
+
if (result.status !== "retry") return result;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
function planMaterializationAttempt(input, attempt) {
|
|
1012
|
+
const { eligibility } = input;
|
|
1013
|
+
const { setByKey, deletes, demoted } = attempt;
|
|
1014
|
+
const head = eligibility.headDocuments;
|
|
1015
|
+
const currentIndex = indexSnapshot(resourceEvidence(input.current));
|
|
1016
|
+
const receiptResults = eligibility.receiptResults;
|
|
1017
|
+
const assignments = eligibility.assignments;
|
|
1018
|
+
const diagnostics = [];
|
|
1019
|
+
const perResource = [];
|
|
1020
|
+
const retry = { status: "retry" };
|
|
1021
|
+
const fail = (key, item) => failMany(key === void 0 ? [] : [key], item);
|
|
1022
|
+
const failMany = (keys, item) => {
|
|
1023
|
+
const pending = keys.filter((key) => setByKey.has(key) && !demoted.has(key));
|
|
1024
|
+
if (pending.length === 0) return (0, import_layoutCore.blocked)([item]);
|
|
1025
|
+
for (const key of pending) demoted.set(key, { reason: "unmaterialized", diagnostic: item });
|
|
1026
|
+
return retry;
|
|
1027
|
+
};
|
|
1028
|
+
const materializations = eligibility.decisions.flatMap(
|
|
1029
|
+
(decision) => decision.mode === "preserve" || decision.mode === "keep-absent" || decision.mode === "suppressed" ? [(0, import_layoutCore.clone)(decision)] : []
|
|
1030
|
+
);
|
|
1031
|
+
const preserveDemoted = (documentKey, reason) => {
|
|
1032
|
+
const candidate = setByKey.get(documentKey);
|
|
1033
|
+
materializations.push({
|
|
1034
|
+
mode: "preserve",
|
|
1035
|
+
documentKey,
|
|
1036
|
+
source: (0, import_layoutCore.clone)(candidate.source),
|
|
1037
|
+
...candidate.currentDocument ? { document: (0, import_layoutCore.clone)(candidate.currentDocument) } : {},
|
|
1038
|
+
contributors: (0, import_layoutCore.clone)(candidate.contributors),
|
|
1039
|
+
reason
|
|
1040
|
+
});
|
|
1041
|
+
};
|
|
1042
|
+
for (const [documentKey, entry] of demoted) preserveDemoted(documentKey, entry.reason);
|
|
1043
|
+
const placed = attempt.setDecisions.filter((decision) => !demoted.has(decision.documentKey));
|
|
1044
|
+
const finish = (extra = {}) => ({
|
|
1045
|
+
status: "complete",
|
|
1046
|
+
receiptResults,
|
|
1047
|
+
assignments,
|
|
1048
|
+
materializations,
|
|
1049
|
+
deletes,
|
|
1050
|
+
identityPatches: (0, import_layoutCore.clone)(eligibility.identityPatches ?? []),
|
|
1051
|
+
groupedPatches: [],
|
|
1052
|
+
groupedCompanions: [],
|
|
1053
|
+
rawPatches: [],
|
|
1054
|
+
attachments: [],
|
|
1055
|
+
pendingDamage: [],
|
|
1056
|
+
derivedSourceProofs: [],
|
|
1057
|
+
unmaterializedDiagnostics: [
|
|
1058
|
+
...eligibility.warnings ?? [],
|
|
1059
|
+
...[...demoted.values()].flatMap((entry) => entry.diagnostic ? [entry.diagnostic] : [])
|
|
1060
|
+
],
|
|
1061
|
+
notices: (0, import_layoutCore.clone)(eligibility.notices ?? []),
|
|
1062
|
+
renderingContext: renderingContext(input.current),
|
|
1063
|
+
submissionProofs: (0, import_layoutCore.clone)(eligibility.submissionProofs?.grouped ?? []),
|
|
1064
|
+
...extra
|
|
1065
|
+
});
|
|
1066
|
+
const checkpoint = () => {
|
|
1067
|
+
const pending = perResource.filter((item) => setByKey.has(item.key) && !demoted.has(item.key));
|
|
1068
|
+
for (const item of pending) demoted.set(item.key, { reason: "unmaterialized", diagnostic: item.diagnostic });
|
|
1069
|
+
if (pending.length > 0) return retry;
|
|
1070
|
+
const rest = [...diagnostics, ...perResource.flatMap((item) => item.diagnostic ? [item.diagnostic] : [])];
|
|
1071
|
+
return rest.length > 0 ? (0, import_layoutCore.blocked)(rest) : void 0;
|
|
1072
|
+
};
|
|
1073
|
+
const placementFailed = (items) => {
|
|
1074
|
+
for (const item of items)
|
|
1075
|
+
if (setByKey.has(item.path)) perResource.push({ key: item.path, diagnostic: item });
|
|
1076
|
+
else diagnostics.push(item);
|
|
1077
|
+
return checkpoint() ?? (0, import_layoutCore.blocked)(items);
|
|
1078
|
+
};
|
|
1079
|
+
if (placed.length === 0) return finish();
|
|
1080
|
+
const place = (candidates) => {
|
|
1081
|
+
const documents = candidates.map((candidate) => candidate.document);
|
|
1082
|
+
const documentKeys = candidates.map((candidate) => candidate.documentKey);
|
|
1083
|
+
const intents = candidates.map(
|
|
1084
|
+
(candidate, documentIndex) => candidate.placement === "replace-proven" ? {
|
|
1085
|
+
mode: "replace-proven",
|
|
1086
|
+
documentIndex,
|
|
1087
|
+
documentKey: candidate.documentKey,
|
|
1088
|
+
current: candidate.current,
|
|
1089
|
+
expectedCurrentHash: candidate.expectedCurrentHash
|
|
1090
|
+
} : {
|
|
1091
|
+
mode: "insert-authoritative",
|
|
1092
|
+
documentIndex,
|
|
1093
|
+
documentKey: candidate.documentKey,
|
|
1094
|
+
...candidate.reinsert ? { reinsert: true } : {}
|
|
1095
|
+
}
|
|
1096
|
+
);
|
|
1097
|
+
return (0, import_layoutReadbackPlacement.planAuthoritativePlacement)({
|
|
1098
|
+
placement: input.current.context.placement,
|
|
1099
|
+
scan: input.current.context.scan,
|
|
1100
|
+
detachedDocumentProofs: input.current.context.evidence.detachedDocumentProofs,
|
|
1101
|
+
detachedFileProofs: input.current.context.evidence.detachedFileProofs,
|
|
1102
|
+
canonicalBaseline: input.current.context.evidence.canonicalBaseline,
|
|
1103
|
+
groupedRoutes: input.current.context.evidence.groupedRoutes,
|
|
1104
|
+
...input.head ? { head: input.head, headDocuments: head } : {},
|
|
1105
|
+
documents,
|
|
1106
|
+
documentKeys,
|
|
1107
|
+
intents,
|
|
1108
|
+
currentGrouped: input.current.grouped,
|
|
1109
|
+
currentBinary: input.current.binary
|
|
1110
|
+
});
|
|
1111
|
+
};
|
|
1112
|
+
const placement = place(placed);
|
|
1113
|
+
if (placement.status === "blocked") return placementFailed(placement.diagnostics);
|
|
1114
|
+
const placedKeys = new Set(placed.map((candidate) => candidate.documentKey));
|
|
1115
|
+
const decidedSourceByKey = new Map(
|
|
1116
|
+
eligibility.decisions.flatMap(
|
|
1117
|
+
(decision) => (decision.mode === "set" || decision.mode === "remove" || decision.mode === "preserve") && decision.documentKey ? [[decision.documentKey, decision.source]] : []
|
|
1118
|
+
)
|
|
1119
|
+
);
|
|
1120
|
+
const relocationDeferred = /* @__PURE__ */ new Set();
|
|
1121
|
+
for (const source of placement.groupedSources) {
|
|
1122
|
+
const siblingKey = tableSiblingKey(source.documentKey);
|
|
1123
|
+
if (!siblingKey || placedKeys.has(siblingKey)) continue;
|
|
1124
|
+
const siblingSource = decidedSourceByKey.get(siblingKey) ?? eligibility.submittedSources?.[siblingKey] ?? (0, import_layoutCore.stableSource)(siblingKey);
|
|
1125
|
+
let siblingMappings = siblingSource ? input.current.grouped.sources.filter(
|
|
1126
|
+
(candidate) => (0, import_layoutCore.sourceIdentityKey)(candidate.source) === (0, import_layoutCore.sourceIdentityKey)(siblingSource)
|
|
1127
|
+
) : [];
|
|
1128
|
+
const currentSource = setByKey.get(source.documentKey)?.current;
|
|
1129
|
+
if (siblingMappings.length !== 1 && currentSource?.codec === "grouped") {
|
|
1130
|
+
const slot = currentSource.mapping.selector.slot === "table-definition" ? "member-set" : "table-definition";
|
|
1131
|
+
siblingMappings = input.current.grouped.sources.filter(
|
|
1132
|
+
(candidate) => candidate.selector.file === currentSource.mapping.selector.file && candidate.selector.slot === slot
|
|
1133
|
+
);
|
|
1134
|
+
}
|
|
1135
|
+
if (siblingMappings.length !== 1) {
|
|
1136
|
+
perResource.push({
|
|
1137
|
+
key: source.documentKey,
|
|
1138
|
+
diagnostic: (0, import_layoutCore.diagnostic)(
|
|
1139
|
+
"unsupported-table-sibling",
|
|
1140
|
+
siblingKey,
|
|
1141
|
+
"Preserved table sibling has no exact current mapping"
|
|
1142
|
+
)
|
|
1143
|
+
});
|
|
1144
|
+
continue;
|
|
1145
|
+
}
|
|
1146
|
+
if (source.destination.file !== siblingMappings[0].selector.file) relocationDeferred.add(source.documentKey);
|
|
1147
|
+
}
|
|
1148
|
+
const afterSiblings = checkpoint();
|
|
1149
|
+
if (afterSiblings) return afterSiblings;
|
|
1150
|
+
if (relocationDeferred.size > 0) {
|
|
1151
|
+
for (const documentKey of relocationDeferred)
|
|
1152
|
+
demoted.set(documentKey, { reason: "shared-container-relocation" });
|
|
1153
|
+
return retry;
|
|
1154
|
+
}
|
|
1155
|
+
const vacating = placement.groupedSources.filter((source) => source.mapping.selector.file !== source.destination.file).map((source) => source.mapping.source).concat(deletes.sources);
|
|
1156
|
+
const grouped = (0, import_layoutProjection.decomposeGroupedLayout)({
|
|
1157
|
+
documents: placement.groupedDocuments,
|
|
1158
|
+
documentKeys: placement.groupedDocumentKeys,
|
|
1159
|
+
sources: placement.groupedSources.map((source) => source.mapping),
|
|
1160
|
+
currentArtifacts: input.current.context.groupedArtifacts,
|
|
1161
|
+
current: input.current.grouped,
|
|
1162
|
+
vacating,
|
|
1163
|
+
...eligibility.submissionProofs ? { submissionProofs: eligibility.submissionProofs.grouped } : {}
|
|
1164
|
+
});
|
|
1165
|
+
if (grouped.diagnostics.length > 0) return (0, import_layoutCore.blocked)(grouped.diagnostics);
|
|
1166
|
+
const groupedCompanions = [];
|
|
1167
|
+
const companionTargets = /* @__PURE__ */ new Set();
|
|
1168
|
+
for (const patch of grouped.patches.filter((candidate) => candidate.selector.slot === "share-group-owner")) {
|
|
1169
|
+
const target = (0, import_documents.canonicalText)(patch.selector);
|
|
1170
|
+
if (companionTargets.has(target))
|
|
1171
|
+
return (0, import_layoutCore.blocked)([
|
|
1172
|
+
(0, import_layoutCore.diagnostic)(
|
|
1173
|
+
"duplicate-share-group-companion",
|
|
1174
|
+
patch.selector.file,
|
|
1175
|
+
"Share group header candidate is duplicated"
|
|
1176
|
+
)
|
|
1177
|
+
]);
|
|
1178
|
+
companionTargets.add(target);
|
|
1179
|
+
const headerProofs = input.current.grouped.observations.filter(
|
|
1180
|
+
(observation) => observation.selector.slot === "share-group-owner" && (0, import_layoutCore.sameSelector)(observation.selector, patch.selector) && (0, import_layoutCore.sourceIdentityKey)(observation.source) === (0, import_layoutCore.sourceIdentityKey)(patch.source) && observation.currentHash !== void 0 && observation.currentHash === patch.expectedCurrentHash
|
|
1181
|
+
);
|
|
1182
|
+
const authorizers = placement.groupedSources.filter((source) => {
|
|
1183
|
+
if (source.mapping.selector.slot !== "share-entry" || source.mapping.selector.file !== patch.selector.file)
|
|
1184
|
+
return false;
|
|
1185
|
+
const document = placement.groupedDocuments[source.mapping.documentIndex];
|
|
1186
|
+
const entryPatch = grouped.patches.filter(
|
|
1187
|
+
(candidate) => (0, import_layoutCore.sameSelector)(candidate.selector, source.mapping.selector) && (0, import_layoutCore.sourceIdentityKey)(candidate.source) === (0, import_layoutCore.sourceIdentityKey)(source.mapping.source)
|
|
1188
|
+
);
|
|
1189
|
+
return document.kind === "share" && entryPatch.length === 1 && (0, import_layoutCore.same)(document.owner, patch.value);
|
|
1190
|
+
});
|
|
1191
|
+
const insertedGroup = authorizers.length > 0 && authorizers.every((source) => setByKey.get(source.documentKey)?.placement === "insert-authoritative");
|
|
1192
|
+
if (headerProofs.length !== 1 && !insertedGroup || authorizers.length === 0)
|
|
1193
|
+
return failMany(
|
|
1194
|
+
placement.groupedSources.filter((source) => source.mapping.selector.file === patch.selector.file).map((source) => source.documentKey),
|
|
1195
|
+
(0, import_layoutCore.diagnostic)(
|
|
1196
|
+
"unbound-share-group-companion",
|
|
1197
|
+
patch.selector.file,
|
|
1198
|
+
"Share group header candidate lacks exact accepted-entry proof"
|
|
1199
|
+
)
|
|
1200
|
+
);
|
|
1201
|
+
groupedCompanions.push({
|
|
1202
|
+
kind: "share-group-owner",
|
|
1203
|
+
authorizingDocumentKeys: authorizers.map((source) => source.documentKey),
|
|
1204
|
+
patch: (0, import_layoutCore.clone)(patch)
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
const ownerDocuments = placement.groupedSources.flatMap((placedSource) => {
|
|
1208
|
+
const document = placement.groupedDocuments[placedSource.mapping.documentIndex];
|
|
1209
|
+
return (0, import_layoutCore.attachmentField)(document) ? [
|
|
1210
|
+
{
|
|
1211
|
+
source: placedSource.mapping.source,
|
|
1212
|
+
ownerFile: placedSource.mapping.selector.file,
|
|
1213
|
+
document
|
|
1214
|
+
}
|
|
1215
|
+
] : [];
|
|
1216
|
+
});
|
|
1217
|
+
const binary = (0, import_layoutBinaryProjection.decomposeBinaryLayout)({
|
|
1218
|
+
rawDocuments: placement.rawDocuments,
|
|
1219
|
+
rawSources: placement.rawSources.map((source) => source.mapping),
|
|
1220
|
+
ownerDocuments,
|
|
1221
|
+
ownerFields: placement.attachments.flatMap((attachment) => attachment.current ? [attachment.current] : []),
|
|
1222
|
+
ownerInsertions: placement.ownerInsertions,
|
|
1223
|
+
current: input.current.binary,
|
|
1224
|
+
vacating: placement.rawSources.filter((source) => !sameRawSelector(source.mapping.selector, source.destination)).map((source) => source.mapping.source).concat(deletes.sources),
|
|
1225
|
+
...eligibility.submissionProofs ? { submissionProofs: eligibility.submissionProofs.binary } : {}
|
|
1226
|
+
});
|
|
1227
|
+
if (binary.diagnostics.length > 0) return (0, import_layoutCore.blocked)(binary.diagnostics);
|
|
1228
|
+
const physicallySet = new Set(placed.map((candidate) => candidate.documentKey));
|
|
1229
|
+
const physicallySetSources = new Set(
|
|
1230
|
+
placement.groupedSources.map((item) => (0, import_layoutCore.sourceIdentityKey)(item.mapping.source)).concat(placement.rawSources.map((item) => (0, import_layoutCore.sourceIdentityKey)(item.mapping.source)))
|
|
1231
|
+
);
|
|
1232
|
+
const decidedDocumentKeys = new Set(materializations.map((decision) => decision.documentKey));
|
|
1233
|
+
const decidedSources = new Set(
|
|
1234
|
+
materializations.flatMap(
|
|
1235
|
+
(decision) => decision.mode === "preserve" ? [(0, import_layoutCore.sourceIdentityKey)(decision.source)] : []
|
|
1236
|
+
)
|
|
1237
|
+
);
|
|
1238
|
+
const preserveSharedSource = (source, hostKey) => {
|
|
1239
|
+
const documentKey = stableSourceKey(source.mapping.source);
|
|
1240
|
+
const sourceKey = (0, import_layoutCore.sourceIdentityKey)(source.mapping.source);
|
|
1241
|
+
if (documentKey && (physicallySet.has(documentKey) || decidedDocumentKeys.has(documentKey)) || physicallySetSources.has(sourceKey) || decidedSources.has(sourceKey))
|
|
1242
|
+
return;
|
|
1243
|
+
const document = mappingDocument(currentIndex.snapshot, source);
|
|
1244
|
+
const contributors = contributorsForMapping(currentIndex, source);
|
|
1245
|
+
if (!document || contributors.length === 0) {
|
|
1246
|
+
perResource.push({
|
|
1247
|
+
key: hostKey,
|
|
1248
|
+
diagnostic: (0, import_layoutCore.diagnostic)(
|
|
1249
|
+
"unsupported-shared-sibling",
|
|
1250
|
+
documentKey ?? sourceKey,
|
|
1251
|
+
"Shared-container sibling evidence is incomplete"
|
|
1252
|
+
)
|
|
1253
|
+
});
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
materializations.push({
|
|
1257
|
+
mode: "preserve",
|
|
1258
|
+
...documentKey ? { documentKey } : {},
|
|
1259
|
+
source: (0, import_layoutCore.clone)(source.mapping.source),
|
|
1260
|
+
document: (0, import_layoutCore.clone)(document),
|
|
1261
|
+
contributors: (0, import_layoutCore.clone)(contributors),
|
|
1262
|
+
reason: "shared-container-sibling"
|
|
1263
|
+
});
|
|
1264
|
+
if (documentKey) decidedDocumentKeys.add(documentKey);
|
|
1265
|
+
decidedSources.add(sourceKey);
|
|
1266
|
+
};
|
|
1267
|
+
for (const placedSource of placement.groupedSources) {
|
|
1268
|
+
const files = /* @__PURE__ */ new Set([placedSource.mapping.selector.file, placedSource.destination.file]);
|
|
1269
|
+
for (const source of input.current.sources)
|
|
1270
|
+
if (source.codec === "grouped" && files.has(source.mapping.selector.file))
|
|
1271
|
+
preserveSharedSource(source, placedSource.documentKey);
|
|
1272
|
+
}
|
|
1273
|
+
const afterSiblingSources = checkpoint();
|
|
1274
|
+
if (afterSiblingSources) return afterSiblingSources;
|
|
1275
|
+
const attachmentDestinations = /* @__PURE__ */ new Map();
|
|
1276
|
+
for (const patch of binary.ownerPatches) {
|
|
1277
|
+
const placedOwner = placement.groupedSources.filter(
|
|
1278
|
+
(source) => (0, import_layoutCore.sourceIdentityKey)(source.mapping.source) === (0, import_layoutCore.sourceIdentityKey)(patch.source) && source.mapping.selector.file === patch.ownerFile && (0, import_layoutCore.attachmentField)(placement.groupedDocuments[source.mapping.documentIndex]) === patch.field
|
|
1279
|
+
);
|
|
1280
|
+
const placedAttachment = placement.attachments.filter(
|
|
1281
|
+
(attachment) => (0, import_layoutCore.sourceIdentityKey)(attachment.source) === (0, import_layoutCore.sourceIdentityKey)(patch.source) && attachment.field === patch.field && (patch.origin === "authoritative-insertion" ? attachment.current === void 0 && attachment.destination.ownerFile === patch.ownerFile : attachment.current?.ownerFile === patch.ownerFile)
|
|
1282
|
+
);
|
|
1283
|
+
const ownerSet = placedOwner.flatMap(
|
|
1284
|
+
(source) => grouped.patches.filter(
|
|
1285
|
+
(candidate) => candidate.action === "set" && (0, import_layoutCore.sourceIdentityKey)(candidate.source) === (0, import_layoutCore.sourceIdentityKey)(source.mapping.source) && (0, import_layoutCore.sameSelector)(candidate.selector, source.mapping.selector) && (0, import_layoutCore.same)(candidate.value, placement.groupedDocuments[source.mapping.documentIndex])
|
|
1286
|
+
)
|
|
1287
|
+
);
|
|
1288
|
+
const authoritativeValue = placedOwner.length === 1 ? placement.groupedDocuments[placedOwner[0].mapping.documentIndex][patch.field] : void 0;
|
|
1289
|
+
const descriptorMatches = patch.action === "set" ? (0, import_layoutCore.same)(patch.value, authoritativeValue) : authoritativeValue === null || authoritativeValue === void 0 && patch.field === "icon";
|
|
1290
|
+
const currentHashMatches = patch.origin === "authoritative-insertion" ? placedAttachment.length === 1 && placedAttachment[0].current === void 0 && placedOwner[0]?.mode === "insert-authoritative" && patch.groupedSelector.file === placedOwner[0]?.destination.file : placedAttachment.length === 1 && patch.expectedCurrentHash === placedAttachment[0].current?.currentHash;
|
|
1291
|
+
if (placedOwner.length !== 1 || placedAttachment.length !== 1 || ownerSet.length !== 1 || !(0, import_layoutCore.same)(patch.ownerDocument, placement.groupedDocuments[placedOwner[0]?.mapping.documentIndex]) || !descriptorMatches || !currentHashMatches)
|
|
1292
|
+
return fail(
|
|
1293
|
+
placedOwner[0]?.documentKey ?? stableSourceKey(patch.source),
|
|
1294
|
+
(0, import_layoutCore.diagnostic)(
|
|
1295
|
+
"unpaired-attachment-patch",
|
|
1296
|
+
patch.ownerFile,
|
|
1297
|
+
"Attachment candidate does not match one whole-owner set"
|
|
1298
|
+
)
|
|
1299
|
+
);
|
|
1300
|
+
attachmentDestinations.set(
|
|
1301
|
+
`${(0, import_layoutCore.sourceIdentityKey)(patch.source)}/${patch.field}`,
|
|
1302
|
+
(0, import_layoutCore.clone)(placedAttachment[0].destination)
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
for (const source of placement.groupedSources) {
|
|
1306
|
+
const candidate = setByKey.get(source.documentKey);
|
|
1307
|
+
const patches = grouped.patches.filter(
|
|
1308
|
+
(patch) => (0, import_layoutCore.sourceIdentityKey)(patch.source) === (0, import_layoutCore.sourceIdentityKey)(source.mapping.source) && (0, import_layoutCore.sameSelector)(patch.selector, source.mapping.selector)
|
|
1309
|
+
);
|
|
1310
|
+
const expectedValue = mappingValue(candidate.document, source.mapping);
|
|
1311
|
+
if (patches.length !== 1 || patches[0].action !== "set" || expectedValue === void 0 || !(0, import_layoutCore.same)(patches[0].value, expectedValue))
|
|
1312
|
+
return fail(
|
|
1313
|
+
source.documentKey,
|
|
1314
|
+
(0, import_layoutCore.diagnostic)(
|
|
1315
|
+
"invalid-accepted-patch",
|
|
1316
|
+
source.documentKey,
|
|
1317
|
+
"Grouped set does not match accepted current and authoritative evidence"
|
|
1318
|
+
)
|
|
1319
|
+
);
|
|
1320
|
+
materializations.push({
|
|
1321
|
+
mode: "set",
|
|
1322
|
+
codec: "grouped",
|
|
1323
|
+
documentKey: source.documentKey,
|
|
1324
|
+
outcomeKey: candidate.outcomeKey ?? candidate.documentKey,
|
|
1325
|
+
placement: source.mode,
|
|
1326
|
+
document: (0, import_layoutCore.clone)(candidate.document),
|
|
1327
|
+
destination: (0, import_layoutCore.clone)(source.destination),
|
|
1328
|
+
...(0, import_layoutCore.sameSelector)(source.mapping.selector, source.destination) ? {} : { origin: (0, import_layoutCore.clone)(source.mapping.selector) },
|
|
1329
|
+
patches: (0, import_layoutCore.clone)(patches)
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
for (const source of placement.rawSources) {
|
|
1333
|
+
const candidate = setByKey.get(source.documentKey);
|
|
1334
|
+
const patches = binary.rawPatches.filter(
|
|
1335
|
+
(patch) => (0, import_layoutCore.sourceIdentityKey)(patch.source) === (0, import_layoutCore.sourceIdentityKey)(source.mapping.source)
|
|
1336
|
+
);
|
|
1337
|
+
if (patches.length !== 1 || patches[0].action !== "set" || !sameBinarySelector(patches[0].selector, source.mapping.selector) || !(0, import_layoutCore.same)(patches[0].document, candidate.document) || patches[0].bytes !== source.mapping.bytes || !(0, import_layoutCore.same)(patches[0].content, candidate.document.content))
|
|
1338
|
+
return fail(
|
|
1339
|
+
source.documentKey,
|
|
1340
|
+
(0, import_layoutCore.diagnostic)(
|
|
1341
|
+
"invalid-accepted-patch",
|
|
1342
|
+
source.documentKey,
|
|
1343
|
+
"Raw set does not match accepted current and authoritative evidence"
|
|
1344
|
+
)
|
|
1345
|
+
);
|
|
1346
|
+
materializations.push({
|
|
1347
|
+
mode: "set",
|
|
1348
|
+
codec: "raw",
|
|
1349
|
+
documentKey: source.documentKey,
|
|
1350
|
+
outcomeKey: candidate.outcomeKey ?? candidate.documentKey,
|
|
1351
|
+
placement: source.mode,
|
|
1352
|
+
document: (0, import_layoutCore.clone)(candidate.document),
|
|
1353
|
+
destination: (0, import_layoutCore.clone)(source.destination),
|
|
1354
|
+
...sameRawSelector(source.mapping.selector, source.destination) ? {} : { origin: (0, import_layoutCore.clone)(source.mapping.selector) },
|
|
1355
|
+
bytes: source.bytes,
|
|
1356
|
+
patch: (0, import_layoutCore.clone)(patches[0])
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
const attachments = binary.ownerPatches.map((patch) => {
|
|
1360
|
+
const ownerDocumentKey = placement.groupedSources.find(
|
|
1361
|
+
(source) => (0, import_layoutCore.sourceIdentityKey)(source.mapping.source) === (0, import_layoutCore.sourceIdentityKey)(patch.source)
|
|
1362
|
+
)?.documentKey ?? stableSourceKey(patch.source);
|
|
1363
|
+
const destination = attachmentDestinations.get(`${(0, import_layoutCore.sourceIdentityKey)(patch.source)}/${patch.field}`);
|
|
1364
|
+
const retainedNull = patch.action === "clear" && input.current.binary.ownerFields.some(
|
|
1365
|
+
(field) => (0, import_layoutCore.sourceIdentityKey)(field.ownerSource) === (0, import_layoutCore.sourceIdentityKey)(patch.source) && field.field === patch.field && field.status === "value" && field.value === null
|
|
1366
|
+
);
|
|
1367
|
+
return retainedNull ? {
|
|
1368
|
+
mode: "known-null",
|
|
1369
|
+
ownerDocumentKey,
|
|
1370
|
+
source: (0, import_layoutCore.clone)(patch.source),
|
|
1371
|
+
ownerFile: destination.ownerFile,
|
|
1372
|
+
field: patch.field
|
|
1373
|
+
} : { mode: patch.action, ownerDocumentKey, patch: (0, import_layoutCore.clone)(patch), destination };
|
|
1374
|
+
});
|
|
1375
|
+
for (const source of placement.groupedSources) {
|
|
1376
|
+
const document = placement.groupedDocuments[source.mapping.documentIndex];
|
|
1377
|
+
const nullField = knownNull(document);
|
|
1378
|
+
if (nullField && !attachments.some(
|
|
1379
|
+
(candidate) => candidate.ownerDocumentKey === source.documentKey && ("patch" in candidate ? candidate.patch.field : candidate.field) === nullField.field
|
|
1380
|
+
))
|
|
1381
|
+
attachments.push({
|
|
1382
|
+
mode: "known-null",
|
|
1383
|
+
ownerDocumentKey: source.documentKey,
|
|
1384
|
+
source: (0, import_layoutCore.clone)(source.mapping.source),
|
|
1385
|
+
ownerFile: source.destination.file,
|
|
1386
|
+
field: nullField.field
|
|
1387
|
+
});
|
|
1388
|
+
}
|
|
1389
|
+
for (const source of placement.groupedSources) {
|
|
1390
|
+
const document = placement.groupedDocuments[source.mapping.documentIndex];
|
|
1391
|
+
const field = (0, import_layoutCore.attachmentField)(document);
|
|
1392
|
+
if (!field || attachments.some(
|
|
1393
|
+
(candidate) => candidate.ownerDocumentKey === source.documentKey && ("patch" in candidate ? candidate.patch.field : candidate.field) === field
|
|
1394
|
+
))
|
|
1395
|
+
continue;
|
|
1396
|
+
const projection = coherentPreservedAttachment(
|
|
1397
|
+
input.current,
|
|
1398
|
+
source.mapping.source,
|
|
1399
|
+
source.mapping.selector.file,
|
|
1400
|
+
field,
|
|
1401
|
+
document[field]
|
|
1402
|
+
);
|
|
1403
|
+
if (!projection)
|
|
1404
|
+
return fail(
|
|
1405
|
+
source.documentKey,
|
|
1406
|
+
(0, import_layoutCore.diagnostic)(
|
|
1407
|
+
"incoherent-accepted-attachment",
|
|
1408
|
+
source.destination.file,
|
|
1409
|
+
"Accepted owner attachment has no coherent current association"
|
|
1410
|
+
)
|
|
1411
|
+
);
|
|
1412
|
+
attachments.push({
|
|
1413
|
+
mode: "preserve",
|
|
1414
|
+
ownerDocumentKey: source.documentKey,
|
|
1415
|
+
source: (0, import_layoutCore.clone)(source.mapping.source),
|
|
1416
|
+
ownerFile: source.destination.file,
|
|
1417
|
+
field,
|
|
1418
|
+
...projection.bytes ? { bytes: projection.bytes } : {}
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
const setKeys = new Set(
|
|
1422
|
+
materializations.filter((decision) => decision.mode === "set").map((decision) => decision.documentKey)
|
|
1423
|
+
);
|
|
1424
|
+
const pendingDamage = placement.damagePlacements.filter((damage) => setKeys.has(damage.evidence.documentKey));
|
|
1425
|
+
const derivedSourceProofs = [];
|
|
1426
|
+
for (const source of placement.groupedSources) {
|
|
1427
|
+
const document = placement.groupedDocuments[source.mapping.documentIndex];
|
|
1428
|
+
const identity = source.mapping.source;
|
|
1429
|
+
const selectorHash = (0, import_layoutProjection.derivedSourceSelectorHash)(document);
|
|
1430
|
+
if (source.mode !== "insert-authoritative" || !selectorHash || identity.type !== "resource" || !("id" in identity.resource) || identity.resource.kind !== "user_permissions")
|
|
1431
|
+
continue;
|
|
1432
|
+
derivedSourceProofs.push({
|
|
1433
|
+
source: { type: "resource", resource: { kind: "user_permissions", id: identity.resource.id } },
|
|
1434
|
+
selectorHash
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
return finish({
|
|
1438
|
+
groupedPatches: grouped.patches,
|
|
1439
|
+
groupedCompanions,
|
|
1440
|
+
rawPatches: binary.rawPatches,
|
|
1441
|
+
attachments,
|
|
1442
|
+
pendingDamage,
|
|
1443
|
+
derivedSourceProofs
|
|
1444
|
+
});
|
|
1445
|
+
}
|
|
1446
|
+
function contributorIsClean(contributor) {
|
|
1447
|
+
if (contributor.kind === "grouped-navigation" || contributor.kind === "raw-navigation") return true;
|
|
1448
|
+
if (contributor.kind === "owner-field") return contributor.state === void 0 || contributor.state === "clean";
|
|
1449
|
+
return contributor.state === "clean";
|
|
1450
|
+
}
|
|
1451
|
+
function ownerGroupDeleted(current, document) {
|
|
1452
|
+
if (document.kind !== "share" || !document.owner) return false;
|
|
1453
|
+
const owner = (0, import_layoutCore.ownerKey)(document.owner);
|
|
1454
|
+
return current.grouped.observations.some(
|
|
1455
|
+
(observation) => observation.state === "absent" && observation.source.type === "group" && (0, import_layoutCore.ownerKey)(observation.source.owner) === owner
|
|
1456
|
+
);
|
|
1457
|
+
}
|
|
1458
|
+
function contributorPaths(contributor) {
|
|
1459
|
+
if (contributor.kind === "raw")
|
|
1460
|
+
return [contributor.selector.sidecar, ...contributor.bytes ? [contributor.bytes] : []];
|
|
1461
|
+
if (contributor.kind === "owner-field") return contributor.bytes ? [contributor.bytes] : [];
|
|
1462
|
+
if (contributor.kind === "attachment")
|
|
1463
|
+
return [contributor.selector.ownerFile, ...contributor.bytes ? [contributor.bytes] : []];
|
|
1464
|
+
if (contributor.kind === "raw-navigation") return [contributor.selector.sidecar];
|
|
1465
|
+
return [contributor.selector.file];
|
|
1466
|
+
}
|
|
1467
|
+
function decisionPaths(decision) {
|
|
1468
|
+
return "contributors" in decision ? decision.contributors.flatMap(contributorPaths) : [];
|
|
1469
|
+
}
|
|
1470
|
+
function holdFolders(current, decisions) {
|
|
1471
|
+
const scan = current.context.scan;
|
|
1472
|
+
const evidence = current.context.evidence;
|
|
1473
|
+
const candidates = decisions.flatMap((decision) => {
|
|
1474
|
+
if (decision.mode === "remove") {
|
|
1475
|
+
const source2 = decision.current;
|
|
1476
|
+
if (decision.document?.kind !== "folder" || source2?.codec !== "grouped" || !source2.mapping.selector.file.endsWith("/_folder.json"))
|
|
1477
|
+
return [];
|
|
1478
|
+
return [
|
|
1479
|
+
{
|
|
1480
|
+
decision,
|
|
1481
|
+
mapping: source2.mapping,
|
|
1482
|
+
directory: (0, import_layoutPaths.folderDirectory)(source2.mapping.selector.file),
|
|
1483
|
+
semanticPath: Array.isArray(decision.document.path) ? decision.document.path : void 0
|
|
1484
|
+
}
|
|
1485
|
+
];
|
|
1486
|
+
}
|
|
1487
|
+
if (decision.mode !== "set" || decision.document.kind !== "folder") return [];
|
|
1488
|
+
if (!folderRouteChanged(decision.currentDocument, decision.document)) return [];
|
|
1489
|
+
const source = decision.current;
|
|
1490
|
+
if (source?.codec !== "grouped" || !source.mapping.selector.file.endsWith("/_folder.json")) return [];
|
|
1491
|
+
return [
|
|
1492
|
+
{
|
|
1493
|
+
decision,
|
|
1494
|
+
mapping: source.mapping,
|
|
1495
|
+
directory: (0, import_layoutPaths.folderDirectory)(source.mapping.selector.file),
|
|
1496
|
+
semanticPath: Array.isArray(decision.document.path) ? decision.document.path : void 0
|
|
1497
|
+
}
|
|
1498
|
+
];
|
|
1499
|
+
});
|
|
1500
|
+
if (candidates.length === 0) return { decisions: [...decisions], notices: [] };
|
|
1501
|
+
const inside = (path, directory) => pathInside(path, directory);
|
|
1502
|
+
const occupied = [
|
|
1503
|
+
...scan.artifacts.filter((artifact) => artifact.present).map((artifact) => artifact.path),
|
|
1504
|
+
...scan.localOnlyFiles,
|
|
1505
|
+
...scan.opaqueDirectories
|
|
1506
|
+
];
|
|
1507
|
+
const heldKeys = /* @__PURE__ */ new Set();
|
|
1508
|
+
const heldDirectories = /* @__PURE__ */ new Set();
|
|
1509
|
+
for (let changed = true; changed; ) {
|
|
1510
|
+
changed = false;
|
|
1511
|
+
for (const candidate of candidates) {
|
|
1512
|
+
if (heldDirectories.has(candidate.directory)) continue;
|
|
1513
|
+
const leaving = new Set(
|
|
1514
|
+
decisions.flatMap(
|
|
1515
|
+
(decision) => (decision.mode === "set" || decision.mode === "remove") && !heldKeys.has(decision.documentKey) ? decisionPaths(decision) : []
|
|
1516
|
+
)
|
|
1517
|
+
);
|
|
1518
|
+
const blockers = occupied.filter((path) => inside(path, candidate.directory) && !leaving.has(path)).sort(import_layoutCore.compareText);
|
|
1519
|
+
const detached = hasDetachedDescendants(
|
|
1520
|
+
candidate.mapping,
|
|
1521
|
+
candidate.directory,
|
|
1522
|
+
candidate.semanticPath,
|
|
1523
|
+
evidence.detachedDocumentProofs,
|
|
1524
|
+
evidence.detachedFileProofs
|
|
1525
|
+
);
|
|
1526
|
+
if (blockers.length === 0 && !detached) continue;
|
|
1527
|
+
heldDirectories.add(candidate.directory);
|
|
1528
|
+
heldKeys.add(candidate.decision.documentKey);
|
|
1529
|
+
for (const decision of decisions)
|
|
1530
|
+
if (decision.mode === "set" && decisionPaths(decision).some((path) => inside(path, candidate.directory)))
|
|
1531
|
+
heldKeys.add(decision.documentKey);
|
|
1532
|
+
changed = true;
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
if (heldKeys.size === 0) return { decisions: [...decisions], notices: [] };
|
|
1536
|
+
const heldRemoval = (decision) => decision.mode === "remove" && decision.document?.kind === "folder";
|
|
1537
|
+
const held = decisions.filter(
|
|
1538
|
+
(decision) => (decision.mode === "set" || heldRemoval(decision)) && heldKeys.has(decision.documentKey ?? "")
|
|
1539
|
+
);
|
|
1540
|
+
const spared = /* @__PURE__ */ new Set([
|
|
1541
|
+
...decisions.flatMap(
|
|
1542
|
+
(decision) => (decision.mode === "set" || decision.mode === "remove") && !heldKeys.has(decision.documentKey) ? decisionPaths(decision) : []
|
|
1543
|
+
),
|
|
1544
|
+
...held.flatMap(decisionPaths)
|
|
1545
|
+
]);
|
|
1546
|
+
const notices = [...heldDirectories].filter((directory) => ![...heldDirectories].some((other) => inside(directory, other))).map((directory) => ({
|
|
1547
|
+
status: "kept-in-place",
|
|
1548
|
+
path: directory,
|
|
1549
|
+
paths: occupied.filter((path) => inside(path, directory) && !spared.has(path)).sort(import_layoutCore.compareText)
|
|
1550
|
+
}));
|
|
1551
|
+
const output = decisions.map(
|
|
1552
|
+
(decision) => (decision.mode === "set" || heldRemoval(decision)) && heldKeys.has(decision.documentKey ?? "") ? {
|
|
1553
|
+
mode: "preserve",
|
|
1554
|
+
documentKey: decision.documentKey,
|
|
1555
|
+
source: (0, import_layoutCore.clone)(decision.source),
|
|
1556
|
+
...decision.mode === "set" ? decision.currentDocument ? { document: (0, import_layoutCore.clone)(decision.currentDocument) } : {} : decision.document ? { document: (0, import_layoutCore.clone)(decision.document) } : {},
|
|
1557
|
+
contributors: (0, import_layoutCore.clone)(decision.contributors),
|
|
1558
|
+
reason: "folder-held"
|
|
1559
|
+
} : decision
|
|
1560
|
+
);
|
|
1561
|
+
return { decisions: output, notices: notices.sort((left, right) => (0, import_layoutCore.compareText)(left.path, right.path)) };
|
|
1562
|
+
}
|
|
1563
|
+
function decideCheckoutEligibility(input) {
|
|
1564
|
+
let head;
|
|
1565
|
+
try {
|
|
1566
|
+
head = (0, import_documents.headDocuments)(input.head);
|
|
1567
|
+
} catch (error) {
|
|
1568
|
+
return (0, import_layoutCore.blocked)([(0, import_layoutCore.diagnostic)("invalid-selected-head", ".", error.message)]);
|
|
1569
|
+
}
|
|
1570
|
+
const currentIndex = indexSnapshot(resourceEvidence(input.current));
|
|
1571
|
+
const currentRawSources = (0, import_layoutCore.groupBySource)(
|
|
1572
|
+
input.current.binary.rawSources,
|
|
1573
|
+
(mapping) => (0, import_layoutCore.sourceIdentityKey)(mapping.source)
|
|
1574
|
+
);
|
|
1575
|
+
const diagnostics = [];
|
|
1576
|
+
const warnings = [];
|
|
1577
|
+
const decisions = [];
|
|
1578
|
+
for (const documentKey of [.../* @__PURE__ */ new Set([...Object.keys(input.baseline), ...head.keys()])].sort()) {
|
|
1579
|
+
const source = (0, import_layoutCore.stableSource)(documentKey);
|
|
1580
|
+
if (!source) {
|
|
1581
|
+
warnings.push(
|
|
1582
|
+
(0, import_layoutCore.diagnostic)("unsupported-checkout-key", documentKey, "Canonical key has no stable resource identity")
|
|
1583
|
+
);
|
|
1584
|
+
continue;
|
|
1585
|
+
}
|
|
1586
|
+
const authoritative = head.get(documentKey);
|
|
1587
|
+
const resource = currentResource(currentIndex, currentRawSources, source);
|
|
1588
|
+
const tracked = input.baseline[documentKey] !== void 0;
|
|
1589
|
+
const headMatchesBaseline = input.head.manifest[documentKey] === input.baseline[documentKey];
|
|
1590
|
+
const preserve = (reason) => {
|
|
1591
|
+
decisions.push({
|
|
1592
|
+
mode: "preserve",
|
|
1593
|
+
documentKey,
|
|
1594
|
+
source: (0, import_layoutCore.clone)(source),
|
|
1595
|
+
...resource.document ? { document: (0, import_layoutCore.clone)(resource.document) } : {},
|
|
1596
|
+
contributors: (0, import_layoutCore.clone)(resource.contributors),
|
|
1597
|
+
reason
|
|
1598
|
+
});
|
|
1599
|
+
};
|
|
1600
|
+
const set = () => {
|
|
1601
|
+
decisions.push({
|
|
1602
|
+
mode: "set",
|
|
1603
|
+
documentKey,
|
|
1604
|
+
placement: resource.presence === "present" ? "replace-proven" : "insert-authoritative",
|
|
1605
|
+
...input.force && resource.presence !== "present" ? { reinsert: true } : {},
|
|
1606
|
+
document: (0, import_layoutCore.clone)(authoritative),
|
|
1607
|
+
source: (0, import_layoutCore.clone)(source),
|
|
1608
|
+
contributors: (0, import_layoutCore.clone)(resource.contributors),
|
|
1609
|
+
...resource.presence === "present" ? { current: (0, import_layoutCore.clone)(resource.current), expectedCurrentHash: resource.currentHash } : {},
|
|
1610
|
+
...resource.document ? { currentDocument: (0, import_layoutCore.clone)(resource.document) } : {}
|
|
1611
|
+
});
|
|
1612
|
+
};
|
|
1613
|
+
const remove = () => {
|
|
1614
|
+
decisions.push({
|
|
1615
|
+
mode: "remove",
|
|
1616
|
+
documentKey,
|
|
1617
|
+
source: (0, import_layoutCore.clone)(source),
|
|
1618
|
+
contributors: (0, import_layoutCore.clone)(resource.contributors),
|
|
1619
|
+
...resource.current ? { current: (0, import_layoutCore.clone)(resource.current) } : {},
|
|
1620
|
+
...resource.document ? { document: (0, import_layoutCore.clone)(resource.document) } : {},
|
|
1621
|
+
...input.force ? { force: true } : {}
|
|
1622
|
+
});
|
|
1623
|
+
};
|
|
1624
|
+
if (resource.presence === "unsupported" && tracked) {
|
|
1625
|
+
preserve("unsupported-evidence");
|
|
1626
|
+
continue;
|
|
1627
|
+
}
|
|
1628
|
+
if (input.force) {
|
|
1629
|
+
if (authoritative) set();
|
|
1630
|
+
else remove();
|
|
1631
|
+
continue;
|
|
1632
|
+
}
|
|
1633
|
+
if (!authoritative) {
|
|
1634
|
+
if (resource.presence === "absent") decisions.push({ mode: "noop", documentKey });
|
|
1635
|
+
else if (resource.contributors.every(contributorIsClean)) remove();
|
|
1636
|
+
else preserve("kept-local-edits");
|
|
1637
|
+
continue;
|
|
1638
|
+
}
|
|
1639
|
+
if (resource.presence === "absent") {
|
|
1640
|
+
preserve(!tracked || headMatchesBaseline ? "local-deletion-pending" : "conflict");
|
|
1641
|
+
continue;
|
|
1642
|
+
}
|
|
1643
|
+
if (resource.presence === "unsupported") {
|
|
1644
|
+
if (ownerGroupDeleted(input.current, authoritative)) preserve("local-deletion-pending");
|
|
1645
|
+
else set();
|
|
1646
|
+
continue;
|
|
1647
|
+
}
|
|
1648
|
+
const clean = resource.contributors.every(contributorIsClean);
|
|
1649
|
+
if (clean && tracked && headMatchesBaseline && placedAsAuthoritative(resource.document, authoritative)) {
|
|
1650
|
+
decisions.push({ mode: "noop", documentKey });
|
|
1651
|
+
continue;
|
|
1652
|
+
}
|
|
1653
|
+
if (!clean && !(0, import_layoutCore.same)(resource.document, authoritative)) {
|
|
1654
|
+
preserve(tracked && headMatchesBaseline ? "kept-local" : "conflict");
|
|
1655
|
+
continue;
|
|
1656
|
+
}
|
|
1657
|
+
set();
|
|
1658
|
+
}
|
|
1659
|
+
if (diagnostics.length > 0) return (0, import_layoutCore.blocked)(diagnostics);
|
|
1660
|
+
const held = holdFolders(input.current, decisions);
|
|
1661
|
+
return {
|
|
1662
|
+
status: "complete",
|
|
1663
|
+
decisions: held.decisions,
|
|
1664
|
+
receiptResults: [],
|
|
1665
|
+
assignments: [],
|
|
1666
|
+
headDocuments: head,
|
|
1667
|
+
warnings,
|
|
1668
|
+
notices: held.notices
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
function sameRawSelector(left, right) {
|
|
1672
|
+
return left.sidecar === right.sidecar && (0, import_documents.canonicalText)(left.identity) === (0, import_documents.canonicalText)(right.identity);
|
|
1673
|
+
}
|
|
1674
|
+
function renderingIndex(context) {
|
|
1675
|
+
return {
|
|
1676
|
+
grouped: (0, import_layoutCore.groupBySource)(context.grouped.observations, (observation) => (0, import_layoutCore.sourceIdentityKey)(observation.source)),
|
|
1677
|
+
raw: (0, import_layoutCore.groupBySource)(context.binary.rawObservations, (observation) => (0, import_layoutCore.sourceIdentityKey)(observation.source)),
|
|
1678
|
+
attachments: (0, import_layoutCore.groupBySource)(
|
|
1679
|
+
context.binary.attachmentObservations,
|
|
1680
|
+
(observation) => (0, import_layoutCore.sourceIdentityKey)(observation.source)
|
|
1681
|
+
),
|
|
1682
|
+
bytes: (0, import_layoutCore.groupBySource)(context.byteFacts, (fact) => fact.path),
|
|
1683
|
+
artifacts: (0, import_layoutCore.groupBySource)(
|
|
1684
|
+
context.authored.artifacts.filter((artifact) => artifact.present),
|
|
1685
|
+
(artifact) => artifact.path
|
|
1686
|
+
),
|
|
1687
|
+
ownerFields: (0, import_layoutCore.groupBySource)(context.binary.ownerFields, (field) => (0, import_layoutCore.sourceIdentityKey)(field.ownerSource))
|
|
1688
|
+
};
|
|
1689
|
+
}
|
|
1690
|
+
function observationForGrouped(index, source, selector) {
|
|
1691
|
+
const matches = (index.grouped.get((0, import_layoutCore.sourceIdentityKey)(source)) ?? []).filter(
|
|
1692
|
+
(observation) => (0, import_layoutCore.sameSelector)(observation.selector, selector)
|
|
1693
|
+
);
|
|
1694
|
+
return matches.length === 1 ? matches[0] : void 0;
|
|
1695
|
+
}
|
|
1696
|
+
function observationForRaw(index, source, selector) {
|
|
1697
|
+
const matches = (index.raw.get((0, import_layoutCore.sourceIdentityKey)(source)) ?? []).filter(
|
|
1698
|
+
(observation) => observation.selector.slot === "file-entry" && sameRawSelector(observation.selector, selector)
|
|
1699
|
+
);
|
|
1700
|
+
return matches.length === 1 ? matches[0] : void 0;
|
|
1701
|
+
}
|
|
1702
|
+
function observationForAttachment(index, patch) {
|
|
1703
|
+
const matches = (index.attachments.get((0, import_layoutCore.sourceIdentityKey)(patch.source)) ?? []).filter(
|
|
1704
|
+
(observation) => observation.selector.slot === "owner-attachment" && observation.selector.ownerFile === patch.ownerFile && observation.selector.field === patch.field
|
|
1705
|
+
);
|
|
1706
|
+
return matches.length === 1 ? matches[0] : void 0;
|
|
1707
|
+
}
|
|
1708
|
+
function originalBytesMatch(index, observation) {
|
|
1709
|
+
if (!observation.contributor) return observation.bytes === void 0 && observation.byteHash === void 0;
|
|
1710
|
+
if (!observation.bytes || !observation.byteHash) return false;
|
|
1711
|
+
const matches = index.bytes.get(observation.bytes) ?? [];
|
|
1712
|
+
return matches.length === 1 && matches[0].state === "present" && (0, import_layoutCore.canonicalFragmentHash)(matches[0].content) === observation.byteHash;
|
|
1713
|
+
}
|
|
1714
|
+
function authoredArtifact(index, path, role) {
|
|
1715
|
+
const matches = (index.artifacts.get(path) ?? []).filter(
|
|
1716
|
+
(artifact) => role === void 0 || artifact.role === role
|
|
1717
|
+
);
|
|
1718
|
+
return matches.length === 1 && (0, import_layoutCore.object)(matches[0].json) ? matches[0] : void 0;
|
|
1719
|
+
}
|
|
1720
|
+
const INSERTED_ROLES = [
|
|
1721
|
+
["access/shares/", "share-group"],
|
|
1722
|
+
["access/groups/", "user-group"],
|
|
1723
|
+
["access/labels/", "label"],
|
|
1724
|
+
["access/grants/", "user-grant"],
|
|
1725
|
+
["definitions/datasets/", "dataset"],
|
|
1726
|
+
["definitions/exports/", "export"],
|
|
1727
|
+
["detached/", "detached-document"]
|
|
1728
|
+
];
|
|
1729
|
+
function insertedRole(file) {
|
|
1730
|
+
if (file.endsWith("/_folder.json")) return "folder-control";
|
|
1731
|
+
if ((0, import_layoutPaths.isRawSidecarPath)(file)) return "raw-metadata";
|
|
1732
|
+
if (file === "definitions/settings.json") return "settings";
|
|
1733
|
+
for (const [root, role] of INSERTED_ROLES) if (file.startsWith(root)) return role;
|
|
1734
|
+
return "document";
|
|
1735
|
+
}
|
|
1736
|
+
function insertedArtifact(file, owner) {
|
|
1737
|
+
const role = insertedRole(file);
|
|
1738
|
+
const json = role === "share-group" ? { owner: (0, import_layoutCore.clone)(owner ?? null), shares: [] } : role === "raw-metadata" ? {} : {};
|
|
1739
|
+
return { path: file, kind: "file", role, present: false, json };
|
|
1740
|
+
}
|
|
1741
|
+
function detachedBuckets(origin) {
|
|
1742
|
+
if (!origin.startsWith("detached/")) return [];
|
|
1743
|
+
return (0, import_layoutCore.pathAncestors)(origin).map((path) => ({ kind: "directory", path }));
|
|
1744
|
+
}
|
|
1745
|
+
function component(file) {
|
|
1746
|
+
return file.slice(file.lastIndexOf("/") + 1);
|
|
1747
|
+
}
|
|
1748
|
+
function directoryComponent(folderControl) {
|
|
1749
|
+
const directory = (0, import_layoutPaths.folderDirectory)(folderControl);
|
|
1750
|
+
return component(directory);
|
|
1751
|
+
}
|
|
1752
|
+
function requiredNameOverride(physicalComponent, role, extension, semanticName) {
|
|
1753
|
+
if ((0, import_layoutPaths.layoutNameNeedsOverride)(semanticName)) return semanticName;
|
|
1754
|
+
try {
|
|
1755
|
+
return (0, import_layoutPaths.decodeLayoutName)(physicalComponent, role, extension) === semanticName ? void 0 : semanticName;
|
|
1756
|
+
} catch {
|
|
1757
|
+
return semanticName;
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
function withLocalField(authoritative, authored, field, value) {
|
|
1761
|
+
if ((0, import_layoutCore.own)(authoritative, field)) {
|
|
1762
|
+
const replaced = {};
|
|
1763
|
+
for (const [key, entry] of Object.entries(authoritative)) replaced[key] = key === field ? (0, import_layoutCore.clone)(value) : entry;
|
|
1764
|
+
return replaced;
|
|
1765
|
+
}
|
|
1766
|
+
const authoredKeys = Object.keys(authored);
|
|
1767
|
+
const localIndex = authoredKeys.indexOf(field);
|
|
1768
|
+
if (localIndex < 0) return { ...authoritative, [field]: (0, import_layoutCore.clone)(value) };
|
|
1769
|
+
const insertBefore = Object.keys(authoritative).find((key) => authoredKeys.indexOf(key) > localIndex);
|
|
1770
|
+
const result = {};
|
|
1771
|
+
for (const [key, entry] of Object.entries(authoritative)) {
|
|
1772
|
+
if (key === insertBefore) result[field] = (0, import_layoutCore.clone)(value);
|
|
1773
|
+
result[key] = entry;
|
|
1774
|
+
}
|
|
1775
|
+
if (!(0, import_layoutCore.own)(result, field)) result[field] = (0, import_layoutCore.clone)(value);
|
|
1776
|
+
return result;
|
|
1777
|
+
}
|
|
1778
|
+
function lowerGroupedValue(document, artifact, attachment, destination) {
|
|
1779
|
+
const authored = (0, import_layoutCore.object)(artifact.json);
|
|
1780
|
+
if (!authored) return void 0;
|
|
1781
|
+
const role = destination === artifact.path ? artifact.role : insertedRole(destination);
|
|
1782
|
+
const output = {};
|
|
1783
|
+
const field = (0, import_layoutCore.attachmentField)(document);
|
|
1784
|
+
const preservedAttachment = field && (!attachment || attachment.mode === "preserve") && (0, import_layoutCore.own)(authored, field) ? authored[field] : void 0;
|
|
1785
|
+
for (const [key, rawValue] of Object.entries(document)) {
|
|
1786
|
+
if (role === "folder-control") {
|
|
1787
|
+
if (key === "kind") continue;
|
|
1788
|
+
if (key === "path") {
|
|
1789
|
+
const path = document.path;
|
|
1790
|
+
const name = Array.isArray(path) ? path.at(-1) : void 0;
|
|
1791
|
+
if (typeof name !== "string") return void 0;
|
|
1792
|
+
const override = requiredNameOverride(directoryComponent(destination), "directory", "", name);
|
|
1793
|
+
if (override !== void 0) output.name = override;
|
|
1794
|
+
continue;
|
|
1795
|
+
}
|
|
1796
|
+
} else if (role === "document") {
|
|
1797
|
+
if (key === "folder") continue;
|
|
1798
|
+
if (key === "name") {
|
|
1799
|
+
if (typeof rawValue !== "string") return void 0;
|
|
1800
|
+
const override = requiredNameOverride(component(destination), "document", ".json", rawValue);
|
|
1801
|
+
if (override !== void 0) output.name = override;
|
|
1802
|
+
continue;
|
|
1803
|
+
}
|
|
1804
|
+
} else if (import_layoutCore.FILENAME_NAME_ROLES.has(role) && key === "name") {
|
|
1805
|
+
if (typeof rawValue !== "string") return void 0;
|
|
1806
|
+
const override = requiredNameOverride(component(destination), "document", ".json", rawValue);
|
|
1807
|
+
if (override !== void 0) output.name = override;
|
|
1808
|
+
continue;
|
|
1809
|
+
}
|
|
1810
|
+
if (field === key && attachment?.mode === "set") {
|
|
1811
|
+
if (!attachment.destination.bytes) return void 0;
|
|
1812
|
+
output[key] = { file: attachment.destination.bytes };
|
|
1813
|
+
} else if (field === key && preservedAttachment !== void 0) output[key] = (0, import_layoutCore.clone)(preservedAttachment);
|
|
1814
|
+
else output[key] = (0, import_layoutCore.clone)(rawValue);
|
|
1815
|
+
}
|
|
1816
|
+
let lowered = output;
|
|
1817
|
+
if (field && preservedAttachment !== void 0 && !(0, import_layoutCore.own)(lowered, field))
|
|
1818
|
+
lowered = withLocalField(lowered, authored, field, preservedAttachment);
|
|
1819
|
+
if ((0, import_layoutCore.own)(authored, "shares")) lowered = withLocalField(lowered, authored, "shares", authored.shares);
|
|
1820
|
+
return lowered;
|
|
1821
|
+
}
|
|
1822
|
+
function outputRole(artifact, value) {
|
|
1823
|
+
if (artifact.role === "raw-metadata") return "raw-metadata";
|
|
1824
|
+
if (artifact.role === "share-group") return "share-group";
|
|
1825
|
+
return value.kind === "dimension_table" ? "combined-table" : "document";
|
|
1826
|
+
}
|
|
1827
|
+
function shareEntryIndex(index, proofs, entries, selector, source) {
|
|
1828
|
+
if (!("transient" in selector.identity)) return shareIdentityIndex(entries, selector);
|
|
1829
|
+
const observation = observationForGrouped(index, source, selector);
|
|
1830
|
+
if (!observation?.currentHash) return -1;
|
|
1831
|
+
const proven = proofs.filter(
|
|
1832
|
+
(proof) => proof.source.token === selector.identity.transient && (0, import_layoutCore.sameSelector)(proof.selector, selector) && (0, import_layoutCore.samePath)(proof.sourcePath, observation.sourcePath) && proof.fragmentHash === observation.currentHash
|
|
1833
|
+
);
|
|
1834
|
+
if (proven.length !== 1) return -1;
|
|
1835
|
+
const sourcePath = observation.sourcePath;
|
|
1836
|
+
const position = sourcePath.length === 2 && sourcePath[0] === "shares" ? sourcePath[1] : void 0;
|
|
1837
|
+
const entry = typeof position === "number" ? (0, import_layoutCore.object)(entries[position]) : void 0;
|
|
1838
|
+
return entry && entry.id === void 0 && entry.key === void 0 ? position : -1;
|
|
1839
|
+
}
|
|
1840
|
+
function shareIdentityIndex(entries, selector) {
|
|
1841
|
+
if ("transient" in selector.identity) return -1;
|
|
1842
|
+
const field = "id" in selector.identity ? "id" : "key";
|
|
1843
|
+
const value = "id" in selector.identity ? selector.identity.id : selector.identity.key;
|
|
1844
|
+
const matches = entries.flatMap((entry, index) => {
|
|
1845
|
+
const candidate = (0, import_layoutCore.object)(entry);
|
|
1846
|
+
return candidate?.[field] === value ? [index] : [];
|
|
1847
|
+
});
|
|
1848
|
+
return matches.length === 1 ? matches[0] : -1;
|
|
1849
|
+
}
|
|
1850
|
+
function groupedPatchKey(selector) {
|
|
1851
|
+
return (0, import_documents.canonicalText)(selector);
|
|
1852
|
+
}
|
|
1853
|
+
function rawPatchKey(set) {
|
|
1854
|
+
return set.destination.sidecar;
|
|
1855
|
+
}
|
|
1856
|
+
function ownerNavigationTarget(context, ownerKey2) {
|
|
1857
|
+
const source = (0, import_layoutCore.stableSource)(ownerKey2);
|
|
1858
|
+
if (!source) return void 0;
|
|
1859
|
+
const key = (0, import_layoutCore.sourceIdentityKey)(source);
|
|
1860
|
+
const grouped = context.grouped.sources.filter(
|
|
1861
|
+
(mapping) => (0, import_layoutCore.sourceIdentityKey)(mapping.source) === key && mapping.selector.slot === "document"
|
|
1862
|
+
);
|
|
1863
|
+
if (grouped.length === 1) return { file: grouped[0].selector.file };
|
|
1864
|
+
const raw = context.binary.rawSources.filter((mapping) => (0, import_layoutCore.sourceIdentityKey)(mapping.source) === key);
|
|
1865
|
+
return raw.length === 1 ? { file: raw[0].selector.sidecar } : void 0;
|
|
1866
|
+
}
|
|
1867
|
+
function containerFragmentsAreClean(context, file) {
|
|
1868
|
+
return context.grouped.observations.every(
|
|
1869
|
+
(observation) => observation.selector.file !== file || observation.state === "clean"
|
|
1870
|
+
) && context.binary.rawObservations.every(
|
|
1871
|
+
(observation) => observation.selector.slot !== "file-entry" || observation.selector.sidecar !== file || observation.state === "clean"
|
|
1872
|
+
);
|
|
1873
|
+
}
|
|
1874
|
+
const KEYED_CHILD_KINDS = /* @__PURE__ */ new Set(["sheet_line", "tile"]);
|
|
1875
|
+
function withoutId(value) {
|
|
1876
|
+
return (0, import_layoutCore.without)(value, ["id"]);
|
|
1877
|
+
}
|
|
1878
|
+
function setAssignedId(document, location, id) {
|
|
1879
|
+
let value = document;
|
|
1880
|
+
for (const part of location.path.slice(0, -1)) value = value[part];
|
|
1881
|
+
value[String(location.path[location.path.length - 1])] = id;
|
|
1882
|
+
}
|
|
1883
|
+
function removeAssignedLocalKeys(patches) {
|
|
1884
|
+
const references = /* @__PURE__ */ new Map();
|
|
1885
|
+
for (const { target, kind, id } of patches) {
|
|
1886
|
+
const key = target.actual.key;
|
|
1887
|
+
if (!KEYED_CHILD_KINDS.has(kind) || typeof key !== "string" || target.submitted.id !== void 0) continue;
|
|
1888
|
+
delete target.actual.key;
|
|
1889
|
+
if (kind !== "sheet_line" || !target.siblings) continue;
|
|
1890
|
+
const axes = references.get(target.siblings) ?? /* @__PURE__ */ new Map();
|
|
1891
|
+
const keys = axes.get(target.actual.axis) ?? /* @__PURE__ */ new Map();
|
|
1892
|
+
keys.set(key, id);
|
|
1893
|
+
axes.set(target.actual.axis, keys);
|
|
1894
|
+
references.set(target.siblings, axes);
|
|
1895
|
+
}
|
|
1896
|
+
for (const [siblings, axes] of references)
|
|
1897
|
+
for (const sibling of siblings) {
|
|
1898
|
+
const line = (0, import_layoutCore.object)(sibling);
|
|
1899
|
+
if (!line) continue;
|
|
1900
|
+
const keys = axes.get(line.axis);
|
|
1901
|
+
if (!keys) continue;
|
|
1902
|
+
if (typeof line.parent === "string" && keys.has(line.parent)) line.parent = keys.get(line.parent);
|
|
1903
|
+
const comparison = (0, import_layoutCore.object)(line.comparison);
|
|
1904
|
+
if (comparison) {
|
|
1905
|
+
for (const field of ["line1", "line2"])
|
|
1906
|
+
if (typeof comparison[field] === "string" && keys.has(comparison[field]))
|
|
1907
|
+
comparison[field] = keys.get(comparison[field]);
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
function locatePatchTarget(current, original, expected, location, assignedId) {
|
|
1912
|
+
let actual = current;
|
|
1913
|
+
let submitted = original;
|
|
1914
|
+
let assigned = expected;
|
|
1915
|
+
let siblings;
|
|
1916
|
+
let field = "";
|
|
1917
|
+
for (const part of location.path.slice(0, -1)) {
|
|
1918
|
+
if (typeof part === "number") {
|
|
1919
|
+
if (!Array.isArray(actual) || !Array.isArray(submitted) || !Array.isArray(assigned)) return void 0;
|
|
1920
|
+
const child = (0, import_layoutCore.object)(submitted[part]);
|
|
1921
|
+
const expectedChild = (0, import_layoutCore.object)(assigned[part]);
|
|
1922
|
+
if (!child || !expectedChild) return void 0;
|
|
1923
|
+
const keyed = (field === "tiles" || field === "lines") && typeof child.key === "string" && child.id === void 0;
|
|
1924
|
+
const sameKey = (candidate) => candidate.key === child.key && (field !== "lines" || candidate.axis === child.axis);
|
|
1925
|
+
if (keyed && actual.filter((item) => (0, import_layoutCore.object)(item) && sameKey((0, import_layoutCore.object)(item))).length > 1) return void 0;
|
|
1926
|
+
const matches = actual.filter((item) => {
|
|
1927
|
+
const candidate = (0, import_layoutCore.object)(item);
|
|
1928
|
+
if (!candidate) return false;
|
|
1929
|
+
return expectedChild.id !== void 0 && candidate.id === expectedChild.id || child.id !== void 0 && candidate.id === child.id || candidate.id === void 0 && (keyed && sameKey(candidate) || (0, import_documents.canonicalText)(withoutId(candidate)) === (0, import_documents.canonicalText)(withoutId(child)) || (0, import_documents.canonicalText)(withoutId(candidate)) === (0, import_documents.canonicalText)(withoutId(expectedChild)));
|
|
1930
|
+
});
|
|
1931
|
+
if (matches.length !== 1) return void 0;
|
|
1932
|
+
siblings = actual;
|
|
1933
|
+
actual = matches[0];
|
|
1934
|
+
submitted = child;
|
|
1935
|
+
assigned = expectedChild;
|
|
1936
|
+
} else {
|
|
1937
|
+
if (!(0, import_layoutCore.object)(actual) || !(0, import_layoutCore.object)(submitted) || !(0, import_layoutCore.object)(assigned)) return void 0;
|
|
1938
|
+
actual = actual[part];
|
|
1939
|
+
submitted = submitted[part];
|
|
1940
|
+
assigned = assigned[part];
|
|
1941
|
+
field = part;
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
const target = (0, import_layoutCore.object)(actual);
|
|
1945
|
+
const source = (0, import_layoutCore.object)(submitted);
|
|
1946
|
+
if (!target || !source) return void 0;
|
|
1947
|
+
if (location.path[location.path.length - 1] !== "id" || target.id !== void 0 && target.id !== assignedId)
|
|
1948
|
+
return void 0;
|
|
1949
|
+
if (KEYED_CHILD_KINDS.has(location.kind) && target.key !== void 0 && target.key !== source.key) return void 0;
|
|
1950
|
+
return { actual: target, submitted: source, siblings };
|
|
1951
|
+
}
|
|
1952
|
+
function applyIdentityPatch(host, patch) {
|
|
1953
|
+
const expected = (0, import_layoutCore.clone)(patch.submittedDocument);
|
|
1954
|
+
for (const item of patch.assignments) setAssignedId(expected, item.location, item.assignment.id);
|
|
1955
|
+
removeAssignedLocalKeys(
|
|
1956
|
+
patch.assignments.map((item) => ({
|
|
1957
|
+
target: {
|
|
1958
|
+
actual: (0, import_layoutCore.object)(pathValue(expected, item.location.path.slice(0, -1))),
|
|
1959
|
+
submitted: (0, import_layoutCore.object)(pathValue(patch.submittedDocument, item.location.path.slice(0, -1))),
|
|
1960
|
+
siblings: item.location.kind === "sheet_line" ? pathValue(expected, item.location.path.slice(0, -2)) : void 0
|
|
1961
|
+
},
|
|
1962
|
+
kind: item.location.kind,
|
|
1963
|
+
id: item.assignment.id
|
|
1964
|
+
}))
|
|
1965
|
+
);
|
|
1966
|
+
const located = patch.assignments.map((item) => ({
|
|
1967
|
+
item,
|
|
1968
|
+
target: locatePatchTarget(host, patch.submittedDocument, expected, item.location, item.assignment.id)
|
|
1969
|
+
}));
|
|
1970
|
+
const ambiguous = located.filter((entry) => !entry.target);
|
|
1971
|
+
if (ambiguous.length > 0)
|
|
1972
|
+
return {
|
|
1973
|
+
status: "ambiguous",
|
|
1974
|
+
keys: ambiguous.map((entry) => `${entry.item.assignment.kind}/${entry.item.assignment.id}`)
|
|
1975
|
+
};
|
|
1976
|
+
for (const entry of located) entry.target.actual.id = entry.item.assignment.id;
|
|
1977
|
+
removeAssignedLocalKeys(
|
|
1978
|
+
located.map((entry) => ({
|
|
1979
|
+
target: entry.target,
|
|
1980
|
+
kind: entry.item.location.kind,
|
|
1981
|
+
id: entry.item.assignment.id
|
|
1982
|
+
}))
|
|
1983
|
+
);
|
|
1984
|
+
if (patch.assignments.some((item) => item.location.path.length === 1)) delete host.key;
|
|
1985
|
+
return { status: "patched" };
|
|
1986
|
+
}
|
|
1987
|
+
function pathValue(value, parts) {
|
|
1988
|
+
return parts.reduce((current, part) => current[part], value);
|
|
1989
|
+
}
|
|
1990
|
+
function renderIdentityPatches(readback, index, json, notices) {
|
|
1991
|
+
const removed = new Set(
|
|
1992
|
+
readback.deletes.json.map((removal) => removal.path).concat(readback.deletes.bytes.map((removal) => removal.path))
|
|
1993
|
+
);
|
|
1994
|
+
for (const patch of readback.identityPatches) {
|
|
1995
|
+
if (removed.has(patch.container)) continue;
|
|
1996
|
+
const target = json.find((candidate) => candidate.path === patch.container);
|
|
1997
|
+
const artifact = authoredArtifact(index, patch.container);
|
|
1998
|
+
const authored = artifact && (0, import_layoutCore.object)(artifact.json);
|
|
1999
|
+
if (!artifact || !authored) {
|
|
2000
|
+
for (const item of patch.assignments)
|
|
2001
|
+
notices.push({
|
|
2002
|
+
status: "ambiguous-patch",
|
|
2003
|
+
path: patch.container,
|
|
2004
|
+
key: `${item.assignment.kind}/${item.assignment.id}`
|
|
2005
|
+
});
|
|
2006
|
+
continue;
|
|
2007
|
+
}
|
|
2008
|
+
const container = target ? target.value : (0, import_layoutCore.clone)(authored);
|
|
2009
|
+
let host;
|
|
2010
|
+
if (patch.slot === "share-entry") {
|
|
2011
|
+
const shares = Array.isArray(container.shares) ? container.shares : [];
|
|
2012
|
+
const at = patch.shareIdentity ? shareEntryIndex(
|
|
2013
|
+
index,
|
|
2014
|
+
readback.submissionProofs,
|
|
2015
|
+
shares,
|
|
2016
|
+
{ file: patch.container, slot: "share-entry", identity: patch.shareIdentity },
|
|
2017
|
+
patch.source
|
|
2018
|
+
) : -1;
|
|
2019
|
+
host = at < 0 ? void 0 : (0, import_layoutCore.object)(shares[at]);
|
|
2020
|
+
} else host = container;
|
|
2021
|
+
if (!host) {
|
|
2022
|
+
notices.push({
|
|
2023
|
+
status: "ambiguous-patch",
|
|
2024
|
+
path: patch.container,
|
|
2025
|
+
key: `${patch.assignments[0].assignment.kind}/${patch.assignments[0].assignment.id}`
|
|
2026
|
+
});
|
|
2027
|
+
continue;
|
|
2028
|
+
}
|
|
2029
|
+
const result = applyIdentityPatch(host, patch);
|
|
2030
|
+
if (result.status === "ambiguous") {
|
|
2031
|
+
for (const key of result.keys) notices.push({ status: "ambiguous-patch", path: patch.container, key });
|
|
2032
|
+
continue;
|
|
2033
|
+
}
|
|
2034
|
+
if (!target)
|
|
2035
|
+
json.push({
|
|
2036
|
+
path: patch.container,
|
|
2037
|
+
role: outputRole(artifact, container),
|
|
2038
|
+
value: container,
|
|
2039
|
+
authorizingDocumentKeys: []
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
2043
|
+
function renderRemovals(readback, index, json, diagnostics, notices, retainedKeys) {
|
|
2044
|
+
const { deletes } = readback;
|
|
2045
|
+
const removals = [];
|
|
2046
|
+
const rendered = new Map(json.map((artifact) => [artifact.path, artifact]));
|
|
2047
|
+
const rewrites = /* @__PURE__ */ new Map();
|
|
2048
|
+
const deletedPaths = new Set(deletes.json.map((removal) => removal.path));
|
|
2049
|
+
const container = (path, role, documentKey) => {
|
|
2050
|
+
const target = rendered.get(path);
|
|
2051
|
+
if (target) return target.value;
|
|
2052
|
+
const existing = rewrites.get(path);
|
|
2053
|
+
if (existing) {
|
|
2054
|
+
existing.keys.add(documentKey);
|
|
2055
|
+
return existing.value;
|
|
2056
|
+
}
|
|
2057
|
+
const artifact = authoredArtifact(index, path);
|
|
2058
|
+
const authored = artifact && (0, import_layoutCore.object)(artifact.json);
|
|
2059
|
+
if (!artifact || !authored) {
|
|
2060
|
+
notices.push({ status: "unmaterialized", path, key: documentKey });
|
|
2061
|
+
retainedKeys.push(documentKey);
|
|
2062
|
+
return void 0;
|
|
2063
|
+
}
|
|
2064
|
+
const entry = { role, value: (0, import_layoutCore.clone)(authored), keys: /* @__PURE__ */ new Set([documentKey]) };
|
|
2065
|
+
rewrites.set(path, entry);
|
|
2066
|
+
return entry.value;
|
|
2067
|
+
};
|
|
2068
|
+
for (const removal of deletes.groupEntries) {
|
|
2069
|
+
const selector = removal.entry;
|
|
2070
|
+
const value = container(removal.container, "share-group", removal.documentKey);
|
|
2071
|
+
if (!value) continue;
|
|
2072
|
+
const shares = Array.isArray(value.shares) ? value.shares : void 0;
|
|
2073
|
+
const at = selector.slot === "share-entry" && shares ? shareIdentityIndex(shares, selector) : -1;
|
|
2074
|
+
if (!shares || at < 0) {
|
|
2075
|
+
notices.push({ status: "unmaterialized", path: removal.container, key: removal.documentKey });
|
|
2076
|
+
retainedKeys.push(removal.documentKey);
|
|
2077
|
+
continue;
|
|
2078
|
+
}
|
|
2079
|
+
value.shares = shares.filter((_, position) => position !== at);
|
|
2080
|
+
}
|
|
2081
|
+
for (const pointer of deletes.pointers) {
|
|
2082
|
+
if (deletedPaths.has(pointer.owner)) continue;
|
|
2083
|
+
const artifact = authoredArtifact(index, pointer.owner);
|
|
2084
|
+
const authored = artifact && (0, import_layoutCore.object)(artifact.json);
|
|
2085
|
+
const value = container(
|
|
2086
|
+
pointer.owner,
|
|
2087
|
+
artifact && authored ? outputRole(artifact, authored) : "document",
|
|
2088
|
+
pointer.documentKey
|
|
2089
|
+
);
|
|
2090
|
+
if (value) delete value.shares;
|
|
2091
|
+
}
|
|
2092
|
+
for (const [path, entry] of rewrites)
|
|
2093
|
+
removals.push({
|
|
2094
|
+
kind: "rewrite",
|
|
2095
|
+
path,
|
|
2096
|
+
role: entry.role,
|
|
2097
|
+
value: entry.value,
|
|
2098
|
+
documentKeys: [...entry.keys].sort(import_layoutCore.compareText)
|
|
2099
|
+
});
|
|
2100
|
+
for (const removal of deletes.json)
|
|
2101
|
+
removals.push({ kind: "file", path: removal.path, documentKeys: [...removal.documentKeys] });
|
|
2102
|
+
for (const removal of deletes.bytes)
|
|
2103
|
+
removals.push({ kind: "bytes", path: removal.path, documentKeys: [...removal.documentKeys] });
|
|
2104
|
+
for (const removal of deletes.directories)
|
|
2105
|
+
removals.push({ kind: "directory", path: removal.path, documentKeys: [...removal.documentKeys] });
|
|
2106
|
+
return removals;
|
|
2107
|
+
}
|
|
2108
|
+
function planReceiptRendering(input) {
|
|
2109
|
+
const excluded = /* @__PURE__ */ new Set();
|
|
2110
|
+
const reasons = [];
|
|
2111
|
+
for (; ; ) {
|
|
2112
|
+
const result = renderReceiptAttempt(input, excluded);
|
|
2113
|
+
if (result.status !== "retry") return result;
|
|
2114
|
+
for (const key of result.keys) excluded.add(key);
|
|
2115
|
+
reasons.push(...result.diagnostics);
|
|
2116
|
+
input = { ...input, unmaterializedDiagnostics: reasons };
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
function renderReceiptAttempt(input, excluded) {
|
|
2120
|
+
const { readback } = input;
|
|
2121
|
+
const context = readback.renderingContext;
|
|
2122
|
+
const index = renderingIndex(context);
|
|
2123
|
+
const diagnostics = [];
|
|
2124
|
+
const perResource = [];
|
|
2125
|
+
const groupedSets = readback.materializations.filter(
|
|
2126
|
+
(item) => item.mode === "set" && item.codec === "grouped" && !excluded.has(item.documentKey)
|
|
2127
|
+
);
|
|
2128
|
+
const rawSets = readback.materializations.filter(
|
|
2129
|
+
(item) => item.mode === "set" && item.codec === "raw" && !excluded.has(item.documentKey)
|
|
2130
|
+
);
|
|
2131
|
+
const groupedTargets = /* @__PURE__ */ new Set();
|
|
2132
|
+
const rawTargets = /* @__PURE__ */ new Set();
|
|
2133
|
+
const groupedByFile = /* @__PURE__ */ new Map();
|
|
2134
|
+
const placedRaw = [];
|
|
2135
|
+
const notices = [];
|
|
2136
|
+
const unmaterialized = (path, keys) => {
|
|
2137
|
+
for (const key of keys.length > 0 ? keys : ["workspace"]) notices.push({ status: "unmaterialized", path, key });
|
|
2138
|
+
};
|
|
2139
|
+
const unplaceable = (keys, item) => {
|
|
2140
|
+
perResource.push({ keys, diagnostic: item });
|
|
2141
|
+
unmaterialized(item.path, keys);
|
|
2142
|
+
};
|
|
2143
|
+
const groupedOrigin = /* @__PURE__ */ new Map();
|
|
2144
|
+
const origins = [];
|
|
2145
|
+
const directoryRenames = [];
|
|
2146
|
+
for (const set of groupedSets) {
|
|
2147
|
+
if (set.patches.length !== 1) {
|
|
2148
|
+
unplaceable(
|
|
2149
|
+
[set.documentKey],
|
|
2150
|
+
(0, import_layoutCore.diagnostic)("invalid-render-patches", set.destination.file, "Grouped set needs one patch")
|
|
2151
|
+
);
|
|
2152
|
+
continue;
|
|
2153
|
+
}
|
|
2154
|
+
const patch = set.patches[0];
|
|
2155
|
+
const key = groupedPatchKey(set.destination);
|
|
2156
|
+
if (groupedTargets.has(key)) {
|
|
2157
|
+
unplaceable(
|
|
2158
|
+
[set.documentKey],
|
|
2159
|
+
(0, import_layoutCore.diagnostic)("duplicate-render-target", set.destination.file, "Grouped target is duplicated")
|
|
2160
|
+
);
|
|
2161
|
+
continue;
|
|
2162
|
+
}
|
|
2163
|
+
groupedTargets.add(key);
|
|
2164
|
+
const inserted = set.placement === "insert-authoritative";
|
|
2165
|
+
const observation = observationForGrouped(index, patch.source, patch.selector);
|
|
2166
|
+
if (patch.action !== "set" || patch.value === void 0 || !(0, import_layoutCore.sameSelector)(patch.selector, set.origin ?? set.destination) || !inserted && !observation?.currentHash) {
|
|
2167
|
+
unplaceable(
|
|
2168
|
+
[set.documentKey],
|
|
2169
|
+
(0, import_layoutCore.diagnostic)("stale-render-source", patch.selector.file, "Grouped patch does not match captured current")
|
|
2170
|
+
);
|
|
2171
|
+
continue;
|
|
2172
|
+
}
|
|
2173
|
+
const originFile = (set.origin ?? set.destination).file;
|
|
2174
|
+
const known = groupedOrigin.get(set.destination.file);
|
|
2175
|
+
if (known !== void 0 && known !== originFile) {
|
|
2176
|
+
unplaceable(
|
|
2177
|
+
[set.documentKey],
|
|
2178
|
+
(0, import_layoutCore.diagnostic)(
|
|
2179
|
+
"invalid-render-container",
|
|
2180
|
+
set.destination.file,
|
|
2181
|
+
"Container fragments disagree about their current file"
|
|
2182
|
+
)
|
|
2183
|
+
);
|
|
2184
|
+
continue;
|
|
2185
|
+
}
|
|
2186
|
+
groupedOrigin.set(set.destination.file, originFile);
|
|
2187
|
+
groupedByFile.set(set.destination.file, [...groupedByFile.get(set.destination.file) ?? [], { set, patch }]);
|
|
2188
|
+
}
|
|
2189
|
+
for (const set of rawSets) {
|
|
2190
|
+
const patch = set.patch;
|
|
2191
|
+
const key = rawPatchKey(set);
|
|
2192
|
+
if (rawTargets.has(key)) {
|
|
2193
|
+
unplaceable(
|
|
2194
|
+
[set.documentKey],
|
|
2195
|
+
(0, import_layoutCore.diagnostic)("duplicate-render-target", set.destination.sidecar, "Raw sidecar is duplicated")
|
|
2196
|
+
);
|
|
2197
|
+
continue;
|
|
2198
|
+
}
|
|
2199
|
+
rawTargets.add(key);
|
|
2200
|
+
const inserted = set.placement === "insert-authoritative";
|
|
2201
|
+
const observation = observationForRaw(index, patch.source, patch.selector);
|
|
2202
|
+
if (patch.action !== "set" || !patch.document || !patch.metadata || !patch.content || !sameRawSelector(patch.selector, set.origin ?? set.destination) || // The patch names the bytes its current source has; the set names where they end up.
|
|
2203
|
+
patch.bytes !== (inserted ? set.bytes : observation?.bytes ?? set.bytes) || !inserted && (!observation?.currentHash || !originalBytesMatch(index, observation))) {
|
|
2204
|
+
unplaceable(
|
|
2205
|
+
[set.documentKey],
|
|
2206
|
+
(0, import_layoutCore.diagnostic)("stale-render-source", patch.selector.sidecar, "Raw patch does not match captured current")
|
|
2207
|
+
);
|
|
2208
|
+
continue;
|
|
2209
|
+
}
|
|
2210
|
+
if (observation?.bytes && observation.bytes !== set.bytes)
|
|
2211
|
+
origins.push({ kind: "bytes", path: observation.bytes });
|
|
2212
|
+
placedRaw.push(set);
|
|
2213
|
+
}
|
|
2214
|
+
const attachmentByOwner = /* @__PURE__ */ new Map();
|
|
2215
|
+
const bytes = [];
|
|
2216
|
+
const ownerSets = new Map(groupedSets.map((set) => [set.documentKey, set]));
|
|
2217
|
+
for (const attachment of readback.attachments) {
|
|
2218
|
+
const owner = ownerSets.get(attachment.ownerDocumentKey);
|
|
2219
|
+
const field = "patch" in attachment ? attachment.patch.field : attachment.field;
|
|
2220
|
+
const target = (0, import_documents.canonicalText)([attachment.ownerDocumentKey, field]);
|
|
2221
|
+
if (attachmentByOwner.has(target)) {
|
|
2222
|
+
unplaceable(
|
|
2223
|
+
[attachment.ownerDocumentKey],
|
|
2224
|
+
(0, import_layoutCore.diagnostic)("duplicate-render-target", attachment.ownerDocumentKey, "Attachment field is duplicated")
|
|
2225
|
+
);
|
|
2226
|
+
continue;
|
|
2227
|
+
}
|
|
2228
|
+
attachmentByOwner.set(target, attachment);
|
|
2229
|
+
if (!owner || owner.destination.file !== ("destination" in attachment ? attachment.destination.ownerFile : attachment.ownerFile)) {
|
|
2230
|
+
unplaceable(
|
|
2231
|
+
[attachment.ownerDocumentKey],
|
|
2232
|
+
(0, import_layoutCore.diagnostic)("unpaired-render-attachment", attachment.ownerDocumentKey, "Attachment lacks its owner set")
|
|
2233
|
+
);
|
|
2234
|
+
continue;
|
|
2235
|
+
}
|
|
2236
|
+
if (!("patch" in attachment)) continue;
|
|
2237
|
+
const patch = attachment.patch;
|
|
2238
|
+
const observation = observationForAttachment(index, patch);
|
|
2239
|
+
if (owner.placement !== "insert-authoritative" && (!observation?.currentHash || !originalBytesMatch(index, observation))) {
|
|
2240
|
+
unplaceable(
|
|
2241
|
+
[attachment.ownerDocumentKey],
|
|
2242
|
+
(0, import_layoutCore.diagnostic)("stale-render-source", patch.ownerFile, "Attachment patch does not match captured current")
|
|
2243
|
+
);
|
|
2244
|
+
continue;
|
|
2245
|
+
}
|
|
2246
|
+
if (attachment.mode === "set") {
|
|
2247
|
+
if (!attachment.destination.bytes || patch.value === null) {
|
|
2248
|
+
unplaceable(
|
|
2249
|
+
[attachment.ownerDocumentKey],
|
|
2250
|
+
(0, import_layoutCore.diagnostic)("invalid-render-bytes", patch.ownerFile, "Attachment set lacks final bytes")
|
|
2251
|
+
);
|
|
2252
|
+
continue;
|
|
2253
|
+
}
|
|
2254
|
+
if (observation?.bytes && observation.bytes !== attachment.destination.bytes)
|
|
2255
|
+
origins.push({ kind: "bytes", path: observation.bytes });
|
|
2256
|
+
bytes.push({
|
|
2257
|
+
kind: "attachment",
|
|
2258
|
+
ownerDocumentKey: attachment.ownerDocumentKey,
|
|
2259
|
+
field: patch.field,
|
|
2260
|
+
destination: attachment.destination.bytes,
|
|
2261
|
+
descriptor: (0, import_layoutCore.clone)(patch.value)
|
|
2262
|
+
});
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
const companionsByFile = /* @__PURE__ */ new Map();
|
|
2266
|
+
for (const companion of readback.groupedCompanions) {
|
|
2267
|
+
const patch = companion.patch;
|
|
2268
|
+
const selectedShares = groupedSets.filter(
|
|
2269
|
+
(set) => set.destination.file === patch.selector.file && set.destination.slot === "share-entry" && companion.authorizingDocumentKeys.includes(set.documentKey)
|
|
2270
|
+
);
|
|
2271
|
+
const allAuthorizersSelected = companion.authorizingDocumentKeys.length > 0 && companion.authorizingDocumentKeys.every(
|
|
2272
|
+
(key) => groupedSets.some(
|
|
2273
|
+
(set) => set.documentKey === key && set.destination.file === patch.selector.file && set.destination.slot === "share-entry"
|
|
2274
|
+
)
|
|
2275
|
+
);
|
|
2276
|
+
const observation = observationForGrouped(index, patch.source, patch.selector);
|
|
2277
|
+
const insertedGroup = selectedShares.every((set) => set.placement === "insert-authoritative");
|
|
2278
|
+
if (patch.action !== "set" || patch.selector.slot !== "share-group-owner" || selectedShares.length === 0 || !allAuthorizersSelected || companionsByFile.has(patch.selector.file) || !insertedGroup && !observation?.currentHash) {
|
|
2279
|
+
unplaceable(
|
|
2280
|
+
groupedSets.filter((set) => set.destination.file === patch.selector.file).map((set) => set.documentKey),
|
|
2281
|
+
(0, import_layoutCore.diagnostic)("invalid-render-companion", patch.selector.file, "Share header is not fully authorized")
|
|
2282
|
+
);
|
|
2283
|
+
continue;
|
|
2284
|
+
}
|
|
2285
|
+
companionsByFile.set(patch.selector.file, patch);
|
|
2286
|
+
}
|
|
2287
|
+
const vacatedFiles = new Set(
|
|
2288
|
+
[...groupedOrigin.entries()].flatMap(([destination, origin]) => origin === destination ? [] : [origin])
|
|
2289
|
+
);
|
|
2290
|
+
for (const removal of readback.deletes.json) vacatedFiles.add(removal.path);
|
|
2291
|
+
const json = [];
|
|
2292
|
+
const insertedShareGroups = /* @__PURE__ */ new Set();
|
|
2293
|
+
for (const [file, selected] of groupedByFile) {
|
|
2294
|
+
const insertedFile = selected.every(({ set }) => set.placement === "insert-authoritative");
|
|
2295
|
+
const owner = selected.find(({ patch }) => patch.selector.slot === "share-entry") ? (0, import_layoutCore.object)(selected[0].set.document)?.owner : void 0;
|
|
2296
|
+
const origin = groupedOrigin.get(file) ?? file;
|
|
2297
|
+
const takesVacated = insertedFile && origin === file && vacatedFiles.has(file);
|
|
2298
|
+
const artifact = (takesVacated ? void 0 : authoredArtifact(index, origin)) ?? (insertedFile ? insertedArtifact(file, owner) : void 0);
|
|
2299
|
+
const authored = artifact && (0, import_layoutCore.object)(artifact.json);
|
|
2300
|
+
if (!artifact || !authored || artifact.role === "raw-metadata" || artifact.role === "raw") {
|
|
2301
|
+
unmaterialized(
|
|
2302
|
+
origin,
|
|
2303
|
+
selected.map(({ set }) => set.documentKey)
|
|
2304
|
+
);
|
|
2305
|
+
continue;
|
|
2306
|
+
}
|
|
2307
|
+
if (origin !== file && artifact.role === "folder-control" && (0, import_layoutPaths.folderDirectory)(origin) !== (0, import_layoutPaths.folderDirectory)(file) && (0, import_layoutPaths.layoutCollisionKey)((0, import_layoutPaths.folderDirectory)(origin)) === (0, import_layoutPaths.layoutCollisionKey)((0, import_layoutPaths.folderDirectory)(file)))
|
|
2308
|
+
directoryRenames.push({ from: (0, import_layoutPaths.folderDirectory)(origin), to: (0, import_layoutPaths.folderDirectory)(file) });
|
|
2309
|
+
const slots = new Set(selected.map(({ patch }) => patch.selector.slot));
|
|
2310
|
+
let value;
|
|
2311
|
+
if (slots.has("table-definition") || slots.has("member-set")) {
|
|
2312
|
+
if (artifact.role === "share-group" || [...slots].some((slot) => slot !== "table-definition" && slot !== "member-set") || !(Array.isArray(authored.members) || insertedFile && authored.members === void 0)) {
|
|
2313
|
+
unplaceable(
|
|
2314
|
+
selected.map(({ set }) => set.documentKey),
|
|
2315
|
+
(0, import_layoutCore.diagnostic)("invalid-render-container", file, "Table container is incomplete")
|
|
2316
|
+
);
|
|
2317
|
+
continue;
|
|
2318
|
+
}
|
|
2319
|
+
const definition = selected.find(({ patch }) => patch.selector.slot === "table-definition");
|
|
2320
|
+
const members = selected.find(({ patch }) => patch.selector.slot === "member-set");
|
|
2321
|
+
const definitionDocument = definition?.patch.value;
|
|
2322
|
+
const lowered = definitionDocument ? lowerGroupedValue(definitionDocument, artifact, void 0, file) : (0, import_layoutCore.clone)(authored);
|
|
2323
|
+
const memberValue = members?.patch.value ?? authored.members ?? [];
|
|
2324
|
+
if (!lowered || !Array.isArray(memberValue)) {
|
|
2325
|
+
unplaceable(
|
|
2326
|
+
selected.map(({ set }) => set.documentKey),
|
|
2327
|
+
(0, import_layoutCore.diagnostic)("invalid-render-container", file, "Table fragments are invalid")
|
|
2328
|
+
);
|
|
2329
|
+
continue;
|
|
2330
|
+
}
|
|
2331
|
+
delete lowered.members;
|
|
2332
|
+
value = { ...lowered, members: (0, import_layoutCore.clone)(memberValue) };
|
|
2333
|
+
} else if (slots.has("share-entry")) {
|
|
2334
|
+
if (artifact.role !== "share-group" || [...slots].some((slot) => slot !== "share-entry")) {
|
|
2335
|
+
unplaceable(
|
|
2336
|
+
selected.map(({ set }) => set.documentKey),
|
|
2337
|
+
(0, import_layoutCore.diagnostic)("invalid-render-container", file, "Share container is invalid")
|
|
2338
|
+
);
|
|
2339
|
+
continue;
|
|
2340
|
+
}
|
|
2341
|
+
if (!(0, import_layoutCore.object)(authored.owner) || !Array.isArray(authored.shares)) {
|
|
2342
|
+
unplaceable(
|
|
2343
|
+
selected.map(({ set }) => set.documentKey),
|
|
2344
|
+
(0, import_layoutCore.diagnostic)("invalid-render-container", file, "Share container is incomplete")
|
|
2345
|
+
);
|
|
2346
|
+
continue;
|
|
2347
|
+
}
|
|
2348
|
+
const shares = (0, import_layoutCore.clone)(authored.shares);
|
|
2349
|
+
let invalid = false;
|
|
2350
|
+
for (const { set, patch } of selected) {
|
|
2351
|
+
const entry = (0, import_layoutCore.object)(patch.value);
|
|
2352
|
+
if (patch.selector.slot !== "share-entry" || !entry) {
|
|
2353
|
+
invalid = true;
|
|
2354
|
+
break;
|
|
2355
|
+
}
|
|
2356
|
+
const at = shareEntryIndex(index, readback.submissionProofs, shares, patch.selector, patch.source);
|
|
2357
|
+
if (at < 0 && set.placement !== "insert-authoritative") {
|
|
2358
|
+
invalid = true;
|
|
2359
|
+
break;
|
|
2360
|
+
}
|
|
2361
|
+
if (at < 0) shares.push((0, import_layoutCore.clone)(entry));
|
|
2362
|
+
else shares[at] = (0, import_layoutCore.clone)(entry);
|
|
2363
|
+
}
|
|
2364
|
+
const companion = companionsByFile.get(file);
|
|
2365
|
+
if (invalid || companion && !(0, import_layoutCore.object)(companion.value)) {
|
|
2366
|
+
unplaceable(
|
|
2367
|
+
selected.map(({ set }) => set.documentKey),
|
|
2368
|
+
(0, import_layoutCore.diagnostic)("invalid-render-container", file, "Selected share entry is missing")
|
|
2369
|
+
);
|
|
2370
|
+
continue;
|
|
2371
|
+
}
|
|
2372
|
+
value = (0, import_layoutCore.clone)(authored);
|
|
2373
|
+
value.owner = (0, import_layoutCore.clone)(companion?.value ?? authored.owner);
|
|
2374
|
+
value.shares = shares;
|
|
2375
|
+
if (insertedFile) insertedShareGroups.add(file);
|
|
2376
|
+
} else {
|
|
2377
|
+
if (selected.length !== 1 || selected[0].patch.selector.slot !== "document") {
|
|
2378
|
+
unplaceable(
|
|
2379
|
+
selected.map(({ set: set2 }) => set2.documentKey),
|
|
2380
|
+
(0, import_layoutCore.diagnostic)("invalid-render-container", file, "Document container is ambiguous")
|
|
2381
|
+
);
|
|
2382
|
+
continue;
|
|
2383
|
+
}
|
|
2384
|
+
const { set, patch } = selected[0];
|
|
2385
|
+
if (!(0, import_layoutCore.object)(patch.value)) {
|
|
2386
|
+
unplaceable(
|
|
2387
|
+
selected.map(({ set: set2 }) => set2.documentKey),
|
|
2388
|
+
(0, import_layoutCore.diagnostic)("invalid-render-container", file, "Document patch is not an object")
|
|
2389
|
+
);
|
|
2390
|
+
continue;
|
|
2391
|
+
}
|
|
2392
|
+
const field = (0, import_layoutCore.attachmentField)(set.document);
|
|
2393
|
+
const attachment = field ? attachmentByOwner.get((0, import_documents.canonicalText)([set.documentKey, field])) : void 0;
|
|
2394
|
+
if (field && !attachment) {
|
|
2395
|
+
unmaterialized(file, [set.documentKey]);
|
|
2396
|
+
continue;
|
|
2397
|
+
}
|
|
2398
|
+
value = lowerGroupedValue(patch.value, artifact, attachment, file);
|
|
2399
|
+
if (!value) {
|
|
2400
|
+
unplaceable(
|
|
2401
|
+
selected.map(({ set: set2 }) => set2.documentKey),
|
|
2402
|
+
(0, import_layoutCore.diagnostic)("invalid-render-container", file, "Document cannot be lowered")
|
|
2403
|
+
);
|
|
2404
|
+
continue;
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
json.push({
|
|
2408
|
+
path: file,
|
|
2409
|
+
role: outputRole(artifact, value),
|
|
2410
|
+
value,
|
|
2411
|
+
authorizingDocumentKeys: selected.map(({ set }) => set.documentKey)
|
|
2412
|
+
});
|
|
2413
|
+
if (origin !== file) {
|
|
2414
|
+
origins.push({ kind: "file", path: origin });
|
|
2415
|
+
if (artifact.role === "folder-control") origins.push({ kind: "directory", path: (0, import_layoutPaths.folderDirectory)(origin) });
|
|
2416
|
+
origins.push(...detachedBuckets(origin));
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
const vacatedSidecars = new Set(readback.deletes.json.map((removal) => removal.path));
|
|
2420
|
+
for (const set of placedRaw)
|
|
2421
|
+
if (set.origin && set.origin.sidecar !== set.destination.sidecar) vacatedSidecars.add(set.origin.sidecar);
|
|
2422
|
+
for (const set of placedRaw) {
|
|
2423
|
+
const destination = set.destination.sidecar;
|
|
2424
|
+
const origin = (set.origin ?? set.destination).sidecar;
|
|
2425
|
+
const inserted = set.placement === "insert-authoritative";
|
|
2426
|
+
const takesVacated = inserted && origin === destination && vacatedSidecars.has(destination);
|
|
2427
|
+
const artifact = (takesVacated ? void 0 : authoredArtifact(index, origin, "raw-metadata")) ?? (inserted ? insertedArtifact(destination, void 0) : void 0);
|
|
2428
|
+
const authored = artifact && (0, import_layoutCore.object)(artifact.json);
|
|
2429
|
+
if (!artifact || !authored) {
|
|
2430
|
+
unmaterialized(origin, [set.documentKey]);
|
|
2431
|
+
continue;
|
|
2432
|
+
}
|
|
2433
|
+
const patchMetadata = set.patch.metadata;
|
|
2434
|
+
let value = (0, import_layoutCore.clone)(patchMetadata);
|
|
2435
|
+
if ((0, import_layoutCore.own)(authored, "shares")) value = withLocalField(value, authored, "shares", authored.shares);
|
|
2436
|
+
json.push({ path: destination, role: "raw-metadata", value, authorizingDocumentKeys: [set.documentKey] });
|
|
2437
|
+
bytes.push({
|
|
2438
|
+
kind: "raw",
|
|
2439
|
+
documentKey: set.documentKey,
|
|
2440
|
+
destination: set.bytes,
|
|
2441
|
+
descriptor: (0, import_layoutCore.clone)(set.patch.content)
|
|
2442
|
+
});
|
|
2443
|
+
if (origin !== destination) origins.push({ kind: "file", path: origin }, ...detachedBuckets(origin));
|
|
2444
|
+
}
|
|
2445
|
+
const renderedOwners = /* @__PURE__ */ new Map();
|
|
2446
|
+
for (const artifact of json)
|
|
2447
|
+
if (artifact.role !== "share-group")
|
|
2448
|
+
for (const key of artifact.authorizingDocumentKeys) renderedOwners.set(key, artifact);
|
|
2449
|
+
const pointsHere = (host, file) => !!host && (0, import_layoutCore.own)(host, "shares") && (0, import_documents.canonicalText)(host.shares) === (0, import_documents.canonicalText)({ file });
|
|
2450
|
+
const dropGroup = (artifact) => {
|
|
2451
|
+
json.splice(json.indexOf(artifact), 1);
|
|
2452
|
+
unplaceable(
|
|
2453
|
+
artifact.authorizingDocumentKeys,
|
|
2454
|
+
(0, import_layoutCore.diagnostic)("unlinked-generated-share", artifact.path, "The owner cannot point to this new share group")
|
|
2455
|
+
);
|
|
2456
|
+
};
|
|
2457
|
+
for (const artifact of [...json]) {
|
|
2458
|
+
if (!insertedShareGroups.has(artifact.path)) continue;
|
|
2459
|
+
const owner = (0, import_layoutCore.object)(artifact.value.owner);
|
|
2460
|
+
const ownerKey2 = owner && typeof owner.kind === "string" && typeof owner.id === "string" ? `${owner.kind}/${owner.id}` : void 0;
|
|
2461
|
+
const ownerArtifact = ownerKey2 ? renderedOwners.get(ownerKey2) : void 0;
|
|
2462
|
+
if (ownerArtifact) {
|
|
2463
|
+
const host2 = ownerArtifact.value;
|
|
2464
|
+
if (!(0, import_layoutCore.own)(host2, "shares")) ownerArtifact.value = { ...host2, shares: { file: artifact.path } };
|
|
2465
|
+
else if (!pointsHere(host2, artifact.path)) dropGroup(artifact);
|
|
2466
|
+
continue;
|
|
2467
|
+
}
|
|
2468
|
+
const patched = readback.identityPatches.find((patch) => patch.documentKey === ownerKey2);
|
|
2469
|
+
const route = patched ? { file: patched.container } : ownerKey2 ? ownerNavigationTarget(context, ownerKey2) : void 0;
|
|
2470
|
+
if (!route) continue;
|
|
2471
|
+
const authored = authoredArtifact(index, route.file);
|
|
2472
|
+
const value = authored && (0, import_layoutCore.object)(authored.json);
|
|
2473
|
+
const host = value;
|
|
2474
|
+
if (pointsHere(host, artifact.path)) continue;
|
|
2475
|
+
if (!containerFragmentsAreClean(context, route.file) || !authored || !value || !host || (0, import_layoutCore.own)(host, "shares")) {
|
|
2476
|
+
dropGroup(artifact);
|
|
2477
|
+
continue;
|
|
2478
|
+
}
|
|
2479
|
+
const target = json.find((candidate) => candidate.path === route.file);
|
|
2480
|
+
const container = target ? target.value : (0, import_layoutCore.clone)(value);
|
|
2481
|
+
container.shares = { file: artifact.path };
|
|
2482
|
+
if (!target)
|
|
2483
|
+
json.push({
|
|
2484
|
+
path: route.file,
|
|
2485
|
+
role: outputRole(authored, container),
|
|
2486
|
+
value: container,
|
|
2487
|
+
authorizingDocumentKeys: [...artifact.authorizingDocumentKeys]
|
|
2488
|
+
});
|
|
2489
|
+
}
|
|
2490
|
+
renderIdentityPatches(readback, index, json, notices);
|
|
2491
|
+
const retainedRemovalKeys = [];
|
|
2492
|
+
const removals = renderRemovals(readback, index, json, diagnostics, notices, retainedRemovalKeys);
|
|
2493
|
+
const jsonTargets = /* @__PURE__ */ new Set();
|
|
2494
|
+
for (const artifact of json) {
|
|
2495
|
+
if (jsonTargets.has(artifact.path))
|
|
2496
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("duplicate-render-target", artifact.path, "JSON output path is duplicated"));
|
|
2497
|
+
jsonTargets.add(artifact.path);
|
|
2498
|
+
}
|
|
2499
|
+
const byteTargets = /* @__PURE__ */ new Set();
|
|
2500
|
+
for (const materialization of bytes) {
|
|
2501
|
+
if (byteTargets.has(materialization.destination) || jsonTargets.has(materialization.destination))
|
|
2502
|
+
diagnostics.push(
|
|
2503
|
+
(0, import_layoutCore.diagnostic)("duplicate-render-target", materialization.destination, "Byte output path is duplicated")
|
|
2504
|
+
);
|
|
2505
|
+
byteTargets.add(materialization.destination);
|
|
2506
|
+
}
|
|
2507
|
+
for (const removal of removals)
|
|
2508
|
+
if (removal.kind !== "rewrite" && (jsonTargets.has(removal.path) || byteTargets.has(removal.path)))
|
|
2509
|
+
diagnostics.push(
|
|
2510
|
+
(0, import_layoutCore.diagnostic)("conflicting-removal-target", removal.path, "Removed path is also a rendered output")
|
|
2511
|
+
);
|
|
2512
|
+
const outputs = [...jsonTargets, ...byteTargets];
|
|
2513
|
+
const holdsOutput = (directory) => outputs.some((target) => target === directory || pathInside(target, directory));
|
|
2514
|
+
const vacated = new Set(removals.filter((removal) => removal.kind !== "rewrite").map((removal) => removal.path));
|
|
2515
|
+
for (const origin of origins) {
|
|
2516
|
+
if (jsonTargets.has(origin.path) || byteTargets.has(origin.path) || vacated.has(origin.path)) continue;
|
|
2517
|
+
if (origin.kind === "directory" && holdsOutput(origin.path)) continue;
|
|
2518
|
+
vacated.add(origin.path);
|
|
2519
|
+
removals.push({ kind: origin.kind, path: origin.path, documentKeys: [] });
|
|
2520
|
+
}
|
|
2521
|
+
const pending = [...new Set(perResource.flatMap((item) => item.keys))].filter((key) => !excluded.has(key));
|
|
2522
|
+
if (pending.length > 0)
|
|
2523
|
+
return { status: "retry", keys: pending, diagnostics: perResource.map((item) => item.diagnostic) };
|
|
2524
|
+
const repeated = perResource.filter((item) => item.keys.length > 0).map((item) => item.diagnostic);
|
|
2525
|
+
if (diagnostics.length > 0 || repeated.length > 0)
|
|
2526
|
+
return { status: "blocked", diagnostics: [...diagnostics, ...repeated] };
|
|
2527
|
+
return {
|
|
2528
|
+
status: "complete",
|
|
2529
|
+
json: json.sort((left, right) => left.path.localeCompare(right.path)),
|
|
2530
|
+
bytes: bytes.sort((left, right) => left.destination.localeCompare(right.destination)),
|
|
2531
|
+
removals,
|
|
2532
|
+
retainedRemovalKeys: [...new Set(retainedRemovalKeys)].sort(import_layoutCore.compareText),
|
|
2533
|
+
unmaterializedKeys: [...excluded].sort(import_layoutCore.compareText),
|
|
2534
|
+
unmaterializedDiagnostics: input.unmaterializedDiagnostics ?? [],
|
|
2535
|
+
notices,
|
|
2536
|
+
directoryRenames: directoryRenames.sort(
|
|
2537
|
+
(left, right) => left.from.split("/").length - right.from.split("/").length
|
|
2538
|
+
)
|
|
2539
|
+
};
|
|
2540
|
+
}
|
|
2541
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2542
|
+
0 && (module.exports = {
|
|
2543
|
+
bindLayoutReceiptSubmission,
|
|
2544
|
+
captureLayoutSubmissionComposition,
|
|
2545
|
+
decideCheckoutEligibility,
|
|
2546
|
+
decideReceiptEligibility,
|
|
2547
|
+
planMaterialization,
|
|
2548
|
+
planReceiptReadback,
|
|
2549
|
+
planReceiptRendering,
|
|
2550
|
+
withoutContainerSuppliedFields
|
|
2551
|
+
});
|
|
2552
|
+
//# sourceMappingURL=layoutReadback.js.map
|