farseer-cli 1.15.0 → 1.17.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 +328 -0
- package/dist/commands/code.js +104 -0
- package/dist/commands/code.js.map +7 -0
- package/dist/commands/model.js +14 -4
- package/dist/commands/model.js.map +2 -2
- package/dist/index.js +16 -0
- package/dist/index.js.map +2 -2
- package/dist/pipeline/generated-reference.json +30 -30
- package/dist/pipeline/service.js +2 -2
- 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/logger.js +9 -3
- package/dist/utils/logger.js.map +2 -2
- package/docs/pipeline-client-guide.md +2 -2
- package/docs/pipeline-reference.md +156 -99
- package/package.json +3 -2
- package/skills/pipeline-authoring/SKILL.md +10 -8
- package/skills/pipeline-authoring/practices.md +39 -25
|
@@ -0,0 +1,1517 @@
|
|
|
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 layoutReadbackPlacement_exports = {};
|
|
20
|
+
__export(layoutReadbackPlacement_exports, {
|
|
21
|
+
extractAuthoritativeDamageEvidence: () => extractAuthoritativeDamageEvidence,
|
|
22
|
+
planAuthoritativePlacement: () => planAuthoritativePlacement
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(layoutReadbackPlacement_exports);
|
|
25
|
+
var import_layoutCore = require("./layoutCore");
|
|
26
|
+
var import_layoutPaths = require("./layoutPaths");
|
|
27
|
+
var import_layoutProjection = require("./layoutProjection");
|
|
28
|
+
const FLAT_ROOTS = {
|
|
29
|
+
user_group: "access/groups",
|
|
30
|
+
label: "access/labels",
|
|
31
|
+
dataset: "definitions/datasets",
|
|
32
|
+
export_configuration: "definitions/exports"
|
|
33
|
+
};
|
|
34
|
+
function sameBinarySelector(left, right) {
|
|
35
|
+
if (left.slot !== right.slot) return false;
|
|
36
|
+
if (left.slot === "file-entry" && right.slot === "file-entry") return left.sidecar === right.sidecar;
|
|
37
|
+
return left.slot === "owner-attachment" && right.slot === "owner-attachment" && left.ownerFile === right.ownerFile && left.field === right.field;
|
|
38
|
+
}
|
|
39
|
+
function sourceFor(documentKey, document) {
|
|
40
|
+
const separator = documentKey.indexOf("/");
|
|
41
|
+
if (separator <= 0 || separator === documentKey.length - 1) return void 0;
|
|
42
|
+
const kind = documentKey.slice(0, separator);
|
|
43
|
+
const id = documentKey.slice(separator + 1);
|
|
44
|
+
if (kind !== document.kind) return void 0;
|
|
45
|
+
if (id.startsWith("key:")) {
|
|
46
|
+
const key = id.slice("key:".length);
|
|
47
|
+
if (!key || document.id !== void 0 || document.key !== key) return void 0;
|
|
48
|
+
return { type: "resource", resource: { kind, key } };
|
|
49
|
+
}
|
|
50
|
+
if (!import_layoutCore.RESOURCE_ID.test(id)) return void 0;
|
|
51
|
+
if (typeof document.id === "string" && document.id !== id) return void 0;
|
|
52
|
+
return { type: "resource", resource: { kind, id } };
|
|
53
|
+
}
|
|
54
|
+
function semanticPath(document) {
|
|
55
|
+
const value = document;
|
|
56
|
+
if (document.kind === "folder") {
|
|
57
|
+
if (!Array.isArray(value.path) || value.path.some((part) => typeof part !== "string" || !part))
|
|
58
|
+
return void 0;
|
|
59
|
+
return value.path;
|
|
60
|
+
}
|
|
61
|
+
if (["variable", "dimension_table", "sheet", "dashboard", "remote_job", "file"].includes(document.kind)) {
|
|
62
|
+
const folder = value.folder === null ? [] : value.folder;
|
|
63
|
+
if (typeof value.name !== "string" || !value.name || !Array.isArray(folder) || folder.some((part) => typeof part !== "string" || !part))
|
|
64
|
+
return void 0;
|
|
65
|
+
return [...folder, value.name];
|
|
66
|
+
}
|
|
67
|
+
return void 0;
|
|
68
|
+
}
|
|
69
|
+
function placementSignature(document) {
|
|
70
|
+
if (!document) return void 0;
|
|
71
|
+
const value = document;
|
|
72
|
+
if (document.kind === "folder") return (0, import_layoutCore.canonicalFragmentHash)({ path: value.path ?? null });
|
|
73
|
+
if (["variable", "dimension_table", "sheet", "dashboard", "remote_job", "file"].includes(document.kind)) {
|
|
74
|
+
const folder = value.folder === null ? [] : value.folder;
|
|
75
|
+
return (0, import_layoutCore.canonicalFragmentHash)({ name: value.name ?? null, folder: folder ?? null });
|
|
76
|
+
}
|
|
77
|
+
if (document.kind === "share") return (0, import_layoutCore.canonicalFragmentHash)({ owner: value.owner ?? null });
|
|
78
|
+
if (document.kind === "dimension_members") return (0, import_layoutCore.canonicalFragmentHash)({ table: value.table ?? null });
|
|
79
|
+
if ((0, import_layoutCore.own)(value, "name")) return (0, import_layoutCore.canonicalFragmentHash)({ name: value.name ?? null });
|
|
80
|
+
if (document.kind === "user_permissions") return (0, import_layoutCore.canonicalFragmentHash)({ user: value.user ?? null });
|
|
81
|
+
return (0, import_layoutCore.canonicalFragmentHash)({ kind: document.kind });
|
|
82
|
+
}
|
|
83
|
+
function identityValue(source) {
|
|
84
|
+
return "id" in source.resource ? source.resource.id : source.resource.key;
|
|
85
|
+
}
|
|
86
|
+
function currentCanBecome(current, authoritative, currentDocument) {
|
|
87
|
+
if (currentDocument.kind !== authoritative.resource.kind) return false;
|
|
88
|
+
if (current.type === "transient") return true;
|
|
89
|
+
if (current.type !== "resource" || current.resource.kind !== authoritative.resource.kind) return false;
|
|
90
|
+
if ("key" in current.resource) return true;
|
|
91
|
+
return "id" in authoritative.resource && current.resource.id === authoritative.resource.id;
|
|
92
|
+
}
|
|
93
|
+
function requiresIdentityReallocation(candidate) {
|
|
94
|
+
if (!candidate.current || (0, import_layoutCore.sourceIdentityKey)(candidate.current.mapping.source) === (0, import_layoutCore.sourceIdentityKey)(candidate.source))
|
|
95
|
+
return false;
|
|
96
|
+
return candidate.current.codec === "raw" ? candidate.current.mapping.detached : candidate.current.mapping.selector.file.startsWith("detached/");
|
|
97
|
+
}
|
|
98
|
+
function selectedHeadDamage(input, diagnostics) {
|
|
99
|
+
if (!input.head) return /* @__PURE__ */ new Map();
|
|
100
|
+
try {
|
|
101
|
+
const authoritative = input.headDocuments;
|
|
102
|
+
if (!authoritative) throw new Error("Selected Head was not validated");
|
|
103
|
+
const damage = extractAuthoritativeDamageEvidence(input.head, authoritative);
|
|
104
|
+
if (input.documents.length !== input.documentKeys.length || new Set(input.documentKeys).size !== input.documentKeys.length)
|
|
105
|
+
throw new Error("Selected documents are missing, duplicated or misaligned");
|
|
106
|
+
for (const [index, document] of input.documents.entries()) {
|
|
107
|
+
const documentKey = input.documentKeys[index];
|
|
108
|
+
const expected = authoritative.get(documentKey);
|
|
109
|
+
if (!expected || (0, import_layoutCore.canonicalFragmentHash)({ documentKey, document }) !== (0, import_layoutCore.canonicalFragmentHash)({ documentKey, document: expected }))
|
|
110
|
+
throw new Error(`Selected document does not match Head: ${documentKey}`);
|
|
111
|
+
}
|
|
112
|
+
return damage;
|
|
113
|
+
} catch (error) {
|
|
114
|
+
diagnostics.push(
|
|
115
|
+
(0, import_layoutCore.diagnostic)(
|
|
116
|
+
"invalid-authoritative-head",
|
|
117
|
+
".",
|
|
118
|
+
error instanceof Error ? error.message : "Selected Head is invalid"
|
|
119
|
+
)
|
|
120
|
+
);
|
|
121
|
+
return /* @__PURE__ */ new Map();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function pathInside(path, root) {
|
|
125
|
+
const candidate = (0, import_layoutPaths.layoutCollisionKey)(path);
|
|
126
|
+
const ancestor = (0, import_layoutPaths.layoutCollisionKey)(root);
|
|
127
|
+
return candidate === ancestor || candidate.startsWith(`${ancestor}/`);
|
|
128
|
+
}
|
|
129
|
+
function currentGroupedMapping(supplied, index) {
|
|
130
|
+
const matches = (index.get((0, import_layoutCore.sourceIdentityKey)(supplied.source)) ?? []).filter(
|
|
131
|
+
(candidate) => (0, import_layoutCore.sameSelector)(candidate.selector, supplied.selector) && (0, import_layoutCore.samePath)(candidate.sourcePath, supplied.sourcePath) && candidate.documentIndex === supplied.documentIndex && candidate.derivedSelectorHash === supplied.derivedSelectorHash && JSON.stringify(candidate.groupOwner) === JSON.stringify(supplied.groupOwner)
|
|
132
|
+
);
|
|
133
|
+
return matches.length === 1 ? matches[0] : void 0;
|
|
134
|
+
}
|
|
135
|
+
function currentRawMapping(supplied, index) {
|
|
136
|
+
const candidate = index.get((0, import_layoutCore.sourceIdentityKey)(supplied.source));
|
|
137
|
+
return candidate && sameBinarySelector(candidate.selector, supplied.selector) && (0, import_layoutCore.samePath)(candidate.sourcePath, supplied.sourcePath) && candidate.bytes === supplied.bytes && candidate.documentIndex === supplied.documentIndex && candidate.detached === supplied.detached && candidate.nameOverride === supplied.nameOverride ? candidate : void 0;
|
|
138
|
+
}
|
|
139
|
+
function groupOwner(document) {
|
|
140
|
+
const owner = (0, import_layoutCore.object)(document.owner);
|
|
141
|
+
if (!owner || typeof owner.kind !== "string") return void 0;
|
|
142
|
+
if (typeof owner.id === "string" && import_layoutCore.RESOURCE_ID.test(owner.id)) return { kind: owner.kind, id: owner.id };
|
|
143
|
+
if (typeof owner.key === "string" && owner.key) return { kind: owner.kind, key: owner.key };
|
|
144
|
+
if (typeof owner.dangling === "string" && import_layoutCore.RESOURCE_ID.test(owner.dangling))
|
|
145
|
+
return { kind: owner.kind, dangling: owner.dangling };
|
|
146
|
+
return void 0;
|
|
147
|
+
}
|
|
148
|
+
function hasExplicitDamageShape(document) {
|
|
149
|
+
const value = document;
|
|
150
|
+
if (document.kind === "folder") return value.path === null && typeof (0, import_layoutCore.object)(value.parent)?.dangling === "string";
|
|
151
|
+
if (document.kind === "share") return typeof (0, import_layoutCore.object)(value.owner)?.dangling === "string";
|
|
152
|
+
return ["variable", "dimension_table", "sheet", "dashboard", "remote_job", "file"].includes(document.kind) && typeof (0, import_layoutCore.object)(value.folder)?.dangling === "string";
|
|
153
|
+
}
|
|
154
|
+
function artifactRouteSourceIndex(grouped, binary) {
|
|
155
|
+
const push = (map, key, source) => {
|
|
156
|
+
const list = map.get(key);
|
|
157
|
+
if (list) list.push(source);
|
|
158
|
+
else map.set(key, [source]);
|
|
159
|
+
};
|
|
160
|
+
const bySidecar = /* @__PURE__ */ new Map();
|
|
161
|
+
const byRawBytes = /* @__PURE__ */ new Map();
|
|
162
|
+
for (const mapping of binary.rawSources) {
|
|
163
|
+
push(bySidecar, mapping.selector.sidecar, mapping.source);
|
|
164
|
+
push(byRawBytes, JSON.stringify([mapping.selector.sidecar, mapping.bytes]), mapping.source);
|
|
165
|
+
}
|
|
166
|
+
const byAttachment = /* @__PURE__ */ new Map();
|
|
167
|
+
for (const observation of binary.attachmentObservations)
|
|
168
|
+
if (observation.currentHash !== void 0 && observation.bytes !== void 0)
|
|
169
|
+
push(byAttachment, observation.bytes, observation.source);
|
|
170
|
+
const byFile = /* @__PURE__ */ new Map();
|
|
171
|
+
for (const mapping of grouped.sources) push(byFile, mapping.selector.file, mapping.source);
|
|
172
|
+
const observedByFile = /* @__PURE__ */ new Map();
|
|
173
|
+
for (const observation of grouped.observations)
|
|
174
|
+
if (observation.currentHash !== void 0) push(observedByFile, observation.selector.file, observation.source);
|
|
175
|
+
return (artifact) => {
|
|
176
|
+
if (artifact.role === "raw-metadata") return [...bySidecar.get(artifact.path) ?? []];
|
|
177
|
+
if (artifact.role === "raw")
|
|
178
|
+
return [...byRawBytes.get(JSON.stringify([artifact.source, artifact.path])) ?? []];
|
|
179
|
+
if (artifact.role === "attachment") return [...byAttachment.get(artifact.path) ?? []];
|
|
180
|
+
return [...byFile.get(artifact.path) ?? [], ...observedByFile.get(artifact.path) ?? []];
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function occupancy(input, diagnostics) {
|
|
184
|
+
const entries = [];
|
|
185
|
+
const classified = /* @__PURE__ */ new Set();
|
|
186
|
+
const artifactRouteSources = artifactRouteSourceIndex(input.currentGrouped, input.currentBinary);
|
|
187
|
+
for (const artifact of input.scan.artifacts) {
|
|
188
|
+
if (!artifact.present) continue;
|
|
189
|
+
const sources = artifactRouteSources(artifact);
|
|
190
|
+
if (sources.length === 0 && artifact.role !== "raw-metadata") {
|
|
191
|
+
diagnostics.push(
|
|
192
|
+
(0, import_layoutCore.diagnostic)(
|
|
193
|
+
"unclassified-layout-occupancy",
|
|
194
|
+
artifact.path,
|
|
195
|
+
"A present managed artifact has no source route"
|
|
196
|
+
)
|
|
197
|
+
);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const claimPath = artifact.role === "folder-control" ? (0, import_layoutPaths.folderDirectory)(artifact.path) : artifact.path;
|
|
201
|
+
const classification = `${claimPath}\0${artifact.role === "folder-control" ? "directory" : "file"}`;
|
|
202
|
+
if (classified.has(classification))
|
|
203
|
+
throw new Error(`Managed path ${JSON.stringify(claimPath)} has more than one classification`);
|
|
204
|
+
classified.add(classification);
|
|
205
|
+
entries.push({
|
|
206
|
+
claim: {
|
|
207
|
+
path: claimPath,
|
|
208
|
+
kind: artifact.role === "folder-control" ? "directory" : "file",
|
|
209
|
+
role: artifact.role === "folder-control" ? "directory" : artifact.role === "raw" ? "raw" : artifact.role === "attachment" ? "attachment" : artifact.role === "raw-metadata" ? "control" : "document",
|
|
210
|
+
identity: `current:${claimPath}`
|
|
211
|
+
},
|
|
212
|
+
sources: new Set(sources.map(import_layoutCore.sourceIdentityKey))
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
for (const path of input.scan.localOnlyFiles)
|
|
216
|
+
if ((0, import_layoutPaths.isPortableWorkspacePointer)(path))
|
|
217
|
+
entries.push({
|
|
218
|
+
claim: { path, kind: "file", role: "raw", identity: `local:${path}` },
|
|
219
|
+
sources: /* @__PURE__ */ new Set()
|
|
220
|
+
});
|
|
221
|
+
for (const path of input.scan.opaqueDirectories)
|
|
222
|
+
if ((0, import_layoutPaths.isPortableWorkspacePointer)(path))
|
|
223
|
+
entries.push({
|
|
224
|
+
claim: { path, kind: "directory", role: "directory", identity: `opaque:${path}` },
|
|
225
|
+
sources: /* @__PURE__ */ new Set(),
|
|
226
|
+
opaque: true
|
|
227
|
+
});
|
|
228
|
+
return entries;
|
|
229
|
+
}
|
|
230
|
+
function planAuthoritativePlacement(input) {
|
|
231
|
+
const diagnostics = [
|
|
232
|
+
...input.scan.diagnostics.map((entry) => ({ ...entry })),
|
|
233
|
+
...input.currentGrouped.diagnostics,
|
|
234
|
+
...input.currentBinary.diagnostics
|
|
235
|
+
];
|
|
236
|
+
const authoritativeDamage = selectedHeadDamage(input, diagnostics);
|
|
237
|
+
const placement = input.placement;
|
|
238
|
+
if (diagnostics.length > 0) return (0, import_layoutCore.blocked)(diagnostics);
|
|
239
|
+
const groupedBySource = (0, import_layoutCore.groupBySource)(input.currentGrouped.sources, (mapping) => (0, import_layoutCore.sourceIdentityKey)(mapping.source));
|
|
240
|
+
const rawBySource = (0, import_layoutCore.indexBySource)(
|
|
241
|
+
input.currentBinary.rawSources,
|
|
242
|
+
(mapping) => (0, import_layoutCore.sourceIdentityKey)(mapping.source),
|
|
243
|
+
"The current binary layout"
|
|
244
|
+
);
|
|
245
|
+
const groupedObservationsBySource = (0, import_layoutCore.groupBySource)(
|
|
246
|
+
input.currentGrouped.observations,
|
|
247
|
+
(observation) => (0, import_layoutCore.sourceIdentityKey)(observation.source)
|
|
248
|
+
);
|
|
249
|
+
const rawObservationsBySource = (0, import_layoutCore.groupBySource)(
|
|
250
|
+
input.currentBinary.rawObservations,
|
|
251
|
+
(observation) => (0, import_layoutCore.sourceIdentityKey)(observation.source)
|
|
252
|
+
);
|
|
253
|
+
const currentSourceKeys = /* @__PURE__ */ new Set([
|
|
254
|
+
...groupedBySource.keys(),
|
|
255
|
+
...rawBySource.keys(),
|
|
256
|
+
...groupedObservationsBySource.keys(),
|
|
257
|
+
...rawObservationsBySource.keys(),
|
|
258
|
+
...input.currentBinary.attachmentObservations.map((observation) => (0, import_layoutCore.sourceIdentityKey)(observation.source))
|
|
259
|
+
]);
|
|
260
|
+
const normalized = [];
|
|
261
|
+
const usedIndexes = /* @__PURE__ */ new Set();
|
|
262
|
+
const usedKeys = /* @__PURE__ */ new Set();
|
|
263
|
+
const usedSources = /* @__PURE__ */ new Set();
|
|
264
|
+
const usedCurrentCorrespondences = /* @__PURE__ */ new Set();
|
|
265
|
+
for (const intent of input.intents) {
|
|
266
|
+
const document = input.documents[intent.documentIndex];
|
|
267
|
+
const documentKey = input.documentKeys[intent.documentIndex];
|
|
268
|
+
const source = document && documentKey === intent.documentKey ? sourceFor(documentKey, document) : void 0;
|
|
269
|
+
if (!document || !source || usedIndexes.has(intent.documentIndex) || usedKeys.has(intent.documentKey) || usedSources.has((0, import_layoutCore.sourceIdentityKey)(source))) {
|
|
270
|
+
diagnostics.push(
|
|
271
|
+
(0, import_layoutCore.diagnostic)(
|
|
272
|
+
"invalid-authoritative-intent",
|
|
273
|
+
intent.documentKey,
|
|
274
|
+
"Intent identity is invalid or duplicated"
|
|
275
|
+
)
|
|
276
|
+
);
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
usedIndexes.add(intent.documentIndex);
|
|
280
|
+
usedKeys.add(intent.documentKey);
|
|
281
|
+
usedSources.add((0, import_layoutCore.sourceIdentityKey)(source));
|
|
282
|
+
const damage = authoritativeDamage.get(documentKey);
|
|
283
|
+
if (hasExplicitDamageShape(document) && !damage) {
|
|
284
|
+
diagnostics.push(
|
|
285
|
+
(0, import_layoutCore.diagnostic)(
|
|
286
|
+
"missing-authoritative-damage-evidence",
|
|
287
|
+
intent.documentKey,
|
|
288
|
+
"Explicit damage requires evidence from the selected Head"
|
|
289
|
+
)
|
|
290
|
+
);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
let current;
|
|
294
|
+
let currentDocument;
|
|
295
|
+
if (intent.mode === "replace-proven") {
|
|
296
|
+
if (typeof intent.expectedCurrentHash !== "string" || !intent.expectedCurrentHash) {
|
|
297
|
+
diagnostics.push(
|
|
298
|
+
(0, import_layoutCore.diagnostic)("invalid-authoritative-intent", intent.documentKey, "Replacement hash is missing")
|
|
299
|
+
);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (intent.current.codec === "grouped") {
|
|
303
|
+
const mapping = currentGroupedMapping(intent.current.mapping, groupedBySource);
|
|
304
|
+
const observations = mapping ? (groupedObservationsBySource.get((0, import_layoutCore.sourceIdentityKey)(mapping.source)) ?? []).filter(
|
|
305
|
+
(observation) => (0, import_layoutCore.sameSelector)(observation.selector, mapping.selector) && observation.currentHash === intent.expectedCurrentHash
|
|
306
|
+
) : [];
|
|
307
|
+
if (!mapping || observations.length !== 1) {
|
|
308
|
+
diagnostics.push(
|
|
309
|
+
(0, import_layoutCore.diagnostic)(
|
|
310
|
+
"stale-authoritative-source",
|
|
311
|
+
intent.documentKey,
|
|
312
|
+
"Grouped replacement source is not exact"
|
|
313
|
+
)
|
|
314
|
+
);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
current = { codec: "grouped", mapping };
|
|
318
|
+
currentDocument = input.currentGrouped.documents[mapping.documentIndex];
|
|
319
|
+
} else {
|
|
320
|
+
const mapping = currentRawMapping(intent.current.mapping, rawBySource);
|
|
321
|
+
const observations = mapping ? (rawObservationsBySource.get((0, import_layoutCore.sourceIdentityKey)(mapping.source)) ?? []).filter(
|
|
322
|
+
(observation) => sameBinarySelector(observation.selector, mapping.selector) && observation.currentHash === intent.expectedCurrentHash
|
|
323
|
+
) : [];
|
|
324
|
+
if (!mapping || observations.length !== 1) {
|
|
325
|
+
diagnostics.push(
|
|
326
|
+
(0, import_layoutCore.diagnostic)(
|
|
327
|
+
"stale-authoritative-source",
|
|
328
|
+
intent.documentKey,
|
|
329
|
+
"Raw replacement source is not exact"
|
|
330
|
+
)
|
|
331
|
+
);
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
current = { codec: "raw", mapping };
|
|
335
|
+
currentDocument = input.currentBinary.rawDocuments[mapping.documentIndex];
|
|
336
|
+
}
|
|
337
|
+
if (!currentDocument) {
|
|
338
|
+
diagnostics.push(
|
|
339
|
+
(0, import_layoutCore.diagnostic)("stale-authoritative-source", intent.documentKey, "Current source has no document")
|
|
340
|
+
);
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (!currentCanBecome(current.mapping.source, source, currentDocument)) {
|
|
344
|
+
diagnostics.push(
|
|
345
|
+
(0, import_layoutCore.diagnostic)(
|
|
346
|
+
"stale-authoritative-source",
|
|
347
|
+
intent.documentKey,
|
|
348
|
+
"Current source cannot bind this identity"
|
|
349
|
+
)
|
|
350
|
+
);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
} else {
|
|
354
|
+
const key = (0, import_layoutCore.sourceIdentityKey)(source);
|
|
355
|
+
const semanticAssociation = document.kind !== "share" && [
|
|
356
|
+
...input.currentGrouped.sources.map((mapping) => ({
|
|
357
|
+
document: input.currentGrouped.documents[mapping.documentIndex],
|
|
358
|
+
source: mapping.source
|
|
359
|
+
})),
|
|
360
|
+
...input.currentBinary.rawSources.map((mapping) => ({
|
|
361
|
+
document: input.currentBinary.rawDocuments[mapping.documentIndex],
|
|
362
|
+
source: mapping.source
|
|
363
|
+
}))
|
|
364
|
+
].some(
|
|
365
|
+
(current2) => (current2.source.type === "transient" || current2.source.type === "resource" && "key" in current2.source.resource) && current2.document?.kind === document.kind && placementSignature(current2.document) === placementSignature(document)
|
|
366
|
+
);
|
|
367
|
+
const priorAssociation = input.groupedRoutes.some((route) => (0, import_layoutCore.sourceIdentityKey)(route.source) === key) || currentSourceKeys.has(key) || (0, import_layoutCore.own)(input.canonicalBaseline, intent.documentKey) || input.scan.tracked.some((tracked) => tracked.route === intent.documentKey);
|
|
368
|
+
if (semanticAssociation || priorAssociation && !intent.reinsert) {
|
|
369
|
+
diagnostics.push(
|
|
370
|
+
(0, import_layoutCore.diagnostic)("invalid-insertion-authority", intent.documentKey, "A prior source association exists")
|
|
371
|
+
);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const raw = document.kind === "file";
|
|
376
|
+
if (raw && current?.codec === "grouped" || !raw && current?.codec === "raw") {
|
|
377
|
+
diagnostics.push(
|
|
378
|
+
(0, import_layoutCore.diagnostic)("invalid-authoritative-intent", intent.documentKey, "Intent uses the wrong codec")
|
|
379
|
+
);
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (current) {
|
|
383
|
+
const currentKey = JSON.stringify([
|
|
384
|
+
current.codec,
|
|
385
|
+
(0, import_layoutCore.sourceIdentityKey)(current.mapping.source),
|
|
386
|
+
current.mapping.selector,
|
|
387
|
+
current.mapping.sourcePath
|
|
388
|
+
]);
|
|
389
|
+
if (usedCurrentCorrespondences.has(currentKey)) {
|
|
390
|
+
diagnostics.push(
|
|
391
|
+
(0, import_layoutCore.diagnostic)(
|
|
392
|
+
"invalid-authoritative-intent",
|
|
393
|
+
intent.documentKey,
|
|
394
|
+
"Current correspondence is used by more than one intent"
|
|
395
|
+
)
|
|
396
|
+
);
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
usedCurrentCorrespondences.add(currentKey);
|
|
400
|
+
}
|
|
401
|
+
normalized.push({
|
|
402
|
+
intent,
|
|
403
|
+
document,
|
|
404
|
+
documentKey,
|
|
405
|
+
source,
|
|
406
|
+
current,
|
|
407
|
+
currentDocument,
|
|
408
|
+
outputIndex: -1,
|
|
409
|
+
damage
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
const semanticOwners = /* @__PURE__ */ new Map();
|
|
413
|
+
for (const candidate of normalized) {
|
|
414
|
+
if (candidate.damage) continue;
|
|
415
|
+
const path = semanticPath(candidate.document);
|
|
416
|
+
if (!path || candidate.document.kind === "dimension_members") continue;
|
|
417
|
+
const key = (0, import_layoutCore.canonicalFragmentHash)(path);
|
|
418
|
+
const previous = semanticOwners.get(key);
|
|
419
|
+
if (previous)
|
|
420
|
+
diagnostics.push(
|
|
421
|
+
(0, import_layoutCore.diagnostic)(
|
|
422
|
+
"duplicate-semantic-target",
|
|
423
|
+
candidate.documentKey,
|
|
424
|
+
`Semantic target is also owned by ${previous}`
|
|
425
|
+
)
|
|
426
|
+
);
|
|
427
|
+
else semanticOwners.set(key, candidate.documentKey);
|
|
428
|
+
}
|
|
429
|
+
if (diagnostics.length > 0) return (0, import_layoutCore.blocked)(diagnostics);
|
|
430
|
+
const groupedIntents = normalized.filter((candidate) => candidate.document.kind !== "file").sort((left, right) => left.intent.documentIndex - right.intent.documentIndex);
|
|
431
|
+
const rawIntents = normalized.filter((candidate) => candidate.document.kind === "file").sort((left, right) => left.intent.documentIndex - right.intent.documentIndex);
|
|
432
|
+
groupedIntents.forEach((candidate, index) => candidate.outputIndex = index);
|
|
433
|
+
rawIntents.forEach((candidate, index) => candidate.outputIndex = index);
|
|
434
|
+
const assignedResourcesByCurrent = /* @__PURE__ */ new Map();
|
|
435
|
+
for (const candidate of normalized) {
|
|
436
|
+
const currentSource = candidate.current?.mapping.source;
|
|
437
|
+
if (candidate.intent.mode === "replace-proven" && currentSource?.type === "resource" && "key" in currentSource.resource && (0, import_layoutCore.sourceIdentityKey)(currentSource) !== (0, import_layoutCore.sourceIdentityKey)(candidate.source))
|
|
438
|
+
assignedResourcesByCurrent.set((0, import_layoutCore.sourceIdentityKey)(currentSource), candidate.source);
|
|
439
|
+
}
|
|
440
|
+
const finalGroupOwner = (owner) => {
|
|
441
|
+
if (!("key" in owner)) return owner;
|
|
442
|
+
const assigned = assignedResourcesByCurrent.get(
|
|
443
|
+
(0, import_layoutCore.sourceIdentityKey)({ type: "resource", resource: { kind: owner.kind, key: owner.key } })
|
|
444
|
+
);
|
|
445
|
+
return assigned && "id" in assigned.resource ? { kind: assigned.resource.kind, id: assigned.resource.id } : owner;
|
|
446
|
+
};
|
|
447
|
+
const finalGroupOwnerKey = (owner) => (0, import_layoutCore.ownerKey)(finalGroupOwner(owner));
|
|
448
|
+
const currentHasDamageProof = (candidate) => {
|
|
449
|
+
if (candidate.current?.codec === "grouped" && candidate.current.mapping.selector.file.startsWith("detached/"))
|
|
450
|
+
return input.detachedDocumentProofs[candidate.current.mapping.selector.file]?.reason === "damage-proof";
|
|
451
|
+
if (candidate.current?.codec === "raw" && candidate.current.mapping.detached)
|
|
452
|
+
return input.detachedFileProofs[candidate.current.mapping.selector.sidecar]?.reason === "damage-proof";
|
|
453
|
+
return false;
|
|
454
|
+
};
|
|
455
|
+
const releasedClaims = /* @__PURE__ */ new Map();
|
|
456
|
+
const vacatedFolderSources = /* @__PURE__ */ new Set();
|
|
457
|
+
for (const candidate of normalized) {
|
|
458
|
+
if (candidate.intent.mode !== "replace-proven" || !candidate.current || !candidate.damage && !(input.head && currentHasDamageProof(candidate)) && placementSignature(candidate.document) === placementSignature(candidate.currentDocument) && !requiresIdentityReallocation(candidate))
|
|
459
|
+
continue;
|
|
460
|
+
const sourceKey = (0, import_layoutCore.sourceIdentityKey)(candidate.current.mapping.source);
|
|
461
|
+
const claimPaths = candidate.current.codec === "raw" ? [candidate.current.mapping.bytes, candidate.current.mapping.selector.sidecar] : [
|
|
462
|
+
candidate.document.kind === "folder" && candidate.current.mapping.selector.file.endsWith("/_folder.json") ? (0, import_layoutPaths.folderDirectory)(candidate.current.mapping.selector.file) : candidate.current.mapping.selector.file
|
|
463
|
+
];
|
|
464
|
+
releasedClaims.set(sourceKey, /* @__PURE__ */ new Set([...releasedClaims.get(sourceKey) ?? [], ...claimPaths]));
|
|
465
|
+
if (candidate.document.kind === "folder") vacatedFolderSources.add(sourceKey);
|
|
466
|
+
}
|
|
467
|
+
const occupied = occupancy(input, diagnostics);
|
|
468
|
+
if (diagnostics.length > 0) return (0, import_layoutCore.blocked)(diagnostics);
|
|
469
|
+
const retainedClaims = occupied.filter(
|
|
470
|
+
(entry) => entry.sources.size === 0 || [...entry.sources].some((source) => !releasedClaims.get(source)?.has(entry.claim.path))
|
|
471
|
+
).map((entry) => entry.claim);
|
|
472
|
+
let allocator;
|
|
473
|
+
try {
|
|
474
|
+
allocator = new import_layoutPaths.LayoutPathAllocator(retainedClaims);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
return (0, import_layoutCore.blocked)([(0, import_layoutCore.diagnostic)("invalid-layout-occupancy", ".", error.message)]);
|
|
477
|
+
}
|
|
478
|
+
const opaqueRoots = occupied.filter((entry) => entry.opaque).map((entry) => entry.claim.path);
|
|
479
|
+
const forbidden = (path) => opaqueRoots.some((root) => pathInside(path, root));
|
|
480
|
+
const allocations = [];
|
|
481
|
+
const detachedDocumentProofs = {};
|
|
482
|
+
const detachedFileProofs = {};
|
|
483
|
+
const groupedSources = [];
|
|
484
|
+
const rawSources = [];
|
|
485
|
+
const attachments = [];
|
|
486
|
+
const ownerInsertions = [];
|
|
487
|
+
const plannedFolders = /* @__PURE__ */ new Map();
|
|
488
|
+
const destinationBySource = /* @__PURE__ */ new Map();
|
|
489
|
+
const claimFixed = (claim) => {
|
|
490
|
+
if (forbidden(claim.path)) {
|
|
491
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("opaque-layout-target", claim.path, "Target is inside an opaque subtree"));
|
|
492
|
+
return false;
|
|
493
|
+
}
|
|
494
|
+
try {
|
|
495
|
+
allocator.claim(claim);
|
|
496
|
+
return true;
|
|
497
|
+
} catch (error) {
|
|
498
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("occupied-layout-target", claim.path, error.message));
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
const reserveControl = (path, identity) => {
|
|
503
|
+
const existing = allocator.snapshot().filter((claim) => claim.path === path);
|
|
504
|
+
if (existing.length > 0) {
|
|
505
|
+
if (existing.length === 1 && existing[0].kind === "file" && existing[0].role === "control") return true;
|
|
506
|
+
diagnostics.push(
|
|
507
|
+
(0, import_layoutCore.diagnostic)("occupied-layout-target", path, "Reserved control path has competing ownership")
|
|
508
|
+
);
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
return claimFixed({ path, kind: "file", role: "control", identity });
|
|
512
|
+
};
|
|
513
|
+
const resolveParent = (path, childKey) => {
|
|
514
|
+
if (path.length === 0) return { type: "normal", directory: "tree" };
|
|
515
|
+
const key = (0, import_layoutCore.canonicalFragmentHash)([...path]);
|
|
516
|
+
const planned = plannedFolders.get(key);
|
|
517
|
+
if (planned) return planned;
|
|
518
|
+
const current = placement.folderPlacement(path);
|
|
519
|
+
if (!current || current.type === "damaged" || vacatedFolderSources.has((0, import_layoutCore.sourceIdentityKey)(current.source))) {
|
|
520
|
+
diagnostics.push(
|
|
521
|
+
(0, import_layoutCore.diagnostic)("missing-layout-parent", childKey, "Semantic parent is missing, ambiguous, or damaged")
|
|
522
|
+
);
|
|
523
|
+
return void 0;
|
|
524
|
+
}
|
|
525
|
+
return current;
|
|
526
|
+
};
|
|
527
|
+
const resolveInheritedFinalParent = (candidate, path, proof) => {
|
|
528
|
+
const required = candidate.intent.mode === "replace-proven" && !requiresIdentityReallocation(candidate) && proof?.reason === "representation-fallback" && proof.inheritedFrom !== void 0;
|
|
529
|
+
return required ? { required: true, parent: resolveParent(path.slice(0, -1), candidate.documentKey) } : { required: false };
|
|
530
|
+
};
|
|
531
|
+
const placementUnchanged = (candidate, inheritedRequired) => candidate.intent.mode === "replace-proven" && !requiresIdentityReallocation(candidate) && !(input.head && !candidate.damage && currentHasDamageProof(candidate)) && !inheritedRequired && placementSignature(candidate.document) === placementSignature(candidate.currentDocument);
|
|
532
|
+
const inheritedProof = (candidate, parent) => ({
|
|
533
|
+
reason: "representation-fallback",
|
|
534
|
+
sourceKey: (0, import_layoutCore.sourceIdentityKey)(candidate.source),
|
|
535
|
+
inheritedFrom: {
|
|
536
|
+
sourceKey: (0, import_layoutCore.sourceIdentityKey)(parent.source),
|
|
537
|
+
semanticPathHash: parent.semanticPathHash
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
const allocateAttachmentBytes = (candidate, ownerFile, field) => {
|
|
541
|
+
try {
|
|
542
|
+
const allocated = allocator.allocate({
|
|
543
|
+
directory: (0, import_layoutPaths.parentDirectory)(ownerFile),
|
|
544
|
+
name: String(candidate.document.name),
|
|
545
|
+
extension: field === "icon" ? ".icon" : ".xlsx",
|
|
546
|
+
role: "attachment",
|
|
547
|
+
identity: `${identityValue(candidate.source)}/${field}`
|
|
548
|
+
});
|
|
549
|
+
allocations.push({ source: candidate.source, path: allocated });
|
|
550
|
+
return allocated.path;
|
|
551
|
+
} catch (error) {
|
|
552
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("invalid-attachment-target", candidate.documentKey, error.message));
|
|
553
|
+
return void 0;
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
const detachedPath = (source) => `detached/${source.resource.kind}/${(0, import_layoutPaths.encodeLayoutSegment)(identityValue(source))}.json`;
|
|
557
|
+
const damagedGroupedDestination = (candidate, slot) => {
|
|
558
|
+
const path = detachedPath(candidate.source);
|
|
559
|
+
if (candidate.intent.mode === "replace-proven" && candidate.current?.codec === "grouped" && candidate.current.mapping.selector.file === path && candidate.current.mapping.selector.slot === slot) {
|
|
560
|
+
if (forbidden(path)) {
|
|
561
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("opaque-layout-target", path, "Target is inside an opaque subtree"));
|
|
562
|
+
return void 0;
|
|
563
|
+
}
|
|
564
|
+
return candidate.current.mapping.selector;
|
|
565
|
+
}
|
|
566
|
+
return claimFixed({ path, kind: "file", role: "document", identity: (0, import_layoutCore.sourceIdentityKey)(candidate.source) }) ? { file: path, slot } : void 0;
|
|
567
|
+
};
|
|
568
|
+
const inheritedDocument = (candidate, parent) => {
|
|
569
|
+
const path = detachedPath(candidate.source);
|
|
570
|
+
if (!claimFixed({ path, kind: "file", role: "document", identity: (0, import_layoutCore.sourceIdentityKey)(candidate.source) }))
|
|
571
|
+
return void 0;
|
|
572
|
+
detachedDocumentProofs[path] = inheritedProof(candidate, parent);
|
|
573
|
+
return { file: path, slot: candidate.document.kind === "dimension_table" ? "table-definition" : "document" };
|
|
574
|
+
};
|
|
575
|
+
const allocateDocument = (candidate, request, slot) => {
|
|
576
|
+
if (forbidden(request.directory)) {
|
|
577
|
+
diagnostics.push(
|
|
578
|
+
(0, import_layoutCore.diagnostic)("opaque-layout-target", request.directory, "Parent is inside an opaque subtree")
|
|
579
|
+
);
|
|
580
|
+
return void 0;
|
|
581
|
+
}
|
|
582
|
+
const before = allocator.snapshot();
|
|
583
|
+
try {
|
|
584
|
+
const allocated = allocator.allocate(request);
|
|
585
|
+
if (forbidden(allocated.path)) {
|
|
586
|
+
diagnostics.push(
|
|
587
|
+
(0, import_layoutCore.diagnostic)("opaque-layout-target", allocated.path, "Target is inside an opaque subtree")
|
|
588
|
+
);
|
|
589
|
+
return void 0;
|
|
590
|
+
}
|
|
591
|
+
allocations.push({ source: candidate.source, path: allocated });
|
|
592
|
+
return { file: allocated.path, slot };
|
|
593
|
+
} catch (error) {
|
|
594
|
+
if (!(error instanceof import_layoutPaths.LayoutPathAllocationError) || error.reason !== "unrepresentable") {
|
|
595
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("invalid-layout-request", candidate.documentKey, error.message));
|
|
596
|
+
return void 0;
|
|
597
|
+
}
|
|
598
|
+
if (!import_layoutCore.ITEM_KINDS.has(candidate.document.kind)) {
|
|
599
|
+
diagnostics.push(
|
|
600
|
+
(0, import_layoutCore.diagnostic)(
|
|
601
|
+
"unrepresentable-layout-target",
|
|
602
|
+
candidate.documentKey,
|
|
603
|
+
"This resource kind cannot use detached representation"
|
|
604
|
+
)
|
|
605
|
+
);
|
|
606
|
+
return void 0;
|
|
607
|
+
}
|
|
608
|
+
const path = detachedPath(candidate.source);
|
|
609
|
+
if (!claimFixed({ path, kind: "file", role: "document", identity: (0, import_layoutCore.sourceIdentityKey)(candidate.source) }))
|
|
610
|
+
return void 0;
|
|
611
|
+
detachedDocumentProofs[path] = {
|
|
612
|
+
reason: "representation-fallback",
|
|
613
|
+
sourceKey: (0, import_layoutCore.sourceIdentityKey)(candidate.source),
|
|
614
|
+
request,
|
|
615
|
+
existingClaims: before
|
|
616
|
+
};
|
|
617
|
+
return { file: path, slot };
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
for (const candidate of groupedIntents.filter(
|
|
621
|
+
(item) => item.document.kind === "folder" && item.damage !== void 0
|
|
622
|
+
)) {
|
|
623
|
+
const destination = damagedGroupedDestination(candidate, "document");
|
|
624
|
+
if (destination) destinationBySource.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), destination);
|
|
625
|
+
}
|
|
626
|
+
const folderIntents = groupedIntents.filter((candidate) => candidate.document.kind === "folder" && !candidate.damage).sort((left, right) => {
|
|
627
|
+
const leftPath = semanticPath(left.document)?.length ?? Number.MAX_SAFE_INTEGER;
|
|
628
|
+
const rightPath = semanticPath(right.document)?.length ?? Number.MAX_SAFE_INTEGER;
|
|
629
|
+
return leftPath - rightPath || (0, import_layoutCore.sourceIdentityKey)(left.source).localeCompare((0, import_layoutCore.sourceIdentityKey)(right.source));
|
|
630
|
+
});
|
|
631
|
+
for (const candidate of folderIntents) {
|
|
632
|
+
const path = semanticPath(candidate.document);
|
|
633
|
+
if (!path || path.length === 0) {
|
|
634
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("invalid-layout-semantics", candidate.documentKey, "Folder path is invalid"));
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
const key = (0, import_layoutCore.canonicalFragmentHash)(path);
|
|
638
|
+
if (plannedFolders.has(key)) {
|
|
639
|
+
diagnostics.push(
|
|
640
|
+
(0, import_layoutCore.diagnostic)("duplicate-folder-target", candidate.documentKey, "Folder semantic path is duplicated")
|
|
641
|
+
);
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
const currentAtPath = placement.folderPlacement(path);
|
|
645
|
+
const owner = (0, import_layoutCore.sourceIdentityKey)(candidate.current?.mapping.source ?? candidate.source);
|
|
646
|
+
const colliding = placement.collidingFolderPlacements(path).filter(
|
|
647
|
+
(folder2) => !vacatedFolderSources.has((0, import_layoutCore.sourceIdentityKey)(folder2.source)) && (0, import_layoutCore.sourceIdentityKey)(folder2.source) !== owner
|
|
648
|
+
);
|
|
649
|
+
if (colliding.length > 0) {
|
|
650
|
+
diagnostics.push(
|
|
651
|
+
(0, import_layoutCore.diagnostic)(
|
|
652
|
+
"ambiguous-folder-target",
|
|
653
|
+
candidate.documentKey,
|
|
654
|
+
"Semantic folder path is still owned by another current source"
|
|
655
|
+
)
|
|
656
|
+
);
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
const currentDetachedProof = candidate.current?.codec === "grouped" && candidate.current.mapping.selector.file.startsWith("detached/") ? input.detachedDocumentProofs[candidate.current.mapping.selector.file] : void 0;
|
|
660
|
+
const inherited = resolveInheritedFinalParent(candidate, path, currentDetachedProof);
|
|
661
|
+
const unchanged = placementUnchanged(candidate, inherited.required) && currentAtPath !== void 0 && (0, import_layoutCore.sourceIdentityKey)(currentAtPath.source) === (0, import_layoutCore.sourceIdentityKey)(candidate.current.mapping.source);
|
|
662
|
+
let destination;
|
|
663
|
+
let folder;
|
|
664
|
+
if (unchanged && currentAtPath?.type === "normal") {
|
|
665
|
+
destination = { file: `${currentAtPath.directory}/_folder.json`, slot: "document" };
|
|
666
|
+
folder = currentAtPath;
|
|
667
|
+
} else if (unchanged && currentAtPath?.type === "detached-representation") {
|
|
668
|
+
destination = candidate.current?.codec === "grouped" ? candidate.current.mapping.selector : void 0;
|
|
669
|
+
folder = currentAtPath;
|
|
670
|
+
const proof = destination && input.detachedDocumentProofs[destination.file];
|
|
671
|
+
if (destination && proof) detachedDocumentProofs[destination.file] = proof;
|
|
672
|
+
} else if (inherited.required && inherited.parent?.type === "detached-representation" && candidate.current?.codec === "grouped") {
|
|
673
|
+
destination = candidate.current.mapping.selector;
|
|
674
|
+
folder = {
|
|
675
|
+
type: "detached-representation",
|
|
676
|
+
source: candidate.source,
|
|
677
|
+
semanticPath: path,
|
|
678
|
+
semanticPathHash: (0, import_layoutCore.canonicalFragmentHash)(path)
|
|
679
|
+
};
|
|
680
|
+
detachedDocumentProofs[destination.file] = inheritedProof(candidate, inherited.parent);
|
|
681
|
+
} else if (candidate.intent.mode === "replace-proven" && currentAtPath?.type === "damaged" && (0, import_layoutCore.sourceIdentityKey)(currentAtPath.source) === (0, import_layoutCore.sourceIdentityKey)(candidate.current.mapping.source) && placementSignature(candidate.document) === placementSignature(candidate.currentDocument) && !input.head) {
|
|
682
|
+
diagnostics.push(
|
|
683
|
+
(0, import_layoutCore.diagnostic)("damaged-current-placement", candidate.documentKey, "Damaged placement requires repair")
|
|
684
|
+
);
|
|
685
|
+
} else {
|
|
686
|
+
const parent = inherited.required ? inherited.parent : resolveParent(path.slice(0, -1), candidate.documentKey);
|
|
687
|
+
if (parent?.type === "detached-representation") {
|
|
688
|
+
destination = inheritedDocument(candidate, parent);
|
|
689
|
+
if (destination)
|
|
690
|
+
folder = {
|
|
691
|
+
type: "detached-representation",
|
|
692
|
+
source: candidate.source,
|
|
693
|
+
semanticPath: path,
|
|
694
|
+
semanticPathHash: (0, import_layoutCore.canonicalFragmentHash)(path)
|
|
695
|
+
};
|
|
696
|
+
} else if (parent?.type === "normal") {
|
|
697
|
+
const request = {
|
|
698
|
+
directory: parent.directory,
|
|
699
|
+
name: path.at(-1),
|
|
700
|
+
role: "directory",
|
|
701
|
+
identity: identityValue(candidate.source),
|
|
702
|
+
requiredChildren: ["_folder.json"]
|
|
703
|
+
};
|
|
704
|
+
const selector = allocateDocument(candidate, request, "document");
|
|
705
|
+
if (selector) {
|
|
706
|
+
if (selector.file.startsWith("detached/")) {
|
|
707
|
+
destination = selector;
|
|
708
|
+
folder = {
|
|
709
|
+
type: "detached-representation",
|
|
710
|
+
source: candidate.source,
|
|
711
|
+
semanticPath: path,
|
|
712
|
+
semanticPathHash: (0, import_layoutCore.canonicalFragmentHash)(path)
|
|
713
|
+
};
|
|
714
|
+
} else {
|
|
715
|
+
const directory = selector.file;
|
|
716
|
+
if (reserveControl(`${directory}/_folder.json`, `folder:${(0, import_layoutCore.sourceIdentityKey)(candidate.source)}`)) {
|
|
717
|
+
destination = { file: `${directory}/_folder.json`, slot: "document" };
|
|
718
|
+
folder = { type: "normal", source: candidate.source, semanticPath: path, directory };
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
if (!destination || !folder) continue;
|
|
725
|
+
plannedFolders.set(key, folder);
|
|
726
|
+
destinationBySource.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), destination);
|
|
727
|
+
}
|
|
728
|
+
const damageBucketAvailable = (candidate, bucket) => {
|
|
729
|
+
if (forbidden(bucket)) {
|
|
730
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("opaque-layout-target", bucket, "Target is inside an opaque subtree"));
|
|
731
|
+
return false;
|
|
732
|
+
}
|
|
733
|
+
const occupants = occupied.filter((entry) => pathInside(entry.claim.path, bucket));
|
|
734
|
+
if (occupants.length === 0) return true;
|
|
735
|
+
const current = candidate.current?.codec === "raw" ? candidate.current.mapping : void 0;
|
|
736
|
+
const currentSource = current && (0, import_layoutCore.sourceIdentityKey)(current.source);
|
|
737
|
+
const bucketSources = input.currentBinary.rawSources.filter(
|
|
738
|
+
(mapping) => (0, import_layoutPaths.parentDirectory)(mapping.selector.sidecar) === bucket
|
|
739
|
+
);
|
|
740
|
+
const exactCurrent = current?.detached === true && (0, import_layoutPaths.parentDirectory)(current.selector.sidecar) === bucket && current.selector.sidecar === (0, import_layoutPaths.rawSidecarPath)(current.bytes) && bucketSources.length === 1 && (0, import_layoutCore.sourceIdentityKey)(bucketSources[0].source) === currentSource && sameBinarySelector(bucketSources[0].selector, current.selector) && bucketSources[0].bytes === current.bytes;
|
|
741
|
+
const exclusivelyCurrent = currentSource !== void 0 && occupants.every((entry) => !entry.opaque && entry.sources.size === 1 && entry.sources.has(currentSource));
|
|
742
|
+
if (exactCurrent && exclusivelyCurrent) return true;
|
|
743
|
+
diagnostics.push(
|
|
744
|
+
(0, import_layoutCore.diagnostic)("occupied-layout-target", bucket, "Detached file bucket belongs to another layout source")
|
|
745
|
+
);
|
|
746
|
+
return false;
|
|
747
|
+
};
|
|
748
|
+
const allocateDetachedRaw = (candidate, parent, direct) => {
|
|
749
|
+
const bucketRequest = {
|
|
750
|
+
directory: "detached/files",
|
|
751
|
+
name: identityValue(candidate.source),
|
|
752
|
+
role: "directory",
|
|
753
|
+
identity: (0, import_layoutCore.sourceIdentityKey)(candidate.source)
|
|
754
|
+
};
|
|
755
|
+
let bucket;
|
|
756
|
+
try {
|
|
757
|
+
bucket = allocator.allocate(bucketRequest);
|
|
758
|
+
allocations.push({ source: candidate.source, path: bucket });
|
|
759
|
+
} catch (error) {
|
|
760
|
+
diagnostics.push(
|
|
761
|
+
(0, import_layoutCore.diagnostic)("unrepresentable-detached-target", candidate.documentKey, error.message)
|
|
762
|
+
);
|
|
763
|
+
return void 0;
|
|
764
|
+
}
|
|
765
|
+
if (forbidden(bucket.path)) {
|
|
766
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("opaque-layout-target", bucket.path, "Target is inside an opaque subtree"));
|
|
767
|
+
return void 0;
|
|
768
|
+
}
|
|
769
|
+
const path = semanticPath(candidate.document);
|
|
770
|
+
let allocated;
|
|
771
|
+
try {
|
|
772
|
+
allocated = allocator.allocate({
|
|
773
|
+
directory: bucket.path,
|
|
774
|
+
name: path.at(-1),
|
|
775
|
+
role: "raw",
|
|
776
|
+
identity: identityValue(candidate.source)
|
|
777
|
+
});
|
|
778
|
+
} catch (error) {
|
|
779
|
+
diagnostics.push(
|
|
780
|
+
(0, import_layoutCore.diagnostic)("unrepresentable-detached-target", candidate.documentKey, error.message)
|
|
781
|
+
);
|
|
782
|
+
return void 0;
|
|
783
|
+
}
|
|
784
|
+
allocations.push({ source: candidate.source, path: allocated });
|
|
785
|
+
const selector = {
|
|
786
|
+
sidecar: (0, import_layoutPaths.rawSidecarPath)(allocated.path),
|
|
787
|
+
slot: "file-entry",
|
|
788
|
+
identity: { id: identityValue(candidate.source) }
|
|
789
|
+
};
|
|
790
|
+
detachedFileProofs[selector.sidecar] = parent ? inheritedProof(candidate, parent) : {
|
|
791
|
+
reason: "representation-fallback",
|
|
792
|
+
sourceKey: (0, import_layoutCore.sourceIdentityKey)(candidate.source),
|
|
793
|
+
request: direct.request,
|
|
794
|
+
existingClaims: direct.claims
|
|
795
|
+
};
|
|
796
|
+
return { selector, bytes: allocated.path };
|
|
797
|
+
};
|
|
798
|
+
const rawDestinations = /* @__PURE__ */ new Map();
|
|
799
|
+
for (const candidate of [...rawIntents].sort(
|
|
800
|
+
(left, right) => (0, import_layoutCore.sourceIdentityKey)(left.source).localeCompare((0, import_layoutCore.sourceIdentityKey)(right.source))
|
|
801
|
+
)) {
|
|
802
|
+
if (candidate.damage) {
|
|
803
|
+
const bucket = `detached/files/${(0, import_layoutPaths.encodeLayoutSegment)(identityValue(candidate.source))}`;
|
|
804
|
+
if (!damageBucketAvailable(candidate, bucket)) continue;
|
|
805
|
+
const bucketClaim = {
|
|
806
|
+
path: bucket,
|
|
807
|
+
kind: "directory",
|
|
808
|
+
role: "directory",
|
|
809
|
+
identity: (0, import_layoutCore.sourceIdentityKey)(candidate.source),
|
|
810
|
+
fallback: false
|
|
811
|
+
};
|
|
812
|
+
if (!claimFixed(bucketClaim)) continue;
|
|
813
|
+
allocations.push({ source: candidate.source, path: bucketClaim });
|
|
814
|
+
const name = candidate.document.name;
|
|
815
|
+
if (typeof name !== "string" || !name) {
|
|
816
|
+
diagnostics.push(
|
|
817
|
+
(0, import_layoutCore.diagnostic)("invalid-layout-semantics", candidate.documentKey, "Damaged raw name is invalid")
|
|
818
|
+
);
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
try {
|
|
822
|
+
const allocated = allocator.allocate({
|
|
823
|
+
directory: bucket,
|
|
824
|
+
name,
|
|
825
|
+
role: "raw",
|
|
826
|
+
identity: identityValue(candidate.source)
|
|
827
|
+
});
|
|
828
|
+
allocations.push({ source: candidate.source, path: allocated });
|
|
829
|
+
rawDestinations.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), {
|
|
830
|
+
selector: {
|
|
831
|
+
sidecar: (0, import_layoutPaths.rawSidecarPath)(allocated.path),
|
|
832
|
+
slot: "file-entry",
|
|
833
|
+
identity: { id: identityValue(candidate.source) }
|
|
834
|
+
},
|
|
835
|
+
bytes: allocated.path
|
|
836
|
+
});
|
|
837
|
+
} catch (error) {
|
|
838
|
+
diagnostics.push(
|
|
839
|
+
(0, import_layoutCore.diagnostic)("unrepresentable-detached-target", candidate.documentKey, error.message)
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
const path = semanticPath(candidate.document);
|
|
845
|
+
if (!path) {
|
|
846
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("invalid-layout-semantics", candidate.documentKey, "Raw placement is invalid"));
|
|
847
|
+
continue;
|
|
848
|
+
}
|
|
849
|
+
let destination;
|
|
850
|
+
const currentDetachedProof = candidate.current?.codec === "raw" && candidate.current.mapping.detached ? input.detachedFileProofs[candidate.current.mapping.selector.sidecar] : void 0;
|
|
851
|
+
const inherited = resolveInheritedFinalParent(candidate, path, currentDetachedProof);
|
|
852
|
+
const unchanged = placementUnchanged(candidate, inherited.required);
|
|
853
|
+
if (unchanged && candidate.current?.codec === "raw") {
|
|
854
|
+
destination = {
|
|
855
|
+
selector: {
|
|
856
|
+
...candidate.current.mapping.selector,
|
|
857
|
+
identity: { id: identityValue(candidate.source) }
|
|
858
|
+
},
|
|
859
|
+
bytes: candidate.current.mapping.bytes
|
|
860
|
+
};
|
|
861
|
+
if (candidate.current.mapping.detached) {
|
|
862
|
+
const proof = input.detachedFileProofs[candidate.current.mapping.selector.sidecar];
|
|
863
|
+
if (proof) detachedFileProofs[candidate.current.mapping.selector.sidecar] = proof;
|
|
864
|
+
}
|
|
865
|
+
} else if (inherited.required && inherited.parent?.type === "detached-representation" && candidate.current?.codec === "raw") {
|
|
866
|
+
destination = {
|
|
867
|
+
selector: {
|
|
868
|
+
...candidate.current.mapping.selector,
|
|
869
|
+
identity: { id: identityValue(candidate.source) }
|
|
870
|
+
},
|
|
871
|
+
bytes: candidate.current.mapping.bytes
|
|
872
|
+
};
|
|
873
|
+
detachedFileProofs[candidate.current.mapping.selector.sidecar] = inheritedProof(
|
|
874
|
+
candidate,
|
|
875
|
+
inherited.parent
|
|
876
|
+
);
|
|
877
|
+
} else {
|
|
878
|
+
const parent = inherited.required ? inherited.parent : resolveParent(path.slice(0, -1), candidate.documentKey);
|
|
879
|
+
if (parent?.type === "detached-representation") destination = allocateDetachedRaw(candidate, parent);
|
|
880
|
+
else if (parent?.type === "normal") {
|
|
881
|
+
if (forbidden(parent.directory)) {
|
|
882
|
+
diagnostics.push(
|
|
883
|
+
(0, import_layoutCore.diagnostic)("opaque-layout-target", parent.directory, "Parent is inside an opaque subtree")
|
|
884
|
+
);
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
const request = {
|
|
888
|
+
directory: parent.directory,
|
|
889
|
+
name: path.at(-1),
|
|
890
|
+
role: "raw",
|
|
891
|
+
identity: identityValue(candidate.source)
|
|
892
|
+
};
|
|
893
|
+
const before = allocator.snapshot();
|
|
894
|
+
try {
|
|
895
|
+
const allocated = allocator.allocate(request);
|
|
896
|
+
allocations.push({ source: candidate.source, path: allocated });
|
|
897
|
+
destination = {
|
|
898
|
+
selector: {
|
|
899
|
+
sidecar: (0, import_layoutPaths.rawSidecarPath)(allocated.path),
|
|
900
|
+
slot: "file-entry",
|
|
901
|
+
identity: { id: identityValue(candidate.source) }
|
|
902
|
+
},
|
|
903
|
+
bytes: allocated.path
|
|
904
|
+
};
|
|
905
|
+
} catch (error) {
|
|
906
|
+
if (error instanceof import_layoutPaths.LayoutPathAllocationError && error.reason === "unrepresentable")
|
|
907
|
+
destination = allocateDetachedRaw(candidate, void 0, { request, claims: before });
|
|
908
|
+
else
|
|
909
|
+
diagnostics.push(
|
|
910
|
+
(0, import_layoutCore.diagnostic)("invalid-layout-request", candidate.documentKey, error.message)
|
|
911
|
+
);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
if (destination) rawDestinations.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), destination);
|
|
916
|
+
}
|
|
917
|
+
const groupedNonFolders = groupedIntents.filter((candidate) => candidate.document.kind !== "folder");
|
|
918
|
+
const tableDestinations = /* @__PURE__ */ new Map();
|
|
919
|
+
const ordinary = [];
|
|
920
|
+
for (const candidate of groupedNonFolders) {
|
|
921
|
+
const document = candidate.document;
|
|
922
|
+
if (document.kind === "dimension_members" || document.kind === "share") continue;
|
|
923
|
+
let destination;
|
|
924
|
+
if (candidate.damage) {
|
|
925
|
+
destination = damagedGroupedDestination(
|
|
926
|
+
candidate,
|
|
927
|
+
document.kind === "dimension_table" ? "table-definition" : "document"
|
|
928
|
+
);
|
|
929
|
+
} else {
|
|
930
|
+
const itemPath = import_layoutCore.ITEM_KINDS.has(document.kind) ? semanticPath(document) : void 0;
|
|
931
|
+
const currentDetachedProof = candidate.current?.codec === "grouped" && candidate.current.mapping.selector.file.startsWith("detached/") ? input.detachedDocumentProofs[candidate.current.mapping.selector.file] : void 0;
|
|
932
|
+
const inherited = itemPath ? resolveInheritedFinalParent(candidate, itemPath, currentDetachedProof) : { required: false };
|
|
933
|
+
const unchanged = placementUnchanged(candidate, inherited.required);
|
|
934
|
+
if (unchanged && candidate.current?.codec === "grouped") destination = candidate.current.mapping.selector;
|
|
935
|
+
else if (inherited.required && inherited.parent?.type === "detached-representation" && candidate.current?.codec === "grouped") {
|
|
936
|
+
destination = candidate.current.mapping.selector;
|
|
937
|
+
detachedDocumentProofs[destination.file] = inheritedProof(candidate, inherited.parent);
|
|
938
|
+
} else if (import_layoutCore.ITEM_KINDS.has(document.kind)) {
|
|
939
|
+
const path = itemPath;
|
|
940
|
+
if (!path) {
|
|
941
|
+
diagnostics.push(
|
|
942
|
+
(0, import_layoutCore.diagnostic)("invalid-layout-semantics", candidate.documentKey, "Item placement is invalid")
|
|
943
|
+
);
|
|
944
|
+
continue;
|
|
945
|
+
}
|
|
946
|
+
const parent = inherited.required ? inherited.parent : resolveParent(path.slice(0, -1), candidate.documentKey);
|
|
947
|
+
if (parent?.type === "detached-representation") destination = inheritedDocument(candidate, parent);
|
|
948
|
+
else if (parent?.type === "normal")
|
|
949
|
+
ordinary.push({
|
|
950
|
+
candidate,
|
|
951
|
+
request: {
|
|
952
|
+
directory: parent.directory,
|
|
953
|
+
name: path.at(-1),
|
|
954
|
+
extension: ".json",
|
|
955
|
+
role: "document",
|
|
956
|
+
identity: identityValue(candidate.source)
|
|
957
|
+
},
|
|
958
|
+
slot: document.kind === "dimension_table" ? "table-definition" : "document"
|
|
959
|
+
});
|
|
960
|
+
} else if (document.kind === "settings") {
|
|
961
|
+
const path = "definitions/settings.json";
|
|
962
|
+
if (claimFixed({
|
|
963
|
+
path,
|
|
964
|
+
kind: "file",
|
|
965
|
+
role: "document",
|
|
966
|
+
identity: (0, import_layoutCore.sourceIdentityKey)(candidate.source)
|
|
967
|
+
}))
|
|
968
|
+
destination = { file: path, slot: "document" };
|
|
969
|
+
} else if (document.kind === "user_permissions") {
|
|
970
|
+
const user = (0, import_layoutCore.object)(document.user);
|
|
971
|
+
const fields = typeof document.user === "string" ? [["users", document.user]] : user ? Object.entries(user) : [];
|
|
972
|
+
if (fields.length !== 1 || typeof fields[0][1] !== "string" || !fields[0][1]) {
|
|
973
|
+
diagnostics.push(
|
|
974
|
+
(0, import_layoutCore.diagnostic)("invalid-layout-semantics", candidate.documentKey, "Grant selector is invalid")
|
|
975
|
+
);
|
|
976
|
+
continue;
|
|
977
|
+
}
|
|
978
|
+
ordinary.push({
|
|
979
|
+
candidate,
|
|
980
|
+
request: {
|
|
981
|
+
directory: `access/grants/${(0, import_layoutPaths.encodeLayoutSegment)(fields[0][0])}`,
|
|
982
|
+
name: fields[0][1],
|
|
983
|
+
extension: ".json",
|
|
984
|
+
role: "document",
|
|
985
|
+
identity: identityValue(candidate.source)
|
|
986
|
+
},
|
|
987
|
+
slot: "document"
|
|
988
|
+
});
|
|
989
|
+
} else {
|
|
990
|
+
const root = FLAT_ROOTS[document.kind];
|
|
991
|
+
const name = document.name;
|
|
992
|
+
if (!root || typeof name !== "string" || !name) {
|
|
993
|
+
diagnostics.push(
|
|
994
|
+
(0, import_layoutCore.diagnostic)(
|
|
995
|
+
"unsupported-layout-kind",
|
|
996
|
+
candidate.documentKey,
|
|
997
|
+
"Document kind has no placement rule"
|
|
998
|
+
)
|
|
999
|
+
);
|
|
1000
|
+
continue;
|
|
1001
|
+
}
|
|
1002
|
+
ordinary.push({
|
|
1003
|
+
candidate,
|
|
1004
|
+
request: {
|
|
1005
|
+
directory: root,
|
|
1006
|
+
name,
|
|
1007
|
+
extension: ".json",
|
|
1008
|
+
role: "document",
|
|
1009
|
+
identity: identityValue(candidate.source)
|
|
1010
|
+
},
|
|
1011
|
+
slot: "document"
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
if (destination) {
|
|
1016
|
+
destinationBySource.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), destination);
|
|
1017
|
+
if (destination.file.startsWith("detached/") && !detachedDocumentProofs[destination.file]) {
|
|
1018
|
+
const proof = input.detachedDocumentProofs[destination.file];
|
|
1019
|
+
if (proof) detachedDocumentProofs[destination.file] = proof;
|
|
1020
|
+
}
|
|
1021
|
+
if (document.kind === "dimension_table")
|
|
1022
|
+
tableDestinations.set(identityValue(candidate.source), destination);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
ordinary.sort(
|
|
1026
|
+
(left, right) => (0, import_layoutCore.sourceIdentityKey)(left.candidate.source).localeCompare((0, import_layoutCore.sourceIdentityKey)(right.candidate.source))
|
|
1027
|
+
);
|
|
1028
|
+
for (const item of ordinary) {
|
|
1029
|
+
const destination = allocateDocument(item.candidate, item.request, item.slot);
|
|
1030
|
+
if (!destination) continue;
|
|
1031
|
+
destinationBySource.set((0, import_layoutCore.sourceIdentityKey)(item.candidate.source), destination);
|
|
1032
|
+
if (item.candidate.document.kind === "dimension_table")
|
|
1033
|
+
tableDestinations.set(identityValue(item.candidate.source), destination);
|
|
1034
|
+
}
|
|
1035
|
+
for (const candidate of groupedNonFolders.filter((item) => item.document.kind === "dimension_members")) {
|
|
1036
|
+
const id = identityValue(candidate.source);
|
|
1037
|
+
let destination = tableDestinations.get(id);
|
|
1038
|
+
if (!destination) {
|
|
1039
|
+
const tableSource = `resource/dimension_table/id/${id}`;
|
|
1040
|
+
const table = input.currentGrouped.sources.filter(
|
|
1041
|
+
(mapping) => (0, import_layoutCore.sourceIdentityKey)(mapping.source) === tableSource && mapping.selector.slot === "table-definition"
|
|
1042
|
+
);
|
|
1043
|
+
const file = table.length === 1 ? table[0].selector.file : candidate.current?.codec === "grouped" ? candidate.current.mapping.selector.file : void 0;
|
|
1044
|
+
if (file) destination = { file, slot: "member-set" };
|
|
1045
|
+
} else destination = { file: destination.file, slot: "member-set" };
|
|
1046
|
+
if (!destination)
|
|
1047
|
+
diagnostics.push(
|
|
1048
|
+
(0, import_layoutCore.diagnostic)("missing-table-placement", candidate.documentKey, "Member set has no table placement")
|
|
1049
|
+
);
|
|
1050
|
+
else destinationBySource.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), destination);
|
|
1051
|
+
}
|
|
1052
|
+
const shareGroups = /* @__PURE__ */ new Map();
|
|
1053
|
+
for (const observation of input.currentGrouped.observations) {
|
|
1054
|
+
if (observation.selector.slot !== "share-group-owner" || observation.source.type !== "group" || observation.currentHash === void 0)
|
|
1055
|
+
continue;
|
|
1056
|
+
const key = finalGroupOwnerKey(observation.source.owner);
|
|
1057
|
+
const existing = shareGroups.get(key);
|
|
1058
|
+
if (!existing)
|
|
1059
|
+
shareGroups.set(key, {
|
|
1060
|
+
owner: observation.source.owner,
|
|
1061
|
+
file: observation.selector.file,
|
|
1062
|
+
nextIndex: 0
|
|
1063
|
+
});
|
|
1064
|
+
else if (existing.file !== observation.selector.file)
|
|
1065
|
+
diagnostics.push(
|
|
1066
|
+
(0, import_layoutCore.diagnostic)("ambiguous-share-group", observation.selector.file, "Owner has more than one share group")
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1069
|
+
for (const mapping of input.currentGrouped.sources) {
|
|
1070
|
+
if (mapping.selector.slot !== "share-entry" || !mapping.groupOwner) continue;
|
|
1071
|
+
const key = finalGroupOwnerKey(mapping.groupOwner);
|
|
1072
|
+
const index = typeof mapping.sourcePath[1] === "number" ? mapping.sourcePath[1] : -1;
|
|
1073
|
+
const existing = shareGroups.get(key);
|
|
1074
|
+
if (!existing)
|
|
1075
|
+
shareGroups.set(key, { owner: mapping.groupOwner, file: mapping.selector.file, nextIndex: index + 1 });
|
|
1076
|
+
else if (existing.file === mapping.selector.file) existing.nextIndex = Math.max(existing.nextIndex, index + 1);
|
|
1077
|
+
else
|
|
1078
|
+
diagnostics.push(
|
|
1079
|
+
(0, import_layoutCore.diagnostic)("ambiguous-share-group", mapping.selector.file, "Owner has more than one share group")
|
|
1080
|
+
);
|
|
1081
|
+
}
|
|
1082
|
+
const pendingShareGroups = /* @__PURE__ */ new Map();
|
|
1083
|
+
const shareSourcePaths = /* @__PURE__ */ new Map();
|
|
1084
|
+
const shareMappingOwners = /* @__PURE__ */ new Map();
|
|
1085
|
+
for (const candidate of groupedNonFolders.filter((item) => item.document.kind === "share")) {
|
|
1086
|
+
const owner = groupOwner(candidate.document);
|
|
1087
|
+
if (!owner) {
|
|
1088
|
+
diagnostics.push(
|
|
1089
|
+
(0, import_layoutCore.diagnostic)("invalid-share-owner", candidate.documentKey, "Share owner is not a stable selector")
|
|
1090
|
+
);
|
|
1091
|
+
continue;
|
|
1092
|
+
}
|
|
1093
|
+
const key = (0, import_layoutCore.ownerKey)(owner);
|
|
1094
|
+
if (candidate.current?.codec === "grouped") {
|
|
1095
|
+
if (candidate.current.mapping.selector.slot !== "share-entry") {
|
|
1096
|
+
diagnostics.push(
|
|
1097
|
+
(0, import_layoutCore.diagnostic)("invalid-share-source", candidate.documentKey, "Current share selector is invalid")
|
|
1098
|
+
);
|
|
1099
|
+
continue;
|
|
1100
|
+
}
|
|
1101
|
+
const currentOwner = candidate.current.mapping.groupOwner;
|
|
1102
|
+
if (!currentOwner || finalGroupOwnerKey(currentOwner) !== key) {
|
|
1103
|
+
diagnostics.push((0, import_layoutCore.diagnostic)("owner-transfer", candidate.documentKey, "Share owner cannot change"));
|
|
1104
|
+
continue;
|
|
1105
|
+
}
|
|
1106
|
+
destinationBySource.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), {
|
|
1107
|
+
...candidate.current.mapping.selector,
|
|
1108
|
+
identity: { id: identityValue(candidate.source) }
|
|
1109
|
+
});
|
|
1110
|
+
} else pendingShareGroups.set(key, [...pendingShareGroups.get(key) ?? [], candidate]);
|
|
1111
|
+
}
|
|
1112
|
+
for (const [key, candidates] of [...pendingShareGroups].sort(([left], [right]) => left.localeCompare(right))) {
|
|
1113
|
+
const owner = groupOwner(candidates[0].document);
|
|
1114
|
+
let group = shareGroups.get(key);
|
|
1115
|
+
if (!group) {
|
|
1116
|
+
const variant = "id" in owner ? owner.id : "key" in owner ? owner.key : owner.dangling;
|
|
1117
|
+
const request = {
|
|
1118
|
+
directory: "access/shares",
|
|
1119
|
+
name: `${owner.kind}-${variant}`,
|
|
1120
|
+
extension: ".json",
|
|
1121
|
+
role: "document",
|
|
1122
|
+
identity: key
|
|
1123
|
+
};
|
|
1124
|
+
const representative = candidates[0];
|
|
1125
|
+
const destination = allocateDocument(representative, request, "document");
|
|
1126
|
+
if (!destination) continue;
|
|
1127
|
+
group = { owner, file: destination.file, nextIndex: 0 };
|
|
1128
|
+
shareGroups.set(key, group);
|
|
1129
|
+
}
|
|
1130
|
+
for (const candidate of candidates.sort(
|
|
1131
|
+
(left, right) => (0, import_layoutCore.sourceIdentityKey)(left.source).localeCompare((0, import_layoutCore.sourceIdentityKey)(right.source))
|
|
1132
|
+
)) {
|
|
1133
|
+
const id = identityValue(candidate.source);
|
|
1134
|
+
shareMappingOwners.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), group.owner);
|
|
1135
|
+
destinationBySource.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), {
|
|
1136
|
+
file: group.file,
|
|
1137
|
+
slot: "share-entry",
|
|
1138
|
+
identity: { id }
|
|
1139
|
+
});
|
|
1140
|
+
shareSourcePaths.set((0, import_layoutCore.sourceIdentityKey)(candidate.source), ["shares", group.nextIndex]);
|
|
1141
|
+
group.nextIndex += 1;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
for (const candidate of rawIntents) {
|
|
1145
|
+
const path = semanticPath(candidate.document);
|
|
1146
|
+
const destination = rawDestinations.get((0, import_layoutCore.sourceIdentityKey)(candidate.source));
|
|
1147
|
+
if (!destination) continue;
|
|
1148
|
+
const current = candidate.current?.codec === "raw" ? candidate.current.mapping : void 0;
|
|
1149
|
+
const detached = destination.selector.sidecar.startsWith("detached/files/");
|
|
1150
|
+
const component = destination.bytes.slice(destination.bytes.lastIndexOf("/") + 1);
|
|
1151
|
+
const mapping = current ? { ...current, documentIndex: candidate.outputIndex } : {
|
|
1152
|
+
documentIndex: candidate.outputIndex,
|
|
1153
|
+
source: candidate.source,
|
|
1154
|
+
selector: destination.selector,
|
|
1155
|
+
sourcePath: [],
|
|
1156
|
+
bytes: destination.bytes,
|
|
1157
|
+
detached,
|
|
1158
|
+
nameOverride: detached || component !== path.at(-1)
|
|
1159
|
+
};
|
|
1160
|
+
rawSources.push({
|
|
1161
|
+
mode: candidate.intent.mode,
|
|
1162
|
+
documentKey: candidate.documentKey,
|
|
1163
|
+
mapping,
|
|
1164
|
+
destination: destination.selector,
|
|
1165
|
+
bytes: destination.bytes
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
for (const candidate of groupedIntents) {
|
|
1169
|
+
const destination = destinationBySource.get((0, import_layoutCore.sourceIdentityKey)(candidate.source));
|
|
1170
|
+
if (!destination) continue;
|
|
1171
|
+
const current = candidate.current?.codec === "grouped" ? candidate.current.mapping : void 0;
|
|
1172
|
+
const owner = candidate.document.kind === "share" ? groupOwner(candidate.document) : void 0;
|
|
1173
|
+
const mappingOwner = shareMappingOwners.get((0, import_layoutCore.sourceIdentityKey)(candidate.source)) ?? owner;
|
|
1174
|
+
const mapping = current ? {
|
|
1175
|
+
...current,
|
|
1176
|
+
documentIndex: candidate.outputIndex,
|
|
1177
|
+
...candidate.document.kind === "user_permissions" ? { derivedSelectorHash: (0, import_layoutProjection.derivedSourceSelectorHash)(candidate.document) } : {}
|
|
1178
|
+
} : {
|
|
1179
|
+
documentIndex: candidate.outputIndex,
|
|
1180
|
+
sourcePath: candidate.document.kind === "dimension_members" ? ["members"] : [],
|
|
1181
|
+
source: candidate.source,
|
|
1182
|
+
selector: destination,
|
|
1183
|
+
...candidate.document.kind === "user_permissions" ? { derivedSelectorHash: (0, import_layoutProjection.derivedSourceSelectorHash)(candidate.document) } : {},
|
|
1184
|
+
...mappingOwner ? {
|
|
1185
|
+
groupOwner: mappingOwner,
|
|
1186
|
+
wireOwner: (0, import_layoutCore.object)(candidate.document.owner),
|
|
1187
|
+
sourcePath: shareSourcePaths.get((0, import_layoutCore.sourceIdentityKey)(candidate.source)) ?? ["shares", 0]
|
|
1188
|
+
} : {}
|
|
1189
|
+
};
|
|
1190
|
+
groupedSources.push({ mode: candidate.intent.mode, documentKey: candidate.documentKey, mapping, destination });
|
|
1191
|
+
}
|
|
1192
|
+
for (const candidate of groupedIntents) {
|
|
1193
|
+
const field = (0, import_layoutCore.attachmentField)(candidate.document);
|
|
1194
|
+
if (!field) continue;
|
|
1195
|
+
const sourceKey = (0, import_layoutCore.sourceIdentityKey)(candidate.source);
|
|
1196
|
+
const destination = destinationBySource.get(sourceKey);
|
|
1197
|
+
if (!destination || destination.slot !== "document") continue;
|
|
1198
|
+
const authored = candidate.document[field];
|
|
1199
|
+
if (field === "template" && authored === void 0) {
|
|
1200
|
+
diagnostics.push(
|
|
1201
|
+
(0, import_layoutCore.diagnostic)(
|
|
1202
|
+
"incomplete-attachment-input",
|
|
1203
|
+
candidate.documentKey,
|
|
1204
|
+
"Template omission has no authoritative value"
|
|
1205
|
+
)
|
|
1206
|
+
);
|
|
1207
|
+
continue;
|
|
1208
|
+
}
|
|
1209
|
+
if (authored !== null && authored !== void 0 && !(0, import_layoutCore.validBlob)(authored)) {
|
|
1210
|
+
diagnostics.push(
|
|
1211
|
+
(0, import_layoutCore.diagnostic)("invalid-attachment-content", candidate.documentKey, "Attachment descriptor is invalid")
|
|
1212
|
+
);
|
|
1213
|
+
continue;
|
|
1214
|
+
}
|
|
1215
|
+
const currentOwnerSource = candidate.current?.mapping.source ?? candidate.source;
|
|
1216
|
+
const currentFields = input.currentBinary.ownerFields.filter(
|
|
1217
|
+
(projection) => projection.field === field && (0, import_layoutCore.sourceIdentityKey)(projection.ownerSource) === (0, import_layoutCore.sourceIdentityKey)(currentOwnerSource)
|
|
1218
|
+
);
|
|
1219
|
+
const current = currentFields.length === 1 ? currentFields[0] : void 0;
|
|
1220
|
+
if (candidate.intent.mode === "replace-proven" && current && current.status !== "value") {
|
|
1221
|
+
if (authored !== void 0 && authored !== null)
|
|
1222
|
+
diagnostics.push(
|
|
1223
|
+
(0, import_layoutCore.diagnostic)(
|
|
1224
|
+
"missing-current-attachment",
|
|
1225
|
+
candidate.documentKey,
|
|
1226
|
+
"Non-null replacement has no current attachment association"
|
|
1227
|
+
)
|
|
1228
|
+
);
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
if (candidate.intent.mode === "replace-proven" && current) {
|
|
1232
|
+
let bytes2 = authored !== null && authored !== void 0 ? current.bytes : void 0;
|
|
1233
|
+
if (authored !== null && authored !== void 0 && !bytes2) {
|
|
1234
|
+
bytes2 = allocateAttachmentBytes(candidate, destination.file, field);
|
|
1235
|
+
if (!bytes2) continue;
|
|
1236
|
+
}
|
|
1237
|
+
attachments.push({
|
|
1238
|
+
source: candidate.source,
|
|
1239
|
+
field,
|
|
1240
|
+
current,
|
|
1241
|
+
destination: {
|
|
1242
|
+
ownerFile: destination.file,
|
|
1243
|
+
field,
|
|
1244
|
+
...bytes2 ? { bytes: bytes2 } : {}
|
|
1245
|
+
}
|
|
1246
|
+
});
|
|
1247
|
+
continue;
|
|
1248
|
+
}
|
|
1249
|
+
if (candidate.intent.mode === "replace-proven" && authored !== null && authored !== void 0) {
|
|
1250
|
+
diagnostics.push(
|
|
1251
|
+
(0, import_layoutCore.diagnostic)(
|
|
1252
|
+
"missing-current-attachment",
|
|
1253
|
+
candidate.documentKey,
|
|
1254
|
+
"Replacement has no current attachment association"
|
|
1255
|
+
)
|
|
1256
|
+
);
|
|
1257
|
+
continue;
|
|
1258
|
+
}
|
|
1259
|
+
if (candidate.intent.mode !== "insert-authoritative" || authored === null || authored === void 0) continue;
|
|
1260
|
+
const bytes = allocateAttachmentBytes(candidate, destination.file, field);
|
|
1261
|
+
if (!bytes) continue;
|
|
1262
|
+
attachments.push({
|
|
1263
|
+
source: candidate.source,
|
|
1264
|
+
field,
|
|
1265
|
+
destination: { ownerFile: destination.file, field, bytes }
|
|
1266
|
+
});
|
|
1267
|
+
ownerInsertions.push({
|
|
1268
|
+
source: candidate.source,
|
|
1269
|
+
documentKey: candidate.documentKey,
|
|
1270
|
+
ownerFile: destination.file,
|
|
1271
|
+
ownerDocument: candidate.document,
|
|
1272
|
+
field,
|
|
1273
|
+
groupedSelector: { file: destination.file, slot: "document" }
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
const groupedTargetKeys = /* @__PURE__ */ new Set();
|
|
1277
|
+
const groupedFileOwners = /* @__PURE__ */ new Map();
|
|
1278
|
+
for (const correspondence of groupedSources) {
|
|
1279
|
+
const selector = correspondence.destination;
|
|
1280
|
+
const key = selector.slot === "share-entry" ? JSON.stringify([selector.file, selector.slot, selector.identity]) : JSON.stringify([selector.file, selector.slot]);
|
|
1281
|
+
if (groupedTargetKeys.has(key))
|
|
1282
|
+
diagnostics.push(
|
|
1283
|
+
(0, import_layoutCore.diagnostic)("duplicate-layout-target", selector.file, "Grouped destination has competing ownership")
|
|
1284
|
+
);
|
|
1285
|
+
groupedTargetKeys.add(key);
|
|
1286
|
+
const fileKey = (0, import_layoutPaths.layoutCollisionKey)(selector.file);
|
|
1287
|
+
groupedFileOwners.set(fileKey, [...groupedFileOwners.get(fileKey) ?? [], correspondence]);
|
|
1288
|
+
}
|
|
1289
|
+
for (const owners of groupedFileOwners.values()) {
|
|
1290
|
+
if (owners.length < 2) continue;
|
|
1291
|
+
const files = new Set(owners.map((owner) => owner.destination.file));
|
|
1292
|
+
const allShares = files.size === 1 && owners.every((owner) => owner.destination.slot === "share-entry" && owner.mapping.groupOwner) && new Set(owners.map((owner) => finalGroupOwnerKey(owner.mapping.groupOwner))).size === 1;
|
|
1293
|
+
const tableSlots = new Set(owners.map((owner) => owner.destination.slot));
|
|
1294
|
+
const tableDocuments = owners.map((owner) => owner.documentKey.split("/"));
|
|
1295
|
+
const tablePair = owners.length === 2 && files.size === 1 && tableSlots.has("table-definition") && tableSlots.has("member-set") && new Set(tableDocuments.map(([kind]) => kind)).size === 2 && tableDocuments.some(([kind]) => kind === "dimension_table") && tableDocuments.some(([kind]) => kind === "dimension_members") && new Set(tableDocuments.map(([, id]) => id)).size === 1;
|
|
1296
|
+
if (!allShares && !tablePair)
|
|
1297
|
+
diagnostics.push(
|
|
1298
|
+
(0, import_layoutCore.diagnostic)(
|
|
1299
|
+
"duplicate-layout-target",
|
|
1300
|
+
owners[0].destination.file,
|
|
1301
|
+
"Grouped file has competing container ownership"
|
|
1302
|
+
)
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
const rawTargetKeys = /* @__PURE__ */ new Set();
|
|
1306
|
+
const physicalTargets = /* @__PURE__ */ new Map();
|
|
1307
|
+
for (const owners of groupedFileOwners.values()) {
|
|
1308
|
+
const path = owners[0].destination.file;
|
|
1309
|
+
physicalTargets.set((0, import_layoutPaths.layoutCollisionKey)(path), { path, kind: "grouped" });
|
|
1310
|
+
}
|
|
1311
|
+
const claimPhysicalTarget = (path, kind) => {
|
|
1312
|
+
const key = (0, import_layoutPaths.layoutCollisionKey)(path);
|
|
1313
|
+
const existing = physicalTargets.get(key);
|
|
1314
|
+
if (!existing) {
|
|
1315
|
+
physicalTargets.set(key, { path, kind });
|
|
1316
|
+
return true;
|
|
1317
|
+
}
|
|
1318
|
+
diagnostics.push(
|
|
1319
|
+
(0, import_layoutCore.diagnostic)("duplicate-layout-target", path, `Physical target conflicts with ${existing.path}`)
|
|
1320
|
+
);
|
|
1321
|
+
return false;
|
|
1322
|
+
};
|
|
1323
|
+
for (const correspondence of rawSources) {
|
|
1324
|
+
const key = correspondence.destination.sidecar;
|
|
1325
|
+
if (rawTargetKeys.has(key))
|
|
1326
|
+
diagnostics.push(
|
|
1327
|
+
(0, import_layoutCore.diagnostic)("duplicate-layout-target", correspondence.bytes, "Raw destination has competing ownership")
|
|
1328
|
+
);
|
|
1329
|
+
rawTargetKeys.add(key);
|
|
1330
|
+
claimPhysicalTarget(correspondence.destination.sidecar, "sidecar");
|
|
1331
|
+
claimPhysicalTarget(correspondence.bytes, "bytes");
|
|
1332
|
+
}
|
|
1333
|
+
for (const correspondence of attachments) {
|
|
1334
|
+
const bytes = correspondence.destination.bytes;
|
|
1335
|
+
if (!bytes) continue;
|
|
1336
|
+
claimPhysicalTarget(bytes, "bytes");
|
|
1337
|
+
}
|
|
1338
|
+
if (groupedSources.length !== groupedIntents.length || rawSources.length !== rawIntents.length || diagnostics.length > 0)
|
|
1339
|
+
return (0, import_layoutCore.blocked)(
|
|
1340
|
+
diagnostics.length > 0 ? diagnostics : [(0, import_layoutCore.diagnostic)("incomplete-layout-placement", ".", "Placement did not produce every requested mapping")]
|
|
1341
|
+
);
|
|
1342
|
+
groupedSources.sort((left, right) => left.mapping.documentIndex - right.mapping.documentIndex);
|
|
1343
|
+
rawSources.sort((left, right) => left.mapping.documentIndex - right.mapping.documentIndex);
|
|
1344
|
+
const groupedDamage = groupedSources.flatMap((correspondence) => {
|
|
1345
|
+
const evidence = authoritativeDamage.get(correspondence.documentKey);
|
|
1346
|
+
return evidence ? [{ codec: "grouped", evidence, destination: correspondence.destination }] : [];
|
|
1347
|
+
});
|
|
1348
|
+
const rawDamage = rawSources.flatMap((correspondence) => {
|
|
1349
|
+
const evidence = authoritativeDamage.get(correspondence.documentKey);
|
|
1350
|
+
return evidence ? [
|
|
1351
|
+
{
|
|
1352
|
+
codec: "raw",
|
|
1353
|
+
evidence,
|
|
1354
|
+
destination: correspondence.destination,
|
|
1355
|
+
bytes: correspondence.bytes
|
|
1356
|
+
}
|
|
1357
|
+
] : [];
|
|
1358
|
+
});
|
|
1359
|
+
return {
|
|
1360
|
+
status: "complete",
|
|
1361
|
+
groupedDocuments: groupedIntents.map((candidate) => candidate.document),
|
|
1362
|
+
groupedDocumentKeys: groupedIntents.map((candidate) => candidate.documentKey),
|
|
1363
|
+
groupedSources,
|
|
1364
|
+
rawDocuments: rawIntents.map((candidate) => candidate.document),
|
|
1365
|
+
rawSources,
|
|
1366
|
+
attachments,
|
|
1367
|
+
ownerInsertions,
|
|
1368
|
+
allocations,
|
|
1369
|
+
detachedDocumentProofs,
|
|
1370
|
+
detachedFileProofs,
|
|
1371
|
+
damagePlacements: [...groupedDamage, ...rawDamage]
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
const MAX_RESOURCE_ID = 9223372036854775807n;
|
|
1375
|
+
const ID_FREE_RENDERED_KINDS = /* @__PURE__ */ new Set(["dimension_members", "user_permissions"]);
|
|
1376
|
+
const SHARE_OWNER_KINDS = /* @__PURE__ */ new Set([
|
|
1377
|
+
"folder",
|
|
1378
|
+
"variable",
|
|
1379
|
+
"dimension_table",
|
|
1380
|
+
"sheet",
|
|
1381
|
+
"dashboard",
|
|
1382
|
+
"file",
|
|
1383
|
+
"remote_job"
|
|
1384
|
+
]);
|
|
1385
|
+
const NULL_LOCATION_CODES = /* @__PURE__ */ new Set(["orphan", "pending-item-node", "item-node-path-invalid"]);
|
|
1386
|
+
function resourceId(value) {
|
|
1387
|
+
return typeof value === "string" && import_layoutCore.RESOURCE_ID.test(value) && BigInt(value) <= MAX_RESOURCE_ID ? value : void 0;
|
|
1388
|
+
}
|
|
1389
|
+
function resourceFromKey(documentKey) {
|
|
1390
|
+
const separator = documentKey.indexOf("/");
|
|
1391
|
+
const kind = documentKey.slice(0, separator);
|
|
1392
|
+
const id = resourceId(documentKey.slice(separator + 1));
|
|
1393
|
+
if (!kind || separator < 1 || !id) throw new Error(`Invalid Head document key: ${documentKey}`);
|
|
1394
|
+
return { type: "resource", resource: { kind, id } };
|
|
1395
|
+
}
|
|
1396
|
+
function validateDiagnostic(diagnostic2) {
|
|
1397
|
+
if (!diagnostic2 || typeof diagnostic2.kind !== "string" || diagnostic2.kind.length === 0 || !resourceId(diagnostic2.id) || typeof diagnostic2.code !== "string" || diagnostic2.code.length === 0 || typeof diagnostic2.message !== "string")
|
|
1398
|
+
throw new Error("Head has an invalid diagnostic");
|
|
1399
|
+
}
|
|
1400
|
+
function validateRenderedIdentity(document, source) {
|
|
1401
|
+
if (Object.prototype.hasOwnProperty.call(document, "key"))
|
|
1402
|
+
throw new Error(`Head document has a local key: ${source.resource.kind}/${source.resource.id}`);
|
|
1403
|
+
if (ID_FREE_RENDERED_KINDS.has(document.kind)) {
|
|
1404
|
+
if (Object.prototype.hasOwnProperty.call(document, "id"))
|
|
1405
|
+
throw new Error(
|
|
1406
|
+
`Head document has an unexpected rendered id: ${source.resource.kind}/${source.resource.id}`
|
|
1407
|
+
);
|
|
1408
|
+
return;
|
|
1409
|
+
}
|
|
1410
|
+
if (resourceId(document.id) !== source.resource.id)
|
|
1411
|
+
throw new Error(`Head document identity does not match its key: ${source.resource.kind}/${source.resource.id}`);
|
|
1412
|
+
}
|
|
1413
|
+
function exactDangling(value) {
|
|
1414
|
+
const marker = (0, import_layoutCore.object)(value);
|
|
1415
|
+
if (!marker || Object.keys(marker).length !== 1) return void 0;
|
|
1416
|
+
return resourceId(marker.dangling);
|
|
1417
|
+
}
|
|
1418
|
+
function validPath(value) {
|
|
1419
|
+
return Array.isArray(value) && value.every((part) => typeof part === "string");
|
|
1420
|
+
}
|
|
1421
|
+
function nullLocationCode(diagnostics, kind, id) {
|
|
1422
|
+
const codes = [
|
|
1423
|
+
...new Set(
|
|
1424
|
+
diagnostics.filter((diagnostic2) => diagnostic2.kind === kind && diagnostic2.id === id).map((diagnostic2) => diagnostic2.code).filter((code) => NULL_LOCATION_CODES.has(code))
|
|
1425
|
+
)
|
|
1426
|
+
];
|
|
1427
|
+
if (codes.length > 1) throw new Error(`Head has conflicting placement diagnostics for ${kind}/${id}`);
|
|
1428
|
+
return codes[0];
|
|
1429
|
+
}
|
|
1430
|
+
function groupedCause(document, source, diagnostics) {
|
|
1431
|
+
const value = document;
|
|
1432
|
+
const folder = value.folder;
|
|
1433
|
+
if (folder === null) {
|
|
1434
|
+
const code = nullLocationCode(diagnostics, source.resource.kind, source.resource.id);
|
|
1435
|
+
return code ? { type: "null-location", code } : void 0;
|
|
1436
|
+
}
|
|
1437
|
+
if (validPath(folder)) return void 0;
|
|
1438
|
+
const folderId = exactDangling(folder);
|
|
1439
|
+
if (folderId) return { type: "dangling-folder", folderId };
|
|
1440
|
+
throw new Error(`Head has an invalid folder location for ${source.resource.kind}/${source.resource.id}`);
|
|
1441
|
+
}
|
|
1442
|
+
function folderCause(document, source) {
|
|
1443
|
+
const value = document;
|
|
1444
|
+
if (validPath(value.path)) return void 0;
|
|
1445
|
+
if (value.path !== null)
|
|
1446
|
+
throw new Error(`Head has an invalid folder path for ${source.resource.kind}/${source.resource.id}`);
|
|
1447
|
+
if (typeof value.name !== "string")
|
|
1448
|
+
throw new Error(`Head has an invalid damaged folder name for ${source.resource.kind}/${source.resource.id}`);
|
|
1449
|
+
const parentId = exactDangling(value.parent);
|
|
1450
|
+
if (!parentId)
|
|
1451
|
+
throw new Error(`Head has an invalid damaged folder parent for ${source.resource.kind}/${source.resource.id}`);
|
|
1452
|
+
return { type: "dangling-folder-parent", parentId };
|
|
1453
|
+
}
|
|
1454
|
+
function shareOwner(value) {
|
|
1455
|
+
const owner = (0, import_layoutCore.object)(value);
|
|
1456
|
+
if (!owner || typeof owner.kind !== "string" || !SHARE_OWNER_KINDS.has(owner.kind) || Object.keys(owner).length !== 2)
|
|
1457
|
+
throw new Error("Head has an invalid share owner");
|
|
1458
|
+
const id = resourceId(owner.id);
|
|
1459
|
+
const dangling = resourceId(owner.dangling);
|
|
1460
|
+
if (id === void 0 === (dangling === void 0)) throw new Error("Head has an invalid share owner");
|
|
1461
|
+
return id ? { kind: owner.kind, id } : { kind: owner.kind, dangling };
|
|
1462
|
+
}
|
|
1463
|
+
function shareCause(document, source, diagnostics) {
|
|
1464
|
+
const owner = shareOwner(document.owner);
|
|
1465
|
+
if ("dangling" in owner) return { type: "damaged-share-owner", owner };
|
|
1466
|
+
const damaged = diagnostics.some(
|
|
1467
|
+
(diagnostic2) => diagnostic2.kind === source.resource.kind && diagnostic2.id === source.resource.id && diagnostic2.code === "share-owner-damaged"
|
|
1468
|
+
);
|
|
1469
|
+
return damaged ? { type: "damaged-share-owner", owner, code: "share-owner-damaged" } : void 0;
|
|
1470
|
+
}
|
|
1471
|
+
function causeFor(document, source, diagnostics) {
|
|
1472
|
+
if (document.kind === "folder") return folderCause(document, source);
|
|
1473
|
+
if (document.kind === "share") return shareCause(document, source, diagnostics);
|
|
1474
|
+
if (document.kind === "file" || import_layoutCore.TREE_DOCUMENT_KINDS.has(document.kind))
|
|
1475
|
+
return groupedCause(document, source, diagnostics);
|
|
1476
|
+
return void 0;
|
|
1477
|
+
}
|
|
1478
|
+
function locationHash(document) {
|
|
1479
|
+
const value = document;
|
|
1480
|
+
if (document.kind === "file")
|
|
1481
|
+
return (0, import_layoutCore.canonicalFragmentHash)({ name: value.name ?? null, folder: value.folder ?? null });
|
|
1482
|
+
if (document.kind === "share") return (0, import_layoutCore.canonicalFragmentHash)({ owner: value.owner ?? null });
|
|
1483
|
+
return (0, import_layoutCore.canonicalFragmentHash)({
|
|
1484
|
+
name: value.name ?? null,
|
|
1485
|
+
folder: value.folder ?? null,
|
|
1486
|
+
path: value.path ?? null,
|
|
1487
|
+
parent: value.parent ?? null
|
|
1488
|
+
});
|
|
1489
|
+
}
|
|
1490
|
+
function extractAuthoritativeDamageEvidence(head, documents) {
|
|
1491
|
+
if (!Array.isArray(head.diagnostics)) throw new Error("Head diagnostics are missing");
|
|
1492
|
+
head.diagnostics.forEach(validateDiagnostic);
|
|
1493
|
+
for (const hash of Object.values(head.manifest))
|
|
1494
|
+
if (typeof hash !== "string" || !import_layoutCore.SHA256.test(hash)) throw new Error("Head manifest has an invalid hash");
|
|
1495
|
+
const evidence = /* @__PURE__ */ new Map();
|
|
1496
|
+
for (const [documentKey, document] of documents) {
|
|
1497
|
+
const source = resourceFromKey(documentKey);
|
|
1498
|
+
validateRenderedIdentity(document, source);
|
|
1499
|
+
const cause = causeFor(document, source, head.diagnostics);
|
|
1500
|
+
if (!cause) continue;
|
|
1501
|
+
evidence.set(documentKey, {
|
|
1502
|
+
documentKey,
|
|
1503
|
+
source,
|
|
1504
|
+
canonicalHash: head.manifest[documentKey],
|
|
1505
|
+
serverDocumentHash: (0, import_layoutCore.canonicalFragmentHash)({ documentKey, document }),
|
|
1506
|
+
locationHash: locationHash(document),
|
|
1507
|
+
cause
|
|
1508
|
+
});
|
|
1509
|
+
}
|
|
1510
|
+
return evidence;
|
|
1511
|
+
}
|
|
1512
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1513
|
+
0 && (module.exports = {
|
|
1514
|
+
extractAuthoritativeDamageEvidence,
|
|
1515
|
+
planAuthoritativePlacement
|
|
1516
|
+
});
|
|
1517
|
+
//# sourceMappingURL=layoutReadbackPlacement.js.map
|